Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc35b714

Browse files
committed
Fix misbehavior with expression indexes on ON COMMIT DELETE ROWS tables.
We implement ON COMMIT DELETE ROWS by truncating tables marked thatway, which requires also truncating/rebuilding their indexes. ButRelationTruncateIndexes asks the relcache for up-to-date copies of anyindex expressions, which may cause execution of eval_const_expressionson them, which can result in actual execution of subexpressions.This is a bad thing to have happening during ON COMMIT. Manuel Riggerreported that use of a SQL function resulted in crashes due toexpectations that ActiveSnapshot would be set, which it isn't.The most obvious fix perhaps would be to push a snapshot duringPreCommit_on_commit_actions, but I think that would just open the doorto more problems: CommitTransaction explicitly expects that nouser-defined code can be running at this point.Fortunately, since we know that no tuples exist to be indexed, thereseems no need to use the real index expressions or predicates duringRelationTruncateIndexes. We can set up dummy index expressionsinstead (we do need something that will expose the right data type,as there are places that build index tupdescs based on this), andjust ignore predicates and exclusion constraints.In a green field it'd likely be better to reimplement ON COMMIT DELETEROWS using the same "init fork" infrastructure used for unloggedrelations. 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 parent4dc6355 commitc35b714

File tree

7 files changed

+118
-2
lines changed

7 files changed

+118
-2
lines changed

‎src/backend/catalog/heap.c

Lines changed: 9 additions & 2 deletions
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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,6 +2324,56 @@ BuildIndexInfo(Relation index)
23242324
returnii;
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(Relationindex)
2343+
{
2344+
IndexInfo*ii;
2345+
Form_pg_indexindexStruct=index->rd_index;
2346+
inti;
2347+
intnumAtts;
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+
returnii;
2375+
}
2376+
23272377
/*
23282378
* CompareIndexInfo
23292379
*Return whether the properties of two indexes (in different tables)

‎src/backend/utils/cache/relcache.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4629,6 +4629,57 @@ RelationGetIndexExpressions(Relation relation)
46294629
returnresult;
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(Relationrelation)
4641+
{
4642+
List*result;
4643+
DatumexprsDatum;
4644+
boolisnull;
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+
returnNIL;
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+
returnresult;
4681+
}
4682+
46324683
/*
46334684
* RelationGetIndexPredicate -- get the index predicate for an index
46344685
*

‎src/include/catalog/index.h

Lines changed: 2 additions & 0 deletions
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
externIndexInfo*BuildIndexInfo(Relationindex);
108108

109+
externIndexInfo*BuildDummyIndexInfo(Relationindex);
110+
109111
externboolCompareIndexInfo(IndexInfo*info1,IndexInfo*info2,
110112
Oid*collations1,Oid*collations2,
111113
Oid*opfamilies1,Oid*opfamilies2,

‎src/include/utils/relcache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ extern List *RelationGetStatExtList(Relation relation);
4848
externOidRelationGetPrimaryKeyIndex(Relationrelation);
4949
externOidRelationGetReplicaIndex(Relationrelation);
5050
externList*RelationGetIndexExpressions(Relationrelation);
51+
externList*RelationGetDummyIndexExpressions(Relationrelation);
5152
externList*RelationGetIndexPredicate(Relationrelation);
5253

5354
typedefenumIndexAttrBitmapKind

‎src/test/regress/expected/temp.out

Lines changed: 2 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ SELECT * FROM temptest;
5555

5656
CREATE TEMP TABLE temptest(colint)ONCOMMITDELETE ROWS;
5757

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp