Skip to content

Commit d6d70f8

Browse files
committed
Fix bogus cache-invalidation logic in logical replication worker.
The code recorded cache invalidation events by zeroing the "localreloid" field of affected cache entries. However, it's possible for an inval event to occur even while we have the entry open and locked. So an ill-timed inval could result in "cache lookup failed for relation 0" errors, if the worker's code tried to use the cleared field. We can fix that by creating a separate bool field to record whether the entry needs to be revalidated. (In the back branches, cram the bool into what had been padding space, to avoid an ABI break in the somewhat unlikely event that any extension is looking at this struct.) Also, rearrange the logic in logicalrep_rel_open so that it does the right thing in cases where table_open would fail. We should retry the lookup by name in that case, but we didn't. The real-world impact of this is probably small. In the first place, the error conditions are very low probability, and in the second place, the worker would just exit and get restarted. We only noticed because in a CLOBBER_CACHE_ALWAYS build, the failure can occur repeatedly, preventing the worker from making progress. Nonetheless, it's clearly a bug, and it impedes a useful type of testing; so back-patch to v10 where this code was introduced. Discussion: https://postgr.es/m/1032727.1600096803@sss.pgh.pa.us
1 parent b6a5b76 commit d6d70f8

File tree

2 files changed

+44
-27
lines changed

2 files changed

+44
-27
lines changed

src/backend/replication/logical/relation.c

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ logicalrep_relmap_invalidate_cb(Datum arg, Oid reloid)
5959
{
6060
if (entry->localreloid == reloid)
6161
{
62-
entry->localreloid = InvalidOid;
62+
entry->localrelvalid = false;
6363
hash_seq_term(&status);
6464
break;
6565
}
@@ -73,7 +73,7 @@ logicalrep_relmap_invalidate_cb(Datum arg, Oid reloid)
7373
hash_seq_init(&status, LogicalRepRelMap);
7474

7575
while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
76-
entry->localreloid = InvalidOid;
76+
entry->localrelvalid = false;
7777
}
7878
}
7979

@@ -212,15 +212,13 @@ logicalrep_rel_att_by_name(LogicalRepRelation *remoterel, const char *attname)
212212
/*
213213
* Open the local relation associated with the remote one.
214214
*
215-
* Optionally rebuilds the Relcache mapping if it was invalidated
216-
* by local DDL.
215+
* Rebuilds the Relcache mapping if it was invalidated by local DDL.
217216
*/
218217
LogicalRepRelMapEntry *
219218
logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
220219
{
221220
LogicalRepRelMapEntry *entry;
222221
bool found;
223-
Oid relid = InvalidOid;
224222
LogicalRepRelation *remoterel;
225223

226224
if (LogicalRepRelMap == NULL)
@@ -236,14 +234,45 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
236234

237235
remoterel = &entry->remoterel;
238236

237+
/* Ensure we don't leak a relcache refcount. */
238+
if (entry->localrel)
239+
elog(ERROR, "remote relation ID %u is already open", remoteid);
240+
239241
/*
240242
* When opening and locking a relation, pending invalidation messages are
241-
* processed which can invalidate the relation. We need to update the
242-
* local cache both when we are first time accessing the relation and when
243-
* the relation is invalidated (aka entry->localreloid is set InvalidOid).
243+
* processed which can invalidate the relation. Hence, if the entry is
244+
* currently considered valid, try to open the local relation by OID and
245+
* see if invalidation ensues.
244246
*/
245-
if (!OidIsValid(entry->localreloid))
247+
if (entry->localrelvalid)
246248
{
249+
entry->localrel = try_relation_open(entry->localreloid, lockmode);
250+
if (!entry->localrel)
251+
{
252+
/* Table was renamed or dropped. */
253+
entry->localrelvalid = false;
254+
}
255+
else if (!entry->localrelvalid)
256+
{
257+
/* Note we release the no-longer-useful lock here. */
258+
heap_close(entry->localrel, lockmode);
259+
entry->localrel = NULL;
260+
}
261+
}
262+
263+
/*
264+
* If the entry has been marked invalid since we last had lock on it,
265+
* re-open the local relation by name and rebuild all derived data.
266+
*/
267+
if (!entry->localrelvalid)
268+
{
269+
Oid relid;
270+
int found;
271+
Bitmapset *idkey;
272+
TupleDesc desc;
273+
MemoryContext oldctx;
274+
int i;
275+
247276
/* Try to find and lock the relation by name. */
248277
relid = RangeVarGetRelid(makeRangeVar(remoterel->nspname,
249278
remoterel->relname, -1),
@@ -254,21 +283,7 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
254283
errmsg("logical replication target relation \"%s.%s\" does not exist",
255284
remoterel->nspname, remoterel->relname)));
256285
entry->localrel = heap_open(relid, NoLock);
257-
258-
}
259-
else
260-
{
261-
relid = entry->localreloid;
262-
entry->localrel = heap_open(entry->localreloid, lockmode);
263-
}
264-
265-
if (!OidIsValid(entry->localreloid))
266-
{
267-
int found;
268-
Bitmapset *idkey;
269-
TupleDesc desc;
270-
MemoryContext oldctx;
271-
int i;
286+
entry->localreloid = relid;
272287

273288
/* Check for supported relkind. */
274289
CheckSubscriptionRelkind(entry->localrel->rd_rel->relkind,
@@ -361,7 +376,7 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
361376
}
362377
}
363378

364-
entry->localreloid = relid;
379+
entry->localrelvalid = true;
365380
}
366381

367382
if (entry->state != SUBREL_STATE_READY)

src/include/replication/logicalrelation.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ typedef struct LogicalRepRelMapEntry
1818
{
1919
LogicalRepRelation remoterel; /* key is remoterel.remoteid */
2020

21-
/* Mapping to local relation, filled as needed. */
21+
/* Mapping to local relation. */
2222
Oid localreloid; /* local relation id */
23-
Relation localrel; /* relcache entry */
23+
Relation localrel; /* relcache entry (NULL when closed) */
2424
AttrNumber *attrmap; /* map of local attributes to remote ones */
2525
bool updatable; /* Can apply updates/deletes? */
2626

2727
/* Sync state. */
2828
char state;
29+
/* Validity flag ... inserted here to avoid ABI break in back branches. */
30+
bool localrelvalid;
2931
XLogRecPtr statelsn;
3032
} LogicalRepRelMapEntry;
3133

0 commit comments

Comments
 (0)