-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhayadecoder.c
More file actions
213 lines (174 loc) · 5.46 KB
/
hayadecoder.c
File metadata and controls
213 lines (174 loc) · 5.46 KB
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
#include "postgres.h"
#include "catalog/pg_class.h"
#include "replication/logical.h"
#include "replication/origin.h"
#include "utils/builtins.h"
#include "utils/relcache.h"
extern void _PG_init(void);
extern void _PG_output_plugin_init(OutputPluginCallbacks *cb);
PG_MODULE_MAGIC;
typedef struct hayadecoder_data
{
bool write_txd;
} hayadecodeData;
/* support routines */
static void output_insert(StringInfo out,
Relation relation,
ReorderBufferChange *change);
static void output_update(StringInfo out,
Relation relation,
ReorderBufferChange *change);
static void output_delete(StringInfo out,
Relation relation,
ReorderBufferChange *change);
/* callback routines */
static void hayade_startup(LogicalDecodingContext *ctx,
OutputPluginOptions *options,
bool is_init);
static void hayade_shutdown(LogicalDecodingContext *ctx);
static void hayade_begin(LogicalDecodingContext *ctx, ReorderBufferTXN *txn);
static void hayade_change(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn,
Relation relation,
ReorderBufferChange *change);
static void hayade_commit(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn,
XLogRecPtr commit_lsn);
static void
hayade_startup(LogicalDecodingContext *ctx,
OutputPluginOptions *options,
bool is_init)
{
hayadecodeData *data;
ListCell *option;
data = palloc(sizeof(hayadecodeData));
data->write_txd = true;
options->output_type = OUTPUT_PLUGIN_TEXTUAL_OUTPUT;
foreach(option, ctx->output_plugin_options)
{
DefElem *elem = lfirst(option);
if (strcmp(elem->defname, "write_txd") == 0)
{
if (elem->arg == NULL)
data->write_txd = true;
else if (!parse_bool(strVal(elem->arg), &data->write_txd))
elog(ERROR, "write_txd: wrong style");
}
else
elog(ERROR, "unknown option");
}
ctx->output_plugin_private = data;
}
static void
hayade_shutdown(LogicalDecodingContext *ctx)
{
hayadecodeData *data = ctx->output_plugin_private;
pfree(data);
}
static void
hayade_begin(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
{
hayadecodeData *data = ctx->output_plugin_private;
OutputPluginPrepareWrite(ctx, true);
if (data->write_txd)
appendStringInfo(ctx->out, "START TRANSACTION %u!", txn->xid);
else
appendStringInfo(ctx->out, "START TRANSACTION!");
OutputPluginWrite(ctx, true);
}
static void
hayade_change(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn,
Relation relation,
ReorderBufferChange *change)
{
switch (change->action)
{
case REORDER_BUFFER_CHANGE_INSERT:
OutputPluginPrepareWrite(ctx, true);
output_insert(ctx->out, relation, change);
OutputPluginWrite(ctx, true);
break;
case REORDER_BUFFER_CHANGE_UPDATE:
OutputPluginPrepareWrite(ctx, true);
output_update(ctx->out, relation, change);
OutputPluginWrite(ctx, true);
break;
case REORDER_BUFFER_CHANGE_DELETE:
OutputPluginPrepareWrite(ctx, true);
output_delete(ctx->out, relation, change);
OutputPluginWrite(ctx, true);
break;
default:
elog(ERROR, "unknown change");
break;
}
}
static void
output_insert(StringInfo out,
Relation relation,
ReorderBufferChange *change)
{
Form_pg_class rel;
char *relname;
Assert(change->action == REORDER_BUFFER_CHANGE_INSERT);
rel = relation->rd_rel;
relname = NameStr(rel->relname);
appendStringInfo(out, "INSERTED ");
appendStringInfo(out, "TABLE: %s ", relname);
}
static void
output_update(StringInfo out,
Relation relation,
ReorderBufferChange *change)
{
Form_pg_class rel;
char *relname;
Assert(change->action == REORDER_BUFFER_CHANGE_UPDATE);
rel = relation->rd_rel;
relname = NameStr(rel->relname);
appendStringInfo(out, "UPDATED");
appendStringInfo(out, "TABLE: %s ", relname);
}
static void
output_delete(StringInfo out,
Relation relation,
ReorderBufferChange *change)
{
Form_pg_class rel;
char *relname;
Assert(change->action == REORDER_BUFFER_CHANGE_DELETE);
rel = relation->rd_rel;
relname = NameStr(rel->relname);
appendStringInfo(out, "DELETED");
appendStringInfo(out, "TABLE: %s ", relname);
}
static void
hayade_commit(LogicalDecodingContext *ctx,
ReorderBufferTXN *txn,
XLogRecPtr commit_lsn)
{
hayadecodeData *data = ctx->output_plugin_private;
OutputPluginPrepareWrite(ctx, true);
if (data->write_txd)
appendStringInfo(ctx->out, "END OF TRANSACTION %u!", txn->xid);
else
appendStringInfo(ctx->out, "END OF TRANSACTION!");
OutputPluginWrite(ctx, true);
}
/* Public Routines */
void
_PG_init(void)
{
/* NOP */
}
void
_PG_output_plugin_init(OutputPluginCallbacks *cb)
{
AssertVariableIsOfType(&_PG_output_plugin_init, LogicalOutputPluginInit);
cb->startup_cb = hayade_startup;
cb->shutdown_cb = hayade_shutdown;
cb->begin_cb = hayade_begin;
cb->change_cb = hayade_change;
cb->commit_cb = hayade_commit;
}