Skip to content

Refuse DETACH DATABASE of a MaterializedPostgreSQL database with tables before stopping its replication - #127918

Closed
buggins wants to merge 1 commit into
ClickHouse:masterfrom
buggins:fix-refused-drop-database-stops-replication-127875
Closed

buggins wants to merge 1 commit into
ClickHouse:masterfrom
buggins:fix-refused-drop-database-stops-replication-127875

Conversation

@buggins

@buggins buggins commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Related: #127875
Related: #123694

The tables of a MaterializedPostgreSQL database can be detached only permanently: DatabaseMaterializedPostgreSQL::detachTable always throws NOT_IMPLEMENTED ("DETACH TABLE not allowed, use DETACH PERMANENTLY"). So DETACH DATABASE of such a database with tables could never succeed, but it failed only at its first table, after InterpreterDropQuery::executeToDatabaseImpl had already called stopReplication and prepared all tables for shutdown (flushAndPrepareForShutdown; the first table also got flushAndShutdown). The database stayed attached, but changes in PostgreSQL were no longer replicated until a server restart, and nothing in the error said so.

Now executeToDatabaseImpl refuses DETACH DATABASE of a non-empty MaterializedPostgreSQL database right after waiting for the database to start, before stopping the replication or touching any table. The error code stays NOT_IMPLEMENTED; the message names the alternatives (DROP DATABASE, or DETACH TABLE ... PERMANENTLY for each table). Detaching an empty database is unchanged.

test_postgresql_replica_database_engine/test_2.py::test_detach_database_keeps_replication checks that DETACH DATABASE is refused and that rows inserted in PostgreSQL afterwards are still replicated. It fails on the official master build and passes with the fix (3 of 3 runs), as do the other tests in test_2.py.

Not covered: a DROP DATABASE that is refused by its checks (for example by max_table_size_to_drop) also runs after stopReplication, and leaves a Replicated or MaterializedPostgreSQL database without replication. That part of #127875 is left to #123694, which moves these checks; the issue stays open for it. Also not covered: a DETACH DATABASE while the initial snapshot of a new database is still running. Its nested tables do not exist yet, so the check lets it through, stopReplication waits for the snapshot to finish, and the detach fails at the first table as before.

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

DETACH DATABASE of a MaterializedPostgreSQL database with tables, which is not supported, no longer stops the replication of the database before failing.

🤖 Generated with Claude Code

…tables before stopping its replication

The tables of a `MaterializedPostgreSQL` database can be detached only permanently: `DatabaseMaterializedPostgreSQL::detachTable`
throws `NOT_IMPLEMENTED`. `DETACH DATABASE` failed at its first table, but only after `executeToDatabaseImpl` had stopped
the replication and prepared all tables for shutdown, so the database stayed attached and no longer replicated until a
restart. Refuse it before anything is stopped.

Related: ClickHouse#127875

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the comp-postgresql PostgreSQL-specific integration (table engine/function, logical replication bridge). label Oct 27, 2026
@buggins

buggins commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Closing in favour of #127896, which was opened earlier and fixes all of #127875: it refuses DETACH DATABASE of a MaterializedPostgreSQL database with tables before the replication is stopped, through DatabaseMaterializedPostgreSQL::assertCanBeDetached rather than a special case in InterpreterDropQuery, and also covers the refused DROP DATABASE. Its test_refused_database_drop_keeps_replication checks what the test here checked.

@buggins buggins closed this Oct 27, 2026
@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-postgresql PostgreSQL-specific integration (table engine/function, logical replication bridge). external Author is not a member of the ClickHouse organization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant