forked from pgEdge/spock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspock_exception_handler.c
200 lines (170 loc) · 6.34 KB
/
spock_exception_handler.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
#include "postgres.h"
#include "miscadmin.h"
#include "libpq/libpq-be.h"
#include "access/commit_ts.h"
#include "access/xact.h"
#include "commands/dbcommands.h"
#include "common/hashfn.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
#include "postmaster/interrupt.h"
#include "storage/ipc.h"
#include "storage/proc.h"
#include "storage/procsignal.h"
#include "storage/procarray.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/timestamp.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "utils/builtins.h"
#include "replication/origin.h"
#include "replication/slot.h"
#include "pgstat.h"
#include "spock_sync.h"
#include "spock_worker.h"
#include "spock_conflict.h"
#include "spock_relcache.h"
#include "spock_exception_handler.h"
#include "spock_jsonb_utils.h"
#define Natts_exception_table 16
#define Anum_exception_log_remote_origin 1
#define Anum_exception_log_remote_commit_ts 2
#define Anum_exception_log_command_counter 3
#define Anum_exception_log_retry_errored_at 4
#define Anum_exception_log_remote_xid 5
#define Anum_exception_log_local_origin 6
#define Anum_exception_log_local_commit_ts 7
#define Anum_exception_log_schema 8
#define Anum_exception_log_table 9
#define Anum_exception_log_operation 10
#define Anum_exception_log_local_tup 11
#define Anum_exception_log_remote_old_tup 12
#define Anum_exception_log_remote_new_tup 13
#define Anum_exception_log_ddl_statement 14
#define Anum_exception_log_ddl_user 15
#define Anum_exception_log_error_message 16
#define CATALOG_EXCEPTION_LOG "exception_log"
SpockExceptionLog *exception_log_ptr = NULL;
int exception_behaviour = TRANSDISCARD;
int exception_logging = LOG_ALL;
int exception_command_counter = 0;
/*
* Add an entry to the error log.
*/
void
add_entry_to_exception_log(Oid remote_origin, TimestampTz remote_commit_ts,
TransactionId remote_xid,
Oid local_origin, TimestampTz local_commit_ts,
SpockRelation *targetrel,
HeapTuple localtup, SpockTupleData *remoteoldtup,
SpockTupleData *remotenewtup,
char *ddl_statement, char *ddl_user,
char *operation,
char *error_message)
{
RangeVar *rv;
Relation rel;
TupleDesc tupDesc;
TupleDesc targetTupDesc = NULL;
HeapTuple tup;
Datum values[Natts_exception_table];
bool nulls[Natts_exception_table];
char *schema = (targetrel == NULL) ? NULL : targetrel->nspname;
char *table = (targetrel == NULL) ? NULL : targetrel->relname;
char *str_local_tup;
char *str_remote_old_tup;
char *str_remote_new_tup;
/* Get the tuple descriptors */
rv = makeRangeVar(EXTENSION_NAME, CATALOG_EXCEPTION_LOG, -1);
rel = table_openrv(rv, RowExclusiveLock);
tupDesc = RelationGetDescr(rel);
if (targetrel != NULL)
targetTupDesc = RelationGetDescr(targetrel->rel);
/* Convert the three (possible) tuples into json strings */
if (localtup != NULL && localtup->t_data != NULL)
str_local_tup = heap_tuple_to_json_cstring(&localtup,
targetTupDesc);
else
str_local_tup = NULL;
if (remoteoldtup != NULL)
str_remote_old_tup = spock_tuple_to_json_cstring(remoteoldtup,
targetTupDesc);
else
str_remote_old_tup = NULL;
if (remotenewtup != NULL)
str_remote_new_tup = spock_tuple_to_json_cstring(remotenewtup,
targetTupDesc);
else
str_remote_new_tup = NULL;
/* Form a tuple. */
memset(nulls, 0, sizeof(nulls));
memset(values, 0, sizeof(values));
values[Anum_exception_log_remote_origin - 1] = ObjectIdGetDatum(remote_origin);
values[Anum_exception_log_remote_commit_ts - 1] = TimestampTzGetDatum(remote_commit_ts);
values[Anum_exception_log_command_counter - 1] = exception_command_counter;
values[Anum_exception_log_remote_xid - 1] = TransactionIdGetDatum(remote_xid);
if (ddl_statement == NULL)
{
if (str_local_tup != NULL)
{
values[Anum_exception_log_local_origin - 1] = local_origin;
values[Anum_exception_log_local_commit_ts - 1] = local_commit_ts;
}
else
{
nulls[Anum_exception_log_local_origin - 1] = true;
nulls[Anum_exception_log_local_commit_ts - 1] = true;
}
values[Anum_exception_log_schema - 1] = CStringGetTextDatum(schema);
values[Anum_exception_log_table - 1] = CStringGetTextDatum(table);
values[Anum_exception_log_operation - 1] = CStringGetTextDatum(operation);
if (str_local_tup != NULL)
values[Anum_exception_log_local_tup - 1] = DirectFunctionCall1(jsonb_in, CStringGetDatum(str_local_tup));
else
nulls[Anum_exception_log_local_tup - 1] = true;
if (str_remote_old_tup != NULL)
values[Anum_exception_log_remote_old_tup - 1] = DirectFunctionCall1(jsonb_in, CStringGetDatum(str_remote_old_tup));
else
nulls[Anum_exception_log_remote_old_tup - 1] = true;
if (str_remote_new_tup != NULL)
values[Anum_exception_log_remote_new_tup - 1] = DirectFunctionCall1(jsonb_in, CStringGetDatum(str_remote_new_tup));
else
nulls[Anum_exception_log_remote_new_tup - 1] = true;
nulls[Anum_exception_log_ddl_statement - 1] = true;
nulls[Anum_exception_log_ddl_user - 1] = true;
}
else
{
nulls[Anum_exception_log_local_origin - 1] = true;
nulls[Anum_exception_log_local_commit_ts - 1] = true;
nulls[Anum_exception_log_schema - 1] = true;
nulls[Anum_exception_log_table - 1] = true;
values[Anum_exception_log_operation - 1] = CStringGetTextDatum("DDL");
nulls[Anum_exception_log_local_tup - 1] = true;
nulls[Anum_exception_log_remote_old_tup - 1] = true;
nulls[Anum_exception_log_remote_new_tup - 1] = true;
values[Anum_exception_log_ddl_statement - 1] = CStringGetTextDatum(ddl_statement);
values[Anum_exception_log_ddl_user - 1] = CStringGetTextDatum(ddl_user);
}
/*
* The error_message column of the spock.exception_log table is marked as NOT NULL,
* but we don't always have a valid error message.
*/
if (error_message == NULL)
values[Anum_exception_log_error_message - 1] = CStringGetTextDatum("unknown");
else
values[Anum_exception_log_error_message - 1] = CStringGetTextDatum(error_message);
values[Anum_exception_log_retry_errored_at - 1] = TimestampTzGetDatum(GetCurrentTimestamp());
tup = heap_form_tuple(tupDesc, values, nulls);
/* Insert the tuple to the catalog. */
CatalogTupleInsert(rel, tup);
/* Cleanup. */
heap_freetuple(tup);
table_close(rel, RowExclusiveLock);
/* Reset the error stack to empty. */
FlushErrorState();
CommandCounterIncrement();
}