Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

higuoxing
Copy link
Contributor

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

@@ -0,0 +1,3 @@
CREATE FUNCTION timestamp9_nsnow() RETURNS timestamp9 AS
'$libdir/timestamp9'
LANGUAGE c IMMUTABLE STRICT PARALLEL SAFE LEAKPROOF;
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

[1] https://github.com/postgres/postgres/blob/a8a44828a2e15c95a15549063205b779f7055f1c/src/backend/access/transam/xact.c#L2111

@higuoxing higuoxing force-pushed the get-current-time-in-ns-precision branch from 1dcc6fd to 7c166b0 Compare February 14, 2023 06:06
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
@higuoxing higuoxing force-pushed the get-current-time-in-ns-precision branch from 7c166b0 to aafa2df Compare February 14, 2023 06:15
{
timestamp9 result;
struct timespec tv;
static TimestampTz current_xact_timestamp;
Copy link
Collaborator

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.

Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants