Repository navigation
Fix MaterializedPostgreSQL replication for upper-case database/table names - #107423
Merged
Merged
Conversation
…names The publication name is created with `CREATE PUBLICATION "<name>"`, which preserves case, but it is also passed to the `pgoutput` plugin as the value of the `publication_names` option. PostgreSQL parses that option with `SplitIdentifierString`, which folds unquoted identifiers to lower case. As a result, for a PostgreSQL database (or table) name containing upper-case letters the consumer kept asking for a lower-cased publication name that did not exist, failing with `publication "..._ch_publication" does not exist` and silently stopping replication of further changes. The replication slot name is already normalized to lower case for the same reason; make the publication name lower case as well so both sides agree. Closes: #64891 Closes: #64615 Related: #49045 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Contributor
|
Workflow [PR], commit [ac7be1a] Summary: ✅
AI ReviewSummaryThis PR fixes Final VerdictStatus: ✅ Approve |
…ublications The previous commit folds the publication name to lower case, which fixes the upper-case database-name case for the `MaterializedPostgreSQL` database engine. But the single-table `MaterializedPostgreSQL` storage path has a separate, related problem with upper-case table names. `StorageMaterializedPostgreSQL` stores the remote table in `materialized_postgresql_tables_list` (the raw `remote_table_name`) and never goes through the quoting pass that `fetchRequiredTables` applies for the database engine. So `createPublicationIfNeeded` emitted `CREATE PUBLICATION ... FOR TABLE ONLY <name>` with an unquoted table name. PostgreSQL folds the unquoted identifier to lower case, the relation is not found, and the `CREATE TABLE` fails before replication can start with `pqxx::undefined_table: ERROR: relation "..." does not exist`. Every other table reference in this path (`loadFromSnapshot`, `fetchTableStructure`, `ALTER PUBLICATION ADD/DROP TABLE`) already quotes the name via `doubleQuoteWithSchema`, so quote the single-table `tables_list` the same way before creating the publication. The database engine path is left unchanged because its `tables_list` is already quoted by `fetchRequiredTables`. Adds an integration test `test_uppercase_table_name_single_storage` that reproduces the failure with `ENGINE = MaterializedPostgreSQL(..., 'Test_Uppercase_Table', ...)`. Verified locally: the test fails (`relation "test_uppercase_table" does not exist`) without the fix and passes with it. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
kssenii
approved these changes
Jun 15, 2026
…-publication # Conflicts: # tests/integration/test_postgresql_replica_database_engine/test_3.py
…nsumer Addressing review feedback: lower-casing the publication name in `getPublicationName` was lossy — two PostgreSQL tables whose names differ only by case would map to the same `..._ch_publication` and collide (with the single-table engine, the second `CREATE PUBLICATION` drops/recreates the first's publication, diverging its ongoing replication). Instead, keep the publication name case-preserving and quote it when it is handed to the `pgoutput` plugin via the `publication_names` option, which PostgreSQL parses with `SplitIdentifierString` (folding unquoted identifiers to lower case). The case-preserving `CREATE PUBLICATION "<name>"` side and the consumer side then agree, including for upper-case names, with no collision between case-distinct names. Add `test_publication_name_case_collision_single_storage`, which replicates two single-table `MaterializedPostgreSQL` storages whose remote table names differ only by case and verifies both keep replicating independently. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Contributor
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 13/13 (100.00%) | Lost baseline coverage: none · Uncovered code |
alexey-milovidov
added a commit
that referenced
this pull request
Jun 21, 2026
…aware After merging master, the original publication tables-list change from this PR became redundant: master's #107423 ("Fix MaterializedPostgreSQL replication for upper-case database/table names", commit `190a65c1556`) added an `else if (!is_materialized_postgresql_database)` branch in `createPublicationIfNeeded` that quotes the single-table `tables_list` via `doubleQuoteWithSchema`, which already applies the `materialized_postgresql_schema` setting. So the `CREATE PUBLICATION ... FOR TABLE ONLY` query is schema-qualified on master already. Revert that part here so master's branch is no longer dead code. What was still missing — and what this commit fixes — is the replication *identity*. `getPublicationName` and the default `getReplicationSlotName` derived the name from `<postgres_database>_<table>` only, ignoring the schema. Two standalone `MaterializedPostgreSQL` tables replicating a table with the same name from two different PostgreSQL schemas of the same database therefore shared one publication and one default replication slot. The second `CREATE` dropped and recreated the shared publication, and because the publication carries only the bare relation name (`schema_as_a_part_of_table_name` stays false in this mode) the consumers cross-talked: one replica could stop receiving its schema's changes or ingest the other schema's rows. Include the schema in the publication name and the default replication slot name for the single-table engine when `materialized_postgresql_schema` is set, so the two configurations get distinct PostgreSQL objects and stay isolated. The database engine path (empty remote table name) and the default-schema table-engine path are unchanged, so existing deployments keep their current publication and slot names. Add an integration test `test_two_schemas_same_table_name_single_storage` that replicates the same table name from two schemas and asserts both the initial snapshot and ongoing replication stay isolated. Shorten the schema and table names in `test_single_table_engine_with_non_default_schema` so the now schema-aware default slot name stays within PostgreSQL's identifier length limit. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 task
This was referenced Jun 27, 2026
Closed
alexey-milovidov
added a commit
that referenced
this pull request
Jul 12, 2026
…eployments The schema-aware publication and default replication-slot names broke backward compatibility for `MaterializedPostgreSQL` deployments created before the identity became schema-aware: on restart / `ATTACH` the handler looked for the schema-aware slot, missed the existing legacy one, ran an initial sync that reloaded a snapshot into the already-populated nested tables, and left the legacy slot orphaned on the PostgreSQL side, retaining WAL forever. This affects the database engine with a non-default `materialized_postgresql_schema` (supported for years, restart-covered by `test_2.py::test_database_with_single_non_default_schema`) and equally the single-table engine with a non-default schema, which works since 26.6 (#107423 schema-qualified the single-table publication, and that commit is in `v26.6.1.1193-stable`), so single-table deployments with legacy names exist in the wild too. A new `adoptLegacyReplicationIdentityIfNeeded` runs on attach, before the publication is consulted (in `startSynchronization` and, for the database engine, in `fetchRequiredTables`, which reads the publication first): when the schema-aware objects do not exist but the legacy schema-blind ones do, the handler switches to the legacy names and keeps using them, including for `DROP` cleanup in `shutdownFinal`. The slot carries the evidence (its loss is what triggers the destructive re-sync); when the slot name does not depend on the schema (a user-managed slot or a unique replication consumer identifier), the publication carries it instead. Adoption is fail-closed: the legacy names are schema-blind and can belong to a coexisting engine over the default schema of the same PostgreSQL database, so the legacy publication is adopted only when every table it publishes belongs to this engine's schema; otherwise the schema-aware identity is kept and the attach proceeds as a fresh setup instead of hijacking another engine's slot and publication (two consumers on one slot/publication would cross-talk, the very failure the schema-aware identity removes). Adds `test_legacy_identity_adopted_on_attach_table_engine` and `test_legacy_identity_adopted_on_restart_database_engine`, which turn a freshly created deployment into a legacy one on the PostgreSQL side (while the table is detached / the server is stopped) and assert that after `ATTACH`/restart the legacy identity is adopted: rows written meanwhile arrive through the legacy slot, the schema-aware objects are not recreated, and `DROP` cleans up the adopted objects. Adds `test_legacy_identity_not_adopted_for_foreign_publication`: a schema-scoped database that lost its schema-aware slot must not adopt the legacy slot/publication of a coexisting default-schema database — it re-syncs under its own identity and both databases stay isolated. Addresses the AI review blocker on the database-engine identity change. Co-Authored-By: Claude Fable 5 <[email protected]>
zeekay
pushed a commit
to hanzoai/datastore
that referenced
this pull request
Sep 11, 2026
…aware After merging master, the original publication tables-list change from this PR became redundant: master's ClickHouse#107423 ("Fix MaterializedPostgreSQL replication for upper-case database/table names", commit `190a65c1556`) added an `else if (!is_materialized_postgresql_database)` branch in `createPublicationIfNeeded` that quotes the single-table `tables_list` via `doubleQuoteWithSchema`, which already applies the `materialized_postgresql_schema` setting. So the `CREATE PUBLICATION ... FOR TABLE ONLY` query is schema-qualified on master already. Revert that part here so master's branch is no longer dead code. What was still missing — and what this commit fixes — is the replication *identity*. `getPublicationName` and the default `getReplicationSlotName` derived the name from `<postgres_database>_<table>` only, ignoring the schema. Two standalone `MaterializedPostgreSQL` tables replicating a table with the same name from two different PostgreSQL schemas of the same database therefore shared one publication and one default replication slot. The second `CREATE` dropped and recreated the shared publication, and because the publication carries only the bare relation name (`schema_as_a_part_of_table_name` stays false in this mode) the consumers cross-talked: one replica could stop receiving its schema's changes or ingest the other schema's rows. Include the schema in the publication name and the default replication slot name for the single-table engine when `materialized_postgresql_schema` is set, so the two configurations get distinct PostgreSQL objects and stay isolated. The database engine path (empty remote table name) and the default-schema table-engine path are unchanged, so existing deployments keep their current publication and slot names. Add an integration test `test_two_schemas_same_table_name_single_storage` that replicates the same table name from two schemas and asserts both the initial snapshot and ongoing replication stay isolated. Shorten the schema and table names in `test_single_table_engine_with_non_default_schema` so the now schema-aware default slot name stays within PostgreSQL's identifier length limit. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay
pushed a commit
to hanzoai/datastore
that referenced
this pull request
Sep 11, 2026
…eployments The schema-aware publication and default replication-slot names broke backward compatibility for `MaterializedPostgreSQL` deployments created before the identity became schema-aware: on restart / `ATTACH` the handler looked for the schema-aware slot, missed the existing legacy one, ran an initial sync that reloaded a snapshot into the already-populated nested tables, and left the legacy slot orphaned on the PostgreSQL side, retaining WAL forever. This affects the database engine with a non-default `materialized_postgresql_schema` (supported for years, restart-covered by `test_2.py::test_database_with_single_non_default_schema`) and equally the single-table engine with a non-default schema, which works since 26.6 (ClickHouse#107423 schema-qualified the single-table publication, and that commit is in `v26.6.1.1193-stable`), so single-table deployments with legacy names exist in the wild too. A new `adoptLegacyReplicationIdentityIfNeeded` runs on attach, before the publication is consulted (in `startSynchronization` and, for the database engine, in `fetchRequiredTables`, which reads the publication first): when the schema-aware objects do not exist but the legacy schema-blind ones do, the handler switches to the legacy names and keeps using them, including for `DROP` cleanup in `shutdownFinal`. The slot carries the evidence (its loss is what triggers the destructive re-sync); when the slot name does not depend on the schema (a user-managed slot or a unique replication consumer identifier), the publication carries it instead. Adoption is fail-closed: the legacy names are schema-blind and can belong to a coexisting engine over the default schema of the same PostgreSQL database, so the legacy publication is adopted only when every table it publishes belongs to this engine's schema; otherwise the schema-aware identity is kept and the attach proceeds as a fresh setup instead of hijacking another engine's slot and publication (two consumers on one slot/publication would cross-talk, the very failure the schema-aware identity removes). Adds `test_legacy_identity_adopted_on_attach_table_engine` and `test_legacy_identity_adopted_on_restart_database_engine`, which turn a freshly created deployment into a legacy one on the PostgreSQL side (while the table is detached / the server is stopped) and assert that after `ATTACH`/restart the legacy identity is adopted: rows written meanwhile arrive through the legacy slot, the schema-aware objects are not recreated, and `DROP` cleans up the adopted objects. Adds `test_legacy_identity_not_adopted_for_foreign_publication`: a schema-scoped database that lost its schema-aware slot must not adopt the legacy slot/publication of a coexisting default-schema database — it re-syncs under its own identity and both databases stay isolated. Addresses the AI review blocker on the database-engine identity change. Co-Authored-By: Claude Fable 5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a PostgreSQL database (or table) name contains upper-case letters,
MaterializedPostgreSQLperformed the initial snapshot correctly but then stopped replicating further changes, logging:Root cause. The publication is created with
CREATE PUBLICATION "<name>", which preserves case, so the catalog stores e.g.Test_Uppercase_ch_publication. But the same name is also handed to thepgoutputplugin as the value of thepublication_namesoption inpg_logical_slot_peek_binary_changes(...). PostgreSQL parses that option withSplitIdentifierString, which folds unquoted identifiers to lower case, so the consumer asks fortest_uppercase_ch_publication, which does not exist.The fix. Keep the publication name case-preserving and make both sides agree by quoting it on the consumer side: the name is wrapped in double quotes when it is passed as the
publication_namesvalue, soSplitIdentifierStringpreserves its case and the consumer looks up the exact publication thatCREATE PUBLICATION "<name>"created. Folding the name to lower case instead would be lossy — two PostgreSQL tables whose names differ only by case would collide on a single..._ch_publication(and, with the single-table engine, the secondCREATE PUBLICATIONwould drop/recreate the first's publication, diverging its ongoing replication). Names that are already all lower case are unaffected.Separately, the single-table
MaterializedPostgreSQLstorage passed the raw remote table name toCREATE PUBLICATION ... FOR TABLE ONLY <name>without quoting — that path setsmaterialized_postgresql_tables_listdirectly and never goes through the quoting pass that the database engine applies infetchRequiredTables. An upper-case table name was therefore folded to lower case and the relation was not found, so the table name is now quoted there too.Integration tests reproduce both paths and the collision case:
test_uppercase_database_name— database engine, upper-case database name.test_uppercase_table_name_single_storage— single-table storage, upper-case table name.test_publication_name_case_collision_single_storage— two single-table storages whose remote table names differ only by case must each get their own publication and keep replicating independently.Each inserts rows after the initial snapshot and verifies they are replicated — the consumer path that previously failed.
Closes: #64891
Closes: #64615
Related: #49045
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
MaterializedPostgreSQLsilently stopping replication of changes when the PostgreSQL database or table name contains upper-case letters (thepgoutputconsumer requested an unquoted, lower-cased publication name that did not match the case-preserving publication).Documentation entry for user-facing changes
🤖 Generated with Claude Code
Version info
26.6.1.871