Skip to content

Commit 9655d2c

Browse files
committed
Also record direct dependency table -> server
1 parent 4f1078a commit 9655d2c

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/pgduckdb_background_worker.cpp

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ BgwMainLoop() {
231231
elog(LOG, "pg_duckdb background worker for database '%s' (%u) has now terminated.", db_name, MyDatabaseId);
232232
}
233233

234+
void recordDependencyOnMDServer(ObjectAddress *object_address) {
235+
ObjectAddress server_address = {
236+
.classId = ForeignServerRelationId,
237+
.objectId = GetMotherduckForeignServerOid(),
238+
.objectSubId = 0,
239+
};
240+
recordDependencyOn(object_address, &server_address, DEPENDENCY_NORMAL);
241+
}
242+
234243
} // namespace pgduckdb
235244

236245
extern "C" {
@@ -774,6 +783,25 @@ CreateTable(const char *postgres_schema_name, const char *table_name, const char
774783
return false;
775784
}
776785

786+
// Record the dependency on the MotherDuck server
787+
{
788+
HeapTuple new_table_tuple = SearchSysCache2(RELNAMENSP, CStringGetDatum(table_name), ObjectIdGetDatum(schema_oid));
789+
if (HeapTupleIsValid(new_table_tuple)) {
790+
Form_pg_class postgres_relation = (Form_pg_class)GETSTRUCT(new_table_tuple);
791+
auto oid = postgres_relation->oid;
792+
ReleaseSysCache(new_table_tuple);
793+
ObjectAddress object_address = {
794+
.classId = RelationRelationId,
795+
.objectId = oid,
796+
.objectSubId = 0,
797+
};
798+
recordDependencyOnMDServer(&object_address);
799+
} else {
800+
// Something went really wrong (since `create_table_succeeded`)
801+
elog(WARNING, "Failed to find table '%s.%s' so won't record dependency to 'motherduck' SERVER", table_name, postgres_schema_name);
802+
}
803+
}
804+
777805
if (did_delete_table) {
778806
/*
779807
* Commit the subtransaction that contains both the drop and the create that
@@ -933,12 +961,7 @@ CreateSchemaIfNotExists(const char *postgres_schema_name, bool is_default_db) {
933961
.objectId = schema_oid,
934962
.objectSubId = 0,
935963
};
936-
ObjectAddress server_address = {
937-
.classId = ForeignServerRelationId,
938-
.objectId = GetMotherduckForeignServerOid(), // pgduckdb::ExtensionOid(),
939-
.objectSubId = 0,
940-
};
941-
recordDependencyOn(&schema_address, &server_address, DEPENDENCY_NORMAL);
964+
recordDependencyOnMDServer(&schema_address);
942965
}
943966

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

0 commit comments

Comments
 (0)