Protect against timestamp underflow in uuidv7(interval)

Started by Tristan Partin3 months ago2 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

won't retrysuccessCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t248620
psql -h localhost -U postgres

Built from patchset v1 (message #1), July 29, 2026 at 12:08 AM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t248620_1 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t248620_1 && git checkout t248620_1

Patchset v1 (message #1) is on t248620_1

Jump to latest
#1Tristan Partin
tristan@partin.io

We were silently allowing underflowing the generated timestamp for the
UUIDv7 value:

# SELECT uuid_extract_timestamp(uuidv7('-57 years'::interval));
uuid_extract_timestamp
-----------------------------
10889-01-23 04:02:36.375+00
(1 row)

RFC 9562[0]https://www.rfc-editor.org/rfc/rfc9562.html doesn't seem to specify that this should be allowed, so
generate an error and forbid the underflow.

I checked the Rust uuid crate[1]https://github.com/uuid-rs/uuid/blob/1e6a9669e30d53bae50fd52f16b7a1961fda236b/src/timestamp.rs#L198-L220, and it also errors out with
a timestamp earlier than the Unix epoch.

[0]: https://www.rfc-editor.org/rfc/rfc9562.html
[1]: https://github.com/uuid-rs/uuid/blob/1e6a9669e30d53bae50fd52f16b7a1961fda236b/src/timestamp.rs#L198-L220

--
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)

Attachments:

t248620_1
v1-0001-Protect-against-timestamp-underflow-in-uuidv7-int.patchtext/x-patch; charset=utf-8; name=v1-0001-Protect-against-timestamp-underflow-in-uuidv7-int.patchDownload+14-1
#2Baji Shaik
baji.pgdev@gmail.com
In reply to: Tristan Partin (#1)
Re: Protect against timestamp underflow in uuidv7(interval)

On Tue, Jun 23, 2026 at 6:39 PM Tristan Partin <tristan@partin.io> wrote:

We were silently allowing underflowing the generated timestamp for the
UUIDv7 value:

# SELECT uuid_extract_timestamp(uuidv7('-57 years'::interval));
uuid_extract_timestamp
-----------------------------
10889-01-23 04:02:36.375+00
(1 row)

Thank you, Tristan.
This has been reported and is being discussed here [1]/messages/by-id/CA+fm-RN4eMEr2tzZU_mAV-=WfdmPXJ4Ea_GpmSS8=yStSy8onQ@mail.gmail.com

[1]: /messages/by-id/CA+fm-RN4eMEr2tzZU_mAV-=WfdmPXJ4Ea_GpmSS8=yStSy8onQ@mail.gmail.com
/messages/by-id/CA+fm-RN4eMEr2tzZU_mAV-=WfdmPXJ4Ea_GpmSS8=yStSy8onQ@mail.gmail.com

Thanks,
Baji Shaik.