-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg_follower.c
247 lines (194 loc) · 4.84 KB
/
pg_follower.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
/*-------------------------------------------------------------------------
*
* pg_follower.c
* Capture changes and follow
*
*
* IDENTIFICATION
* pg_follower/pg_follower.c
*
*-------------------------------------------------------------------------
*/
#include <string.h>
#include "postgres.h"
#include "fmgr.h"
#include "catalog/namespace.h"
#include "catalog/pg_class.h"
#include "commands/event_trigger.h"
#include "lib/stringinfo.h"
#include "nodes/pg_list.h"
#include "replication/message.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(detect_ddl);
static void handle_createstmt(CreateStmt *stmt);
static void handle_dropstmt(DropStmt *stmt);
static char *
deparse_dropstmt(DropStmt *stmt)
{
StringInfoData deparsed;
ListCell *lc;
bool first_try = true;
Assert(stmt->removeType == OBJECT_TABLE);
initStringInfo(&deparsed);
appendStringInfo(&deparsed, "DROP TABLE %s ",
stmt->missing_ok ? "IF EXISTS" : "");
foreach(lc, stmt->objects)
{
RangeVar *rel = makeRangeVarFromNameList((List *) lfirst(lc));
/* Only parmanent tables are supported */
if (rel->relpersistence != RELPERSISTENCE_PERMANENT)
{
elog(INFO, "detected");
continue;
}
if (!first_try)
appendStringInfoString(&deparsed, ", ");
if (rel->schemaname)
appendStringInfo(&deparsed, "%s.%s", rel->schemaname, rel->relname);
else
appendStringInfoString(&deparsed, rel->relname);
first_try = false;
}
switch (stmt->behavior)
{
case DROP_RESTRICT:
appendStringInfoString(&deparsed, " RESTRICT");
break;
case DROP_CASCADE:
appendStringInfoString(&deparsed, " CASCADE");
break;
default:
break;
}
appendStringInfoChar(&deparsed, ';');
return deparsed.data;
}
static void
handle_dropstmt(DropStmt *stmt)
{
char *query;
query = deparse_dropstmt(stmt);
elog(DEBUG1, "deparse result: %s", query);
/* Emit the result to the log. */
LogLogicalMessage("pg_follower", query, strlen(query), true, false);
pfree(query);
}
/*
* Check whether the relation can be exported.
*/
static bool
verify_createstatement(CreateStmt *stmt)
{
ListCell *lc;
/* Both UNLOGGED and TEMPORARY clauses are not supported */
if (stmt->relation->relpersistence != RELPERSISTENCE_PERMANENT)
{
elog(WARNING, "unlogged/temporary tables are not supported");
return false;
}
/* PARTITION OF clause is not supported */
if (stmt->partspec != NULL)
{
elog(WARNING, "partition tables are not supported");
return false;
}
/* PARTITION BY clause is not supported */
if (stmt->partspec != NULL)
{
elog(WARNING, "partitioned tables are not supported");
return false;
}
/* INHERITS clause is not supported */
if (stmt->inhRelations != NIL)
{
elog(WARNING, "inherited tables are not supported");
return false;
}
/* OF type_name clause is not supported */
if (stmt->ofTypename != NULL)
{
elog(WARNING, "typed tables are not supported");
return false;
}
/*
* TypeName must have valid "names" attribute.
*
* XXX: When would it become NIL?
*/
foreach(lc, stmt->tableElts)
{
ColumnDef *colDef = lfirst(lc);
if (colDef->typeName->names == NIL)
{
elog(WARNING, "not supported");
return false;
}
}
return true;
}
/*
* Deparse CreateStmt structure. Returns deparsed result.
*/
static char *
deparse_createstmt(CreateStmt *stmt)
{
StringInfoData deparsed;
ListCell *lc;
bool first_try = true;
initStringInfo(&deparsed);
appendStringInfo(&deparsed, "CREATE TABLE %s %s.%s ( ",
stmt->if_not_exists ? "IF NOT EXISTS" : "",
stmt->relation->schemaname, stmt->relation->relname);
foreach(lc, stmt->tableElts)
{
ColumnDef *colDef = lfirst(lc);
char *typename = NameListToString(colDef->typeName->names);
if (!first_try)
appendStringInfoString(&deparsed, ", ");
appendStringInfo(&deparsed, "%s %s" , colDef->colname, typename);
pfree(typename);
first_try = false;
}
appendStringInfo(&deparsed, " );");
return deparsed.data;
}
static void
handle_createstmt(CreateStmt *stmt)
{
char *query;
/* Can we deparse the statement? If not, do nothing */
if (!verify_createstatement(stmt))
return;
query = deparse_createstmt(stmt);
elog(DEBUG1, "deparse result: %s", query);
/* Emit the result to the log. */
LogLogicalMessage("pg_follower", query, strlen(query), true, false);
pfree(query);
}
/*
* Trigger function
*/
Datum
detect_ddl(PG_FUNCTION_ARGS)
{
EventTriggerData *trigdata;
CommandTag tag;
if (!CALLED_AS_EVENT_TRIGGER(fcinfo))
elog(ERROR, "must be called as event trigger");
trigdata = (EventTriggerData *) fcinfo->context;
tag = trigdata->tag;
switch (tag)
{
case CMDTAG_CREATE_TABLE:
handle_createstmt((CreateStmt *) trigdata->parsetree);
break;
case CMDTAG_DROP_TABLE:
handle_dropstmt((DropStmt *) trigdata->parsetree);
break;
default:
elog(WARNING, "this DDL is not supported: %s",
GetCommandTagName(trigdata->tag));
break;
}
PG_RETURN_NULL();
}