forked from 2ndQuadrant/pglogical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pglogical_executor.c
342 lines (278 loc) · 8.13 KB
/
pglogical_executor.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*-------------------------------------------------------------------------
*
* pglogical_executor.c
* pglogical executor related functions
*
* Copyright (c) 2015, PostgreSQL Global Development Group
*
* IDENTIFICATION
* pglogical_executor.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "miscadmin.h"
#include "access/hash.h"
#include "access/htup_details.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "catalog/dependency.h"
#include "catalog/namespace.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_extension.h"
#include "catalog/pg_type.h"
#include "commands/extension.h"
#include "commands/trigger.h"
#include "executor/executor.h"
#include "nodes/nodeFuncs.h"
#if PG_VERSION_NUM >= 120000
#include "optimizer/optimizer.h"
#else
#include "optimizer/planner.h"
#endif
#include "parser/parse_coerce.h"
#include "tcop/utility.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/json.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/snapmgr.h"
#include "pglogical_node.h"
#include "pglogical_executor.h"
#include "pglogical_repset.h"
#include "pglogical_queue.h"
#include "pglogical_dependency.h"
#include "pglogical.h"
List *pglogical_truncated_tables = NIL;
static DropBehavior pglogical_lastDropBehavior = DROP_RESTRICT;
static bool dropping_pglogical_obj = false;
static object_access_hook_type next_object_access_hook = NULL;
static ProcessUtility_hook_type next_ProcessUtility_hook = NULL;
EState *
create_estate_for_relation(Relation rel, bool forwrite)
{
EState *estate;
RangeTblEntry *rte;
/* Dummy range table entry needed by executor. */
rte = makeNode(RangeTblEntry);
rte->rtekind = RTE_RELATION;
rte->relid = RelationGetRelid(rel);
rte->relkind = rel->rd_rel->relkind;
/* Initialize executor state. */
estate = CreateExecutorState();
#if PG_VERSION_NUM >= 120000
ExecInitRangeTable(estate, list_make1(rte));
#elif PG_VERSION_NUM >= 110000 && SECONDQ_VERSION_NUM >= 103
/* 2ndQPostgres 11 r1.3 changes executor API */
estate->es_range_table = alist_add(NULL, rte);
#else
estate->es_range_table = list_make1(rte);
#endif
#if PG_VERSION_NUM < 120000
if (rel->trigdesc)
estate->es_trig_tuple_slot = ExecInitExtraTupleSlot(estate);
#endif
estate->es_output_cid = GetCurrentCommandId(forwrite);
return estate;
}
ExprContext *
prepare_per_tuple_econtext(EState *estate, TupleDesc tupdesc)
{
ExprContext *econtext;
MemoryContext oldContext;
econtext = GetPerTupleExprContext(estate);
oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
econtext->ecxt_scantuple = ExecInitExtraTupleSlot(estate);
MemoryContextSwitchTo(oldContext);
ExecSetSlotDescriptor(econtext->ecxt_scantuple, tupdesc);
return econtext;
}
ExprState *
pglogical_prepare_row_filter(Node *row_filter)
{
ExprState *exprstate;
Expr *expr;
Oid exprtype;
exprtype = exprType(row_filter);
expr = (Expr *) coerce_to_target_type(NULL, /* no UNKNOWN params here */
row_filter, exprtype,
BOOLOID, -1,
COERCION_ASSIGNMENT,
COERCE_IMPLICIT_CAST,
-1);
/* This should never happen but just to be sure. */
if (expr == NULL)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("cannot cast the row_filter to boolean"),
errhint("You will need to rewrite the row_filter.")));
expr = expression_planner(expr);
exprstate = ExecInitExpr(expr, NULL);
return exprstate;
}
static void
pglogical_start_truncate(void)
{
pglogical_truncated_tables = NIL;
}
static void
pglogical_finish_truncate(void)
{
ListCell *tlc;
PGLogicalLocalNode *local_node;
/* If this is not pglogical node, don't do anything. */
local_node = get_local_node(false, true);
if (!local_node || !list_length(pglogical_truncated_tables))
return;
foreach (tlc, pglogical_truncated_tables)
{
Oid reloid = lfirst_oid(tlc);
char *nspname;
char *relname;
List *repsets;
StringInfoData json;
/* Format the query. */
nspname = get_namespace_name(get_rel_namespace(reloid));
relname = get_rel_name(reloid);
/* It's easier to construct json manually than via Jsonb API... */
initStringInfo(&json);
appendStringInfo(&json, "{\"schema_name\": ");
escape_json(&json, nspname);
appendStringInfo(&json, ",\"table_name\": ");
escape_json(&json, relname);
appendStringInfo(&json, "}");
repsets = get_table_replication_sets(local_node->node->id, reloid);
if (list_length(repsets))
{
List *repset_names = NIL;
ListCell *rlc;
foreach (rlc, repsets)
{
PGLogicalRepSet *repset = (PGLogicalRepSet *) lfirst(rlc);
repset_names = lappend(repset_names, pstrdup(repset->name));
}
/* Queue the truncate for replication. */
queue_message(repset_names, GetUserId(),
QUEUE_COMMAND_TYPE_TRUNCATE, json.data);
}
}
list_free(pglogical_truncated_tables);
pglogical_truncated_tables = NIL;
}
static void
pglogical_ProcessUtility(
#if PG_VERSION_NUM >= 100000
PlannedStmt *pstmt,
#else
Node *pstmt,
#endif
const char *queryString,
#if PG_VERSION_NUM >= 140000
bool readOnlyTree,
#endif
ProcessUtilityContext context,
ParamListInfo params,
#if PG_VERSION_NUM >= 100000
QueryEnvironment *queryEnv,
#endif
DestReceiver *dest,
#ifdef XCP
bool sentToRemote,
#endif
QueryCompletion *qc)
{
#if PG_VERSION_NUM >= 100000
Node *parsetree = pstmt->utilityStmt;
#else
Node *parsetree = pstmt;
#define queryEnv NULL
#endif
#ifndef XCP
#define sentToRemote NULL
#endif
dropping_pglogical_obj = false;
if (nodeTag(parsetree) == T_TruncateStmt)
pglogical_start_truncate();
if (nodeTag(parsetree) == T_DropStmt)
pglogical_lastDropBehavior = ((DropStmt *)parsetree)->behavior;
/* There's no reason we should be in a long lived context here */
Assert(CurrentMemoryContext != TopMemoryContext
&& CurrentMemoryContext != CacheMemoryContext);
if (next_ProcessUtility_hook)
PGLnext_ProcessUtility_hook(pstmt, queryString, readOnlyTree, context, params,
queryEnv, dest,
sentToRemote,
qc);
else
PGLstandard_ProcessUtility(pstmt, queryString, readOnlyTree, context, params,
queryEnv, dest,
sentToRemote,
qc);
if (nodeTag(parsetree) == T_TruncateStmt)
pglogical_finish_truncate();
}
/*
* Handle object drop.
*
* Calls to dependency tracking code.
*/
static void
pglogical_object_access(ObjectAccessType access,
Oid classId,
Oid objectId,
int subId,
void *arg)
{
if (next_object_access_hook)
(*next_object_access_hook) (access, classId, objectId, subId, arg);
if (access == OAT_DROP)
{
ObjectAccessDrop *drop_arg = (ObjectAccessDrop *) arg;
ObjectAddress object;
DropBehavior behavior;
/* No need to check for internal deletions. */
if ((drop_arg->dropflags & PERFORM_DELETION_INTERNAL) != 0)
return;
/* Dropping pglogical itself? */
if (classId == ExtensionRelationId &&
objectId == get_extension_oid(EXTENSION_NAME, true) &&
objectId != InvalidOid /* Should not happen but check anyway */)
dropping_pglogical_obj = true;
/* Dropping relation within pglogical? */
if (classId == RelationRelationId)
{
Oid relnspoid;
Oid pglnspoid;
pglnspoid = get_namespace_oid(EXTENSION_NAME, true);
relnspoid = get_rel_namespace(objectId);
if (pglnspoid == relnspoid)
dropping_pglogical_obj = true;
}
/*
* Don't do extra dependency checks for internal objects, those
* should be handled by Postgres.
*/
if (dropping_pglogical_obj)
return;
/* No local node? */
if (!get_local_node(false, true))
return;
ObjectAddressSubSet(object, classId, objectId, subId);
if (SessionReplicationRole == SESSION_REPLICATION_ROLE_REPLICA)
behavior = DROP_CASCADE;
else
behavior = pglogical_lastDropBehavior;
pglogical_checkDependency(&object, behavior);
}
}
void
pglogical_executor_init(void)
{
next_ProcessUtility_hook = ProcessUtility_hook;
ProcessUtility_hook = pglogical_ProcessUtility;
/* Object access hook */
next_object_access_hook = object_access_hook;
object_access_hook = pglogical_object_access;
}