Skip to content

Fix MaterializedPostgreSQL replication for upper-case database/table names - #107423

Merged
alexey-milovidov merged 4 commits into
masterfrom
fix-matpg-uppercase-publication
Jun 16, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
fix-matpg-uppercase-publication

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jun 13, 2026 •

Copy link
Copy Markdown
Member

When a PostgreSQL database (or table) name contains upper-case letters, MaterializedPostgreSQL performed the initial snapshot correctly but then stopped replicating further changes, logging:

pqxx::sql_error, e.what() = Failure during '[END COPY]': ERROR: publication "..._ch_publication" does not exist

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 the pgoutput plugin as the value of the publication_names option in pg_logical_slot_peek_binary_changes(...). PostgreSQL parses that option with SplitIdentifierString, which folds unquoted identifiers to lower case, so the consumer asks for test_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_names value, so SplitIdentifierString preserves its case and the consumer looks up the exact publication that CREATE 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 second CREATE PUBLICATION would drop/recreate the first's publication, diverging its ongoing replication). Names that are already all lower case are unaffected.

Separately, the single-table MaterializedPostgreSQL storage passed the raw remote table name to CREATE PUBLICATION ... FOR TABLE ONLY <name> without quoting — that path sets materialized_postgresql_tables_list directly and never goes through the quoting pass that the database engine applies in fetchRequiredTables. 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):

  • 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):

Fix MaterializedPostgreSQL silently stopping replication of changes when the PostgreSQL database or table name contains upper-case letters (the pgoutput consumer requested an unquoted, lower-cased publication name that did not match the case-preserving publication).

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

🤖 Generated with Claude Code

Version info

  • Merged into: 26.6.1.871

…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]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ac7be1a]

Summary: ✅


AI Review

Summary

This PR fixes MaterializedPostgreSQL replication for case-sensitive PostgreSQL database and table names by keeping the publication identifier case-preserving, quoting it when it is passed to the pgoutput publication_names option, and quoting the single-table engine's raw remote table name when building CREATE PUBLICATION. The current code addresses the prior single-table and case-collision review threads, and the added integration coverage exercises the initial snapshot and ongoing replication paths.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 13, 2026
…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 kssenii self-assigned this Jun 15, 2026
alexey-milovidov and others added 2 commits June 15, 2026 19:15
…-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]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.20% 85.20% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.40% 77.40% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 13/13 (100.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 16, 2026
Merged via the queue into master with commit 6068272 Jun 16, 2026
166 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-matpg-uppercase-publication branch June 16, 2026 15:18
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 16, 2026
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]>
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

3 participants