postgres_scan: reject out-of-range timestamps - #484
Open
marknefedov wants to merge 3 commits into
Open
Conversation
Signed-off-by: Mark Nefedov <mvnefedov@avito.ru>
marknefedov
requested review from
sfc-gh-abozkurt,
sfc-gh-amzheng,
sfc-gh-dachristensen,
sfc-gh-mslot,
sfc-gh-npuka and
sfc-gh-okalaci
as code owners
July 24, 2026 20:00
Signed-off-by: Mark Nefedov <mvnefedov@avito.ru>
marknefedov
marked this pull request as draft
July 28, 2026 10:51
Signed-off-by: Mark Nefedov <mvnefedov@avito.ru>
marknefedov
marked this pull request as ready for review
July 28, 2026 12:04
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
postgres_scansilently corrupts out-of-range timestamps instead of rejecting them. PostgreSQL'stimestamp/timestamptzrange is wider than DuckDB's. WhenPostgresBinaryReader::ReadTimestamp()adds the PostgreSQL→DuckDB epoch offset to a value near either end of PostgreSQL's range, the addition overflows DuckDB's internalint64microseconds representation and silently wraps — e.g. a valid PostgreSQL value of294276-12-31 23:59:59is returned as290279-12-14 (BC) 15:58:09.448384. The same unchecked arithmetic exists in reverse inpostgres_binary_writer.hpp, so a DuckDB value near its own bounds can silently wrap into a bogus PostgreSQL timestamp on write.Fix
This backports duckdb/duckdb-postgres#530 onto the older vendored
duckdb-postgresrevision pg_lake currently pins, via a new patch induckdb_pglake/patches/duckdb-postgres/timestamp-range.patch(the upstream PR targets a later API —PostgresBinaryParservs. this revision'sPostgresBinaryReader— so the checks are ported rather than cherry-picked verbatim):TryAddOperatorfor the epoch-offset addition and reject the result withConversionException("timestamp out of range")on overflow or if it isn'tTimestamp::IsFinite.TrySubtractOperatorand validate against PostgreSQL's exactMIN_TIMESTAMP/END_TIMESTAMPbounds (fromdatatype/timestamp.h), raisingInvalidInputExceptioninstead of wrapping.Tests
Added
pgduck_server/tests/pytests/test_postgres_scanner_timestamp_range.py, covering the reproduction from #439:timestampandtimestamptzscanning reject the out-of-range value with an error instead of returning wrapped dataCOPY ... TO PARQUETpostgres_scanconverts a whole batch before DuckDB's outer filter applies, so a single mixed table can't isolate the failing rowpostgres_binary.testfixture fromduckdb-postgres, adjusted to use a cross-database-representable BC boundary instead of DuckDB's own (more extreme) minimum, which previously round-tripped silently through matching reader/writer overflowsBefore the patch:
4 failed, 1 passed. After:5 passed. Focused scanner suite (test_postgres_scanner.py+test_postgres_scanner_timestamp_range.py):18 passed.Fixes #439.
Upstream fix: duckdb/duckdb-postgres#530