Use statement_timestamp() instead of now() #19
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Postgresql has a handful of functions to get the current timestamp, with varying exact meanings for each one.
So far we've been using the
NOW()
function which returns the timestamp of the beginning of a transaction block rather than the actual clock time. This PR changes it to use theSTATEMENT_TIMESTAMP()
function instead, which returns the timestamp of the beginning of a statement. Postgres documentation does a better job of explaining how exactly each of those functions work: https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-CURRENTIn rails, a common pattern is to do
and in this case, the timestamp returned by
NOW()
will be set by whenActiveRecord::Base.transaction
is called and not when statements inside the block are issued.There are ups and downs to using each one and no right answer as to which one to use. But we feel that with this change, timestamps will behave more like a wall clock, especially in long or slow transactions.
Related tasks: