Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c35b714

Browse files
committed
Fix misbehavior with expression indexes on ON COMMIT DELETE ROWS tables.
We implement ON COMMIT DELETE ROWS by truncating tables marked that way, which requires also truncating/rebuilding their indexes. But RelationTruncateIndexes asks the relcache for up-to-date copies of any index expressions, which may cause execution of eval_const_expressions on them, which can result in actual execution of subexpressions. This is a bad thing to have happening during ON COMMIT. Manuel Rigger reported that use of a SQL function resulted in crashes due to expectations that ActiveSnapshot would be set, which it isn't. The most obvious fix perhaps would be to push a snapshot during PreCommit_on_commit_actions, but I think that would just open the door to more problems: CommitTransaction explicitly expects that no user-defined code can be running at this point. Fortunately, since we know that no tuples exist to be indexed, there seems no need to use the real index expressions or predicates during RelationTruncateIndexes. We can set up dummy index expressions instead (we do need something that will expose the right data type, as there are places that build index tupdescs based on this), and just ignore predicates and exclusion constraints. In a green field it'd likely be better to reimplement ON COMMIT DELETE ROWS using the same "init fork" infrastructure used for unlogged relations. That seems impractical without catalog changes though, and even without that it'd be too big a change to back-patch. So for now do it like this. Per private report from Manuel Rigger. This has been broken forever, so back-patch to all supported branches.
1 parent 4dc6355 commit c35b714

File tree

7 files changed

+118
-2
lines changed

7 files changed

+118
-2
lines changed

src/backend/catalog/heap.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -3148,8 +3148,15 @@ RelationTruncateIndexes(Relation heapRelation)
31483148
/* Open the index relation; use exclusive lock, just to be sure */
31493149
currentIndex = index_open(indexId, AccessExclusiveLock);
31503150

3151-
/* Fetch info needed for index_build */
3152-
indexInfo = BuildIndexInfo(currentIndex);
3151+
/*
3152+
* Fetch info needed for index_build. Since we know there are no
3153+
* tuples that actually need indexing, we can use a dummy IndexInfo.
3154+
* This is slightly cheaper to build, but the real point is to avoid
3155+
* possibly running user-defined code in index expressions or
3156+
* predicates. We might be getting invoked during ON COMMIT
3157+
* processing, and we don't want to run any such code then.
3158+
*/
3159+
indexInfo = BuildDummyIndexInfo(currentIndex);
31533160

