-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for getting current timestamp in nano second precision. #7
base: master
Are you sure you want to change the base?
Add support for getting current timestamp in nano second precision. #7
Conversation
src/timestamp9--1.0.1--1.1.0.sql
Outdated
@@ -0,0 +1,3 @@ | |||
CREATE FUNCTION timestamp9_nsnow() RETURNS timestamp9 AS | |||
'$libdir/timestamp9' | |||
LANGUAGE c IMMUTABLE STRICT PARALLEL SAFE LEAKPROOF; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function as it is implemented is not IMMUTABLE
, but it's VOLATILE
. Normally, the Postgres now()/current_date
etc. functions are neither of these, but are STABLE
, which means that they return the same value across different calls in the same statement. I think any 'now' function that timestamp9 provides should be STABLE too. This means it likely requires a different implementation though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your comments! Sounds reasonable, I'm will revise it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm using two static variables to 'cache' the timestamp inside a transaction. It's a little bit different from PostgreSQL's now()
, since now()
returns the first statement's timestamp inside a transaction to avoid extra cost of calling GetCurrentTimestamp()
[1]. In this this approach, timestamp9_nsnow()
returns the timestamp of the first timestamp9_nsnow()
function call.
1dcc6fd
to
7c166b0
Compare
This patch adds support for getting current timestamp in nano second precision by using clock_gettime() [1]. [1] https://man7.org/linux/man-pages/man2/clock_gettime.2.html
7c166b0
to
aafa2df
Compare
{ | ||
timestamp9 result; | ||
struct timespec tv; | ||
static TimestampTz current_xact_timestamp; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work in the case parallelism is enabled in queries? You can try using force_parallel_mode
to test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch! It seems that the current_xact_ts9_timestamp
cannot be synced to parallel workers in my implementation like xactStartTimestamp
. Maybe we can mark it with PARALLEL RESTRICTED
?
https://www.postgresql.org/docs/current/parallel-safety.html
This patch adds support for getting current timestamp in nano second precision by using clock_gettime() [1]. I didn't have a Windows machine so I didn't implement it for Windows. Feedbacks are welcome.
[1] https://man7.org/linux/man-pages/man2/clock_gettime.2.html