forked from citusdata/pg_shard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distribution_metadata.c
840 lines (684 loc) · 25.8 KB
/
distribution_metadata.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
/*-------------------------------------------------------------------------
*
* distribution_metadata.c
*
* This file contains functions to access and manage the distributed table
* metadata.
*
* Copyright (c) 2014, Citus Data, Inc.
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "pg_config.h"
#include "distribution_metadata.h"
#include <stddef.h>
#include <string.h>
#include "access/attnum.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/htup.h"
#include "access/sdir.h"
#include "access/skey.h"
#include "access/tupdesc.h"
#include "access/xact.h"
#include "catalog/indexing.h"
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/sequence.h"
#include "nodes/makefuncs.h"
#include "nodes/memnodes.h"
#include "nodes/pg_list.h"
#include "nodes/primnodes.h"
#include "storage/lock.h"
#include "utils/builtins.h"
#include "utils/elog.h"
#include "utils/errcodes.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/palloc.h"
#include "utils/rel.h"
#include "utils/relcache.h"
#include "utils/tqual.h"
/*
* ShardIntervalListCache is used for caching shard interval lists. It begins
* initialized to empty list as there are no items in the cache.
*/
static List *ShardIntervalListCache = NIL;
/* local function forward declarations */
static void LoadShardIntervalRow(int64 shardId, Oid *relationId,
char **minValue, char **maxValue);
static ShardPlacement * TupleToShardPlacement(HeapTuple heapTuple,
TupleDesc tupleDescriptor);
/*
* LookupShardIntervalList is wrapper around LoadShardIntervalList that uses a
* cache to avoid multiple lookups of a distributed table's shards within a
* single session.
*/
List *
LookupShardIntervalList(Oid distributedTableId)
{
ShardIntervalListCacheEntry *matchingCacheEntry = NULL;
ListCell *cacheEntryCell = NULL;
/* search the cache */
foreach(cacheEntryCell, ShardIntervalListCache)
{
ShardIntervalListCacheEntry *cacheEntry = lfirst(cacheEntryCell);
if (cacheEntry->distributedTableId == distributedTableId)
{
matchingCacheEntry = cacheEntry;
break;
}
}
/* if not found in the cache, load the shard interval and put it in cache */
if (matchingCacheEntry == NULL)
{
MemoryContext oldContext = MemoryContextSwitchTo(CacheMemoryContext);
List *loadedIntervalList = LoadShardIntervalList(distributedTableId);
if (loadedIntervalList != NIL)
{
matchingCacheEntry = palloc0(sizeof(ShardIntervalListCacheEntry));
matchingCacheEntry->distributedTableId = distributedTableId;
matchingCacheEntry->shardIntervalList = loadedIntervalList;
ShardIntervalListCache = lappend(ShardIntervalListCache, matchingCacheEntry);
}
MemoryContextSwitchTo(oldContext);
}
/*
* The only case we don't cache the shard list is when the distributed table
* doesn't have any shards. This is to force reloading shard list on next call.
*/
if (matchingCacheEntry == NULL)
{
return NIL;
}
return matchingCacheEntry->shardIntervalList;
}
/*
* LoadShardIntervalList returns a list of shard intervals related for a given
* distributed table. The function returns an empty list if no shards can be
* found for the given relation.
*/
List *
LoadShardIntervalList(Oid distributedTableId)
{
List *shardIntervalList = NIL;
RangeVar *heapRangeVar = NULL;
RangeVar *indexRangeVar = NULL;
Relation heapRelation = NULL;
Relation indexRelation = NULL;
IndexScanDesc indexScanDesc = NULL;
const int scanKeyCount = 1;
ScanKeyData scanKey[scanKeyCount];
HeapTuple heapTuple = NULL;
heapRangeVar = makeRangeVar(METADATA_SCHEMA_NAME, SHARD_TABLE_NAME, -1);
indexRangeVar = makeRangeVar(METADATA_SCHEMA_NAME, SHARD_RELATION_INDEX_NAME, -1);
heapRelation = relation_openrv(heapRangeVar, AccessShareLock);
indexRelation = relation_openrv(indexRangeVar, AccessShareLock);
ScanKeyInit(&scanKey[0], 1, BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(distributedTableId));
indexScanDesc = index_beginscan(heapRelation, indexRelation, SnapshotSelf,
scanKeyCount, 0);
index_rescan(indexScanDesc, scanKey, scanKeyCount, NULL, 0);
heapTuple = index_getnext(indexScanDesc, ForwardScanDirection);
while (HeapTupleIsValid(heapTuple))
{
TupleDesc tupleDescriptor = RelationGetDescr(heapRelation);
bool isNull = false;
Datum shardIdDatum = heap_getattr(heapTuple, ATTR_NUM_SHARD_ID,
tupleDescriptor, &isNull);
int64 shardId = DatumGetInt64(shardIdDatum);
ShardInterval *shardInterval = LoadShardInterval(shardId);
shardIntervalList = lappend(shardIntervalList, shardInterval);
heapTuple = index_getnext(indexScanDesc, ForwardScanDirection);
}
index_endscan(indexScanDesc);
index_close(indexRelation, AccessShareLock);
relation_close(heapRelation, AccessShareLock);
return shardIntervalList;
}
/*
* LoadShardInterval collects metadata for a specified shard in a ShardInterval
* and returns a pointer to that structure. The function throws an error if no
* shard can be found using the provided identifier.
*/
ShardInterval *
LoadShardInterval(int64 shardId)
{
ShardInterval *shardInterval = NULL;
Datum minValue = 0;
Datum maxValue = 0;
char partitionType = '\0';
Oid intervalTypeId = InvalidOid;
int32 intervalTypeMod = -1;
Oid inputFunctionId = InvalidOid;
Oid typeIoParam = InvalidOid;
Oid relationId = InvalidOid;
char *minValueString = NULL;
char *maxValueString = NULL;
/* first read the related row from the shard table */
LoadShardIntervalRow(shardId, &relationId, &minValueString, &maxValueString);
/* then find min/max values' actual types */
partitionType = PartitionType(relationId);
if (partitionType == HASH_PARTITION_TYPE)
{
intervalTypeId = INT4OID;
}
else
{
Var *partitionColumn = PartitionColumn(relationId);
intervalTypeId = partitionColumn->vartype;
intervalTypeMod = partitionColumn->vartypmod;
}
getTypeInputInfo(intervalTypeId, &inputFunctionId, &typeIoParam);
/* finally convert min/max values to their actual types */
minValue = OidInputFunctionCall(inputFunctionId, minValueString,
typeIoParam, intervalTypeMod);
maxValue = OidInputFunctionCall(inputFunctionId, maxValueString,
typeIoParam, intervalTypeMod);
shardInterval = (ShardInterval *) palloc0(sizeof(ShardInterval));
shardInterval->id = shardId;
shardInterval->relationId = relationId;
shardInterval->minValue = minValue;
shardInterval->maxValue = maxValue;
shardInterval->valueTypeId = intervalTypeId;
return shardInterval;
}
/*
* LoadFinalizedShardPlacementList returns all placements for a given shard that
* are in the finalized state. Like LoadShardPlacementList, this function throws
* an error if the specified shard has not been placed.
*/
List *
LoadFinalizedShardPlacementList(uint64 shardId)
{
List *finalizedPlacementList = NIL;
List *shardPlacementList = LoadShardPlacementList(shardId);
ListCell *shardPlacementCell = NULL;
foreach(shardPlacementCell, shardPlacementList)
{
ShardPlacement *shardPlacement = (ShardPlacement *) lfirst(shardPlacementCell);
if (shardPlacement->shardState == STATE_FINALIZED)
{
finalizedPlacementList = lappend(finalizedPlacementList, shardPlacement);
}
}
return finalizedPlacementList;
}
/*
* LoadShardPlacementList gathers metadata for every placement of a given shard
* and returns a list of ShardPlacements containing that metadata. The function
* throws an error if the specified shard has not been placed.
*/
List *
LoadShardPlacementList(int64 shardId)
{
List *shardPlacementList = NIL;
RangeVar *heapRangeVar = NULL;
RangeVar *indexRangeVar = NULL;
Relation heapRelation = NULL;
Relation indexRelation = NULL;
IndexScanDesc indexScanDesc = NULL;
const int scanKeyCount = 1;
ScanKeyData scanKey[scanKeyCount];
HeapTuple heapTuple = NULL;
heapRangeVar = makeRangeVar(METADATA_SCHEMA_NAME, SHARD_PLACEMENT_TABLE_NAME, -1);
indexRangeVar = makeRangeVar(METADATA_SCHEMA_NAME,
SHARD_PLACEMENT_SHARD_INDEX_NAME, -1);
heapRelation = relation_openrv(heapRangeVar, AccessShareLock);
indexRelation = relation_openrv(indexRangeVar, AccessShareLock);
ScanKeyInit(&scanKey[0], 1, BTEqualStrategyNumber, F_INT8EQ, Int64GetDatum(shardId));
indexScanDesc = index_beginscan(heapRelation, indexRelation, SnapshotSelf,
scanKeyCount, 0);
index_rescan(indexScanDesc, scanKey, scanKeyCount, NULL, 0);
heapTuple = index_getnext(indexScanDesc, ForwardScanDirection);
while (HeapTupleIsValid(heapTuple))
{
TupleDesc tupleDescriptor = RelationGetDescr(heapRelation);
ShardPlacement *shardPlacement = TupleToShardPlacement(heapTuple,
tupleDescriptor);
shardPlacementList = lappend(shardPlacementList, shardPlacement);
heapTuple = index_getnext(indexScanDesc, ForwardScanDirection);
}
index_endscan(indexScanDesc);
index_close(indexRelation, AccessShareLock);
relation_close(heapRelation, AccessShareLock);
/* if no shard placements are found, error out */
if (shardPlacementList == NIL)
{
ereport(ERROR, (errmsg("could not find any placements for shardId "
INT64_FORMAT, shardId)));
}
return shardPlacementList;
}
/*
* PartitionColumn looks up the column used to partition a given distributed
* table and returns a reference to a Var representing that column. If no entry
* can be found using the provided identifer, this function throws an error.
*/
Var *
PartitionColumn(Oid distributedTableId)
{
Var *partitionColumn = NULL;
RangeVar *heapRangeVar = NULL;
Relation heapRelation = NULL;
HeapScanDesc scanDesc = NULL;
const int scanKeyCount = 1;
ScanKeyData scanKey[scanKeyCount];
HeapTuple heapTuple = NULL;
heapRangeVar = makeRangeVar(METADATA_SCHEMA_NAME, PARTITION_TABLE_NAME, -1);
heapRelation = relation_openrv(heapRangeVar, AccessShareLock);
ScanKeyInit(&scanKey[0], ATTR_NUM_PARTITION_RELATION_ID, InvalidStrategy,
F_OIDEQ, ObjectIdGetDatum(distributedTableId));
scanDesc = heap_beginscan(heapRelation, SnapshotSelf, scanKeyCount, scanKey);
heapTuple = heap_getnext(scanDesc, ForwardScanDirection);
if (HeapTupleIsValid(heapTuple))
{
TupleDesc tupleDescriptor = RelationGetDescr(heapRelation);
bool isNull = false;
Datum keyDatum = heap_getattr(heapTuple, ATTR_NUM_PARTITION_KEY,
tupleDescriptor, &isNull);
char *partitionColumnName = TextDatumGetCString(keyDatum);
partitionColumn = ColumnNameToColumn(distributedTableId, partitionColumnName);
}
else
{
ereport(ERROR, (errmsg("could not find partition for distributed "
"relation %u", distributedTableId)));
}
heap_endscan(scanDesc);
relation_close(heapRelation, AccessShareLock);
return partitionColumn;
}
/*
* PartitionType looks up the type used to partition a given distributed
* table and returns a char representing this type. If no entry can be found
* using the provided identifer, this function throws an error.
*/
char
PartitionType(Oid distributedTableId)
{
char partitionType = 0;
RangeVar *heapRangeVar = NULL;
Relation heapRelation = NULL;
HeapScanDesc scanDesc = NULL;
const int scanKeyCount = 1;
ScanKeyData scanKey[scanKeyCount];
HeapTuple heapTuple = NULL;
heapRangeVar = makeRangeVar(METADATA_SCHEMA_NAME, PARTITION_TABLE_NAME, -1);
heapRelation = relation_openrv(heapRangeVar, AccessShareLock);
ScanKeyInit(&scanKey[0], ATTR_NUM_PARTITION_RELATION_ID, InvalidStrategy,
F_OIDEQ, ObjectIdGetDatum(distributedTableId));
scanDesc = heap_beginscan(heapRelation, SnapshotSelf, scanKeyCount, scanKey);
heapTuple = heap_getnext(scanDesc, ForwardScanDirection);
if (HeapTupleIsValid(heapTuple))
{
TupleDesc tupleDescriptor = RelationGetDescr(heapRelation);
bool isNull = false;
Datum partitionTypeDatum = heap_getattr(heapTuple, ATTR_NUM_PARTITION_TYPE,
tupleDescriptor, &isNull);
partitionType = DatumGetChar(partitionTypeDatum);
}
else
{
ereport(ERROR, (errmsg("could not find partition for distributed "
"relation %u", distributedTableId)));
}
heap_endscan(scanDesc);
relation_close(heapRelation, AccessShareLock);
return partitionType;
}
/*
* IsDistributedTable simply returns whether the specified table is distributed.
*/
bool
IsDistributedTable(Oid tableId)
{
bool isDistributedTable = false;
RangeVar *heapRangeVar = NULL;
Relation heapRelation = NULL;
HeapScanDesc scanDesc = NULL;
const int scanKeyCount = 1;
ScanKeyData scanKey[scanKeyCount];
HeapTuple heapTuple = NULL;
heapRangeVar = makeRangeVar(METADATA_SCHEMA_NAME, PARTITION_TABLE_NAME, -1);
heapRelation = relation_openrv(heapRangeVar, AccessShareLock);
ScanKeyInit(&scanKey[0], ATTR_NUM_PARTITION_RELATION_ID, InvalidStrategy,
F_OIDEQ, ObjectIdGetDatum(tableId));
scanDesc = heap_beginscan(heapRelation, SnapshotSelf, scanKeyCount, scanKey);
heapTuple = heap_getnext(scanDesc, ForwardScanDirection);
isDistributedTable = HeapTupleIsValid(heapTuple);
heap_endscan(scanDesc);
relation_close(heapRelation, AccessShareLock);
return isDistributedTable;
}
/*
* DistributedTablesExist returns true if pg_shard has a record of any
* distributed tables; otherwise this function returns false.
*/
bool
DistributedTablesExist(void)
{
bool distributedTablesExist = false;
RangeVar *heapRangeVar = NULL;
Relation heapRelation = NULL;
HeapScanDesc scanDesc = NULL;
HeapTuple heapTuple = NULL;
heapRangeVar = makeRangeVar(METADATA_SCHEMA_NAME, PARTITION_TABLE_NAME, -1);
heapRelation = relation_openrv(heapRangeVar, AccessShareLock);
scanDesc = heap_beginscan(heapRelation, SnapshotSelf, 0, NULL);
heapTuple = heap_getnext(scanDesc, ForwardScanDirection);
/*
* Check whether the partition metadata table contains any tuples. If so,
* at least one distributed table exists.
*/
distributedTablesExist = HeapTupleIsValid(heapTuple);
heap_endscan(scanDesc);
relation_close(heapRelation, AccessShareLock);
return distributedTablesExist;
}
/*
* ColumnNameToColumn accepts a relation identifier and column name and returns
* a Var that represents that column in that relation. This function throws an
* error if the column doesn't exist or is a system column.
*/
Var *
ColumnNameToColumn(Oid relationId, char *columnName)
{
Var *partitionColumn = NULL;
Oid columnTypeOid = InvalidOid;
int32 columnTypeMod = -1;
Oid columnCollationOid = InvalidOid;
/* dummy indexes needed by makeVar */
const Index tableId = 1;
const Index columnLevelsUp = 0;
AttrNumber columnId = get_attnum(relationId, columnName);
if (columnId == InvalidAttrNumber)
{
ereport(ERROR, (errcode(ERRCODE_UNDEFINED_COLUMN),
errmsg("partition column \"%s\" not found", columnName)));
}
else if (!AttrNumberIsForUserDefinedAttr(columnId))
{
ereport(ERROR, (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
errmsg("specified partition column \"%s\" is a system "
"column", columnName)));
}
get_atttypetypmodcoll(relationId, columnId, &columnTypeOid, &columnTypeMod,
&columnCollationOid);
partitionColumn = makeVar(tableId, columnId, columnTypeOid, columnTypeMod,
columnCollationOid, columnLevelsUp);
return partitionColumn;
}
/*
* LoadShardIntervalRow finds the row for the specified shard identifier in the
* shard table and copies values from that row into the provided output params.
*/
static void
LoadShardIntervalRow(int64 shardId, Oid *relationId, char **minValue,
char **maxValue)
{
RangeVar *heapRangeVar = NULL;
RangeVar *indexRangeVar = NULL;
Relation heapRelation = NULL;
Relation indexRelation = NULL;
IndexScanDesc indexScanDesc = NULL;
const int scanKeyCount = 1;
ScanKeyData scanKey[scanKeyCount];
HeapTuple heapTuple = NULL;
heapRangeVar = makeRangeVar(METADATA_SCHEMA_NAME, SHARD_TABLE_NAME, -1);
indexRangeVar = makeRangeVar(METADATA_SCHEMA_NAME, SHARD_PKEY_INDEX_NAME, -1);
heapRelation = relation_openrv(heapRangeVar, AccessShareLock);
indexRelation = relation_openrv(indexRangeVar, AccessShareLock);
ScanKeyInit(&scanKey[0], 1, BTEqualStrategyNumber, F_INT8EQ, Int64GetDatum(shardId));
indexScanDesc = index_beginscan(heapRelation, indexRelation, SnapshotSelf,
scanKeyCount, 0);
index_rescan(indexScanDesc, scanKey, scanKeyCount, NULL, 0);
heapTuple = index_getnext(indexScanDesc, ForwardScanDirection);
if (HeapTupleIsValid(heapTuple))
{
TupleDesc tupleDescriptor = RelationGetDescr(heapRelation);
bool isNull = false;
Datum relationIdDatum = heap_getattr(heapTuple, ATTR_NUM_SHARD_RELATION_ID,
tupleDescriptor, &isNull);
Datum minValueDatum = heap_getattr(heapTuple, ATTR_NUM_SHARD_MIN_VALUE,
tupleDescriptor, &isNull);
Datum maxValueDatum = heap_getattr(heapTuple, ATTR_NUM_SHARD_MAX_VALUE,
tupleDescriptor, &isNull);
/* convert and deep copy row's values */
(*relationId) = DatumGetObjectId(relationIdDatum);
(*minValue) = TextDatumGetCString(minValueDatum);
(*maxValue) = TextDatumGetCString(maxValueDatum);
}
else
{
ereport(ERROR, (errmsg("could not find entry for shard " INT64_FORMAT,
shardId)));
}
index_endscan(indexScanDesc);
index_close(indexRelation, AccessShareLock);
relation_close(heapRelation, AccessShareLock);
return;
}
/*
* TupleToShardPlacement populates a ShardPlacement using values from a row of
* the placements configuration table and returns a pointer to that struct. The
* input tuple must not contain any NULLs.
*/
static ShardPlacement *
TupleToShardPlacement(HeapTuple heapTuple, TupleDesc tupleDescriptor)
{
ShardPlacement *shardPlacement = NULL;
bool isNull = false;
Datum idDatum = heap_getattr(heapTuple, ATTR_NUM_SHARD_PLACEMENT_ID,
tupleDescriptor, &isNull);
Datum shardIdDatum = heap_getattr(heapTuple, ATTR_NUM_SHARD_PLACEMENT_SHARD_ID,
tupleDescriptor, &isNull);
Datum shardStateDatum = heap_getattr(heapTuple, ATTR_NUM_SHARD_PLACEMENT_SHARD_STATE,
tupleDescriptor, &isNull);
Datum nodeNameDatum = heap_getattr(heapTuple, ATTR_NUM_SHARD_PLACEMENT_NODE_NAME,
tupleDescriptor, &isNull);
Datum nodePortDatum = heap_getattr(heapTuple, ATTR_NUM_SHARD_PLACEMENT_NODE_PORT,
tupleDescriptor, &isNull);
shardPlacement = palloc0(sizeof(ShardPlacement));
shardPlacement->id = DatumGetInt64(idDatum);
shardPlacement->shardId = DatumGetInt64(shardIdDatum);
shardPlacement->shardState = DatumGetInt32(shardStateDatum);
shardPlacement->nodeName = TextDatumGetCString(nodeNameDatum);
shardPlacement->nodePort = DatumGetInt32(nodePortDatum);
return shardPlacement;
}
/*
* InsertPartitionRow opens the partition metadata table and inserts a new row
* with the given values.
*/
void
InsertPartitionRow(Oid distributedTableId, char partitionType, text *partitionKeyText)
{
Relation partitionRelation = NULL;
RangeVar *partitionRangeVar = NULL;
TupleDesc tupleDescriptor = NULL;
HeapTuple heapTuple = NULL;
Datum values[PARTITION_TABLE_ATTRIBUTE_COUNT];
bool isNulls[PARTITION_TABLE_ATTRIBUTE_COUNT];
/* form new partition tuple */
memset(values, 0, sizeof(values));
memset(isNulls, false, sizeof(isNulls));
values[ATTR_NUM_PARTITION_RELATION_ID - 1] = ObjectIdGetDatum(distributedTableId);
values[ATTR_NUM_PARTITION_TYPE - 1] = CharGetDatum(partitionType);
values[ATTR_NUM_PARTITION_KEY - 1] = PointerGetDatum(partitionKeyText);
/* open the partition relation and insert new tuple */
partitionRangeVar = makeRangeVar(METADATA_SCHEMA_NAME, PARTITION_TABLE_NAME, -1);
partitionRelation = heap_openrv(partitionRangeVar, RowExclusiveLock);
tupleDescriptor = RelationGetDescr(partitionRelation);
heapTuple = heap_form_tuple(tupleDescriptor, values, isNulls);
simple_heap_insert(partitionRelation, heapTuple);
CatalogUpdateIndexes(partitionRelation, heapTuple);
CommandCounterIncrement();
/* close relation */
relation_close(partitionRelation, RowExclusiveLock);
}
/*
* InsertShardRow opens the shard metadata table and inserts a new row with
* the given values into that table. Note that we allow the user to pass in
* null min/max values.
*/
void
InsertShardRow(Oid distributedTableId, uint64 shardId, char shardStorage,
text *shardMinValue, text *shardMaxValue)
{
Relation shardRelation = NULL;
RangeVar *shardRangeVar = NULL;
TupleDesc tupleDescriptor = NULL;
HeapTuple heapTuple = NULL;
Datum values[SHARD_TABLE_ATTRIBUTE_COUNT];
bool isNulls[SHARD_TABLE_ATTRIBUTE_COUNT];
/* form new shard tuple */
memset(values, 0, sizeof(values));
memset(isNulls, false, sizeof(isNulls));
values[ATTR_NUM_SHARD_ID - 1] = Int64GetDatum(shardId);
values[ATTR_NUM_SHARD_RELATION_ID - 1] = ObjectIdGetDatum(distributedTableId);
values[ATTR_NUM_SHARD_STORAGE - 1] = CharGetDatum(shardStorage);
/* check if shard min/max values are null */
if (shardMinValue != NULL && shardMaxValue != NULL)
{
values[ATTR_NUM_SHARD_MIN_VALUE - 1] = PointerGetDatum(shardMinValue);
values[ATTR_NUM_SHARD_MAX_VALUE - 1] = PointerGetDatum(shardMaxValue);
}
else
{
isNulls[ATTR_NUM_SHARD_MIN_VALUE - 1] = true;
isNulls[ATTR_NUM_SHARD_MAX_VALUE - 1] = true;
}
/* open shard relation and insert new tuple */
shardRangeVar = makeRangeVar(METADATA_SCHEMA_NAME, SHARD_TABLE_NAME, -1);
shardRelation = heap_openrv(shardRangeVar, RowExclusiveLock);
tupleDescriptor = RelationGetDescr(shardRelation);
heapTuple = heap_form_tuple(tupleDescriptor, values, isNulls);
simple_heap_insert(shardRelation, heapTuple);
CatalogUpdateIndexes(shardRelation, heapTuple);
CommandCounterIncrement();
/* close relation */
heap_close(shardRelation, RowExclusiveLock);
}
/*
* InsertShardPlacementRow opens the shard placement metadata table and inserts
* a row with the given values into the table.
*/
void
InsertShardPlacementRow(uint64 shardPlacementId, uint64 shardId,
ShardState shardState, char *nodeName, uint32 nodePort)
{
Relation shardPlacementRelation = NULL;
RangeVar *shardPlacementRangeVar = NULL;
TupleDesc tupleDescriptor = NULL;
HeapTuple heapTuple = NULL;
Datum values[SHARD_PLACEMENT_TABLE_ATTRIBUTE_COUNT];
bool isNulls[SHARD_PLACEMENT_TABLE_ATTRIBUTE_COUNT];
/* form new shard placement tuple */
memset(values, 0, sizeof(values));
memset(isNulls, false, sizeof(isNulls));
values[ATTR_NUM_SHARD_PLACEMENT_ID - 1] = Int64GetDatum(shardPlacementId);
values[ATTR_NUM_SHARD_PLACEMENT_SHARD_ID - 1] = Int64GetDatum(shardId);
values[ATTR_NUM_SHARD_PLACEMENT_SHARD_STATE - 1] = UInt32GetDatum(shardState);
values[ATTR_NUM_SHARD_PLACEMENT_NODE_NAME - 1] = CStringGetTextDatum(nodeName);
values[ATTR_NUM_SHARD_PLACEMENT_NODE_PORT - 1] = UInt32GetDatum(nodePort);
/* open shard placement relation and insert new tuple */
shardPlacementRangeVar = makeRangeVar(METADATA_SCHEMA_NAME,
SHARD_PLACEMENT_TABLE_NAME, -1);
shardPlacementRelation = heap_openrv(shardPlacementRangeVar, RowExclusiveLock);
tupleDescriptor = RelationGetDescr(shardPlacementRelation);
heapTuple = heap_form_tuple(tupleDescriptor, values, isNulls);
simple_heap_insert(shardPlacementRelation, heapTuple);
CatalogUpdateIndexes(shardPlacementRelation, heapTuple);
CommandCounterIncrement();
/* close relation */
heap_close(shardPlacementRelation, RowExclusiveLock);
}
/*
* DeleteShardPlacementRow removes the row corresponding to the provided shard
* placement identifier, erroring out if it cannot find such a row.
*/
void
DeleteShardPlacementRow(uint64 shardPlacementId)
{
RangeVar *heapRangeVar = NULL;
RangeVar *indexRangeVar = NULL;
Relation heapRelation = NULL;
Relation indexRelation = NULL;
IndexScanDesc indexScanDesc = NULL;
const int scanKeyCount = 1;
ScanKeyData scanKey[scanKeyCount];
HeapTuple heapTuple = NULL;
heapRangeVar = makeRangeVar(METADATA_SCHEMA_NAME, SHARD_PLACEMENT_TABLE_NAME, -1);
indexRangeVar = makeRangeVar(METADATA_SCHEMA_NAME,
SHARD_PLACEMENT_PKEY_INDEX_NAME, -1);
heapRelation = relation_openrv(heapRangeVar, RowExclusiveLock);
indexRelation = relation_openrv(indexRangeVar, AccessShareLock);
ScanKeyInit(&scanKey[0], 1, BTEqualStrategyNumber, F_INT8EQ,
Int64GetDatum(shardPlacementId));
indexScanDesc = index_beginscan(heapRelation, indexRelation, SnapshotSelf,
scanKeyCount, 0);
index_rescan(indexScanDesc, scanKey, scanKeyCount, NULL, 0);
heapTuple = index_getnext(indexScanDesc, ForwardScanDirection);
if (HeapTupleIsValid(heapTuple))
{
simple_heap_delete(heapRelation, &heapTuple->t_self);
}
else
{
ereport(ERROR, (errmsg("could not find entry for shard placement " INT64_FORMAT,
shardPlacementId)));
}
index_endscan(indexScanDesc);
index_close(indexRelation, AccessShareLock);
relation_close(heapRelation, RowExclusiveLock);
return;
}
/*
* NextSequenceId allocates and returns a new unique id generated from the given
* sequence name.
*/
uint64
NextSequenceId(char *sequenceName)
{
RangeVar *sequenceRangeVar = makeRangeVar(METADATA_SCHEMA_NAME,
sequenceName, -1);
bool failOk = false;
Oid sequenceRelationId = RangeVarGetRelid(sequenceRangeVar, NoLock, failOk);
Datum sequenceRelationIdDatum = ObjectIdGetDatum(sequenceRelationId);
/* generate new and unique id from sequence */
Datum sequenceIdDatum = DirectFunctionCall1(nextval_oid, sequenceRelationIdDatum);
uint64 nextSequenceId = (uint64) DatumGetInt64(sequenceIdDatum);
return nextSequenceId;
}
/*
* LockShard returns after acquiring a lock for the specified shard, blocking
* indefinitely if required. Only the ExclusiveLock and ShareLock modes are
* supported: all others will trigger an error. Locks acquired with this method
* are automatically released at transaction end.
*/
void
LockShard(int64 shardId, LOCKMODE lockMode)
{
/* locks use 32-bit identifier fields, so split shardId */
uint32 keyUpperHalf = (uint32) (shardId >> 32);
uint32 keyLowerHalf = (uint32) shardId;
LOCKTAG lockTag;
memset(&lockTag, 0, sizeof(LOCKTAG));
SET_LOCKTAG_ADVISORY(lockTag, MyDatabaseId, keyUpperHalf, keyLowerHalf, 0);
if (lockMode == ExclusiveLock || lockMode == ShareLock)
{
bool sessionLock = false; /* we want a transaction lock */
bool dontWait = false; /* block indefinitely until acquired */
(void) LockAcquire(&lockTag, lockMode, sessionLock, dontWait);
}
else
{
ereport(ERROR, (errmsg("attempted to lock shard using unsupported mode")));
}
}