Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Fix a crash in logical replication
authorPeter Eisentraut <peter@eisentraut.org>
Mon, 28 Jan 2019 21:09:33 +0000 (22:09 +0100)
committerPeter Eisentraut <peter@eisentraut.org>
Wed, 30 Jan 2019 10:17:05 +0000 (11:17 +0100)
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/

src/backend/utils/cache/relcache.c

index 4b78b5979f5aaf5b61e4c2a9aef06158b294703d..35f66f42376c3427d105d9d4a32bce922765bc6f 100644 (file)
@@ -37,7 +37,6 @@
 #include "access/transam.h"
 #include "access/xact.h"
 #include "catalog/catalog.h"
-#include "catalog/index.h"
 #include "catalog/indexing.h"
 #include "catalog/namespace.h"
 #include "catalog/pg_amproc.h"
@@ -4201,28 +4200,51 @@ restart:
    {
        Oid         indexOid = lfirst_oid(l);
        Relation    indexDesc;
-       IndexInfo  *indexInfo;
+       Datum       datum;
+       bool        isnull;
+       Node       *indexExpressions;
+       Node       *indexPredicate;
        int         i;
        bool        isKey;      /* candidate key */
        bool        isIDKey;    /* replica identity index */
 
        indexDesc = index_open(indexOid, AccessShareLock);
 
-       /* Extract index key information from the index's pg_index row */
-       indexInfo = BuildIndexInfo(indexDesc);
+       /*
+        * Extract index expressions and index predicate.  Note: Don't use
+        * RelationGetIndexExpressions()/RelationGetIndexPredicate(), because
+        * those might run constant expressions evaluation, which needs a
+        * snapshot, which we might not have here.  (Also, it's probably more
+        * sound to collect the bitmaps before any transformations that might
+        * eliminate columns, but the practical impact of this is limited.)
+        */
+
+       datum = heap_getattr(indexDesc->rd_indextuple, Anum_pg_index_indexprs,
+                            GetPgIndexDescriptor(), &isnull);
+       if (!isnull)
+           indexExpressions = stringToNode(TextDatumGetCString(datum));
+       else
+           indexExpressions = NULL;
+
+       datum = heap_getattr(indexDesc->rd_indextuple, Anum_pg_index_indpred,
+                            GetPgIndexDescriptor(), &isnull);
+       if (!isnull)
+           indexPredicate = stringToNode(TextDatumGetCString(datum));
+       else
+           indexPredicate = NULL;
 
        /* Can this index be referenced by a foreign key? */
-       isKey = indexInfo->ii_Unique &&
-           indexInfo->ii_Expressions == NIL &&
-           indexInfo->ii_Predicate == NIL;
+       isKey = indexDesc->rd_index->indisunique &&
+           indexExpressions == NULL &&
+           indexPredicate == NULL;
 
        /* Is this index the configured (or default) replica identity? */
        isIDKey = (indexOid == relreplindex);
 
        /* Collect simple attribute references */
-       for (i = 0; i < indexInfo->ii_NumIndexAttrs; i++)
+       for (i = 0; i < indexDesc->rd_index->indnatts; i++)
        {
-           int         attrnum = indexInfo->ii_KeyAttrNumbers[i];
+           int         attrnum = indexDesc->rd_index->indkey.values[i];
 
            if (attrnum != 0)
            {
@@ -4240,10 +4262,10 @@ restart:
        }
 
        /* Collect all attributes used in expressions, too */
-       pull_varattnos((Node *) indexInfo->ii_Expressions, 1, &indexattrs);
+       pull_varattnos(indexExpressions, 1, &indexattrs);
 
        /* Collect all attributes in the index predicate, too */
-       pull_varattnos((Node *) indexInfo->ii_Predicate, 1, &indexattrs);
+       pull_varattnos(indexPredicate, 1, &indexattrs);
 
        index_close(indexDesc, AccessShareLock);
    }