-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdest.c
288 lines (268 loc) · 11.3 KB
/
dest.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
#include "include.h"
#include <access/xact.h>
#include <miscadmin.h>
#include <pgstat.h>
#include <replication/slot.h>
#include <storage/proc.h>
#include <tcop/tcopprot.h>
#include <unistd.h>
#include <utils/lsyscache.h>
#include <utils/memutils.h>
#include <utils/ps_status.h>
#include <utils/snapmgr.h>
#include <utils/timeout.h>
#if PG_VERSION_NUM >= 110000
#include <jit/jit.h>
#endif
static Task task = {0};
Task *get_task(void) {
return &task;
}
static char *SPI_getvalue_my(TupleTableSlot *slot, TupleDesc tupdesc, int fnumber) {
bool isnull;
bool typisvarlena;
Datum attr = slot_getattr(slot, fnumber, &isnull);
Oid foutoid;
if (isnull) return NULL;
getTypeOutputInfo(TupleDescAttr(tupdesc, fnumber - 1)->atttypid, &foutoid, &typisvarlena);
return OidOutputFunctionCall(foutoid, attr);
}
static void headers(TupleDesc tupdesc) {
if (task.output.len) appendStringInfoString(&task.output, "\n");
for (int col = 1; col <= tupdesc->natts; col++) {
if (col > 1) appendStringInfoChar(&task.output, task.delimiter);
appendBinaryStringInfoEscapeQuote(&task.output, SPI_fname(tupdesc, col), strlen(SPI_fname(tupdesc, col)), false, task.escape, task.quote);
}
}
static
#if PG_VERSION_NUM >= 90600
bool
#else
void
#endif
receiveSlot(TupleTableSlot *slot, DestReceiver *self) {
TupleDesc tupdesc = slot->tts_tupleDescriptor;
if (!task.shared)
return
#if PG_VERSION_NUM >= 90600
true
#endif
;
if (!task.output.data) initStringInfoMy(&task.output);
if (task.header && !task.row && tupdesc->natts > 1) headers(tupdesc);
if (task.output.len) appendStringInfoString(&task.output, "\n");
for (int col = 1; col <= tupdesc->natts; col++) {
char *value = SPI_getvalue_my(slot, tupdesc, col);
if (col > 1) appendStringInfoChar(&task.output, task.delimiter);
if (!value) appendStringInfoString(&task.output, task.null); else {
appendBinaryStringInfoEscapeQuote(&task.output, value, strlen(value), !init_oid_is_string(SPI_gettypeid(tupdesc, col)) && task.string, task.escape, task.quote);
pfree(value);
}
}
task.row++;
#if PG_VERSION_NUM >= 90600
return true;
#endif
}
static void rStartup(DestReceiver *self, int operation, TupleDesc tupdesc) {
if (!task.shared) return;
switch (operation) {
case CMD_UNKNOWN: elog(DEBUG1, "id = %li, operation = CMD_UNKNOWN", task.shared->id); break;
case CMD_SELECT: elog(DEBUG1, "id = %li, operation = CMD_SELECT", task.shared->id); break;
case CMD_UPDATE: elog(DEBUG1, "id = %li, operation = CMD_UPDATE", task.shared->id); break;
case CMD_INSERT: elog(DEBUG1, "id = %li, operation = CMD_INSERT", task.shared->id); break;
case CMD_DELETE: elog(DEBUG1, "id = %li, operation = CMD_DELETE", task.shared->id); break;
case CMD_UTILITY: elog(DEBUG1, "id = %li, operation = CMD_UTILITY", task.shared->id); break;
case CMD_NOTHING: elog(DEBUG1, "id = %li, operation = CMD_NOTHING", task.shared->id); break;
default: elog(DEBUG1, "id = %li, operation = %i", task.shared->id, operation); break;
}
task.row = 0;
task.skip = 1;
}
static void rShutdown(DestReceiver *self) {
if (task.shared) elog(DEBUG1, "id = %li", task.shared->id);
}
static void rDestroy(DestReceiver *self) {
if (task.shared) elog(DEBUG1, "id = %li", task.shared->id);
}
static
#if PG_VERSION_NUM >= 120000
const
#endif
DestReceiver myDestReceiver = {
.receiveSlot = receiveSlot,
.rStartup = rStartup,
.rShutdown = rShutdown,
.rDestroy = rDestroy,
.mydest = DestDebug,
};
DestReceiver *CreateDestReceiverMy(CommandDest dest) {
if (task.shared) elog(DEBUG1, "id = %li", task.shared->id);
#if PG_VERSION_NUM >= 120000
return unconstify(DestReceiver *, &myDestReceiver);
#else
return &myDestReceiver;
#endif
}
static void ReadyForQueryMy(CommandDest dest) {
if (task.shared) elog(DEBUG1, "id = %li", task.shared->id);
}
void NullCommandMy(CommandDest dest) {
if (task.shared) elog(DEBUG1, "id = %li", task.shared->id);
}
#if PG_VERSION_NUM >= 130000
void BeginCommandMy(CommandTag commandTag, CommandDest dest) {
if (task.shared) elog(DEBUG1, "id = %li, commandTag = %s", task.shared->id, GetCommandTagName(commandTag));
}
void EndCommandMy(const QueryCompletion *qc, CommandDest dest, bool force_undecorated_output) {
char completionTag[COMPLETION_TAG_BUFSIZE];
CommandTag tag = qc->commandTag;
const char *tagname = GetCommandTagName(tag);
if (!task.shared) return;
if (command_tag_display_rowcount(tag) && !force_undecorated_output) snprintf(completionTag, COMPLETION_TAG_BUFSIZE, tag == CMDTAG_INSERT ? "%s 0 %lu" : "%s %lu", tagname, qc->nprocessed);
else snprintf(completionTag, COMPLETION_TAG_BUFSIZE, "%s", tagname);
elog(DEBUG1, "id = %li, completionTag = %s", task.shared->id, completionTag);
if (task.skip) task.skip = 0; else {
if (!task.output.data) initStringInfoMy(&task.output);
if (task.output.len) appendStringInfoString(&task.output, "\n");
appendStringInfoString(&task.output, completionTag);
}
}
#else
void BeginCommandMy(const char *commandTag, CommandDest dest) {
if (task.shared) elog(DEBUG1, "id = %li, commandTag = %s", task.shared->id, commandTag);
}
void EndCommandMy(const char *commandTag, CommandDest dest) {
if (!task.shared) return;
elog(DEBUG1, "id = %li, commandTag = %s", task.shared->id, commandTag);
if (task.skip) task.skip = 0; else {
if (!task.output.data) initStringInfoMy(&task.output);
if (task.output.len) appendStringInfoString(&task.output, "\n");
appendStringInfoString(&task.output, commandTag);
}
}
#endif
static void dest_execute(void) {
if (!task.shared->spi) {
MemoryContext oldMemoryContext = MemoryContextSwitchTo(MessageContext);
MemoryContextResetAndDeleteChildren(MessageContext);
InvalidateCatalogSnapshotConditionally();
MemoryContextSwitchTo(oldMemoryContext);
whereToSendOutput = DestDebug;
ReadyForQueryMy(whereToSendOutput);
SetCurrentStatementStartTimestamp();
exec_simple_query_my(task.input);
if (IsTransactionState()) {
exec_simple_query_my(SQL(END));
if (IsTransactionState()) ereport(ERROR, (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION), errmsg("still active sql transaction")));
}
} else {
bool count = false;
bool insert = false;
char completionTag[COMPLETION_TAG_BUFSIZE];
int rc = SPI_execute(task.input, false, 0);
const char *tagname = SPI_result_code_string(rc) + sizeof("SPI_OK_") - 1;
switch (rc) {
case SPI_ERROR_ARGUMENT: ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("SPI_ERROR_ARGUMENT"))); break;
case SPI_ERROR_COPY: ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("SPI_ERROR_COPY"))); break;
case SPI_ERROR_OPUNKNOWN: ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("SPI_ERROR_OPUNKNOWN"))); break;
case SPI_ERROR_TRANSACTION: ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("SPI_ERROR_TRANSACTION"))); break;
case SPI_OK_DELETE: count = true; break;
case SPI_OK_DELETE_RETURNING: count = true; break;
case SPI_OK_INSERT: count = true; insert = true; break;
case SPI_OK_INSERT_RETURNING: count = true; insert = true; break;
case SPI_OK_SELECT: count = true; task.skip = 1; break;
case SPI_OK_UPDATE: count = true; break;
case SPI_OK_UPDATE_RETURNING: count = true; break;
}
elog(DEBUG1, "id = %li, commandTag = %s", task.shared->id, tagname);
if (SPI_tuptable) for (uint64 row = 0; row < SPI_processed; row++) {
task.skip = 1;
if (!task.output.data) initStringInfoMy(&task.output);
if (task.header && !row && SPI_tuptable->tupdesc->natts > 1) {
if (task.output.len) appendStringInfoString(&task.output, "\n");
for (int col = 1; col <= SPI_tuptable->tupdesc->natts; col++) {
if (col > 1) appendStringInfoChar(&task.output, task.delimiter);
appendBinaryStringInfoEscapeQuote(&task.output, SPI_fname(SPI_tuptable->tupdesc, col), strlen(SPI_fname(SPI_tuptable->tupdesc, col)), false, task.escape, task.quote);
}
}
if (task.output.len) appendStringInfoString(&task.output, "\n");
for (int col = 1; col <= SPI_tuptable->tupdesc->natts; col++) {
char *value = SPI_getvalue(SPI_tuptable->vals[row], SPI_tuptable->tupdesc, col);
if (col > 1) appendStringInfoChar(&task.output, task.delimiter);
if (!value) appendStringInfoString(&task.output, task.null); else {
appendBinaryStringInfoEscapeQuote(&task.output, value, strlen(value), !init_oid_is_string(SPI_gettypeid(SPI_tuptable->tupdesc, col)) && task.string, task.escape, task.quote);
pfree(value);
}
}
}
if (count) snprintf(completionTag, COMPLETION_TAG_BUFSIZE, insert ? "%s 0 %lu" : "%s %lu", tagname, (long)SPI_processed);
else snprintf(completionTag, COMPLETION_TAG_BUFSIZE, "%s", tagname);
elog(DEBUG1, "id = %li, completionTag = %s", task.shared->id, completionTag);
if (task.skip) task.skip = 0; else {
if (!task.output.data) initStringInfoMy(&task.output);
if (task.output.len) appendStringInfoString(&task.output, "\n");
appendStringInfoString(&task.output, completionTag);
}
}
}
static void dest_catch(void) {
if (!task.shared->spi) {
HOLD_INTERRUPTS();
disable_all_timeouts(false);
QueryCancelPending = false;
}
EmitErrorReport();
if (!task.shared->spi) {
debug_query_string = NULL;
AbortOutOfAnyTransaction();
#if PG_VERSION_NUM >= 110000
PortalErrorCleanup();
#endif
if (MyReplicationSlot) ReplicationSlotRelease();
#if PG_VERSION_NUM >= 170000
ReplicationSlotCleanup(false);
#elif PG_VERSION_NUM >= 100000
ReplicationSlotCleanup();
#endif
#if PG_VERSION_NUM >= 110000
jit_reset_after_error();
#endif
MemoryContextSwitchTo(TopMemoryContext);
}
FlushErrorState();
if (!task.shared->spi) {
xact_started_my(false);
RESUME_INTERRUPTS();
}
}
bool dest_timeout(void) {
int StatementTimeoutMy = StatementTimeout;
if (task_work(&task)) return true;
elog(DEBUG1, "id = %li, timeout = %i, input = %s, count = %i", task.shared->id, task.timeout, task.input, task.count);
set_ps_display_my("timeout");
StatementTimeout = task.timeout;
if (task.shared->spi) {
SPI_connect_my(task.input);
BeginInternalSubTransaction(NULL);
}
PG_TRY();
dest_execute();
if (task.shared->spi) ReleaseCurrentSubTransaction();
PG_CATCH();
task_error(&task);
dest_catch();
if (task.shared->spi) {
RollbackAndReleaseCurrentSubTransaction();
#if PG_VERSION_NUM < 100000
SPI_restore_connection();
#endif
}
PG_END_TRY();
if (task.shared->spi) SPI_finish_my();
StatementTimeout = StatementTimeoutMy;
pgstat_report_stat(false);
pgstat_report_activity(STATE_IDLE, NULL);
set_ps_display_my("idle");
return task_done(&task);
}