31543161
/*
31553162
* Now truncate the actual file (and discard buffers).

src/backend/catalog/index.c

+50
Original file line numberDiff line numberDiff line change
@@ -2324,6 +2324,56 @@ BuildIndexInfo(Relation index)
23242324
return ii;
23252325
}
23262326

2327+
/* ----------------
2328+
* BuildDummyIndexInfo
2329+
* Construct a dummy IndexInfo record for an open index
2330+
*
2331+
* This differs from the real BuildIndexInfo in that it will never run any
2332+
* user-defined code that might exist in index expressions or predicates.
2333+
* Instead of the real index expressions, we return null constants that have
2334+
* the right types/typmods/collations. Predicates and exclusion clauses are
2335+
* just ignored. This is sufficient for the purpose of truncating an index,
2336+
* since we will not need to actually evaluate the expressions or predicates;
2337+
* the only thing that's likely to be done with the data is construction of
2338+
* a tupdesc describing the index's rowtype.
2339+
* ----------------
2340+
*/
2341+
IndexInfo *
2342+
BuildDummyIndexInfo(Relation index)
2343+
{
2344+
IndexInfo *ii;
2345+
Form_pg_index indexStruct = index->rd_index;
2346+
int i;
2347+
int numAtts;
2348+
2349+
/* check the number of keys, and copy attr numbers into the IndexInfo */
2350+
numAtts = indexStruct->indnatts;
2351+
if (numAtts < 1 || numAtts > INDEX_MAX_KEYS)
2352+
elog(ERROR, "invalid indnatts %d for index %u",
2353+
numAtts, RelationGetRelid(index));
2354+
2355+
/*
2356+
* Create the node, using dummy index expressions, and pretending there is
2357+
* no predicate.
2358+
*/
2359+
ii = makeIndexInfo(indexStruct->indnatts,
2360+
indexStruct->indnkeyatts,
2361+
index->rd_rel->relam,
2362+
RelationGetDummyIndexExpressions(index),
2363+
NIL,
2364+
indexStruct->indisunique,
2365+
indexStruct->indisready,
2366+
false);
2367+
2368+
/* fill in attribute numbers */
2369+
for (i = 0; i < numAtts; i++)
2370+
ii->ii_IndexAttrNumbers[i] = indexStruct->indkey.values[i];
2371+
2372+
/* We ignore the exclusion constraint if any */
2373+
2374+
return ii;
2375+
}
2376+
23272377
/*
23282378
* CompareIndexInfo
23292379
* Return whether the properties of two indexes (in different tables)

src/backend/utils/cache/relcache.c

+51
Original file line numberDiff line numberDiff line change
@@ -4629,6 +4629,57 @@ RelationGetIndexExpressions(Relation relation)
46294629
return result;
46304630
}
46314631

4632+
/*
4633+
* RelationGetDummyIndexExpressions -- get dummy expressions for an index
4634+
*
4635+
* Return a list of dummy expressions (just Const nodes) with the same
4636+
* types/typmods/collations as the index's real expressions. This is
4637+
* useful in situations where we don't want to run any user-defined code.
4638+
*/
4639+
List *
4640+
RelationGetDummyIndexExpressions(Relation relation)
4641+
{
4642+
List *result;
4643+
Datum exprsDatum;
4644+
bool isnull;
4645+
char *exprsString;
4646+
List *rawExprs;
4647+
ListCell *lc;
4648+
4649+
/* Quick exit if there is nothing to do. */
4650+
if (relation->rd_indextuple == NULL ||
4651+
heap_attisnull(relation->rd_indextuple, Anum_pg_index_indexprs, NULL))
4652+
return NIL;
4653+
4654+
/* Extract raw node tree(s) from index tuple. */
4655+
exprsDatum = heap_getattr(relation->rd_indextuple,
4656+
Anum_pg_index_indexprs,
4657+
GetPgIndexDescriptor(),
4658+
&isnull);
4659+
Assert(!isnull);
4660+
exprsString = TextDatumGetCString(exprsDatum);
4661+
rawExprs = (List *) stringToNode(exprsString);
4662+
pfree(exprsString);
4663+
4664+
/* Construct null Consts; the typlen and typbyval are arbitrary. */
4665+
result = NIL;
4666+
foreach(lc, rawExprs)
4667+
{
4668+
Node *rawExpr = (Node *) lfirst(lc);
4669+
4670+
result = lappend(result,
4671+
makeConst(exprType(rawExpr),
4672+
exprTypmod(rawExpr),
4673+
exprCollation(rawExpr),
4674+
1,
4675+
(Datum) 0,
4676+
true,
4677+
true));
4678+
}
4679+
4680+
return result;
4681+
}
4682+
46324683
/*
46334684
* RelationGetIndexPredicate -- get the index predicate for an index
46344685
*

src/include/catalog/index.h

+2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ extern void index_drop(Oid indexId, bool concurrent, bool concurrent_lock_mode);
106106

107107
extern IndexInfo *BuildIndexInfo(Relation index);
108108

109+
extern IndexInfo *BuildDummyIndexInfo(Relation index);
110+
109111
extern bool CompareIndexInfo(IndexInfo *info1, IndexInfo *info2,
110112
Oid *collations1, Oid *collations2,
111113
Oid *opfamilies1, Oid *opfamilies2,

src/include/utils/relcache.h

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ extern List *RelationGetStatExtList(Relation relation);
4848
extern Oid RelationGetPrimaryKeyIndex(Relation relation);
4949
extern Oid RelationGetReplicaIndex(Relation relation);
5050
extern List *RelationGetIndexExpressions(Relation relation);
51+
extern List *RelationGetDummyIndexExpressions(Relation relation);
5152
extern List *RelationGetIndexPredicate(Relation relation);
5253

5354
typedef enum IndexAttrBitmapKind

src/test/regress/expected/temp.out

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ LINE 1: SELECT * FROM temptest;
4949
^
5050
-- Test ON COMMIT DELETE ROWS
5151
CREATE TEMP TABLE temptest(col int) ON COMMIT DELETE ROWS;
52+
-- while we're here, verify successful truncation of index with SQL function
53+
CREATE INDEX ON temptest(bit_length(''));
5254
BEGIN;
5355
INSERT INTO temptest VALUES (1);
5456
INSERT INTO temptest VALUES (2);

src/test/regress/sql/temp.sql

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ SELECT * FROM temptest;
5555

5656
CREATE TEMP TABLE temptest(col int) ON COMMIT DELETE ROWS;
5757

58+
-- while we're here, verify successful truncation of index with SQL function
59+
CREATE INDEX ON temptest(bit_length(''));
60+
5861
BEGIN;
5962
INSERT INTO temptest VALUES (1);
6063
INSERT INTO temptest VALUES (2);

0 commit comments

Comments
 (0)