|
14 | 14 | #include "duckdb/main/attached_database.hpp"
|
15 | 15 | #include "pgduckdb/pgduckdb_types.hpp"
|
16 | 16 | #include "pgduckdb/pgduckdb_utils.hpp"
|
| 17 | +#include "pgduckdb/pg/relations.hpp" |
17 | 18 | #include "pgduckdb/utility/cpp_wrapper.hpp"
|
18 | 19 | #include <string>
|
19 | 20 | #include <unordered_map>
|
|
22 | 23 |
|
23 | 24 | extern "C" {
|
24 | 25 | #include "postgres.h"
|
25 |
| -#include "fmgr.h" |
26 | 26 | #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" |
30 | 35 | #include "commands/dbcommands.h"
|
31 | 36 | #include "common/file_utils.h"
|
| 37 | +#include "executor/spi.h" |
| 38 | +#include "fmgr.h" |
| 39 | +#include "miscadmin.h" |
| 40 | +#include "pgstat.h" |
32 | 41 | #include "postmaster/bgworker.h"
|
33 | 42 | #include "postmaster/interrupt.h"
|
34 | 43 | #include "storage/ipc.h"
|
35 | 44 | #include "storage/latch.h"
|
36 |
| -#include "tcop/tcopprot.h" |
37 | 45 | #include "storage/proc.h"
|
38 | 46 | #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" |
46 | 48 | #include "utils/acl.h"
|
| 49 | +#include "utils/builtins.h" |
47 | 50 | #include "utils/guc.h"
|
48 | 51 | #include "utils/memutils.h"
|
49 | 52 | #include "utils/palloc.h"
|
50 | 53 | #include "utils/snapmgr.h"
|
51 | 54 | #include "utils/syscache.h"
|
52 |
| -#include "catalog/objectaddress.h" |
53 | 55 | }
|
54 | 56 |
|
55 | 57 | #include "pgduckdb/pgduckdb.h"
|
@@ -231,6 +233,16 @@ BgwMainLoop() {
|
231 | 233 | elog(LOG, "pg_duckdb background worker for database '%s' (%u) has now terminated.", db_name, MyDatabaseId);
|
232 | 234 | }
|
233 | 235 |
|
| 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 | + |
234 | 246 | } // namespace pgduckdb
|
235 | 247 |
|
236 | 248 | extern "C" {
|
@@ -774,6 +786,26 @@ CreateTable(const char *postgres_schema_name, const char *table_name, const char
|
774 | 786 | return false;
|
775 | 787 | }
|
776 | 788 |
|
| 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 | + |
777 | 809 | if (did_delete_table) {
|
778 | 810 | /*
|
779 | 811 | * 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) {
|
933 | 965 | .objectId = schema_oid,
|
934 | 966 | .objectSubId = 0,
|
935 | 967 | };
|
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); |
942 | 969 | }
|
943 | 970 |
|
944 | 971 | /* Success, so we commit the subtransaction */
|
|
0 commit comments