Repository navigation
Keep index column order in MaterializedPostgreSQL sorting key - #127137
Conversation
|
Workflow [PR], commit [534d214] Summary: ❌
AI ReviewSummaryThe PR makes Final VerdictStatus: ✅ Approve. |
|
@AI-Stratov |
|
@PedroTadim Done, both queries now cast |
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, 9 s compile time in total, 7 of them have a recent master baseline. |
703aec7
…erializedPostgreSQL table `DatabaseMaterializedPostgreSQL::getCreateTableQueryImpl` rebuilt the definition of every table from the current PostgreSQL catalog when called with a query context, even when the nested `ReplacingMergeTree` exists. BACKUP stores that definition together with the nested table's parts, whose columns and sorting key were fixed when the nested table was created. When the PostgreSQL primary key or replica identity changes after replication started (logical replication carries no DDL and an existing nested table is never re-created), or when the key column order returned by `fetchPostgreSQLTableStructure` changes with an upgrade (ClickHouse#127137), RESTORE created the table with the new key over parts sorted by the old one, and key-filtered reads returned too few rows (81 of 999 in ClickHouse#128271) while `CHECK TABLE` passed. `SHOW CREATE TABLE` reported a key the table does not have. Return the stored definition (`DatabaseAtomic::getCreateTableQueryImpl`, already used without a query context) when the nested table exists. The PostgreSQL-derived definition remains for a name without a nested table, which `ATTACH TABLE <db>.<table>` needs to add a table to replication. The UUID lookup added in ClickHouse#107433 to keep repeated reads stable for BACKUP is dead now (an existing table returns its stored UUID) and is removed. `SHOW CREATE TABLE` now includes `SETTINGS index_granularity = 8192`, so `test_table_override` expects it, and the restore in `test_backup_database` no longer needs `allow_different_table_def` (kept, as in the documented recipe). Closes ClickHouse#128271 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e nested sorting key Since PostgreSQL 11, pg_index.indkey lists all indnatts columns of an index: first the indnkeyatts key columns, then the INCLUDE columns. The primary key and replica identity queries in fetchPostgreSQLTableStructure matched every indkey entry, so for `PRIMARY KEY (key) INCLUDE (value)` the nested ReplacingMergeTree of a MaterializedPostgreSQL database got `ORDER BY tuple(key, value)`. PostgreSQL identifies replicated rows by the key columns only. An UPDATE of an INCLUDE column sends a row with a new sorting key, and DELETE or a key-changing UPDATE sends an old-key tuple whose non-key columns are NULL (inserted as defaults). Neither collapses with the stored row, so the old versions stay visible. A nullable INCLUDE column made the sorting key nullable, and creating the nested table failed with ILLEGAL_COLUMN, so the table was not replicated. Read only indkey[0:indnkeyatts - 1] in both queries. The key columns come first in indkey, so their order (ClickHouse#127137) is unchanged, and an index without INCLUDE columns gives the same result as before. indnkeyatts exists since PostgreSQL 11, which the engine already requires (pg_replication_slot_advance). Existing nested tables keep their sorting key until they are reloaded with DETACH TABLE ... PERMANENTLY and ATTACH TABLE. Closes ClickHouse#128270 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #127135
Closes: #79375
The sorting key of a MaterializedPostgreSQL nested table didn't follow the order of the key columns in PostgreSQL. The primary key query had no
ORDER BY, so the columns came back in table order, and the replica identity query sorted them by name. Both now sort the columns by their position inindkey. The fix for the primary key query was suggested by @arthur3336 in #79375.The test creates a table with
PRIMARY KEY (id1, id2)on columns(id2, id1)and a table with a replica identity index on(z, b, a), and checkssorting_keyfor both.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
MaterializedPostgreSQLnow builds the sorting key of a replicated table in the order of the primary key or replica identity index columns. Before, primary key columns followed the table column order and replica identity columns were sorted alphabetically.Workflow [PR]
Sync PR [sync-upstream/pr/127137]
Version info
26.11.1.929-master(included in26.11and later)