forked from pgEdge/spock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spock_apply_spi.c
703 lines (574 loc) · 17.9 KB
/
spock_apply_spi.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/*-------------------------------------------------------------------------
*
* spock_apply_spi.c
* spock apply functions using SPI
*
* Copyright (c) 2022-2023, pgEdge, Inc.
* Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, The Regents of the University of California
*
* NOTES
* The multi-insert support is not done through SPI but using binary
* COPY through a pipe (virtual or file).
*
*-------------------------------------------------------------------------
*/
#include <stdio.h>
#include <unistd.h>
#include "postgres.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "access/xact.h"
#include "commands/copy.h"
#include "executor/executor.h"
#include "executor/spi.h"
#include "replication/origin.h"
#include "replication/reorderbuffer.h"
#include "storage/fd.h"
#include "tcop/pquery.h"
#include "tcop/utility.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/builtins.h"
#include "spock.h"
#include "spock_apply_spi.h"
#include "spock_conflict.h"
/* for htonl/htons */
#include <arpa/inet.h>
/* State related to bulk insert */
typedef struct spock_copyState
{
SpockRelation *rel;
StringInfo copy_stmt;
List *copy_parsetree;
File copy_file;
char copy_mechanism;
FILE *copy_read_file;
FILE *copy_write_file;
StringInfo msgbuf;
MemoryContext rowcontext;
FmgrInfo *out_functions;
List *attnumlist;
int copy_buffered_tuples;
size_t copy_buffered_size;
} spock_copyState;
static spock_copyState *spkcstate = NULL;
static const char BinarySignature[11] = "PGCOPY\n\377\r\n\0";
static void spock_start_copy(SpockRelation *rel);
static void spock_proccess_copy(spock_copyState *spkcstate);
static void spock_copySendData(spock_copyState *spkcstate,
const void *databuf, int datasize);
static void spock_copySendEndOfRow(spock_copyState *spkcstate);
static void spock_copySendInt32(spock_copyState *spkcstate, int32 val);
static void spock_copySendInt16(spock_copyState *spkcstate, int16 val);
static void spock_copyOneRowTo(spock_copyState *spkcstate,
Datum *values, bool *nulls);
/*
* Handle begin (connect SPI).
*/
void
spock_apply_spi_begin(void)
{
if (SPI_connect() != SPI_OK_CONNECT)
elog(ERROR, "SPI_connect failed");
MemoryContextSwitchTo(MessageContext);
}
/*
* Handle commit (finish SPI).
*/
void
spock_apply_spi_commit(void)
{
if (SPI_finish() != SPI_OK_FINISH)
elog(ERROR, "SPI_finish failed");
MemoryContextSwitchTo(MessageContext);
}
/*
* Handle insert via SPI.
*/
void
spock_apply_spi_insert(SpockRelation *rel, SpockTupleData *newtup)
{
TupleDesc desc = RelationGetDescr(rel->rel);
Oid argtypes[MaxTupleAttributeNumber];
Datum values[MaxTupleAttributeNumber];
char nulls[MaxTupleAttributeNumber];
StringInfoData cmd;
int att,
narg;
initStringInfo(&cmd);
appendStringInfo(&cmd, "INSERT INTO %s (",
quote_qualified_identifier(rel->nspname, rel->relname));
for (att = 0, narg = 0; att < desc->natts; att++)
{
if (TupleDescAttr(desc,att)->attisdropped)
continue;
if (!newtup->changed[att])
continue;
if (narg > 0)
appendStringInfo(&cmd, ", %s",
quote_identifier(NameStr(TupleDescAttr(desc,att)->attname)));
else
appendStringInfo(&cmd, "%s",
quote_identifier(NameStr(TupleDescAttr(desc,att)->attname)));
narg++;
}
appendStringInfoString(&cmd, ") VALUES (");
for (att = 0, narg = 0; att < desc->natts; att++)
{
if (TupleDescAttr(desc,att)->attisdropped)
continue;
if (!newtup->changed[att])
continue;
if (narg > 0)
appendStringInfo(&cmd, ", $%u", narg + 1);
else
appendStringInfo(&cmd, "$%u", narg + 1);
argtypes[narg] = TupleDescAttr(desc,att)->atttypid;
values[narg] = newtup->values[att];
nulls[narg] = newtup->nulls[att] ? 'n' : ' ';
narg++;
}
appendStringInfoString(&cmd, ")");
if (SPI_execute_with_args(cmd.data, narg, argtypes, values, nulls, false,
0) != SPI_OK_INSERT)
elog(ERROR, "SPI_execute_with_args failed");
MemoryContextSwitchTo(MessageContext);
pfree(cmd.data);
}
/*
* Handle update via SPI.
*/
void
spock_apply_spi_update(SpockRelation *rel, SpockTupleData *oldtup,
SpockTupleData *newtup)
{
TupleDesc desc = RelationGetDescr(rel->rel);
Oid argtypes[MaxTupleAttributeNumber];
Datum values[MaxTupleAttributeNumber];
char nulls[MaxTupleAttributeNumber];
StringInfoData cmd;
Bitmapset *id_attrs;
int att,
narg,
firstarg;
id_attrs = RelationGetIndexAttrBitmap(rel->rel,
INDEX_ATTR_BITMAP_IDENTITY_KEY);
initStringInfo(&cmd);
appendStringInfo(&cmd, "UPDATE %s SET ",
quote_qualified_identifier(rel->nspname, rel->relname));
for (att = 0, narg = 0; att < desc->natts; att++)
{
if (TupleDescAttr(desc,att)->attisdropped)
continue;
if (!newtup->changed[att])
continue;
if (narg > 0)
appendStringInfo(&cmd, ", %s = $%u",
quote_identifier(NameStr(TupleDescAttr(desc,att)->attname)),
narg + 1);
else
appendStringInfo(&cmd, "%s = $%u",
quote_identifier(NameStr(TupleDescAttr(desc,att)->attname)),
narg + 1);
argtypes[narg] = TupleDescAttr(desc,att)->atttypid;
values[narg] = newtup->values[att];
nulls[narg] = newtup->nulls[att] ? 'n' : ' ';
narg++;
}
appendStringInfoString(&cmd, " WHERE");
firstarg = narg;
for (att = 0; att < desc->natts; att++)
{
if (!bms_is_member(TupleDescAttr(desc,att)->attnum - FirstLowInvalidHeapAttributeNumber,
id_attrs))
continue;
if (narg > firstarg)
appendStringInfo(&cmd, " AND %s = $%u",
quote_identifier(NameStr(TupleDescAttr(desc,att)->attname)),
narg + 1);
else
appendStringInfo(&cmd, " %s = $%u",
quote_identifier(NameStr(TupleDescAttr(desc,att)->attname)),
narg + 1);
argtypes[narg] = TupleDescAttr(desc,att)->atttypid;
values[narg] = oldtup->values[att];
nulls[narg] = oldtup->nulls[att] ? 'n' : ' ';
narg++;
}
if (SPI_execute_with_args(cmd.data, narg, argtypes, values, nulls, false,
0) != SPI_OK_UPDATE)
elog(ERROR, "SPI_execute_with_args failed");
MemoryContextSwitchTo(MessageContext);
pfree(cmd.data);
}
/*
* Handle delete via SPI.
*/
void
spock_apply_spi_delete(SpockRelation *rel, SpockTupleData *oldtup)
{
TupleDesc desc = RelationGetDescr(rel->rel);
Oid argtypes[MaxTupleAttributeNumber];
Datum values[MaxTupleAttributeNumber];
char nulls[MaxTupleAttributeNumber];
StringInfoData cmd;
Bitmapset *id_attrs;
int att,
narg;
id_attrs = RelationGetIndexAttrBitmap(rel->rel,
INDEX_ATTR_BITMAP_IDENTITY_KEY);
initStringInfo(&cmd);
appendStringInfo(&cmd, "DELETE FROM %s WHERE",
quote_qualified_identifier(rel->nspname, rel->relname));
for (att = 0, narg = 0; att < desc->natts; att++)
{
if (!bms_is_member(TupleDescAttr(desc,att)->attnum - FirstLowInvalidHeapAttributeNumber,
id_attrs))
continue;
if (narg > 0)
appendStringInfo(&cmd, " AND %s = $%u",
quote_identifier(NameStr(TupleDescAttr(desc,att)->attname)),
narg + 1);
else
appendStringInfo(&cmd, " %s = $%u",
quote_identifier(NameStr(TupleDescAttr(desc,att)->attname)),
narg + 1);
argtypes[narg] = TupleDescAttr(desc,att)->atttypid;
values[narg] = oldtup->values[att];
nulls[narg] = oldtup->nulls[att] ? 'n' : ' ';
narg++;
}
if (SPI_execute_with_args(cmd.data, narg, argtypes, values, nulls, false,
0) != SPI_OK_DELETE)
elog(ERROR, "SPI_execute_with_args failed");
MemoryContextSwitchTo(MessageContext);
pfree(cmd.data);
}
/* We currently can't support multi insert using COPY on windows. */
#if !defined(WIN32) && !defined(SPK_NO_STDIN_ASSIGN)
bool
spock_apply_spi_can_mi(SpockRelation *rel)
{
/* Multi insert is only supported when conflicts result in errors. */
return spock_conflict_resolver == SPOCK_RESOLVE_ERROR;
}
void
spock_apply_spi_mi_add_tuple(SpockRelation *rel,
SpockTupleData *tup)
{
Datum *values;
bool *nulls;
/* Start COPY if not already done so */
spock_start_copy(rel);
#define MAX_BUFFERED_TUPLES 10000
#define MAX_BUFFER_SIZE 60000
/*
* If sufficient work is pending, process that first
*/
if (spkcstate->copy_buffered_tuples > MAX_BUFFERED_TUPLES ||
spkcstate->copy_buffered_size > MAX_BUFFER_SIZE)
{
spock_apply_spi_mi_finish(rel);
spock_start_copy(rel);
}
/*
* Write the tuple to the COPY stream.
*/
values = (Datum *) tup->values;
nulls = (bool *) tup->nulls;
spock_copyOneRowTo(spkcstate, values, nulls);
}
/*
* Initialize copy state for reation.
*/
static void
spock_start_copy(SpockRelation *rel)
{
MemoryContext oldcontext;
TupleDesc desc;
ListCell *cur;
int num_phys_attrs;
char *delim;
StringInfoData attrnames;
int i;
/* We are already doing COPY for requested relation, nothing to do. */
if (spkcstate && spkcstate->rel == rel)
return;
/* We are in COPY but for different relation, finish it first. */
if (spkcstate && spkcstate->rel != rel)
spock_apply_spi_mi_finish(spkcstate->rel);
oldcontext = MemoryContextSwitchTo(TopTransactionContext);
/* Initialize new COPY state. */
spkcstate = palloc0(sizeof(spock_copyState));
spkcstate->copy_file = -1;
spkcstate->msgbuf = makeStringInfo();
spkcstate->rowcontext = AllocSetContextCreate(CurrentMemoryContext,
"COPY TO",
ALLOCSET_DEFAULT_SIZES);
spkcstate->rel = rel;
for (i = 0; i < rel->natts; i++)
spkcstate->attnumlist = lappend_int(spkcstate->attnumlist,
rel->attmap[i]);
desc = RelationGetDescr(rel->rel);
num_phys_attrs = desc->natts;
/* Get info about the columns we need to process. */
spkcstate->out_functions = (FmgrInfo *) palloc(num_phys_attrs * sizeof(FmgrInfo));
/* Get attribute list in a CSV form */
initStringInfo(&attrnames);
delim = "";
/*
* Now that we have a list of attributes from the remote side and their
* mapping to our side, build a COPY statement that can be parsed and
* executed later to bulk load the incoming tuples.
*/
foreach(cur, spkcstate->attnumlist)
{
int attnum = lfirst_int(cur);
Oid out_func_oid;
bool isvarlena;
getTypeBinaryOutputInfo(TupleDescAttr(desc,attnum)->atttypid,
&out_func_oid,
&isvarlena);
fmgr_info(out_func_oid, &spkcstate->out_functions[attnum]);
appendStringInfo(&attrnames, "%s %s",
delim,
quote_identifier(NameStr(TupleDescAttr(desc,attnum)->attname)));
delim = ", ";
}
spkcstate->copy_stmt = makeStringInfo();
appendStringInfo(spkcstate->copy_stmt, "COPY %s.%s (%s) FROM STDIN "
"WITH (FORMAT BINARY)",
quote_identifier(rel->nspname),
quote_identifier(rel->relname),
attrnames.data);
pfree(attrnames.data);
/*
* This is a bit of kludge to let COPY FROM read from the STDIN. In
* spock, the apply worker is accumulating tuples received from the
* publisher and queueing them for a bulk load. But the COPY API can only
* deal with either a file or a PROGRAM or STDIN.
*
* We could either use pipe-based implementation where the apply worker
* first writes to one end of the pipe and later reads from the other end.
* But pipe's internal buffer is limited in size and hence we cannot
* accumulate much data without writing it out to the table.
*
* The temporary file based implementation is more flexible. The only
* disadvantage being that the data may get written to the disk and that
* may cause performance issues.
*
* A more ideal solution would be to teach COPY to write to and read from a
* buffer. But that will require changes to the in-core COPY
* infrastructure. Instead, we setup things such that a pipe is created
* between STDIN and a unnamed stream. The tuples are written to the one
* end of the pipe and read back from the other end. Since we can fiddle
* with the existing STDIN, we assign the read end of the pipe to STDIN.
*
* This seems ok since the apply worker being a background worker is not
* going to read anything from the STDIN normally. So our highjacking of
* the stream seems ok.
*/
if (spkcstate->copy_file == -1)
spkcstate->copy_file = OpenTemporaryFile(true);
Assert(spkcstate->copy_file > 0);
spkcstate->copy_write_file = fopen(FilePathName(spkcstate->copy_file), "w");
spkcstate->copy_read_file = fopen(FilePathName(spkcstate->copy_file), "r");
spkcstate->copy_mechanism = 'f';
spkcstate->copy_parsetree = pg_parse_query(spkcstate->copy_stmt->data);
MemoryContextSwitchTo(oldcontext);
spock_copySendData(spkcstate, BinarySignature,
sizeof(BinarySignature));
spock_copySendInt32(spkcstate, 0);
spock_copySendInt32(spkcstate, 0);
}
static void
spock_proccess_copy(spock_copyState *spkcstate)
{
uint64 processed;
FILE *save_stdin;
if (!spkcstate->copy_parsetree || !spkcstate->copy_buffered_tuples)
return;
/*
* First send a file trailer so that when DoCopy is run below, it sees an
* end of the file marker and terminates COPY once all queued tuples are
* processed. We also close the file descriptor because DoCopy expects to
* see a real EOF too
*/
spock_copySendInt16(spkcstate, -1);
/* Also ensure that the data is flushed to the stream */
spock_copySendEndOfRow(spkcstate);
/*
* Now close the write end of the pipe so that DoCopy sees end of the
* stream.
*
* XXX This is really sad because ideally we would have liked to keep the
* pipe open and use that for next batch of bulk copy. But given the way
* COPY protocol currently works, we don't have any other option but to
* close the stream.
*/
fflush(spkcstate->copy_write_file);
fclose(spkcstate->copy_write_file);
spkcstate->copy_write_file = NULL;
/*
* The COPY statement previously crafted will read from STDIN. So we
* override the 'stdin' stream to point to the read end of the pipe created
* for this relation. Before that we save the current 'stdin' stream and
* restore it back when the COPY is done
*/
save_stdin = stdin;
stdin = spkcstate->copy_read_file;
/* COPY may call into SPI (triggers, ...) and we already are in SPI. */
SPI_push();
/* Initiate the actual COPY */
SPKDoCopy((CopyStmt*)((RawStmt *)linitial(spkcstate->copy_parsetree))->stmt,
spkcstate->copy_stmt->data, &processed);
/* Clean up SPI state */
SPI_pop();
fclose(spkcstate->copy_read_file);
spkcstate->copy_read_file = NULL;
stdin = save_stdin;
/* Ensure we processed correct number of tuples */
Assert(processed == spkcstate->copy_buffered_tuples);
list_free_deep(spkcstate->copy_parsetree);
spkcstate->copy_parsetree = NIL;
spkcstate->copy_buffered_tuples = 0;
spkcstate->copy_buffered_size = 0;
CommandCounterIncrement();
}
void
spock_apply_spi_mi_finish(SpockRelation *rel)
{
if (!spkcstate)
return;
Assert(spkcstate->rel == rel);
spock_proccess_copy(spkcstate);
if (spkcstate->copy_stmt)
{
pfree(spkcstate->copy_stmt->data);
pfree(spkcstate->copy_stmt);
}
if (spkcstate->attnumlist)
list_free(spkcstate->attnumlist);
if (spkcstate->copy_file != -1)
FileClose(spkcstate->copy_file);
if (spkcstate->copy_write_file)
fclose(spkcstate->copy_write_file);
if (spkcstate->copy_read_file)
fclose(spkcstate->copy_read_file);
if (spkcstate->msgbuf)
{
pfree(spkcstate->msgbuf->data);
pfree(spkcstate->msgbuf);
}
if (spkcstate->rowcontext)
{
MemoryContextDelete(spkcstate->rowcontext);
spkcstate->rowcontext = NULL;
}
pfree(spkcstate);
spkcstate = NULL;
}
/*
* spock_copySendInt32 sends an int32 in network byte order
*/
static void
spock_copySendInt32(spock_copyState *spkcstate, int32 val)
{
uint32 buf;
buf = htonl((uint32) val);
spock_copySendData(spkcstate, &buf, sizeof(buf));
}
/*
* spock_copySendInt16 sends an int16 in network byte order
*/
static void
spock_copySendInt16(spock_copyState *spkcstate, int16 val)
{
uint16 buf;
buf = htons((uint16) val);
spock_copySendData(spkcstate, &buf, sizeof(buf));
}
/*----------
* spock_copySendData sends output data to the destination (file or frontend)
* spock_copySendEndOfRow does the appropriate thing at end of each data row
* (data is not actually flushed except by spock_copySendEndOfRow)
*
* NB: no data conversion is applied by these functions
*----------
*/
static void
spock_copySendData(spock_copyState *spkcstate, const void *databuf,
int datasize)
{
appendBinaryStringInfo(spkcstate->msgbuf, databuf, datasize);
}
static void
spock_copySendEndOfRow(spock_copyState *spkcstate)
{
StringInfo msgbuf = spkcstate->msgbuf;
if (fwrite(msgbuf->data, msgbuf->len, 1,
spkcstate->copy_write_file) != 1 ||
ferror(spkcstate->copy_write_file))
{
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not write to COPY file: %m")));
}
resetStringInfo(msgbuf);
}
/*
* Emit one row during CopyTo().
*/
static void
spock_copyOneRowTo(spock_copyState *spkcstate, Datum *values,
bool *nulls)
{
FmgrInfo *out_functions = spkcstate->out_functions;
MemoryContext oldcontext;
ListCell *cur;
MemoryContextReset(spkcstate->rowcontext);
oldcontext = MemoryContextSwitchTo(spkcstate->rowcontext);
/* Binary per-tuple header */
spock_copySendInt16(spkcstate, list_length(spkcstate->attnumlist));
foreach(cur, spkcstate->attnumlist)
{
int attnum = lfirst_int(cur);
Datum value = values[attnum];
bool isnull = nulls[attnum];
if (isnull)
spock_copySendInt32(spkcstate, -1);
else
{
bytea *outputbytes;
outputbytes = SendFunctionCall(&out_functions[attnum],
value);
spock_copySendInt32(spkcstate, VARSIZE(outputbytes) - VARHDRSZ);
spock_copySendData(spkcstate, VARDATA(outputbytes),
VARSIZE(outputbytes) - VARHDRSZ);
}
}
spkcstate->copy_buffered_tuples++;
spkcstate->copy_buffered_size += spkcstate->msgbuf->len;
spock_copySendEndOfRow(spkcstate);
MemoryContextSwitchTo(oldcontext);
}
#else /* WIN32 */
bool
spock_apply_spi_can_mi(SpockRelation *rel)
{
return false;
}
void
spock_apply_spi_mi_add_tuple(SpockRelation *rel,
SpockTupleData *tup)
{
elog(ERROR, "spock_apply_spi_mi_add_tuple called unexpectedly");
}
void
spock_apply_spi_mi_finish(SpockRelation *rel)
{
elog(ERROR, "spock_apply_spi_mi_finish called unexpectedly");
}
#endif /* WIN32 */