Skip to content

Commit 452253e

Browse files
committed
Fix a crash in logical replication
The bug was that determining which columns are part of the replica identity index using RelationGetIndexAttrBitmap() would run eval_const_expressions() on index expressions and predicates across all indexes of the table, which in turn might require a snapshot, but there wasn't one set, so it crashes. There were actually two separate bugs, one on the publisher and one on the subscriber. To trigger the bug, a table that is part of a publication or subscription needs to have an index with a predicate or expression that lends itself to constant expressions simplification. The fix is to avoid the constant expressions simplification in RelationGetIndexAttrBitmap(), so that it becomes safe to call in these contexts. The constant expressions simplification comes from the calls to RelationGetIndexExpressions()/RelationGetIndexPredicate() via BuildIndexInfo(). But RelationGetIndexAttrBitmap() calling BuildIndexInfo() is overkill. The latter just takes pg_index catalog information, packs it into the IndexInfo structure, which former then just unpacks again and throws away. We can just do this directly with less overhead and skip the troublesome calls to eval_const_expressions(). This also removes the awkward cross-dependency between relcache.c and index.c. Bug: #15114 Reported-by: Петър Славов <pet.slavov@gmail.com> Reviewed-by: Noah Misch <noah@leadboat.com> Reviewed-by: Michael Paquier <michael@paquier.xyz> Discussion: https://www.postgresql.org/message-id/flat/152110589574.1223.17983600132321618383@wrigleys.postgresql.org/
1 parent 549d2a2 commit 452253e

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

src/backend/utils/cache/relcache.c

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#include "access/transam.h"
3838
#include "access/xact.h"
3939
#include "catalog/catalog.h"
40-
#include "catalog/index.h"
4140
#include "catalog/indexing.h"
4241
#include "catalog/namespace.h"
4342
#include "catalog/pg_amproc.h"
@@ -4201,28 +4200,51 @@ RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
42014200
{
42024201
Oid indexOid = lfirst_oid(l);
42034202
Relation indexDesc;
4204-
IndexInfo *indexInfo;
4203+
Datum datum;
4204+
bool isnull;
4205+
Node *indexExpressions;
4206+
Node *indexPredicate;
42054207
int i;
42064208
bool isKey; /* candidate key */
42074209
bool isIDKey; /* replica identity index */
42084210

42094211
indexDesc = index_open(indexOid, AccessShareLock);
42104212

4211-
/* Extract index key information from the index's pg_index row */
4212-
indexInfo = BuildIndexInfo(indexDesc);
4213+
/*
4214+
* Extract index expressions and index predicate. Note: Don't use
4215+
* RelationGetIndexExpressions()/RelationGetIndexPredicate(), because
4216+
* those might run constant expressions evaluation, which needs a
4217+
* snapshot, which we might not have here. (Also, it's probably more
4218+
* sound to collect the bitmaps before any transformations that might
4219+
* eliminate columns, but the practical impact of this is limited.)
4220+
*/
4221+
4222+
datum = heap_getattr(indexDesc->rd_indextuple, Anum_pg_index_indexprs,
4223+
GetPgIndexDescriptor(), &isnull);
4224+
if (!isnull)
4225+
indexExpressions = stringToNode(TextDatumGetCString(datum));
4226+
else
4227+
indexExpressions = NULL;
4228+
4229+
datum = heap_getattr(indexDesc->rd_indextuple, Anum_pg_index_indpred,
4230+
GetPgIndexDescriptor(), &isnull);
4231+
if (!isnull)
4232+
indexPredicate = stringToNode(TextDatumGetCString(datum));
4233+
else
4234+
indexPredicate = NULL;
42134235

42144236
/* Can this index be referenced by a foreign key? */
4215-
isKey = indexInfo->ii_Unique &&
4216-
indexInfo->ii_Expressions == NIL &&
4217-
indexInfo->ii_Predicate == NIL;
4237+
isKey = indexDesc->rd_index->indisunique &&
4238+
indexExpressions == NULL &&
4239+
indexPredicate == NULL;
42184240

42194241
/* Is this index the configured (or default) replica identity? */
42204242
isIDKey = (indexOid == relreplindex);
42214243

42224244
/* Collect simple attribute references */
4223-
for (i = 0; i < indexInfo->ii_NumIndexAttrs; i++)
4245+
for (i = 0; i < indexDesc->rd_index->indnatts; i++)
42244246
{
4225-
int attrnum = indexInfo->ii_KeyAttrNumbers[i];
4247+
int attrnum = indexDesc->rd_index->indkey.values[i];
42264248

42274249
if (attrnum != 0)
42284250
{
@@ -4240,10 +4262,10 @@ RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
42404262
}
42414263

42424264
/* Collect all attributes used in expressions, too */
4243-
pull_varattnos((Node *) indexInfo->ii_Expressions, 1, &indexattrs);
4265+
pull_varattnos(indexExpressions, 1, &indexattrs);
42444266

42454267
/* Collect all attributes in the index predicate, too */
4246-
pull_varattnos((Node *) indexInfo->ii_Predicate, 1, &indexattrs);
4268+
pull_varattnos(indexPredicate, 1, &indexattrs);
42474269

42484270
index_close(indexDesc, AccessShareLock);
42494271
}

0 commit comments

Comments
 (0)