forked from 2ndQuadrant/pglogical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pglogical_relcache.c
297 lines (238 loc) · 6.92 KB
/
pglogical_relcache.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
/* -------------------------------------------------------------------------
*
* pglogical_relcache.c
* Caching relation specific information
*
* Copyright (C) 2015, PostgreSQL Global Development Group
*
* IDENTIFICATION
* pglogical_relcache.c
*
* -------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/heapam.h"
#include "catalog/pg_trigger.h"
#include "commands/trigger.h"
#include "utils/builtins.h"
#include "utils/catcache.h"
#include "utils/hsearch.h"
#include "utils/fmgroids.h"
#include "utils/inval.h"
#include "utils/rel.h"
#include "pglogical.h"
#include "pglogical_relcache.h"
#define PGLOGICALRELATIONHASH_INITIAL_SIZE 128
static HTAB *PGLogicalRelationHash = NULL;
static void pglogical_relcache_init(void);
static int tupdesc_get_att_by_name(TupleDesc desc, const char *attname);
static void
relcache_free_entry(PGLogicalRelation *entry)
{
pfree(entry->nspname);
pfree(entry->relname);
if (entry->natts > 0)
{
int i;
for (i = 0; i < entry->natts; i++)
pfree(entry->attnames[i]);
pfree(entry->attnames);
}
if (entry->attmap)
pfree(entry->attmap);
entry->natts = 0;
entry->reloid = InvalidOid;
entry->rel = NULL;
}
PGLogicalRelation *
pglogical_relation_open(uint32 remoteid, LOCKMODE lockmode)
{
PGLogicalRelation *entry;
bool found;
if (PGLogicalRelationHash == NULL)
pglogical_relcache_init();
/* Search for existing entry. */
entry = hash_search(PGLogicalRelationHash, (void *) &remoteid,
HASH_FIND, &found);
if (!found)
elog(ERROR, "cache lookup failed for remote relation %u",
remoteid);
/* Need to update the local cache? */
if (!OidIsValid(entry->reloid))
{
RangeVar *rv = makeNode(RangeVar);
int i;
TupleDesc desc;
rv->schemaname = (char *) entry->nspname;
rv->relname = (char *) entry->relname;
entry->rel = table_openrv(rv, lockmode);
desc = RelationGetDescr(entry->rel);
for (i = 0; i < entry->natts; i++)
entry->attmap[i] = tupdesc_get_att_by_name(desc, entry->attnames[i]);
entry->reloid = RelationGetRelid(entry->rel);
/* Cache trigger info. */
entry->hasTriggers = false;
if (entry->rel->trigdesc != NULL)
{
TriggerDesc *trigdesc = entry->rel->trigdesc;
int i;
for (i = 0; i < trigdesc->numtriggers; i++)
{
Trigger *trigger = &trigdesc->triggers[i];
/* We only fire replica triggers on rows */
if (!(trigger->tgenabled == TRIGGER_FIRES_ON_ORIGIN ||
trigger->tgenabled == TRIGGER_DISABLED) &&
TRIGGER_FOR_ROW(trigger->tgtype))
{
entry->hasTriggers = true;
break;
}
}
}
}
else if (!entry->rel)
entry->rel = table_open(entry->reloid, lockmode);
return entry;
}
void
pglogical_relation_cache_update(uint32 remoteid, char *schemaname,
char *relname, int natts, char **attnames)
{
MemoryContext oldcontext;
PGLogicalRelation *entry;
bool found;
int i;
if (PGLogicalRelationHash == NULL)
pglogical_relcache_init();
/*
* HASH_ENTER returns the existing entry if present or creates a new one.
*/
entry = hash_search(PGLogicalRelationHash, (void *) &remoteid,
HASH_ENTER, &found);
if (found)
relcache_free_entry(entry);
/* Make cached copy of the data */
oldcontext = MemoryContextSwitchTo(CacheMemoryContext);
entry->nspname = pstrdup(schemaname);
entry->relname = pstrdup(relname);
entry->natts = natts;
entry->attnames = palloc(natts * sizeof(char *));
for (i = 0; i < natts; i++)
entry->attnames[i] = pstrdup(attnames[i]);
entry->attmap = palloc(natts * sizeof(int));
MemoryContextSwitchTo(oldcontext);
/* XXX Should we validate the relation against local schema here? */
entry->reloid = InvalidOid;
}
void
pglogical_relation_cache_updater(PGLogicalRemoteRel *remoterel)
{
MemoryContext oldcontext;
PGLogicalRelation *entry;
bool found;
int i;
if (PGLogicalRelationHash == NULL)
pglogical_relcache_init();
/*
* HASH_ENTER returns the existing entry if present or creates a new one.
*/
entry = hash_search(PGLogicalRelationHash, (void *) &remoterel->relid,
HASH_ENTER, &found);
if (found)
relcache_free_entry(entry);
/* Make cached copy of the data */
oldcontext = MemoryContextSwitchTo(CacheMemoryContext);
entry->nspname = pstrdup(remoterel->nspname);
entry->relname = pstrdup(remoterel->relname);
entry->natts = remoterel->natts;
entry->attnames = palloc(remoterel->natts * sizeof(char *));
for (i = 0; i < remoterel->natts; i++)
entry->attnames[i] = pstrdup(remoterel->attnames[i]);
entry->attmap = palloc(remoterel->natts * sizeof(int));
MemoryContextSwitchTo(oldcontext);
/* XXX Should we validate the relation against local schema here? */
entry->reloid = InvalidOid;
}
void
pglogical_relation_close(PGLogicalRelation * rel, LOCKMODE lockmode)
{
table_close(rel->rel, lockmode);
rel->rel = NULL;
}
static void
pglogical_relcache_invalidate_callback(Datum arg, Oid reloid)
{
PGLogicalRelation *entry;
/* Just to be sure. */
if (PGLogicalRelationHash == NULL)
return;
if (reloid != InvalidOid)
{
HASH_SEQ_STATUS status;
hash_seq_init(&status, PGLogicalRelationHash);
/* TODO, use inverse lookup hastable */
while ((entry = (PGLogicalRelation *) hash_seq_search(&status)) != NULL)
{
if (entry->reloid == reloid)
entry->reloid = InvalidOid;
}
}
else
{
/* invalidate all cache entries */
HASH_SEQ_STATUS status;
hash_seq_init(&status, PGLogicalRelationHash);
while ((entry = (PGLogicalRelation *) hash_seq_search(&status)) != NULL)
entry->reloid = InvalidOid;
}
}
static void
pglogical_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(uint32);
ctl.entrysize = sizeof(PGLogicalRelation);
ctl.hcxt = CacheMemoryContext;
hashflags = HASH_ELEM | HASH_CONTEXT;
#if PG_VERSION_NUM < 90500
/*
* Handle the old hash API in PostgreSQL 9.4.
* Note, this assumes that Oid is uint32 which is the case for 9.4 anyway.
*
* 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
PGLogicalRelationHash = hash_create("pglogical relation cache",
PGLOGICALRELATIONHASH_INITIAL_SIZE,
&ctl, hashflags);
/* Watch for invalidation events. */
CacheRegisterRelcacheCallback(pglogical_relcache_invalidate_callback,
(Datum) 0);
}
/*
* Find attribute index in TupleDesc struct by attribute name.
*/
static int
tupdesc_get_att_by_name(TupleDesc desc, const char *attname)
{
int i;
for (i = 0; i < desc->natts; i++)
{
Form_pg_attribute att = TupleDescAttr(desc,i);
if (strcmp(NameStr(att->attname), attname) == 0)
return i;
}
elog(ERROR, "unknown column name %s", attname);
}