-
Notifications
You must be signed in to change notification settings - Fork 641
/
foreign_data_wrapper.c
125 lines (102 loc) · 3.42 KB
/
foreign_data_wrapper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*-------------------------------------------------------------------------
*
* foreign_data_wrapper.c
* Commands for FOREIGN DATA WRAPPER statements.
*
* Copyright (c) Citus Data, Inc.
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "catalog/pg_foreign_data_wrapper.h"
#include "distributed/commands/utility_hook.h"
#include "distributed/commands.h"
#include "distributed/deparser.h"
#include "distributed/listutils.h"
#include "distributed/metadata/distobject.h"
#include "distributed/metadata_sync.h"
#include "distributed/worker_transaction.h"
#include "foreign/foreign.h"
#include "nodes/primnodes.h"
List *
PreprocessCreateFdwStmt(Node *node, const char *queryString,
ProcessUtilityContext processUtilityContext)
{
EnsureCoordinator();
/* to prevent recursion with mx we disable ddl propagation */
List *commands = list_make3(DISABLE_DDL_PROPAGATION,
(void *) queryString,
ENABLE_DDL_PROPAGATION);
return NodeDDLTaskList(NON_COORDINATOR_NODES, commands);
}
List *
PreprocessDropFdwStmt(Node *node, const char *queryString,
ProcessUtilityContext processUtilityContext)
{
DropStmt *stmt = castNode(DropStmt, node);
Assert(stmt->removeType == OBJECT_FDW);
List *allFdwNamesToDrop = stmt->objects;
List *distributedFdwAddresses = NIL;
List *distributedFdwNames = NIL;
Value *fdwValue = NULL;
foreach_ptr(fdwValue, stmt->objects)
{
char *fdwString = strVal(fdwValue);
ForeignDataWrapper *fdw = GetForeignDataWrapperByName(fdwString, false);
ObjectAddress address = { 0 };
ObjectAddressSet(address, ForeignDataWrapperRelationId, fdw->fdwid);
/* filter distributed fdws */
if (IsObjectDistributed(&address))
{
distributedFdwAddresses = lappend(distributedFdwAddresses, &address);
distributedFdwNames = lappend(distributedFdwNames, fdwValue);
}
}
if (list_length(distributedFdwNames) <= 0)
{
return NIL;
}
EnsureCoordinator();
/* unmark each distributed fdw */
ObjectAddress *address = NULL;
foreach_ptr(address, distributedFdwAddresses)
{
UnmarkObjectDistributed(address);
}
/*
* Temporary swap the lists of objects to delete with the distributed
* objects and deparse to an sql statement for the workers.
* Then switch back to allFdwNamesToDrop to drop all specified
* servers in coordinator after PreprocessDropForeignServerStmt completes
* its execution.
*/
stmt->objects = distributedFdwNames;
const char *deparsedStmt = DeparseTreeNode((Node *) stmt);
stmt->objects = allFdwNamesToDrop;
/*
* To prevent recursive propagation in mx architecture, we disable ddl
* propagation before sending the command to workers.
*/
List *commands = list_make3(DISABLE_DDL_PROPAGATION,
(void *) deparsedStmt,
ENABLE_DDL_PROPAGATION);
return NodeDDLTaskList(NON_COORDINATOR_NODES, commands);
}
List *
PostprocessCreateFdwStmt(Node *node, const char *queryString)
{
ObjectAddress typeAddress = GetObjectAddressFromParseTree(node, false);
EnsureDependenciesExistOnAllNodes(&typeAddress);
MarkObjectDistributed(&typeAddress);
return NIL;
}
ObjectAddress
CreateFdwStmtObjectAddress(Node *node, bool missing_ok)
{
CreateFdwStmt *stmt = castNode(CreateFdwStmt, node);
ForeignDataWrapper *fdw = GetForeignDataWrapperByName(stmt->fdwname, false);
Oid fdwOid = fdw->fdwid;
ObjectAddress address = { 0 };
ObjectAddressSet(address, ForeignDataWrapperRelationId, fdwOid);
return address;
}