Skip to content

Commit 8580631

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 af2d09f commit 8580631

File tree

2 files changed

+45
-27
lines changed

2 files changed

+45
-27
lines changed

src/backend/replication/logical/relation.c

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "postgres.h"
1818

19+
#include "access/relation.h"
1920
#include "access/sysattr.h"
2021
#include "access/table.h"
2122
#include "catalog/namespace.h"
@@ -59,7 +60,7 @@ logicalrep_relmap_invalidate_cb(Datum arg, Oid reloid)
5960
{
6061
if (entry->localreloid == reloid)
6162
{
62-
entry->localreloid = InvalidOid;
63+
entry->localrelvalid = false;
6364
hash_seq_term(&status);
6465
break;
6566
}
@@ -73,7 +74,7 @@ logicalrep_relmap_invalidate_cb(Datum arg, Oid reloid)
7374
hash_seq_init(&status, LogicalRepRelMap);
7475

7576
while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
76-
entry->localreloid = InvalidOid;
77+
entry->localrelvalid = false;
7778
}
7879
}
7980

@@ -212,15 +213,13 @@ logicalrep_rel_att_by_name(LogicalRepRelation *remoterel, const char *attname)
212213
/*
213214
* Open the local relation associated with the remote one.
214215
*
215-
* Optionally rebuilds the Relcache mapping if it was invalidated
216-
* by local DDL.
216+
* Rebuilds the Relcache mapping if it was invalidated by local DDL.
217217
*/
218218
LogicalRepRelMapEntry *
219219
logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
220220
{
221221
LogicalRepRelMapEntry *entry;
222222
bool found;
223-
Oid relid = InvalidOid;
224223
LogicalRepRelation *remoterel;
225224

226225
if (LogicalRepRelMap == NULL)
@@ -236,14 +235,45 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
236235

237236
remoterel = &entry->remoterel;
238237

238+
/* Ensure we don't leak a relcache refcount. */
239+
if (entry->localrel)
240+
elog(ERROR, "remote relation ID %u is already open", remoteid);
241+
239242
/*
240243
* 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).
244+
* processed which can invalidate the relation. Hence, if the entry is
245+
* currently considered valid, try to open the local relation by OID and
246+
* see if invalidation ensues.
244247
*/
245-
if (!OidIsValid(entry->localreloid))
248+
if (entry->localrelvalid)
246249
{
250+
entry->localrel = try_relation_open(entry->localreloid, lockmode);
251+
if (!entry->localrel)
252+
{
253+
/* Table was renamed or dropped. */
254+
entry->localrelvalid = false;
255+
}
256+
else if (!entry->localrelvalid)
257+
{
258+
/* Note we release the no-longer-useful lock here. */
259+
table_close(entry->localrel, lockmode);
260+
entry->localrel = NULL;
261+
}
262+
}
263+
264+
/*
265+
* If the entry has been marked invalid since we last had lock on it,
266+
* re-open the local relation by name and rebuild all derived data.
267+
*/
268+
if (!entry->localrelvalid)
269+
{
270+
Oid relid;
271+
int found;
272+
Bitmapset *idkey;
273+
TupleDesc desc;
274+
MemoryContext oldctx;
275+
int i;
276+
247277
/* Try to find and lock the relation by name. */
248278
relid = RangeVarGetRelid(makeRangeVar(remoterel->nspname,
249279
remoterel->relname, -1),
@@ -254,21 +284,7 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
254284
errmsg("logical replication target relation \"%s.%s\" does not exist",
255285
remoterel->nspname, remoterel->relname)));
256286
entry->localrel = table_open(relid, NoLock);
257-
258-
}
259-
else
260-
{
261-
relid = entry->localreloid;
262-
entry->localrel = table_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;
287+
entry->localreloid = relid;
272288

273289
/* Check for supported relkind. */
274290
CheckSubscriptionRelkind(entry->localrel->rd_rel->relkind,
@@ -362,7 +378,7 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
362378
}
363379
}
364380

365-
entry->localreloid = relid;
381+
entry->localrelvalid = true;
366382
}
367383

368384
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)