skip to Main Content

In my postgres database I have a table I’ll call users with a timestamptz type column updated_time. When I execute a select statement on this field I will get something like this – ‘2024-07-01 12:30:30.821 -0500’. However, if I turn around and select from this table using this value such as

select * from users where updated_time = '2024-07-01 12:30:30.821 -0500'

I get no results. This is due to a hidden precision. If I play around with it I can trial/error the milliseconds to construct a query with a millisecond precision of 6 decimal places which will return results such as:

select * from users where updated_time = '2024-07-01 12:30:30.821898 -0500'

My question is, does anyone know why this could be happening? The table column seems to have a 6 millisecond precision that the select statement does not reflect.

Additional note: I did try running the query

select * '2024-07-01 12:30:30.821898 -0500'::timestamptz

Which gives me the millisecond truncated value: ‘2024-07-01 12:30:30.821 -0500’

I then tried

select '2024-07-01 12:30:30.821898 -0500'::timestamptz(6);

And this still gives me the truncated value: ‘2024-07-01 12:30:30.821 -0500 -0500’

It seems like no matter what I do I’m stuck with a precision of 3 millisecond decimal places when selecting.

2

Answers


  1. timestamptz (and all times in postgres) have a resolution of 1 microsecond or 0.000001 seconds.

    The tool you’re using to display query results from Postgres is truncating timestamp values causing confusion. If you’re using a programming language, that language’s equivalent data type may only have 1 millisecond accuracy or 0.001 seconds. For example, a Javascript Date.

    For example, you might see 2024-07-01 12:30:30.821 -0500 but the real value is 2024-07-01 12:30:30.821898 -0500. 2024-07-01 12:30:30.821 -0500 will not match 2024-07-01 12:30:30.821898 -0500.

    Configure your tool so it no longer truncates, or try a simpler tool such as the official psql client. In general, use psql to debug PostgreSQL problems; it will tell you if it’s a PostgreSQL issue or something in between.

    Demonstration.

    Login or Signup to reply.
  2. The values aren’t truncated.

    It’s DBeaver default display format showing them like that. Since 5.2.2 you can switch to native format if you prefer to see the full thing.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search