BUG #19935: comparing timestamps milliseocnds = microseconds

Started by PG Bug reporting form4 days ago2 messagesbugs

Thread notes

1 note#bug#not-a-bug
hackorum-bot#bug#not-a-bug
hackorum-bot
2 days ago

@global #bug #not-a-bug

#bug#not-a-bug
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

The following bug has been logged on the website:

Bug reference: 19935
Logged by: Stepan Yankevych
Email address: stepya@ukr.net
PostgreSQL version: 16.16
Operating system: RHEL 9.*
Description:

The issue is reproducible in up to 19 and looks like it is expected
behavior. but it is too strage behavior.

to_timestamp('20261124-09:57:25.000123', 'YYYYMMDD-hh:mi:ss.ms') evaluates
to 123 milliseconds, whereas the expected result is 0 milliseconds.

select to_timestamp('2026.11.24 09:57:25.123', 'YYYY.MM.DD-hh:mi:ss.ms') ,
to_timestamp('2026.11.24 09:57:25.000123', 'YYYY.MM.DD-hh:mi:ss.ms') ,
'2026.11.24 09:57:25.000123'::timestamp(3)

result is
to_timestamp |to_timestamp |timestamp |
--------------------------+--------------------------+-------------------+
2026-11-24 09:57:25.123-05|2026-11-24 09:57:25.123-05|2026-11-24 09:57:25|

Most confusingly, the following query returns true
select to_timestamp('2026.11.24 09:57:25.123', 'YYYY.MM.DD hh:mi:ss.ms') =
to_timestamp('2026.11.24 09:57:25.000123', 'YYYY.MM.DD hh:mi:ss.ms')

As far as i understand Postgres treats .123 and .000123 as substring after
'.' converted to int '000123'::int and '123'::int .
That is root cause of such behavior.
I'd suggest to use mulityply like (0.123*1000000)::int and
(0.000123*1000000)::int . So 123000 vs 123 now
It should fix the issue

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: PG Bug reporting form (#1)
Re: BUG #19935: comparing timestamps milliseocnds = microseconds

PG Bug reporting form <noreply@postgresql.org> writes:

The issue is reproducible in up to 19 and looks like it is expected
behavior. but it is too strage behavior.

It's acting as designed.

to_timestamp('20261124-09:57:25.000123', 'YYYYMMDD-hh:mi:ss.ms') evaluates
to 123 milliseconds, whereas the expected result is 0 milliseconds.

You told it the field was milliseconds, so it read it as milliseconds.
If you wanted it to be read as microseconds, you should have said "us".

In general, I don't have much sympathy for arguments about how
to_timestamp should interpret input loosely. If you want the
"natural" reading of the input string, just casting text to timestamp
will almost always do what you want. The point of to_timestamp is
to be able to parse strings that are in some weird format. But you
have to describe that format accurately.

Even if I agreed this was wrong, I'd be hesitant to change the
behavior for fear of breaking existing use-cases.

regards, tom lane