-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathspock_queue.c
187 lines (152 loc) · 4.53 KB
/
spock_queue.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
/*-------------------------------------------------------------------------
*
* spock_queue.c
* spock queue and connection catalog manipulation functions
*
* Copyright (c) 2022-2024, 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 "access/genam.h"
#include "access/hash.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/xact.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/namespace.h"
#include "catalog/objectaddress.h"
#include "catalog/pg_extension.h"
#include "catalog/pg_trigger.h"
#include "catalog/pg_type.h"
#include "commands/extension.h"
#include "executor/spi.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "parser/parse_func.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/json.h"
#include "utils/jsonb.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/timestamp.h"
#include "spock_common.h"
#include "spock_queue.h"
#include "spock_repset.h"
#include "spock.h"
#define CATALOG_QUEUE "queue"
#define Natts_queue 5
#define Anum_queue_queued_at 1
#define Anum_queue_role 2
#define Anum_queue_replication_sets 3
#define Anum_queue_message_type 4
#define Anum_queue_message 5
typedef struct QueueTuple
{
TimestampTz queued_at;
NameData replication_set;
NameData role;
char message_type;
/* json message;*/
} QueueTuple;
/*
* Add tuple to the queue table.
*/
void
queue_message(List *replication_sets, Oid roleoid, char message_type,
char *message)
{
RangeVar *rv;
Relation rel;
TupleDesc tupDesc;
HeapTuple tup;
Datum values[Natts_queue];
bool nulls[Natts_queue];
const char *role;
TimestampTz ts = GetCurrentTimestamp();
role = GetUserNameFromId(roleoid
#if PG_VERSION_NUM >= 90500
, false
#endif
);
rv = makeRangeVar(EXTENSION_NAME, CATALOG_QUEUE, -1);
rel = table_openrv(rv, RowExclusiveLock);
tupDesc = RelationGetDescr(rel);
/* Form a tuple. */
memset(nulls, false, sizeof(nulls));
values[Anum_queue_queued_at - 1] = TimestampTzGetDatum(ts);
values[Anum_queue_role - 1] =
DirectFunctionCall1(namein, CStringGetDatum(role));
if (replication_sets)
values[Anum_queue_replication_sets - 1] =
PointerGetDatum(strlist_to_textarray(replication_sets));
else
nulls[Anum_queue_replication_sets - 1] = true;
values[Anum_queue_message_type - 1] = CharGetDatum(message_type);
values[Anum_queue_message - 1] =
DirectFunctionCall1(json_in, CStringGetDatum(message));
tup = heap_form_tuple(tupDesc, values, nulls);
/* Insert the tuple to the catalog. */
CatalogTupleInsert(rel, tup);
/* Cleanup. */
heap_freetuple(tup);
table_close(rel, NoLock);
}
/*
* Parse the tuple from the queue table into palloc'd QueuedMessage struct.
*
* The caller must have the queue table locked in at least AccessShare mode.
*/
QueuedMessage *
queued_message_from_tuple(HeapTuple queue_tup)
{
RangeVar *rv;
Relation rel;
TupleDesc tupDesc;
bool isnull;
Datum d;
QueuedMessage *res;
/* Open relation to get the tuple descriptor. */
rv = makeRangeVar(EXTENSION_NAME, CATALOG_QUEUE, -1);
rel = table_openrv(rv, NoLock);
tupDesc = RelationGetDescr(rel);
res = (QueuedMessage *) palloc(sizeof(QueuedMessage));
d = fastgetattr(queue_tup, Anum_queue_queued_at, tupDesc, &isnull);
Assert(!isnull);
res->queued_at = DatumGetTimestampTz(d);
d = fastgetattr(queue_tup, Anum_queue_role, tupDesc, &isnull);
Assert(!isnull);
res->role = pstrdup(NameStr(*DatumGetName(d)));
d = fastgetattr(queue_tup, Anum_queue_replication_sets, tupDesc, &isnull);
if (!isnull)
res->replication_sets = textarray_to_list(DatumGetArrayTypeP(d));
else
res->replication_sets = NULL;
d = fastgetattr(queue_tup, Anum_queue_message_type, tupDesc, &isnull);
Assert(!isnull);
res->message_type = DatumGetChar(d);
d = fastgetattr(queue_tup, Anum_queue_message, tupDesc, &isnull);
Assert(!isnull);
/* Parse the json inside the message into Jsonb object. */
res->message = DatumGetJsonb(
DirectFunctionCall1(jsonb_in, DirectFunctionCall1(json_out, d)));
/* Close the relation. */
table_close(rel, NoLock);
return res;
}
/*
* Get (cached) oid of the queue table.
*/
Oid
get_queue_table_oid(void)
{
static Oid queuetableoid = InvalidOid;
if (queuetableoid == InvalidOid)
queuetableoid = get_spock_table_oid(CATALOG_QUEUE);
return queuetableoid;
}