user1005909
user1005909

Reputation: 1975

PostgreSQL support for timestamps to nanosecond resolution

The data I'm receiving has timestamps down to nanoseconds (which we actually care about). Is there a way for Postgres timestamps to go to nanoseconds?

Upvotes: 23

Views: 21727

Answers (3)

fvannee
fvannee

Reputation: 812

As others have pointed out, Postgres doesn't provide such type out of the box. However, it's relatively simple to create an extension that supports nanosecond resolution due to the open-source nature of Postgres. I faced similar issues a while ago and created this timestamp9 extension for Postgres.

It internally stores the timestamp as a bigint and defines it as the number of nanoseconds since the UNIX epoch. It provides some convenience functions around it that make it easy to view and manipulate the timestamps. If you can live with the limited time range that these timestamps can have, between the year 1970 and the year 2262, then this is a good solution.

Disclaimer: I'm the author of the extension

Upvotes: 16

jalmasi
jalmasi

Reputation: 352

Nope, but you could trim timestamps to milliseconds, and store nanosecond part to a separate column. You can create index on both, and view or function to return your wanted nanosecond timestamp, and you can even create index on your function.

Upvotes: 3

On the one hand, the documented resolution is 1 microsecond.

On the other hand, PostgreSQL is open source. Maybe you can hack together something to support nanoseconds.

Upvotes: 0

Related Questions