In PostgreSQL 16.1 I have a very large table mytable
with a column _modified
with type Timestamp with time zone. When I run below query on the whole table, it returns a lower timestamp value compared to when I run the query on as subset of the table.
A query on the whole table returns 06-11-2023 17:06:38.000
:
select max(cast(_modified at time zone 'UTC' as timestamp))
from mytable
;
A subset of the table returns 11-06-2023 17:06:38.001
:
select max(cast(_modified at time zone 'UTC' as timestamp))
from mytable
where mycondition = true
;
I would expect the query without where clause to return at least the value of a subset of the query. Not sure if it is related but I run these queries in DBeaver. Anyone any idea how this could happen and how to force the max operator to return the correct value?
2
Answers
I think your queries works correctly, it’s just that the format in which you get the result is
dd-mm-yyyy
.Thus, the value in the entire table (
06-11-2023
) is greater than the value in the subset of rows (11-06-2023
).Try reindexing the table using: