Skip to content

Commit 537ce12

Browse files
committed
Drop MD tables when disabled
1 parent 50ff487 commit 537ce12

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/pgduckdb_background_worker.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ extern "C" {
4242
#include "catalog/namespace.h"
4343
#include "catalog/pg_namespace.h"
4444
#include "catalog/pg_extension.h"
45+
#include "catalog/pg_foreign_server.h"
4546
#include "utils/acl.h"
4647
#include "utils/guc.h"
4748
#include "utils/memutils.h"
@@ -918,7 +919,7 @@ CreateSchemaIfNotExists(const char *postgres_schema_name, bool is_default_db) {
918919
if (!is_default_db) {
919920
/*
920921
* For ddb$ schemas we need to record a dependency between the schema
921-
* and the extension, so that DROP EXTENSION also drops these schemas.
922+
* and the FDW, so that DROP SERVER motherduck also drops these schemas.
922923
*/
923924
schema_oid = get_namespace_oid(postgres_schema_name, true);
924925
if (schema_oid == InvalidOid) {
@@ -932,12 +933,12 @@ CreateSchemaIfNotExists(const char *postgres_schema_name, bool is_default_db) {
932933
.objectId = schema_oid,
933934
.objectSubId = 0,
934935
};
935-
ObjectAddress extension_address = {
936-
.classId = ExtensionRelationId,
937-
.objectId = pgduckdb::ExtensionOid(),
936+
ObjectAddress server_address = {
937+
.classId = ForeignServerRelationId,
938+
.objectId = GetMotherduckForeignServerOid(), // pgduckdb::ExtensionOid(),
938939
.objectSubId = 0,
939940
};
940-
recordDependencyOn(&schema_address, &extension_address, DEPENDENCY_NORMAL);
941+
recordDependencyOn(&schema_address, &server_address, DEPENDENCY_NORMAL);
941942
}
942943

943944
/* Success, so we commit the subtransaction */

test/pycheck/motherduck_test.py

+17
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,23 @@ def test_md_create_table(md_cur: Cursor, ddb):
6262
md_cur.sql("CREATE TABLE t4(a int) USING heap")
6363

6464

65+
def test_md_table_dropped_when_disabled(md_cur: Cursor, ddb):
66+
ddb.sql("CREATE TABLE t1(a int, b varchar)")
67+
ddb.sql("INSERT INTO t1 VALUES (1, 'abc')")
68+
md_cur.wait_until_table_exists("t1")
69+
70+
assert md_cur.sql("SELECT * FROM t1") == (1, "abc")
71+
72+
# Disable MD, should CASCADE drop the table
73+
md_cur.sql("DROP SERVER motherduck CASCADE;")
74+
75+
with pytest.raises(
76+
psycopg.errors.UndefinedTable,
77+
match=r'relation "t1" does not exist',
78+
):
79+
md_cur.sql("SELECT * FROM t1")
80+
81+
6582
def test_md_default_db_escape(pg: Postgres, ddb, default_db_name, md_test_user):
6683
# Make sure MD is not enabled
6784
pg.sql("DROP SERVER IF EXISTS motherduck CASCADE;")

0 commit comments

Comments
 (0)