forked from citusdata/pg_shard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistribution_metadata.h
157 lines (130 loc) · 5.48 KB
/
distribution_metadata.h
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
/*-------------------------------------------------------------------------
*
* distribution_metadata.h
*
* Declarations for public functions and types related to metadata handling.
*
* Copyright (c) 2014, Citus Data, Inc.
*
*-------------------------------------------------------------------------
*/
#ifndef PG_SHARD_DISTRIBUTION_METADATA_H
#define PG_SHARD_DISTRIBUTION_METADATA_H
#include "postgres.h"
#include "c.h"
#include "postgres_ext.h"
#include "nodes/pg_list.h"
#include "nodes/primnodes.h"
#include "storage/lock.h"
/* schema for configuration related to distributed tables */
#define METADATA_SCHEMA_NAME "pgs_distribution_metadata"
/* table and index names for shard interval information */
#define SHARD_TABLE_NAME "shard"
#define SHARD_PKEY_INDEX_NAME "shard_pkey"
#define SHARD_RELATION_INDEX_NAME "shard_relation_index"
/* denotes storage type of the underlying shard */
#define SHARD_STORAGE_TABLE 't'
#define SHARD_STORAGE_FOREIGN 'f'
/* human-readable names for addressing columns of shard table */
#define SHARD_TABLE_ATTRIBUTE_COUNT 5
#define ATTR_NUM_SHARD_ID 1
#define ATTR_NUM_SHARD_RELATION_ID 2
#define ATTR_NUM_SHARD_STORAGE 3
#define ATTR_NUM_SHARD_MIN_VALUE 4
#define ATTR_NUM_SHARD_MAX_VALUE 5
/* table and index names for shard placement information */
#define SHARD_PLACEMENT_TABLE_NAME "shard_placement"
#define SHARD_PLACEMENT_PKEY_INDEX_NAME "shard_placement_pkey"
#define SHARD_PLACEMENT_SHARD_INDEX_NAME "shard_placement_shard_index"
/* human-readable names for addressing columns of shard placement table */
#define SHARD_PLACEMENT_TABLE_ATTRIBUTE_COUNT 5
#define ATTR_NUM_SHARD_PLACEMENT_ID 1
#define ATTR_NUM_SHARD_PLACEMENT_SHARD_ID 2
#define ATTR_NUM_SHARD_PLACEMENT_SHARD_STATE 3
#define ATTR_NUM_SHARD_PLACEMENT_NODE_NAME 4
#define ATTR_NUM_SHARD_PLACEMENT_NODE_PORT 5
/* table containing information about how to partition distributed tables */
#define PARTITION_TABLE_NAME "partition"
/* denotes partition type of the distributed table */
#define HASH_PARTITION_TYPE 'h'
#define RANGE_PARTITION_TYPE 'r'
/* human-readable names for addressing columns of partition table */
#define PARTITION_TABLE_ATTRIBUTE_COUNT 3
#define ATTR_NUM_PARTITION_RELATION_ID 1
#define ATTR_NUM_PARTITION_TYPE 2
#define ATTR_NUM_PARTITION_KEY 3
/* sequence names to generate new shard id and shard placement id */
#define SHARD_ID_SEQUENCE_NAME "shard_id_sequence"
#define SHARD_PLACEMENT_ID_SEQUENCE_NAME "shard_placement_id_sequence"
/* ShardState represents the last known state of a shard on a given node */
typedef enum
{
STATE_INVALID_FIRST = 0,
STATE_FINALIZED = 1,
STATE_CACHED = 2,
STATE_INACTIVE = 3,
STATE_TO_DELETE = 4
} ShardState;
/*
* ShardInterval contains information about a particular shard in a distributed
* table. ShardIntervals have a unique identifier, a reference back to the table
* they distribute, and min and max values for the partition column of rows that
* are contained within the shard (this range is inclusive).
*
* All fields are required.
*/
typedef struct ShardInterval
{
int64 id; /* unique identifier for the shard */
Oid relationId; /* id of the shard's distributed table */
Datum minValue; /* a shard's typed min value datum */
Datum maxValue; /* a shard's typed max value datum */
Oid valueTypeId; /* typeId for minValue and maxValue Datums */
} ShardInterval;
/*
* ShardPlacement represents information about the placement of a shard in a
* distributed table. ShardPlacements have a unique identifier, a reference to
* the shard they place, a textual hostname to identify the host on which the
* shard resides, and a port number to use when connecting to that host.
*
* All fields are required.
*/
typedef struct ShardPlacement
{
int64 id; /* unique identifier for the shard placement */
int64 shardId; /* identifies shard for this shard placement */
ShardState shardState; /* represents last known state of this placement */
char *nodeName; /* hostname of machine hosting this shard */
int32 nodePort; /* port number for connecting to host */
} ShardPlacement;
/*
* ShardIntervalListCacheEntry contains the information for a cache entry in
* shard interval list cache entry.
*/
typedef struct ShardIntervalListCacheEntry
{
Oid distributedTableId; /* cache key */
List *shardIntervalList;
} ShardIntervalListCacheEntry;
/* function declarations to access and manipulate the metadata */
extern List * LookupShardIntervalList(Oid distributedTableId);
extern List * LoadShardIntervalList(Oid distributedTableId);
extern ShardInterval * LoadShardInterval(int64 shardId);
extern List * LoadFinalizedShardPlacementList(uint64 shardId);
extern List * LoadShardPlacementList(int64 shardId);
extern Var * PartitionColumn(Oid distributedTableId);
extern char PartitionType(Oid distributedTableId);
extern bool IsDistributedTable(Oid tableId);
extern bool DistributedTablesExist(void);
extern Var * ColumnNameToColumn(Oid relationId, char *columnName);
extern void InsertPartitionRow(Oid distributedTableId, char partitionType,
text *partitionKeyText);
extern void InsertShardRow(Oid distributedTableId, uint64 shardId, char shardStorage,
text *shardMinValue, text *shardMaxValue);
extern void InsertShardPlacementRow(uint64 shardPlacementId, uint64 shardId,
ShardState shardState, char *nodeName,
uint32 nodePort);
extern void DeleteShardPlacementRow(uint64 shardPlacementId);
extern uint64 NextSequenceId(char *sequenceName);
extern void LockShard(int64 shardId, LOCKMODE lockMode);
#endif /* PG_SHARD_DISTRIBUTION_METADATA_H */