16 |
PG_FUNCTION_INFO_V1(_rserv_log_); |
PG_FUNCTION_INFO_V1(_rserv_log_); |
17 |
PG_FUNCTION_INFO_V1(_rserv_sync_); |
PG_FUNCTION_INFO_V1(_rserv_sync_); |
18 |
PG_FUNCTION_INFO_V1(_rserv_debug_); |
PG_FUNCTION_INFO_V1(_rserv_debug_); |
19 |
Datum _rserv_log_(PG_FUNCTION_ARGS); |
Datum _rserv_log_(PG_FUNCTION_ARGS); |
20 |
Datum _rserv_sync_(PG_FUNCTION_ARGS); |
Datum _rserv_sync_(PG_FUNCTION_ARGS); |
21 |
Datum _rserv_debug_(PG_FUNCTION_ARGS); |
Datum _rserv_debug_(PG_FUNCTION_ARGS); |
22 |
|
|
23 |
#else |
#else |
24 |
HeapTuple _rserv_log_(void); |
HeapTuple _rserv_log_(void); |
25 |
int32 _rserv_sync_(int32); |
int32 _rserv_sync_(int32); |
28 |
|
|
29 |
static int debug = 0; |
static int debug = 0; |
30 |
|
|
31 |
static char* OutputValue(char *key, char *buf, int size); |
static char *OutputValue(char *key, char *buf, int size); |
32 |
|
|
33 |
#ifdef PG_FUNCTION_INFO_V1 |
#ifdef PG_FUNCTION_INFO_V1 |
34 |
Datum |
Datum |
43 |
char **args; /* argument: argnum */ |
char **args; /* argument: argnum */ |
44 |
Relation rel; /* triggered relation */ |
Relation rel; /* triggered relation */ |
45 |
HeapTuple tuple; /* tuple to return */ |
HeapTuple tuple; /* tuple to return */ |
46 |
HeapTuple newtuple = NULL;/* tuple to return */ |
HeapTuple newtuple = NULL; /* tuple to return */ |
47 |
TupleDesc tupdesc; /* tuple description */ |
TupleDesc tupdesc; /* tuple description */ |
48 |
int keynum; |
int keynum; |
49 |
char *key; |
char *key; |
50 |
char *okey; |
char *okey; |
51 |
char *newkey = NULL; |
char *newkey = NULL; |
52 |
int deleted; |
int deleted, inserted, updated; |
53 |
char sql[8192]; |
char sql[8192]; |
54 |
char outbuf[8192]; |
char outbuf[8192]; |
55 |
char oidbuf[64]; |
char oidbuf[64]; |
69 |
nargs = trigger->tgnargs; |
nargs = trigger->tgnargs; |
70 |
args = trigger->tgargs; |
args = trigger->tgargs; |
71 |
|
|
72 |
if (nargs != 1) /* odd number of arguments! */ |
if (nargs != 1) /* odd number of arguments! */ |
73 |
elog(ERROR, "_rserv_log_: need in *one* argument"); |
elog(ERROR, "_rserv_log_: need in *one* argument"); |
74 |
|
|
75 |
keynum = atoi(args[0]); |
keynum = atoi(args[0]); |
80 |
rel = CurrentTriggerData->tg_relation; |
rel = CurrentTriggerData->tg_relation; |
81 |
tupdesc = rel->rd_att; |
tupdesc = rel->rd_att; |
82 |
|
|
83 |
deleted = (TRIGGER_FIRED_BY_DELETE(CurrentTriggerData->tg_event)) ? |
deleted = (TRIGGER_FIRED_BY_DELETE(CurrentTriggerData->tg_event)) ? |
84 |
1 : 0; |
1 : 0; |
85 |
|
|
86 |
if (TRIGGER_FIRED_BY_UPDATE(CurrentTriggerData->tg_event)) |
inserted = (TRIGGER_FIRED_BY_INSERT(CurrentTriggerData->tg_event)) ? 1 : 0; |
87 |
|
|
88 |
|
updated = 0; |
89 |
|
if (TRIGGER_FIRED_BY_UPDATE(CurrentTriggerData->tg_event)) { |
90 |
|
updated = 1; |
91 |
newtuple = CurrentTriggerData->tg_newtuple; |
newtuple = CurrentTriggerData->tg_newtuple; |
92 |
|
} |
93 |
|
|
94 |
|
#ifndef PG_FUNCTION_INFO_V1 |
95 |
|
|
96 |
/* |
/* |
97 |
* Setting CurrentTriggerData to NULL prevents direct calls to trigger |
* Setting CurrentTriggerData to NULL prevents direct calls to trigger |
99 |
* by trigger manager code only. |
* by trigger manager code only. |
100 |
*/ |
*/ |
101 |
CurrentTriggerData = NULL; |
CurrentTriggerData = NULL; |
102 |
|
#endif |
103 |
|
|
104 |
/* Connect to SPI manager */ |
/* Connect to SPI manager */ |
105 |
if ((ret = SPI_connect()) < 0) |
if ((ret = SPI_connect()) < 0) |
107 |
|
|
108 |
if (keynum == ObjectIdAttributeNumber) |
if (keynum == ObjectIdAttributeNumber) |
109 |
{ |
{ |
110 |
sprintf(oidbuf, "%u", tuple->t_data->t_oid); |
snprintf(oidbuf, sizeof(oidbuf), "%u", HeapTupleGetOid(tuple)); |
111 |
key = oidbuf; |
key = oidbuf; |
112 |
} |
} |
113 |
else |
else |
124 |
if (strcmp(newkey, key) == 0) |
if (strcmp(newkey, key) == 0) |
125 |
newkey = NULL; |
newkey = NULL; |
126 |
else |
else |
127 |
deleted = 1; /* old key was deleted */ |
deleted = 1; /* old key was deleted */ |
128 |
} |
} |
129 |
|
|
130 |
if (strpbrk(key, "\\ \n'")) |
if (strpbrk(key, "\\ \n'")) |
132 |
else |
else |
133 |
okey = key; |
okey = key; |
134 |
|
|
135 |
sprintf(sql, "update _RSERV_LOG_ set logid = %d, logtime = now(), " |
snprintf(sql, 8192, "update _RSERV_LOG_ set logid = %d, logtime = now(), " |
136 |
"deleted = %d where reloid = %u and key = '%s'", |
"insert = %d, update = %d, delete = %d where reloid = %u and key = '%s'", |
137 |
GetCurrentTransactionId(), deleted, rel->rd_id, okey); |
GetCurrentTransactionId(), inserted, updated, deleted, rel->rd_id, okey); |
138 |
|
|
139 |
if (debug) |
if (debug) |
140 |
elog(NOTICE, sql); |
elog(DEBUG3, "sql: %s", sql); |
141 |
|
|
142 |
ret = SPI_exec(sql, 0); |
ret = SPI_exec(sql, 0); |
143 |
|
|
151 |
elog(ERROR, "_rserv_log_: duplicate tuples"); |
elog(ERROR, "_rserv_log_: duplicate tuples"); |
152 |
else if (SPI_processed == 0) |
else if (SPI_processed == 0) |
153 |
{ |
{ |
154 |
sprintf(sql, "insert into _RSERV_LOG_ " |
snprintf(sql, 8192, "insert into _RSERV_LOG_ " |
155 |
"(reloid, logid, logtime, deleted, key) " |
"(reloid, logid, logtime, insert, update, delete, key) " |
156 |
"values (%u, %d, now(), %d, '%s')", |
"values (%u, %d, now(), %d, %d, %d, '%s')", |
157 |
rel->rd_id, GetCurrentTransactionId(), |
rel->rd_id, GetCurrentTransactionId(), |
158 |
deleted, okey); |
inserted, updated, deleted, okey); |
159 |
|
|
160 |
if (debug) |
if (debug) |
161 |
elog(NOTICE, sql); |
elog(DEBUG3, "sql: %s", sql); |
162 |
|
|
163 |
ret = SPI_exec(sql, 0); |
ret = SPI_exec(sql, 0); |
164 |
|
|
176 |
else |
else |
177 |
okey = newkey; |
okey = newkey; |
178 |
|
|
179 |
sprintf(sql, "insert into _RSERV_LOG_ " |
snprintf(sql, 8192, "insert into _RSERV_LOG_ " |
180 |
"(reloid, logid, logtime, deleted, key) " |
"(reloid, logid, logtime, insert, update, deleted, key) " |
181 |
"values (%u, %d, now(), 0, '%s')", |
"values (%u, %d, now(), %d, %d, 0, '%s')", |
182 |
rel->rd_id, GetCurrentTransactionId(), okey); |
rel->rd_id, GetCurrentTransactionId(), inserted, updated, okey); |
183 |
|
|
184 |
if (debug) |
if (debug) |
185 |
elog(NOTICE, sql); |
elog(DEBUG3, "sql: %s", sql); |
186 |
|
|
187 |
ret = SPI_exec(sql, 0); |
ret = SPI_exec(sql, 0); |
188 |
|
|
211 |
#endif |
#endif |
212 |
{ |
{ |
213 |
#ifdef PG_FUNCTION_INFO_V1 |
#ifdef PG_FUNCTION_INFO_V1 |
214 |
int32 server = PG_GETARG_INT32(0); |
int32 server = PG_GETARG_INT32(0); |
215 |
#endif |
#endif |
216 |
char sql[8192]; |
char sql[8192]; |
217 |
char buf[8192]; |
char buf[8192]; |
218 |
char *active = buf; |
char *active = buf; |
219 |
uint32 xcnt; |
uint32 xcnt; |
220 |
int ret; |
int ret; |
221 |
|
|
222 |
if (SerializableSnapshot == NULL) |
if (SerializableSnapshot == NULL) |
223 |
elog(ERROR, "_rserv_sync_: SerializableSnapshot is NULL"); |
elog(ERROR, "_rserv_sync_: SerializableSnapshot is NULL"); |
225 |
buf[0] = 0; |
buf[0] = 0; |
226 |
for (xcnt = 0; xcnt < SerializableSnapshot->xcnt; xcnt++) |
for (xcnt = 0; xcnt < SerializableSnapshot->xcnt; xcnt++) |
227 |
{ |
{ |
228 |
sprintf(buf + strlen(buf), "%s%u", (xcnt) ? ", " : "", |
snprintf(buf + strlen(buf), 8192 - strlen(buf), |
229 |
SerializableSnapshot->xip[xcnt]); |
"%s%u", (xcnt) ? ", " : "", |
230 |
|
SerializableSnapshot->xip[xcnt]); |
231 |
} |
} |
232 |
|
|
233 |
if ((ret = SPI_connect()) < 0) |
if ((ret = SPI_connect()) < 0) |
234 |
elog(ERROR, "_rserv_sync_: SPI_connect returned %d", ret); |
elog(ERROR, "_rserv_sync_: SPI_connect returned %d", ret); |
235 |
|
|
236 |
sprintf(sql, "insert into _RSERV_SYNC_ " |
snprintf(sql, 8192, "insert into _RSERV_SYNC_ " |
237 |
"(server, syncid, synctime, status, minid, maxid, active) " |
"(server, syncid, synctime, status, minid, maxid, active) " |
238 |
"values (%u, currval('_rserv_sync_seq_'), now(), 0, %d, %d, '%s')", |
"values (%u, currval('_rserv_sync_seq_'), now(), 0, %d, %d, '%s')", |
239 |
server, SerializableSnapshot->xmin, SerializableSnapshot->xmax, active); |
server, SerializableSnapshot->xmin, SerializableSnapshot->xmax, active); |
240 |
|
|
241 |
ret = SPI_exec(sql, 0); |
ret = SPI_exec(sql, 0); |
242 |
|
|
257 |
#endif |
#endif |
258 |
{ |
{ |
259 |
#ifdef PG_FUNCTION_INFO_V1 |
#ifdef PG_FUNCTION_INFO_V1 |
260 |
int32 newval = PG_GETARG_INT32(0); |
int32 newval = PG_GETARG_INT32(0); |
261 |
#endif |
#endif |
262 |
int32 oldval = debug; |
int32 oldval = debug; |
263 |
|
|
264 |
debug = newval; |
debug = newval; |
265 |
|
|
266 |
return (oldval); |
return (oldval); |
267 |
} |
} |
268 |
|
|
269 |
#define ExtendBy 1024 |
#define ExtendBy 1024 |
270 |
|
|
271 |
static char* |
static char * |
272 |
OutputValue(char *key, char *buf, int size) |
OutputValue(char *key, char *buf, int size) |
273 |
{ |
{ |
274 |
int i = 0; |
int i = 0; |
277 |
int slen = 0; |
int slen = 0; |
278 |
|
|
279 |
size--; |
size--; |
280 |
for ( ; ; ) |
for (;;) |
281 |
{ |
{ |
282 |
switch (*key) |
switch (*key) |
283 |
{ |
{ |
284 |
case '\\': subst ="\\\\"; |
case '\\': |
285 |
slen = 2; |
subst = "\\\\"; |
286 |
break; |
slen = 2; |
287 |
case ' ': subst = "\\011"; |
break; |
288 |
slen = 4; |
case ' ': |
289 |
break; |
subst = "\\011"; |
290 |
case '\n': subst = "\\012"; |
slen = 4; |
291 |
slen = 4; |
break; |
292 |
break; |
case '\n': |
293 |
case '\'': subst = "\\047"; |
subst = "\\012"; |
294 |
slen = 4; |
slen = 4; |
295 |
break; |
break; |
296 |
case '\0': out[i] = 0; |
case '\'': |
297 |
return(out); |
subst = "\\047"; |
298 |
default: slen = 1; |
slen = 4; |
299 |
break; |
break; |
300 |
|
case '\0': |
301 |
|
out[i] = 0; |
302 |
|
return (out); |
303 |
|
default: |
304 |
|
slen = 1; |
305 |
|
break; |
306 |
} |
} |
307 |
|
|
308 |
if (i + slen >= size) |
if (i + slen >= size) |
309 |
{ |
{ |
310 |
if (out == buf) |
if (out == buf) |
311 |
{ |
{ |
312 |
out = (char*) palloc(size + ExtendBy); |
out = (char *) palloc(size + ExtendBy); |
313 |
strncpy(out, buf, i); |
strncpy(out, buf, i); |
314 |
size += ExtendBy; |
size += ExtendBy; |
315 |
} |
} |
316 |
else |
else |
317 |
{ |
{ |
318 |
out = (char*) repalloc(out, size + ExtendBy); |
out = (char *) repalloc(out, size + ExtendBy); |
319 |
size += ExtendBy; |
size += ExtendBy; |
320 |
} |
} |
321 |
} |
} |
330 |
key++; |
key++; |
331 |
} |
} |
332 |
|
|
333 |
return(out); |
return (out); |
334 |
|
|
335 |
} |
} |