Skip to content

Keep index column order in MaterializedPostgreSQL sorting key - #127137

Merged
PedroTadim merged 4 commits into
ClickHouse:masterfrom
AI-Stratov:fix-replica-identity-order
Oct 30, 2026
Merged

PedroTadim merged 4 commits into
ClickHouse:masterfrom
AI-Stratov:fix-replica-identity-order

Conversation

@AI-Stratov

@AI-Stratov AI-Stratov commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

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 in indkey. 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 checks sorting_key for both.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

MaterializedPostgreSQL now 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

  • Merged into: 26.11.1.929-master (included in 26.11 and later)

@CLAassistant

CLAassistant commented Oct 21, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@clickhouse-gh clickhouse-gh Bot added the comp-postgresql PostgreSQL-specific integration (table engine/function, logical replication bridge). label Oct 21, 2026
@AI-Stratov AI-Stratov changed the title Keep replica identity index column order in MaterializedPostgreSQL Keep index column order in MaterializedPostgreSQL sorting key Oct 21, 2026
@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Oct 22, 2026
@PedroTadim PedroTadim self-assigned this Oct 22, 2026
@PedroTadim PedroTadim added the can be tested Allows running workflows for external contributors label Oct 22, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [534d214]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel) FAIL
Server liveness check failed FAIL cidb IGNORED
Logical error: Trying to deserialize data into Variant column with not global variants order (STID: 6636-4f2a) FAIL cidb IGNORED

AI Review

Summary

The PR makes MaterializedPostgreSQL preserve PostgreSQL primary-key and replica-identity index column order when building nested-table sorting keys. The regression test covers both paths and passed in targeted ASan/UBSan CI on the last code-changing commit. No actionable findings.

Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 22, 2026
@PedroTadim

Copy link
Copy Markdown
Member

@AI-Stratov indkey is an int2vector, not a real int2[], and polymorphic functions like array_position(anyarray, anyelement) don't accept it on every PostgreSQL version without a cast. Could you cast it, e.g. array_position(i.indkey::int2[], a.attnum) (and the same for ix.indkey)? That's the usual safe form and makes the queries independent of the PostgreSQL version.

@AI-Stratov

Copy link
Copy Markdown
Contributor Author

@PedroTadim Done, both queries now cast indkey to int2[]. Thanks!

@clickhouse-gh

clickhouse-gh Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 534d21428 with master ca9786f6b (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, 9 s compile time in total, 7 of them have a recent master baseline.

Job report

@PedroTadim
PedroTadim enabled auto-merge October 30, 2026 09:32
@PedroTadim
PedroTadim added this pull request to the merge queue Oct 30, 2026
Merged via the queue into ClickHouse:master with commit 703aec7 Oct 30, 2026
177 of 179 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 30, 2026
clickgapai pushed a commit to clickgapai/ClickHouse that referenced this pull request Nov 16, 2026
…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]>
mosya415 pushed a commit to mosya415/ClickHouse that referenced this pull request Nov 19, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-postgresql PostgreSQL-specific integration (table engine/function, logical replication bridge). external Author is not a member of the ClickHouse organization pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

4 participants