forked from pgEdge/spock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spock_executor.c
548 lines (455 loc) · 14.2 KB
/
spock_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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*-------------------------------------------------------------------------
*
* spock_executor.c
* spock executor related functions
*
* Copyright (c) 2022-2023, pgEdge, Inc.
* Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, The Regents of the University of California
*
*-------------------------------------------------------------------------
*/
#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_authid_d.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"
#include "optimizer/optimizer.h"
#include "parser/analyze.h"
#include "parser/parse_coerce.h"
#include "parser/parse_relation.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 "spock_node.h"
#include "spock_executor.h"
#include "spock_repset.h"
#include "spock_queue.h"
#include "spock_dependency.h"
#include "spock.h"
List *spock_truncated_tables = NIL;
static DropBehavior spock_lastDropBehavior = DROP_RESTRICT;
static bool dropping_spock_obj = false;
static object_access_hook_type next_object_access_hook = NULL;
static ProcessUtility_hook_type next_ProcessUtility_hook = NULL;
extern post_parse_analyze_hook_type prev_post_parse_analyze_hook;
extern ExecutorStart_hook_type prev_executor_start_hook;
EState *
create_estate_for_relation(Relation rel, bool forwrite)
{
EState *estate;
RangeTblEntry *rte;
List *perminfos = NIL;
/* 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();
addRTEPermissionInfo(&perminfos, rte);
ExecInitRangeTable(estate, list_make1(rte)
#if PG_VERSION_NUM >= 160000
, perminfos
#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 *
spock_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
spock_start_truncate(void)
{
spock_truncated_tables = NIL;
}
static void
spock_finish_truncate(void)
{
ListCell *tlc;
SpockLocalNode *local_node;
Oid session_userid = GetUserId();
Oid save_userid = 0;
int save_sec_context = 0;
/* Elevate permissions to access spock objects */
GetUserIdAndSecContext(&save_userid, &save_sec_context);
SetUserIdAndSecContext(BOOTSTRAP_SUPERUSERID,
save_sec_context | SECURITY_LOCAL_USERID_CHANGE);
/* If this is not a spock node, don't do anything. */
local_node = get_local_node(false, true);
if (!local_node || !list_length(spock_truncated_tables))
{
SetUserIdAndSecContext(save_userid, save_sec_context);
return;
}
foreach (tlc, spock_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);
elog(DEBUG3, "truncating the table %s.%s", nspname, relname);
/* 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)
{
SpockRepSet *repset = (SpockRepSet *) lfirst(rlc);
repset_names = lappend(repset_names, pstrdup(repset->name));
elog(DEBUG1, "truncating the table %s.%s for %s repset",
nspname, relname, repset->name);
}
/* Queue the truncate for replication. */
queue_message(repset_names, session_userid,
QUEUE_COMMAND_TYPE_TRUNCATE, json.data);
}
}
/* Restore original session permissions */
SetUserIdAndSecContext(save_userid, save_sec_context);
list_free(spock_truncated_tables);
spock_truncated_tables = NIL;
}
/*
* remove_table_from_repsets
* removes given table from the other replication sets.
*/
static void
remove_table_from_repsets(Oid nodeid, Oid reloid, bool only_for_update)
{
ListCell *lc;
List *repsets;
repsets = get_table_replication_sets(nodeid, reloid);
foreach(lc, repsets)
{
SpockRepSet *rs = (SpockRepSet *) lfirst(lc);
if (only_for_update)
{
if (rs->replicate_update || rs->replicate_delete)
replication_set_remove_table(rs->id, reloid, true);
}
else
replication_set_remove_table(rs->id, reloid, true);
}
}
/*
* add_ddl_to_repset
* Check if the DDL statement can be added to the replication set. (For
* now only tables are added). The function also checks whether the table has
* needed indexes to be added to replication set. If not, they are ignored.
*/
static void
add_ddl_to_repset(Node *parsetree)
{
Relation targetrel;
SpockRepSet *repset;
SpockLocalNode *node;
Oid reloid = InvalidOid;
RangeVar *relation = NULL;
if (nodeTag(parsetree) == T_AlterTableStmt)
relation = castNode(AlterTableStmt, parsetree)->relation;
else if (nodeTag(parsetree) == T_CreateStmt)
relation = castNode(CreateStmt, parsetree)->relation;
else
{
/* only tables are added to repset. */
return;
}
node = get_local_node(false, true);
if (!node)
return;
targetrel = table_openrv(relation, AccessShareLock);
reloid = RelationGetRelid(targetrel);
/* UNLOGGED and TEMP relations cannot be part of replication set. */
if (!RelationNeedsWAL(targetrel))
{
/* remove table from the repsets. */
remove_table_from_repsets(node->node->id, reloid, false);
table_close(targetrel, NoLock);
return;
}
if (targetrel->rd_indexvalid == 0)
RelationGetIndexList(targetrel);
/* choose the 'default' repset, if table has PK or replica identity defined. */
if (OidIsValid(targetrel->rd_pkindex) || OidIsValid(targetrel->rd_replidindex))
{
repset = get_replication_set_by_name(node->node->id, DEFAULT_REPSET_NAME, false);
/*
* remove table from previous repsets, it will be added to 'default'
* down below.
*/
remove_table_from_repsets(node->node->id, reloid, false);
}
else
{
repset = get_replication_set_by_name(node->node->id, DEFAULT_INSONLY_REPSET_NAME, false);
/*
* no primary key defined. let's see if the table is part of any other
* repset or not?
*/
remove_table_from_repsets(node->node->id, reloid, true);
}
if (!OidIsValid(targetrel->rd_replidindex) &&
(repset->replicate_update || repset->replicate_delete))
{
table_close(targetrel, NoLock);
return;
}
/* Add if not already present. */
if (get_table_replication_row(repset->id, reloid, NULL, NULL) == NULL)
{
replication_set_add_table(repset->id, reloid, NIL, NULL);
elog(LOG, "table '%s' was added to '%s' replication set.",
relation->relname, repset->name);
}
table_close(targetrel, NoLock);
}
static void
spock_ProcessUtility(
PlannedStmt *pstmt,
const char *queryString,
#if PG_VERSION_NUM >= 140000
bool readOnlyTree,
#endif
ProcessUtilityContext context,
ParamListInfo params,
QueryEnvironment *queryEnv,
DestReceiver *dest,
#ifdef XCP
bool sentToRemote,
#endif
QueryCompletion *qc)
{
Node *parsetree = pstmt->utilityStmt;
static bool skip_ext_objs = false;
#ifndef XCP
#define sentToRemote NULL
#endif
dropping_spock_obj = false;
if (spock_deny_ddl && GetCommandLogLevel(parsetree) == LOGSTMT_DDL)
{
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("cannot execute %s within spock cluster",
CreateCommandName(parsetree))));
}
if (nodeTag(parsetree) == T_TruncateStmt)
spock_start_truncate();
if (nodeTag(parsetree) == T_DropStmt)
{
/*
* Allow one to drop replication tables without specifying the CASCADE
* option when in auto ddl replicatino mode.
*/
if (spock_enable_ddl_replication || in_spock_queue_ddl_command)
spock_lastDropBehavior = DROP_CASCADE;
else
spock_lastDropBehavior = ((DropStmt *)parsetree)->behavior;
}
if (context == PROCESS_UTILITY_TOPLEVEL &&
nodeTag(parsetree) == T_CreateExtensionStmt)
skip_ext_objs = true;
/* There's no reason we should be in a long lived context here */
Assert(CurrentMemoryContext != TopMemoryContext
&& CurrentMemoryContext != CacheMemoryContext);
if (next_ProcessUtility_hook)
SPKnext_ProcessUtility_hook(pstmt, queryString, readOnlyTree, context, params,
queryEnv, dest,
sentToRemote,
qc);
else
SPKstandard_ProcessUtility(pstmt, queryString, readOnlyTree, context, params,
queryEnv, dest,
sentToRemote,
qc);
if (nodeTag(parsetree) == T_TruncateStmt)
spock_finish_truncate();
/*
* we don't want to replicate if it's coming from spock.queue. But we do
* add tables to repset whenever there is one.
*/
if (in_spock_queue_ddl_command || in_spock_replicate_ddl_command)
{
/* if DDL is from spoc.queue, add it to the repset. */
if (in_spock_queue_ddl_command)
add_ddl_to_repset(parsetree);
/*
* Do Nothing else. Hook was called as a result of spock.replicate_ddl().
* The action has already been taken, so no need for the duplication.
*/
}
else if (GetCommandLogLevel(parsetree) == LOGSTMT_DDL &&
spock_enable_ddl_replication &&
get_local_node(false, true))
{
/*
* Normally only allow the top level commands to be replicate. However,
* we also want to allow DDL from within a function/procedure to replicate
* based on 'allow_ddl_from_functions'.
* One caveat though, don't allow DDLs within the extension scripts.
*/
if (context == PROCESS_UTILITY_TOPLEVEL ||
(context == PROCESS_UTILITY_QUERY &&
allow_ddl_from_functions &&
!skip_ext_objs))
{
const char *curr_qry;
int loc = pstmt->stmt_location;
int len = pstmt->stmt_len;
skip_ext_objs = false;
queryString = CleanQuerytext(queryString, &loc, &len);
curr_qry = pnstrdup(queryString, len);
spock_auto_replicate_ddl(curr_qry,
list_make1(DEFAULT_INSONLY_REPSET_NAME),
GetUserNameFromId(GetUserId(), false),
parsetree);
if (spock_include_ddl_repset)
add_ddl_to_repset(parsetree);
}
}
}
/*
* Handle object drop.
*
* Calls to dependency tracking code.
*/
static void
spock_object_access(ObjectAccessType access,
Oid classId,
Oid objectId,
int subId,
void *arg)
{
Oid save_userid = 0;
int save_sec_context = 0;
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 spock itself? */
if (classId == ExtensionRelationId &&
objectId == get_extension_oid(EXTENSION_NAME, true) &&
objectId != InvalidOid /* Should not happen but check anyway */)
dropping_spock_obj = true;
/* Dropping relation within spock? */
if (classId == RelationRelationId)
{
Oid relnspoid;
Oid spknspoid;
spknspoid = get_namespace_oid(EXTENSION_NAME, true);
relnspoid = get_rel_namespace(objectId);
if (spknspoid == relnspoid)
dropping_spock_obj = true;
}
/*
* Don't do extra dependency checks for internal objects, those
* should be handled by Postgres.
*/
if (dropping_spock_obj)
return;
/*
* Check that we have a local node. We need to elevate access
* because this is called as an executor DROP hook under the
* session user, who not necessarily has access permission to
* Spock extension objects.
*/
GetUserIdAndSecContext(&save_userid, &save_sec_context);
SetUserIdAndSecContext(BOOTSTRAP_SUPERUSERID,
save_sec_context | SECURITY_LOCAL_USERID_CHANGE);
if(!get_local_node(false, true))
{
SetUserIdAndSecContext(save_userid, save_sec_context);
return;
}
ObjectAddressSubSet(object, classId, objectId, subId);
if (SessionReplicationRole == SESSION_REPLICATION_ROLE_REPLICA)
behavior = DROP_CASCADE;
else
behavior = spock_lastDropBehavior;
spock_checkDependency(&object, behavior);
/* Restore previous session privileges */
SetUserIdAndSecContext(save_userid, save_sec_context);
}
}
void
spock_executor_init(void)
{
next_ProcessUtility_hook = ProcessUtility_hook;
ProcessUtility_hook = spock_ProcessUtility;
/* Object access hook */
next_object_access_hook = object_access_hook;
object_access_hook = spock_object_access;
/* analyzer hook for spock readonly */
prev_post_parse_analyze_hook = post_parse_analyze_hook;
post_parse_analyze_hook = spock_post_parse_analyze;
/* executor hook for spock readonly */
prev_executor_start_hook = ExecutorStart_hook;
ExecutorStart_hook = spock_ExecutorStart;
}