forked from 2ndQuadrant/pglogical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpglogical_executor.c
236 lines (188 loc) · 5.56 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
/*-------------------------------------------------------------------------
*
* pglogical_executor.c
* pglogical execurot 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/pg_type.h"
#include "commands/trigger.h"
#include "executor/executor.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/planner.h"
#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/rel.h"
#include "utils/snapmgr.h"
#include "pglogical_node.h"
#include "pglogical_executor.h"
#include "pglogical_repset.h"
#include "pglogical_queue.h"
#include "pglogical.h"
List *pglogical_truncated_tables = NIL;
static ProcessUtility_hook_type next_ProcessUtility_hook = NULL;
EState *
create_estate_for_relation(Relation rel, bool hasTriggers)
{
EState *estate;
ResultRelInfo *resultRelInfo;
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;
resultRelInfo = makeNode(ResultRelInfo);
InitResultRelInfo(resultRelInfo,
rel,
1,
0);
/* Initialize executor state. */
estate = CreateExecutorState();
estate->es_result_relations = resultRelInfo;
estate->es_num_result_relations = 1;
estate->es_result_relation_info = resultRelInfo;
estate->es_range_table = list_make1(rte);
if (hasTriggers)
resultRelInfo->ri_TrigDesc = CopyTriggerDesc(rel->trigdesc);
if (resultRelInfo->ri_TrigDesc)
{
int n = resultRelInfo->ri_TrigDesc->numtriggers;
resultRelInfo->ri_TrigFunctions = (FmgrInfo *)
palloc0(n * sizeof(FmgrInfo));
resultRelInfo->ri_TrigWhenExprs = (List **)
palloc0(n * sizeof(List *));
/* Triggers might need a slot */
estate->es_trig_tuple_slot = ExecInitExtraTupleSlot(estate);
}
else
{
resultRelInfo->ri_TrigFunctions = NULL;
resultRelInfo->ri_TrigWhenExprs = NULL;
}
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(Node *parsetree,
const char *queryString,
ProcessUtilityContext context,
ParamListInfo params,
DestReceiver *dest,
char *completionTag)
{
if (nodeTag(parsetree) == T_TruncateStmt)
pglogical_start_truncate();
if (next_ProcessUtility_hook)
next_ProcessUtility_hook(parsetree, queryString, context, params,
dest, completionTag);
else
standard_ProcessUtility(parsetree, queryString, context, params,
dest, completionTag);
if (nodeTag(parsetree) == T_TruncateStmt)
pglogical_finish_truncate();
}
void
pglogical_executor_init(void)
{
next_ProcessUtility_hook = ProcessUtility_hook;
ProcessUtility_hook = pglogical_ProcessUtility;
}