Skip to content

Commit d313255

Browse files
committed
Also record direct dependency table -> server
1 parent 537ce12 commit d313255

File tree

3 files changed

+51
-19
lines changed

3 files changed

+51
-19
lines changed

include/pgduckdb/pg/relations.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ const char *QuoteIdentifier(const char *ident);
3535

3636
const char *GetRelationName(Relation rel);
3737

38+
Oid GetOid(Form_pg_class* rel);
39+
3840
} // namespace pgduckdb

src/pg/relations.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,8 @@ GetRelationName(Relation rel) {
172172
return RelationGetRelationName(rel);
173173
}
174174

175+
Oid GetOid(Form_pg_class* rel) {
176+
return rel->oid;
177+
}
178+
175179
} // namespace pgduckdb

src/pgduckdb_background_worker.cpp

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,35 @@
2222

2323
extern "C" {
2424
#include "postgres.h"
25-
#include "fmgr.h"
2625
#include "access/xact.h"
27-
#include "miscadmin.h"
28-
#include "pgstat.h"
29-
#include "executor/spi.h"
26+
#include "catalog/dependency.h"
27+
#include "catalog/namespace.h"
28+
#include "catalog/objectaddress.h"
29+
#include "catalog/pg_authid.h"
30+
#include "catalog/pg_class.h"
31+
#include "catalog/pg_extension.h"
32+
#include "catalog/pg_foreign_server.h"
33+
#include "catalog/pg_namespace.h"
3034
#include "commands/dbcommands.h"
3135
#include "common/file_utils.h"
36+
#include "executor/spi.h"
37+
#include "fmgr.h"
38+
#include "miscadmin.h"
39+
#include "pgstat.h"
3240
#include "postmaster/bgworker.h"
3341
#include "postmaster/interrupt.h"
3442
#include "storage/ipc.h"
3543
#include "storage/latch.h"
36-
#include "tcop/tcopprot.h"
3744
#include "storage/proc.h"
3845
#include "storage/shmem.h"
39-
#include "utils/builtins.h"
40-
#include "catalog/dependency.h"
41-
#include "catalog/pg_authid.h"
42-
#include "catalog/namespace.h"
43-
#include "catalog/pg_namespace.h"
44-
#include "catalog/pg_extension.h"
45-
#include "catalog/pg_foreign_server.h"
46+
#include "tcop/tcopprot.h"
4647
#include "utils/acl.h"
48+
#include "utils/builtins.h"
4749
#include "utils/guc.h"
4850
#include "utils/memutils.h"
4951
#include "utils/palloc.h"
5052
#include "utils/snapmgr.h"
5153
#include "utils/syscache.h"
52-
#include "catalog/objectaddress.h"
5354
}
5455

5556
#include "pgduckdb/pgduckdb.h"
@@ -231,6 +232,16 @@ BgwMainLoop() {
231232
elog(LOG, "pg_duckdb background worker for database '%s' (%u) has now terminated.", db_name, MyDatabaseId);
232233
}
233234

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

236247
extern "C" {
@@ -774,6 +785,26 @@ CreateTable(const char *postgres_schema_name, const char *table_name, const char
774785
return false;
775786
}
776787

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

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

0 commit comments

Comments
 (0)