Repository navigation
Follow-up to #115066: case-insensitive pg_catalog qualifier and unique synthetic oids - #115960
Conversation
…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
|
Workflow [PR], commit [4684df4] Summary: ✅
AI ReviewSummaryThis PR fixes the case-insensitive handling of unquoted 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.
Final VerdictStatus: ❌ Block Minimum required actions:
LLVM Coverage ReportMeasured on commit 4684df4.
Changed lines: Changed C/C++ lines covered: 26/26 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 11 s compile time in total, 7 of them have a recent master baseline. |
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]>
…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.
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 |
There was a problem hiding this comment.
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.
alexey-milovidov
left a comment
There was a problem hiding this comment.
This is a small follow-up, and the remaining nitpicks are not worth considering.
Related: #115066
A follow-up to #115066 (merged), addressing two review findings on it that were left unaddressed at merge time.
1. The
pg_catalogqualifier rewrite was case-sensitive. PostgreSQL folds unquoted identifiers to lower case, soSELECT * FROM PG_CATALOG.pg_classandSELECT 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 inremovePgCatalogQualifiermissed those spellings, so such a query fell through to a real database lookup and failed withDatabase PG_CATALOG does not existinstead 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.oidandpg_class.oid/pg_class.relnamespaceweresipHash64values 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 everypg_classrow of the current database to bothpg_namespacerows, making\d/\dtduplicate 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 ofpg_catalog.,PG_CATALOG.,Pg_Catalog.and"pg_catalog".resolve,"PG_CATALOG".still errors, the oids ofpg_namespaceandpg_classare pairwise distinct and disjoint, and\dlists each table exactly once under the right schema.Adds
test_catalog_qualifier_is_case_insensitive,test_catalog_oids_are_uniqueandtest_catalog_oids_are_stabletotests/integration/test_postgresql_protocol.Changelog category (leave one):
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
26.9.1.745(included in26.9and later)