Skip to content

Commit 7a99398

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

File tree

3 files changed

+53
-19
lines changed

3 files changed

+53
-19
lines changed

include/pgduckdb/pg/relations.hpp

+2
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

+5
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,9 @@ GetRelationName(Relation rel) {
172172
return RelationGetRelationName(rel);
173173
}
174174

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

src/pgduckdb_background_worker.cpp

+46-19
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "duckdb/main/attached_database.hpp"
1515
#include "pgduckdb/pgduckdb_types.hpp"
1616
#include "pgduckdb/pgduckdb_utils.hpp"
17+
#include "pgduckdb/pg/relations.hpp"
1718
#include "pgduckdb/utility/cpp_wrapper.hpp"
1819
#include <string>
1920
#include <unordered_map>
@@ -22,34 +23,35 @@
2223

2324
extern "C" {
2425
#include "postgres.h"
25-
#include "fmgr.h"
2626
#include "access/xact.h"
27-
#include "miscadmin.h"
28-
#include "pgstat.h"
29-
#include "executor/spi.h"
27+
#include "catalog/dependency.h"
28+
#include "catalog/namespace.h"
29+
#include "catalog/objectaddress.h"
30+
#include "catalog/pg_authid.h"
31+
#include "catalog/pg_class.h"
32+
#include "catalog/pg_extension.h"
33+
#include "catalog/pg_foreign_server.h"
34+
#include "catalog/pg_namespace.h"
3035
#include "commands/dbcommands.h"
3136
#include "common/file_utils.h"
37+
#include "executor/spi.h"
38+
#include "fmgr.h"
39+
#include "miscadmin.h"
40+
#include "pgstat.h"
3241
#include "postmaster/bgworker.h"
3342
#include "postmaster/interrupt.h"
3443
#include "storage/ipc.h"
3544
#include "storage/latch.h"
36-
#include "tcop/tcopprot.h"
3745
#include "storage/proc.h"
3846
#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"
47+
#include "tcop/tcopprot.h"
4648
#include "utils/acl.h"
49+
#include "utils/builtins.h"
4750
#include "utils/guc.h"
4851
#include "utils/memutils.h"
4952
#include "utils/palloc.h"
5053
#include "utils/snapmgr.h"
5154
#include "utils/syscache.h"
52-
#include "catalog/objectaddress.h"
5355
}
5456

5557
#include "pgduckdb/pgduckdb.h"
@@ -231,6 +233,16 @@ BgwMainLoop() {
231233
elog(LOG, "pg_duckdb background worker for database '%s' (%u) has now terminated.", db_name, MyDatabaseId);
232234
}
233235

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

236248
extern "C" {
@@ -774,6 +786,26 @@ CreateTable(const char *postgres_schema_name, const char *table_name, const char
774786
return false;
775787
}
776788

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

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

0 commit comments

Comments
 (0)