foreign_key test is sensitive to the OID counter

Started by Richard Guo19 days ago7 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

This thread has been committed, so CI has stopped here. Anything below is the last result it produced.

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:t253630
psql -h localhost -U postgres

Built from patchset v4 (message #4), September 02, 2026 at 01:07 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 t253630_4 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 t253630_4 && git checkout t253630_4

Patchset v4 (message #4) is on t253630_4

Jump to latest
#1Richard Guo
guofenglinux@gmail.com

While running installcheck against a long-lived cluster, I got a diff
in the foreign_key test:

@@ -2045,11 +2045,11 @@
 ORDER BY oid::regclass::text;
   conname   | conenforced | convalidated
 ------------+-------------+--------------
+ selffk_3   | t           | f
+ selffk_3_1 | t           | f
  selffk     | t           | f
  selffk_1   | t           | f
  selffk_2   | t           | f
- selffk_3   | t           | f
- selffk_3_1 | t           | f
 (5 rows)

At first I thought the planner patch I was working on had caused an
unexpected plan diff, and wasted some time chasing that. But it
turned out to be the "ORDER BY oid::regclass::text" in these queries.

These OIDs are constraint OIDs, not relation OIDs, so the regclass
cast just renders them as numeric strings, and comparing those as text
depends on the number of digits. Once the OID counter crosses a power
of ten while the test runs, '1000006' sorts before '999987' and the
output order flips.

I plan to push the attached patch to fix it. Any thoughts?

- Richard

Attachments:

t253630_1
v1-0001-Fix-OID-counter-sensitive-row-ordering-in-foreign.patchapplication/octet-stream; name=v1-0001-Fix-OID-counter-sensitive-row-ordering-in-foreign.patchDownload+16-17
#2jian he
jian.universality@gmail.com
In reply to: Richard Guo (#1)
Re: foreign_key test is sensitive to the OID counter

On Tue, Sep 1, 2026 at 4:32 PM Richard Guo <guofenglinux@gmail.com> wrote:

I plan to push the attached patch to fix it. Any thoughts?

 SELECT conname, convalidated, conrelid::regclass FROM pg_constraint
-WHERE conrelid::regclass::text like 'fk_partitioned_fk%' ORDER BY
oid::regclass::text;
+WHERE conrelid::regclass::text like 'fk_partitioned_fk%' ORDER BY oid;

Perhaps what they wanted at the time was

WHERE conrelid::regclass::text like 'fk_partitioned_fk%' ORDER BY
conrelid::regclass::text;

in https://git.postgresql.org/cgit/postgresql.git/commit/?id=6beb38cfc9ddd4cd3d2eb5402981ebdd69a618b4
we use ``::regclass::text COLLATE "C"`` is stable the tests.

IMHO, Using
ORDER BY conrelid::regclass::text COLLATE "C"
is more future-proof.

--
jian
https://www.enterprisedb.com/

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Richard Guo (#1)
Re: foreign_key test is sensitive to the OID counter

Richard Guo <guofenglinux@gmail.com> writes:

These OIDs are constraint OIDs, not relation OIDs, so the regclass
cast just renders them as numeric strings, and comparing those as text
depends on the number of digits. Once the OID counter crosses a power
of ten while the test runs, '1000006' sorts before '999987' and the
output order flips.

Right, that's bogus.

I plan to push the attached patch to fix it. Any thoughts?

This is only somewhat less bogus, though: it'd still fail at OID
wraparound. How about sorting by the constraint name?

regards, tom lane

#4Richard Guo
guofenglinux@gmail.com
In reply to: Tom Lane (#3)
Re: foreign_key test is sensitive to the OID counter

On Tue, Sep 1, 2026 at 11:48 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:

This is only somewhat less bogus, though: it'd still fail at OID
wraparound. How about sorting by the constraint name?

Good point. I didn't take OID wraparound into consideration. I think
sorting by conname works, as the connames are unique within each of
these queries.

I noticed that several other test files use COLLATE "C" when sorting
by names, so that the order is independent of the database's default
collation. So I end up using ORDER BY conname COLLATE "C" in v2.

- Richard

Attachments:

t253630_4
v2-0001-Fix-OID-counter-sensitive-row-ordering-in-foreign.patchapplication/octet-stream; name=v2-0001-Fix-OID-counter-sensitive-row-ordering-in-foreign.patchDownload+27-28
#5Richard Guo
guofenglinux@gmail.com
In reply to: jian he (#2)
Re: foreign_key test is sensitive to the OID counter

On Tue, Sep 1, 2026 at 6:05 PM jian he <jian.universality@gmail.com> wrote:

IMHO, Using
ORDER BY conrelid::regclass::text COLLATE "C"
is more future-proof.

Hm, I don't think so. In most of the queries the WHERE clause is
literally conrelid = 'something'::regclass, and sorting by a constant
is no sort at all. In the other queries the conrelid is not even
unique in the output.

- Richard

#6Tom Lane
tgl@sss.pgh.pa.us
In reply to: Richard Guo (#4)
Re: foreign_key test is sensitive to the OID counter

Richard Guo <guofenglinux@gmail.com> writes:

I noticed that several other test files use COLLATE "C" when sorting
by names, so that the order is independent of the database's default
collation. So I end up using ORDER BY conname COLLATE "C" in v2.

That looks good to me.

regards, tom lane

#7Richard Guo
guofenglinux@gmail.com
In reply to: Tom Lane (#6)
Re: foreign_key test is sensitive to the OID counter

On Wed, Sep 2, 2026 at 10:22 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:

Richard Guo <guofenglinux@gmail.com> writes:

I noticed that several other test files use COLLATE "C" when sorting
by names, so that the order is independent of the database's default
collation. So I end up using ORDER BY conname COLLATE "C" in v2.

That looks good to me.

Thanks! Pushed.

- Richard