-
Notifications
You must be signed in to change notification settings - Fork 15
/
spock_repset.c
1574 lines (1282 loc) · 38.5 KB
/
spock_repset.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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------------
*
* spock_repset.c
* spock replication set manipulation functions
*
* 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
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "access/genam.h"
#include "access/hash.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "access/xact.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/namespace.h"
#include "catalog/objectaddress.h"
#include "catalog/pg_type.h"
#include "executor/spi.h"
#include "nodes/makefuncs.h"
#include "replication/reorderbuffer.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/catcache.h"
#include "utils/fmgroids.h"
#include "utils/inval.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "spock_dependency.h"
#include "spock_node.h"
#include "spock_queue.h"
#include "spock_repset.h"
#include "spock.h"
#define CATALOG_REPSET "replication_set"
#define CATALOG_REPSET_SEQ "replication_set_seq"
#define CATALOG_REPSET_TABLE "replication_set_table"
#define CATALOG_REPSET_RELATION "replication_set_relation"
typedef struct RepSetTuple
{
Oid id;
Oid nodeid;
NameData name;
bool replicate_insert;
bool replicate_update;
bool replicate_delete;
bool replicate_truncate;
} RepSetTuple;
#define Natts_repset 7
#define Anum_repset_id 1
#define Anum_repset_nodeid 2
#define Anum_repset_name 3
#define Anum_repset_replicate_insert 4
#define Anum_repset_replicate_update 5
#define Anum_repset_replicate_delete 6
#define Anum_repset_replicate_truncate 7
typedef struct RepSetSeqTuple
{
Oid id;
Oid seqoid;
} RepSetSeqTuple;
#define Natts_repset_seq 2
#define Anum_repset_seq_setid 1
#define Anum_repset_seq_seqoid 2
typedef struct RepSetTableTuple
{
Oid setid;
Oid reloid;
#if 0 /* Only for info here. */
text att_list[1];
text row_filter;
#endif
} RepSetTableTuple;
#define Natts_repset_table 4
#define Anum_repset_table_setid 1
#define Anum_repset_table_reloid 2
#define Anum_repset_table_att_list 3
#define Anum_repset_table_row_filter 4
#define REPSETTABLEHASH_INITIAL_SIZE 128
static HTAB *RepSetTableHash = NULL;
/*
* Read the replication set.
*/
SpockRepSet *
get_replication_set(Oid setid)
{
SpockRepSet *repset;
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
ScanKeyData key[1];
Assert(IsTransactionState());
rv = makeRangeVar(EXTENSION_NAME, CATALOG_REPSET, -1);
rel = table_openrv(rv, RowExclusiveLock);
/* Search for repset record. */
ScanKeyInit(&key[0],
Anum_repset_id,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(setid));
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
tuple = systable_getnext(scan);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "replication set %u not found", setid);
repset = replication_set_from_tuple(tuple);
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
return repset;
}
/*
* Find replication set by name
*/
SpockRepSet *
get_replication_set_by_name(Oid nodeid, const char *setname, bool missing_ok)
{
SpockRepSet *repset;
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
ScanKeyData key[2];
Assert(IsTransactionState());
rv = makeRangeVar(EXTENSION_NAME, CATALOG_REPSET, -1);
rel = table_openrv(rv, RowExclusiveLock);
/* Search for repset record. */
ScanKeyInit(&key[0],
Anum_repset_nodeid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(nodeid));
ScanKeyInit(&key[1],
Anum_repset_name,
BTEqualStrategyNumber, F_NAMEEQ,
CStringGetDatum(setname));
scan = systable_beginscan(rel, 0, true, NULL, 2, key);
tuple = systable_getnext(scan);
if (!HeapTupleIsValid(tuple))
{
if (missing_ok)
{
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
return NULL;
}
elog(ERROR, "replication set %s not found", setname);
}
repset = replication_set_from_tuple(tuple);
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
return repset;
}
static void
repset_relcache_invalidate_callback(Datum arg, Oid reloid)
{
SpockTableRepInfo *entry;
/* Just to be sure. */
if (RepSetTableHash == NULL)
return;
if (reloid == InvalidOid)
{
HASH_SEQ_STATUS status;
hash_seq_init(&status, RepSetTableHash);
while ((entry = hash_seq_search(&status)) != NULL)
{
entry->isvalid = false;
if (entry->att_list)
pfree(entry->att_list);
entry->att_list = NULL;
if (list_length(entry->row_filter))
list_free_deep(entry->row_filter);
entry->row_filter = NIL;
}
}
else if ((entry = hash_search(RepSetTableHash, &reloid,
HASH_FIND, NULL)) != NULL)
{
entry->isvalid = false;
if (entry->att_list)
pfree(entry->att_list);
entry->att_list = NULL;
if (list_length(entry->row_filter))
list_free_deep(entry->row_filter);
entry->row_filter = NIL;
}
}
static void
repset_relcache_init(void)
{
HASHCTL ctl;
int hashflags;
/* Make sure we've initialized CacheMemoryContext. */
if (CacheMemoryContext == NULL)
CreateCacheMemoryContext();
/* Initialize the hash table. */
MemSet(&ctl, 0, sizeof(ctl));
ctl.keysize = sizeof(Oid);
ctl.entrysize = sizeof(SpockTableRepInfo);
ctl.hcxt = CacheMemoryContext;
hashflags = HASH_ELEM | HASH_CONTEXT;
#if PG_VERSION_NUM < 90500
/*
* Handle the old hash API in PostgreSQL 9.4.
*
* See postgres commit:
*
* 4a14f13a0ab Improve hash_create's API for selecting simple-binary-key hash functions.
*/
ctl.hash = oid_hash;
hashflags |= HASH_FUNCTION;
#else
hashflags |= HASH_BLOBS;
#endif
RepSetTableHash = hash_create("spock repset table cache",
REPSETTABLEHASH_INITIAL_SIZE, &ctl,
hashflags);
/*
* Watch for invalidation events fired when the relcache changes.
*
* Note that no invalidations are fired when the replication sets are
* created, destroyed, modified, or change membership since there's no
* syscache management for user catalogs. We do our own invalidations for
* those separately.
*/
CacheRegisterRelcacheCallback(repset_relcache_invalidate_callback,
(Datum) 0);
}
List *
get_node_replication_sets(Oid nodeid)
{
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
ScanKeyData key[1];
List *replication_sets = NIL;
Assert(IsTransactionState());
rv = makeRangeVar(EXTENSION_NAME, CATALOG_REPSET, -1);
rel = table_openrv(rv, RowExclusiveLock);
ScanKeyInit(&key[0],
Anum_repset_nodeid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(nodeid));
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
while (HeapTupleIsValid(tuple = systable_getnext(scan)))
{
RepSetTuple *t = (RepSetTuple *) GETSTRUCT(tuple);
SpockRepSet *repset = get_replication_set(t->id);
replication_sets = lappend(replication_sets, repset);
}
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
return replication_sets;
}
List *
get_replication_sets(Oid nodeid, List *replication_set_names, bool missing_ok)
{
RangeVar *rv;
Relation rel;
ListCell *lc;
ScanKeyData key[2];
List *replication_sets = NIL;
Assert(IsTransactionState());
rv = makeRangeVar(EXTENSION_NAME, CATALOG_REPSET, -1);
rel = table_openrv(rv, RowExclusiveLock);
/* Setup common part of key. */
ScanKeyInit(&key[0],
Anum_repset_nodeid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(nodeid));
foreach(lc, replication_set_names)
{
char *setname = lfirst(lc);
SysScanDesc scan;
HeapTuple tuple;
/* Search for repset record. */
ScanKeyInit(&key[1],
Anum_repset_name,
BTEqualStrategyNumber, F_NAMEEQ,
CStringGetDatum(setname));
/* TODO: use index. */
scan = systable_beginscan(rel, 0, true, NULL, 2, key);
tuple = systable_getnext(scan);
if (!HeapTupleIsValid(tuple))
{
if (missing_ok)
{
systable_endscan(scan);
continue;
}
else
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("replication set %s not found", setname)));
}
replication_sets = lappend(replication_sets,
replication_set_from_tuple(tuple));
systable_endscan(scan);
}
table_close(rel, RowExclusiveLock);
return replication_sets;
}
SpockTableRepInfo *
get_table_replication_info(Oid nodeid, Relation table,
List *subs_replication_sets)
{
SpockTableRepInfo *entry;
bool found;
RangeVar *rv;
Oid reloid = RelationGetRelid(table);
Oid repset_reloid;
Relation repset_rel;
ScanKeyData key[1];
SysScanDesc scan;
HeapTuple tuple;
TupleDesc table_desc,
repset_rel_desc;
if (RepSetTableHash == NULL)
repset_relcache_init();
/*
* HASH_ENTER returns the existing entry if present or creates a new one.
*
* It might seem that it's weird to use just reloid here for the cache key
* when we are searching for nodeid + relation. But this function is only
* used by the output plugin which means the nodeid is always the same as
* only one node is connected to current process.
*/
entry = hash_search(RepSetTableHash, (void *) &reloid,
HASH_ENTER, &found);
if (found && entry->isvalid)
return entry;
/* Fill the entry */
entry->reloid = reloid;
entry->replicate_insert = false;
entry->replicate_update = false;
entry->replicate_delete = false;
entry->att_list = NULL;
entry->row_filter = NIL;
/*
* Check for match between table's replication sets and the subscription
* list of replication sets that was given as parameter.
*
* Note that tables can have no replication sets. This will be commonly
* true for example for internal tables which are created during table
* rewrites, so if we'll want to support replicating those, we'll have
* to have special handling for them.
*/
rv = makeRangeVar(EXTENSION_NAME, CATALOG_REPSET_TABLE, -1);
repset_reloid = RangeVarGetRelid(rv, RowExclusiveLock, true);
/* Backwards compat with 1.1/1.2 where the relation name was different. */
if (!OidIsValid(repset_reloid))
{
rv = makeRangeVar(EXTENSION_NAME, CATALOG_REPSET_RELATION, -1);
repset_reloid = RangeVarGetRelid(rv, RowExclusiveLock, true);
if (!OidIsValid(repset_reloid))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_TABLE),
errmsg("relation \"%s.%s\" does not exist",
rv->schemaname, rv->relname)));
}
repset_rel = table_open(repset_reloid, NoLock);
repset_rel_desc = RelationGetDescr(repset_rel);
table_desc = RelationGetDescr(table);
ScanKeyInit(&key[0],
Anum_repset_table_reloid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(reloid));
/* TODO: use index */
scan = systable_beginscan(repset_rel, 0, true, NULL, 1, key);
while (HeapTupleIsValid(tuple = systable_getnext(scan)))
{
RepSetTableTuple *t = (RepSetTableTuple *) GETSTRUCT(tuple);
ListCell *lc;
foreach (lc, subs_replication_sets)
{
SpockRepSet *repset = lfirst(lc);
bool isnull;
Datum d;
if (t->setid == repset->id)
{
/* Update the action filter. */
if (repset->replicate_insert)
entry->replicate_insert = true;
if (repset->replicate_update)
entry->replicate_update = true;
if (repset->replicate_delete)
entry->replicate_delete = true;
/* Update replicated column map. */
d = heap_getattr(tuple, Anum_repset_table_att_list,
repset_rel_desc, &isnull);
if (!isnull)
{
Datum *elems;
int nelems, i;
deconstruct_array(DatumGetArrayTypePCopy(d),
TEXTOID, -1, false, 'i',
&elems, NULL, &nelems);
for (i = 0; i < nelems; i++)
{
const char *attname = TextDatumGetCString(elems[i]);
int attnum = get_att_num_by_name(table_desc,
attname);
MemoryContext olctx = MemoryContextSwitchTo(CacheMemoryContext);
entry->att_list = bms_add_member(entry->att_list,
attnum - FirstLowInvalidHeapAttributeNumber);
MemoryContextSwitchTo(olctx);
}
}
/* Add row filter if any. */
d = heap_getattr(tuple, Anum_repset_table_row_filter,
repset_rel_desc, &isnull);
if (!isnull)
{
MemoryContext olctx = MemoryContextSwitchTo(CacheMemoryContext);
Node *row_filter = stringToNode(TextDatumGetCString(d));
entry->row_filter = lappend(entry->row_filter, row_filter);
MemoryContextSwitchTo(olctx);
}
}
}
}
systable_endscan(scan);
table_close(repset_rel, RowExclusiveLock);
entry->isvalid = true;
return entry;
}
RepSetTableTuple*
get_table_replication_row(Oid repsetid, Oid reloid,
List **att_list, Node **row_filter)
{
Oid repset_reloid;
Oid repset_indoid;
Relation repset_rel;
ScanKeyData key[2];
SysScanDesc scan;
HeapTuple tuple;
TupleDesc repset_rel_desc;
RepSetTableTuple *reptuple = NULL;
repset_reloid = get_replication_set_table_rel_oid();
repset_rel = table_open(repset_reloid, RowExclusiveLock);
repset_rel_desc = RelationGetDescr(repset_rel);
repset_indoid = RelationGetPrimaryKeyIndex(repset_rel);
ScanKeyInit(&key[0],
Anum_repset_table_setid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(repsetid));
ScanKeyInit(&key[1],
Anum_repset_table_reloid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(reloid));
scan = systable_beginscan(repset_rel, repset_indoid, true, NULL, 2, key);
tuple = systable_getnext(scan);
if (HeapTupleIsValid(tuple))
{
bool isnull;
Datum d;
reptuple = (RepSetTableTuple *) GETSTRUCT(tuple);
/* Update replicated column map. */
if (att_list != NULL)
{
d = heap_getattr(tuple, Anum_repset_table_att_list,
repset_rel_desc, &isnull);
if (!isnull)
*att_list = textarray_to_list(DatumGetArrayTypePCopy(d));
}
if (row_filter != NULL)
{
/* Add row filter if any. */
d = heap_getattr(tuple, Anum_repset_table_row_filter,
repset_rel_desc, &isnull);
if (!isnull)
{
MemoryContext olctx = MemoryContextSwitchTo(CacheMemoryContext);
*row_filter = stringToNode(TextDatumGetCString(d));
MemoryContextSwitchTo(olctx);
}
}
}
systable_endscan(scan);
table_close(repset_rel, RowExclusiveLock);
return reptuple;
}
List *
get_table_replication_sets(Oid nodeid, Oid reloid)
{
RangeVar *rv;
Oid relid;
Relation rel;
ScanKeyData key[1];
SysScanDesc scan;
HeapTuple tuple;
List *replication_sets = NIL;
Assert(IsTransactionState());
rv = makeRangeVar(EXTENSION_NAME, CATALOG_REPSET_TABLE, -1);
relid = RangeVarGetRelid(rv, RowExclusiveLock, true);
/* Backwards compat with 1.1/1.2 where the relation name was different. */
if (!OidIsValid(relid))
{
rv = makeRangeVar(EXTENSION_NAME, CATALOG_REPSET_RELATION, -1);
relid = RangeVarGetRelid(rv, RowExclusiveLock, true);
if (!OidIsValid(relid))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_TABLE),
errmsg("relation \"%s.%s\" does not exist",
rv->schemaname, rv->relname)));
}
rel = table_open(relid, NoLock);
ScanKeyInit(&key[0],
Anum_repset_table_reloid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(reloid));
/* TODO: use index */
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
while (HeapTupleIsValid(tuple = systable_getnext(scan)))
{
RepSetSeqTuple *t = (RepSetSeqTuple *) GETSTRUCT(tuple);
SpockRepSet *repset = get_replication_set(t->id);
if (repset->nodeid != nodeid)
continue;
replication_sets = lappend(replication_sets, repset);
}
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
return replication_sets;
}
static bool
sequence_has_replication_sets(Oid nodeid, Oid seqoid)
{
RangeVar *rv;
Relation rel;
ScanKeyData key[1];
SysScanDesc scan;
HeapTuple tuple;
bool res = false;
Assert(IsTransactionState());
rv = makeRangeVar(EXTENSION_NAME, CATALOG_REPSET_SEQ, -1);
rel = table_openrv(rv, RowExclusiveLock);
ScanKeyInit(&key[0],
Anum_repset_seq_seqoid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(seqoid));
/* TODO: use index */
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
if (HeapTupleIsValid(tuple = systable_getnext(scan)))
res = true;
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
return res;
}
List *
get_seq_replication_sets(Oid nodeid, Oid seqoid)
{
RangeVar *rv;
Relation rel;
ScanKeyData key[1];
SysScanDesc scan;
HeapTuple tuple;
List *replication_sets = NIL;
Assert(IsTransactionState());
rv = makeRangeVar(EXTENSION_NAME, CATALOG_REPSET_SEQ, -1);
rel = table_openrv(rv, RowExclusiveLock);
ScanKeyInit(&key[0],
Anum_repset_table_reloid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(seqoid));
/* TODO: use index */
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
while (HeapTupleIsValid(tuple = systable_getnext(scan)))
{
RepSetSeqTuple *t = (RepSetSeqTuple *) GETSTRUCT(tuple);
SpockRepSet *repset = get_replication_set(t->id);
if (repset->nodeid != nodeid)
continue;
replication_sets = lappend(replication_sets, repset);
}
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
return replication_sets;
}
/*
* Add new tuple to the replication_sets catalog.
*/
void
create_replication_set(SpockRepSet *repset)
{
RangeVar *rv;
Relation rel;
TupleDesc tupDesc;
HeapTuple tup;
Datum values[Natts_repset];
bool nulls[Natts_repset];
NameData repset_name;
if (strlen(repset->name) == 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_NAME),
errmsg("replication set name cannot be empty")));
if (get_replication_set_by_name(repset->nodeid, repset->name, true) != NULL)
elog(ERROR, "replication set %s already exists", repset->name);
/*
* Generate new id unless one was already specified.
*/
if (repset->id == InvalidOid)
{
uint32 hashinput[2];
hashinput[0] = repset->nodeid;
hashinput[1] = DatumGetUInt32(hash_any((const unsigned char *) repset->name,
strlen(repset->name)));
repset->id = DatumGetUInt32(hash_any((const unsigned char *) hashinput,
(int) sizeof(hashinput)));
}
rv = makeRangeVar(EXTENSION_NAME, CATALOG_REPSET, -1);
rel = table_openrv(rv, RowExclusiveLock);
tupDesc = RelationGetDescr(rel);
/* Form a tuple. */
memset(nulls, false, sizeof(nulls));
values[Anum_repset_id - 1] = ObjectIdGetDatum(repset->id);
values[Anum_repset_nodeid - 1] = ObjectIdGetDatum(repset->nodeid);
namestrcpy(&repset_name, repset->name);
values[Anum_repset_name - 1] = NameGetDatum(&repset_name);
values[Anum_repset_replicate_insert - 1] =
BoolGetDatum(repset->replicate_insert);
values[Anum_repset_replicate_update - 1] =
BoolGetDatum(repset->replicate_update);
values[Anum_repset_replicate_delete - 1] =
BoolGetDatum(repset->replicate_delete);
values[Anum_repset_replicate_truncate - 1] =
BoolGetDatum(repset->replicate_truncate);
tup = heap_form_tuple(tupDesc, values, nulls);
/* Insert the tuple to the catalog. */
CatalogTupleInsert(rel, tup);
/* Cleanup. */
heap_freetuple(tup);
table_close(rel, RowExclusiveLock);
CommandCounterIncrement();
}
/*
* Alter the existing replication set.
*/
void
alter_replication_set(SpockRepSet *repset)
{
RangeVar *rv;
SysScanDesc scan;
ScanKeyData key[1];
Relation rel;
TupleDesc tupDesc;
HeapTuple oldtup,
newtup;
Datum values[Natts_repset];
bool nulls[Natts_repset];
bool replaces[Natts_repset];
rv = makeRangeVar(EXTENSION_NAME, CATALOG_REPSET, -1);
rel = table_openrv(rv, RowExclusiveLock);
tupDesc = RelationGetDescr(rel);
/* Search for repset record. */
ScanKeyInit(&key[0],
Anum_repset_id,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(repset->id));
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
oldtup = systable_getnext(scan);
if (!HeapTupleIsValid(oldtup))
elog(ERROR, "replication set %u not found", repset->id);
/*
* Validate that replication is not being changed to replicate UPDATEs
* and DELETEs if it contains any tables without replication identity.
*/
if (repset->replicate_update || repset->replicate_delete)
{
RangeVar *tablesrv;
Relation tablesrel;
SysScanDesc tablesscan;
HeapTuple tablestup;
ScanKeyData tableskey[1];
tablesrv = makeRangeVar(EXTENSION_NAME, CATALOG_REPSET_TABLE, -1);
tablesrel = table_openrv(tablesrv, RowExclusiveLock);
/* Search for the record. */
ScanKeyInit(&tableskey[0],
Anum_repset_table_setid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(repset->id));
tablesscan = systable_beginscan(tablesrel, 0, true, NULL, 1, tableskey);
/* Process every individual table in the set. */
while (HeapTupleIsValid(tablestup = systable_getnext(tablesscan)))
{
RepSetTableTuple *t = (RepSetTableTuple *) GETSTRUCT(tablestup);
Relation targetrel;
targetrel = table_open(t->reloid, AccessShareLock);
/* Check of relation has replication index. */
if (RelationGetForm(targetrel)->relkind == RELKIND_RELATION)
{
if (targetrel->rd_indexvalid == 0)
RelationGetIndexList(targetrel);
if (!OidIsValid(targetrel->rd_replidindex) &&
(repset->replicate_update || repset->replicate_delete))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("replication set %s cannot be altered to "
"replicate UPDATEs or DELETEs because it "
"contains tables without PRIMARY KEY",
repset->name)));
}
table_close(targetrel, NoLock);
}
systable_endscan(tablesscan);
table_close(tablesrel, RowExclusiveLock);
}
/* Everything ok, form a new tuple. */
memset(nulls, false, sizeof(nulls));
memset(replaces, true, sizeof(replaces));
replaces[Anum_repset_id - 1] = false;
replaces[Anum_repset_name - 1] = false;
replaces[Anum_repset_nodeid - 1] = false;
values[Anum_repset_replicate_insert - 1] =
BoolGetDatum(repset->replicate_insert);
values[Anum_repset_replicate_update - 1] =
BoolGetDatum(repset->replicate_update);
values[Anum_repset_replicate_delete - 1] =
BoolGetDatum(repset->replicate_delete);
values[Anum_repset_replicate_truncate - 1] =
BoolGetDatum(repset->replicate_truncate);
newtup = heap_modify_tuple(oldtup, tupDesc, values, nulls, replaces);
/* Update the tuple in catalog. */
CatalogTupleUpdate(rel, &oldtup->t_self, newtup);
/* Cleanup. */
heap_freetuple(newtup);
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
}
/*
* Remove all tables from replication set.
*/
static void
replication_set_remove_tables(Oid setid, Oid nodeid)
{
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
ScanKeyData key[1];
ObjectAddress myself;
rv = makeRangeVar(EXTENSION_NAME, CATALOG_REPSET_TABLE, -1);
rel = table_openrv(rv, RowExclusiveLock);
myself.classId = get_replication_set_table_rel_oid();
myself.objectId = setid;
/* Search for the record. */
ScanKeyInit(&key[0],
Anum_repset_table_setid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(setid));
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
while (HeapTupleIsValid(tuple = systable_getnext(scan)))
{
RepSetTableTuple *t = (RepSetTableTuple *) GETSTRUCT(tuple);
Oid reloid = t->reloid;
/* Remove the tuple. */
simple_heap_delete(rel, &tuple->t_self);
CacheInvalidateRelcacheByRelid(reloid);
/* Dependency cleanup. */
myself.objectSubId = reloid;
spock_tryDropDependencies(&myself, DROP_CASCADE);
}
/* Cleanup. */
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
}
/*
* Remove all sequences from replication set.
*/
static void
replication_set_remove_seqs(Oid setid, Oid nodeid)
{
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
ScanKeyData key[1];
ObjectAddress myself;
rv = makeRangeVar(EXTENSION_NAME, CATALOG_REPSET_SEQ, -1);
rel = table_openrv(rv, RowExclusiveLock);
/* Search for the record. */
ScanKeyInit(&key[0],
Anum_repset_table_setid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(setid));
scan = systable_beginscan(rel, 0, true, NULL, 1, key);
myself.classId = get_replication_set_seq_rel_oid();
myself.objectId = setid;
while (HeapTupleIsValid(tuple = systable_getnext(scan)))
{
RepSetSeqTuple *t = (RepSetSeqTuple *) GETSTRUCT(tuple);
Oid seqoid = t->seqoid;
/* Remove the tuple. */
simple_heap_delete(rel, &tuple->t_self);
/* Make sure the sequence_has_replication_sets sees the changes. */
CommandCounterIncrement();
if (!sequence_has_replication_sets(nodeid, seqoid))
spock_drop_sequence_state_record(seqoid);
CacheInvalidateRelcacheByRelid(seqoid);
/* Dependency cleanup. */
myself.objectSubId = seqoid;
spock_tryDropDependencies(&myself, DROP_CASCADE);
}
/* Cleanup. */
systable_endscan(scan);
table_close(rel, RowExclusiveLock);
}
/*
* Delete the tuple from replication sets catalog.
*/
void
drop_replication_set(Oid setid)
{
RangeVar *rv;
Relation rel;
SysScanDesc scan;
HeapTuple tuple;
ScanKeyData key[1];
RepSetTuple *repset;