-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbdr_ddlrep_truncate.c
320 lines (261 loc) · 8.77 KB
/
bdr_ddlrep_truncate.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
/* -------------------------------------------------------------------------
*
* bdr_ddlrep_truncate.c
* Support for replicating table truncation
*
* Copyright (C) 2012-2015, PostgreSQL Global Development Group
*
* IDENTIFICATION
* bdr_ddlrep_truncate.c
*
* -------------------------------------------------------------------------
*/
#include "postgres.h"
#include "bdr.h"
#include "access/xact.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_trigger.h"
#include "commands/event_trigger.h"
#include "commands/trigger.h"
#include "nodes/makefuncs.h"
#include "parser/parse_func.h"
#include "replication/origin.h"
#include "storage/lmgr.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
PGDLLEXPORT Datum bdr_queue_truncate(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(bdr_queue_truncate);
PGDLLEXPORT Datum bdr_truncate_trigger_add(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(bdr_truncate_trigger_add);
PGDLLEXPORT Datum bdr_internal_create_truncate_trigger(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(bdr_internal_create_truncate_trigger);
static List *bdr_truncated_tables = NIL;
/*
* Create a TRUNCATE trigger for a persistent table and mark
* it tgisinternal so that it's not dumped by pg_dump.
*
* We create such triggers automatically on restore or
* bdr_group_create so dumping the triggers isn't necessary,
* and dumping them makes it harder to restore to a DB
* without BDR.
*
* The target object oid may be InvalidOid, in which case
* it will be looked up from the catalogs.
*/
static void
bdr_create_truncate_trigger(char *schemaname, char *relname, Oid relid)
{
CreateTrigStmt *tgstmt;
RangeVar *relrv = makeRangeVar(schemaname, relname, -1);
Relation rel;
List *funcname;
ObjectAddress tgaddr, procaddr;
int nfound;
Oid fargtypes[1]; /* dummy, see 0a52d378 */
if (OidIsValid(relid))
rel = heap_open(relid, AccessExclusiveLock);
else
rel = heap_openrv(relrv, AccessExclusiveLock);
funcname = list_make2(makeString("bdr"), makeString("queue_truncate"));
/*
* Check for already existing trigger on the table to avoid adding
* duplicate ones.
*/
if (rel->trigdesc)
{
Trigger *trigger = rel->trigdesc->triggers;
int i;
Oid funcoid = LookupFuncName(funcname, 0, &fargtypes[0], false);
for (i = 0; i < rel->trigdesc->numtriggers; i++)
{
if (!TRIGGER_FOR_TRUNCATE(trigger->tgtype))
continue;
if (trigger->tgfoid == funcoid)
{
heap_close(rel, AccessExclusiveLock);
return;
}
trigger++;
}
}
tgstmt = makeNode(CreateTrigStmt);
tgstmt->trigname = "truncate_trigger";
tgstmt->relation = copyObject(relrv);
tgstmt->funcname = funcname;
tgstmt->args = NIL;
tgstmt->row = false;
tgstmt->timing = TRIGGER_TYPE_AFTER;
tgstmt->events = TRIGGER_TYPE_TRUNCATE;
tgstmt->columns = NIL;
tgstmt->whenClause = NULL;
tgstmt->isconstraint = false;
tgstmt->deferrable = false;
tgstmt->initdeferred = false;
tgstmt->constrrel = NULL;
tgaddr = CreateTrigger(tgstmt, NULL, rel->rd_id, InvalidOid,
InvalidOid, InvalidOid,
InvalidOid, InvalidOid, NULL,
true /* tgisinternal */, false);
/*
* The trigger was created with a 'n'ormal dependency on
* bdr.queue_truncate(), which will cause DROP EXTENSION bdr to fail with
* something like:
*
* trigger truncate_trigger_26908 on table sometable depends on function bdr.queue_truncate()
*
* We want the trigger to bdr dropped if EITHER the BDR extension is dropped
* (thus so is bdr.queue_truncate()) OR if the table the trigger is attached
* to is dropped, so we want an automatic dependency on the target table.
* CreateTrigger doesn't offer this directly and we'd rather not cause an
* API break by adding a param, so just twiddle the created dependency.
*/
procaddr.classId = ProcedureRelationId;
procaddr.objectId = LookupFuncName(list_make2(makeString("bdr"), makeString("queue_truncate")), 0, &fargtypes[0], false);
procaddr.objectSubId = 0;
/* We need to be able to see the pg_depend entry to delete it */
CommandCounterIncrement();
if ((nfound = deleteDependencyRecordsForClass(tgaddr.classId, tgaddr.objectId, ProcedureRelationId, 'n')) != 1)
{
ereport(ERROR,
(errmsg_internal("expected exectly one 'n'ormal dependency from a newly created trigger to a pg_proc entry, got %u", nfound)));
}
recordDependencyOn(&tgaddr, &procaddr, DEPENDENCY_AUTO);
/* We should also record that the trigger is part of the extension */
recordDependencyOnCurrentExtension(&tgaddr, false);
heap_close(rel, AccessExclusiveLock);
/* Make the new trigger visible within this session */
CommandCounterIncrement();
}
/*
* Wrapper to call bdr_create_truncate_trigger from SQL for
* during bdr_group_create(...).
*/
Datum
bdr_internal_create_truncate_trigger(PG_FUNCTION_ARGS)
{
Oid relid = PG_GETARG_OID(0);
Relation rel = heap_open(relid, AccessExclusiveLock);
char *schemaname = get_namespace_name(RelationGetNamespace(rel));
bdr_create_truncate_trigger(schemaname, RelationGetRelationName(rel), relid);
pfree(schemaname);
heap_close(rel, AccessExclusiveLock);
PG_RETURN_VOID();
}
/*
* bdr_truncate_trigger_add
*
* This function, which is called as an event trigger handler, adds TRUNCATE
* trigger to newly created tables where appropriate.
*
* Note: it's important that this function be named so that it comes
* after bdr_queue_ddl_commands when triggers are alphabetically sorted.
*/
Datum
bdr_truncate_trigger_add(PG_FUNCTION_ARGS)
{
EventTriggerData *trigdata;
if (!CALLED_AS_EVENT_TRIGGER(fcinfo)) /* internal error */
elog(ERROR, "not fired by event trigger manager");
/*
* Since triggers are created tgisinternal and their creation is
* not replicated or dumped we must create truncate triggers on
* tables even if they're created by a replicated command or
* restore of a dump. Recursion is not a problem since we don't
* queue anything for replication anymore.
*/
trigdata = (EventTriggerData *) fcinfo->context;
if (strcmp(trigdata->tag, "CREATE TABLE") == 0 &&
IsA(trigdata->parsetree, CreateStmt))
{
CreateStmt *stmt = (CreateStmt *)trigdata->parsetree;
char *nspname;
/* Skip temporary and unlogged tables */
if (stmt->relation->relpersistence != RELPERSISTENCE_PERMANENT)
PG_RETURN_VOID();
nspname = get_namespace_name(RangeVarGetCreationNamespace(stmt->relation));
/*
* By this time the relation has been created so it's safe to
* call RangeVarGetRelid
*/
bdr_create_truncate_trigger(nspname, stmt->relation->relname, InvalidOid);
pfree(nspname);
}
PG_RETURN_VOID();
}
/*
* Initializes the internal table list.
*/
void
bdr_start_truncate(void)
{
bdr_truncated_tables = NIL;
}
/*
* Write the list of truncated tables to the replication queue.
*/
void
bdr_finish_truncate(void)
{
ListCell *lc;
char *sep = "";
StringInfoData buf;
/* Nothing to do if the list of truncated table is empty. */
if (list_length(bdr_truncated_tables) < 1)
return;
initStringInfo(&buf);
appendStringInfoString(&buf, "TRUNCATE TABLE ONLY ");
foreach (lc, bdr_truncated_tables)
{
Oid reloid = lfirst_oid(lc);
char *relname;
relname = quote_qualified_identifier(
get_namespace_name(get_rel_namespace(reloid)),
get_rel_name(reloid));
appendStringInfoString(&buf, sep);
appendStringInfoString(&buf, relname);
sep = ", ";
}
bdr_queue_ddl_command("TRUNCATE (automatic)", buf.data, NULL);
list_free(bdr_truncated_tables);
bdr_truncated_tables = NIL;
}
/*
* bdr_queue_truncate
* TRUNCATE trigger
*
* This function only writes to internal linked list, actual queueing is done
* by bdr_finish_truncate().
*/
Datum
bdr_queue_truncate(PG_FUNCTION_ARGS)
{
TriggerData *tdata = (TriggerData *) fcinfo->context;
MemoryContext oldcontext;
if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */
ereport(ERROR,
(errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
errmsg("function \"%s\" was not called by trigger manager",
"bdr_queue_truncate")));
if (!TRIGGER_FIRED_BY_TRUNCATE(tdata->tg_event)) /* internal error */
elog(ERROR, "function \"%s\" was not called by TRUNCATE",
"bdr_queue_truncate");
/*
* If the trigger comes from DDL executed by bdr_replicate_ddl_command,
* don't queue it as it would insert duplicate commands into the queue.
*/
if (in_bdr_replicate_ddl_command)
PG_RETURN_VOID(); /* XXX return type? */
/*
* If we're currently replaying something from a remote node, don't queue
* the commands; that would cause recursion.
*/
if (replorigin_session_origin != InvalidRepOriginId)
PG_RETURN_VOID(); /* XXX return type? */
/* Make sure the list change survives the trigger call. */
oldcontext = MemoryContextSwitchTo(TopTransactionContext);
bdr_truncated_tables = lappend_oid(bdr_truncated_tables,
RelationGetRelid(tdata->tg_relation));
MemoryContextSwitchTo(oldcontext);
PG_RETURN_VOID();
}