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

Commit768a401

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 parent9668bf5 commit768a401

File tree

7 files changed

+138
-2
lines changed

7 files changed

+138
-2
lines changed

‎src/backend/catalog/heap.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3065,8 +3065,15 @@ RelationTruncateIndexes(Relation heapRelation)
30653065
/* Open the index relation; use exclusive lock, just to be sure */
30663066
currentIndex=index_open(indexId,AccessExclusiveLock);
30673067

3068-
/* Fetch info needed for index_build */
3069-
indexInfo=BuildIndexInfo(currentIndex);
3068+
/*
3069+
* Fetch info needed for index_build. Since we know there are no
3070+
* tuples that actually need indexing, we can use a dummy IndexInfo.
3071+
* This is slightly cheaper to build, but the real point is to avoid
3072+
* possibly running user-defined code in index expressions or
3073+
* predicates. We might be getting invoked during ON COMMIT
3074+
* processing, and we don't want to run any such code then.
3075+
*/
3076+
indexInfo=BuildDummyIndexInfo(currentIndex);
30703077

30713078
/*
30723079
* Now truncate the actual file (and discard buffers).

‎src/backend/catalog/index.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,6 +1838,75 @@ BuildIndexInfo(Relation index)
18381838
returnii;
18391839
}
18401840

1841+
/* ----------------
1842+
*BuildDummyIndexInfo
1843+
*Construct a dummy IndexInfo record for an open index
1844+
*
1845+
* This differs from the real BuildIndexInfo in that it will never run any
1846+
* user-defined code that might exist in index expressions or predicates.
1847+
* Instead of the real index expressions, we return null constants that have
1848+
* the right types/typmods/collations. Predicates and exclusion clauses are
1849+
* just ignored. This is sufficient for the purpose of truncating an index,
1850+
* since we will not need to actually evaluate the expressions or predicates;
1851+
* the only thing that's likely to be done with the data is construction of
1852+
* a tupdesc describing the index's rowtype.
1853+
* ----------------
1854+
*/
1855+
IndexInfo*
1856+
BuildDummyIndexInfo(Relationindex)
1857+
{
1858+
IndexInfo*ii=makeNode(IndexInfo);
1859+
Form_pg_indexindexStruct=index->rd_index;
1860+
inti;
1861+
intnumAtts;
1862+
1863+
/* check the number of keys, and copy attr numbers into the IndexInfo */
1864+
numAtts=indexStruct->indnatts;
1865+
if (numAtts<1||numAtts>INDEX_MAX_KEYS)
1866+
elog(ERROR,"invalid indnatts %d for index %u",
1867+
numAtts,RelationGetRelid(index));
1868+
ii->ii_NumIndexAttrs=numAtts;
1869+
ii->ii_NumIndexKeyAttrs=indexStruct->indnkeyatts;
1870+
Assert(ii->ii_NumIndexKeyAttrs!=0);
1871+
Assert(ii->ii_NumIndexKeyAttrs <=ii->ii_NumIndexAttrs);
1872+
1873+
for (i=0;i<numAtts;i++)
1874+
ii->ii_IndexAttrNumbers[i]=indexStruct->indkey.values[i];
1875+
1876+
/* fetch dummy expressions for expressional indexes */
1877+
ii->ii_Expressions=RelationGetDummyIndexExpressions(index);
1878+
ii->ii_ExpressionsState=NIL;
1879+
1880+
/* pretend there is no predicate */
1881+
ii->ii_Predicate=NIL;
1882+
ii->ii_PredicateState=NULL;
1883+
1884+
/* We ignore the exclusion constraint if any */
1885+
ii->ii_ExclusionOps=NULL;
1886+
ii->ii_ExclusionProcs=NULL;
1887+
ii->ii_ExclusionStrats=NULL;
1888+
1889+
/* other info */
1890+
ii->ii_Unique=indexStruct->indisunique;
1891+
ii->ii_ReadyForInserts=IndexIsReady(indexStruct);
1892+
/* assume not doing speculative insertion for now */
1893+
ii->ii_UniqueOps=NULL;
1894+
ii->ii_UniqueProcs=NULL;
1895+
ii->ii_UniqueStrats=NULL;
1896+
1897+
/* initialize index-build state to default */
1898+
ii->ii_Concurrent= false;
1899+
ii->ii_BrokenHotChain= false;
1900+
ii->ii_ParallelWorkers=0;
1901+
1902+
/* set up for possible use by index AM */
1903+
ii->ii_Am=index->rd_rel->relam;
1904+
ii->ii_AmCache=NULL;
1905+
ii->ii_Context=CurrentMemoryContext;
1906+
1907+
returnii;
1908+
}
1909+
18411910
/*
18421911
* CompareIndexInfo
18431912
*Return whether the properties of two indexes (in different tables)

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
#include"commands/policy.h"
6868
#include"commands/trigger.h"
6969
#include"miscadmin.h"
70+
#include"nodes/makefuncs.h"
7071
#include"nodes/nodeFuncs.h"
7172
#include"optimizer/clauses.h"
7273
#include"optimizer/cost.h"
@@ -4647,6 +4648,57 @@ RelationGetIndexExpressions(Relation relation)
46474648
returnresult;
46484649
}
46494650

4651+
/*
4652+
* RelationGetDummyIndexExpressions -- get dummy expressions for an index
4653+
*
4654+
* Return a list of dummy expressions (just Const nodes) with the same
4655+
* types/typmods/collations as the index's real expressions. This is
4656+
* useful in situations where we don't want to run any user-defined code.
4657+
*/
4658+
List*
4659+
RelationGetDummyIndexExpressions(Relationrelation)
4660+
{
4661+
List*result;
4662+
DatumexprsDatum;
4663+
boolisnull;
4664+
char*exprsString;
4665+
List*rawExprs;
4666+
ListCell*lc;
4667+
4668+
/* Quick exit if there is nothing to do. */
4669+
if (relation->rd_indextuple==NULL||
4670+
heap_attisnull(relation->rd_indextuple,Anum_pg_index_indexprs,NULL))
4671+
returnNIL;
4672+
4673+
/* Extract raw node tree(s) from index tuple. */
4674+
exprsDatum=heap_getattr(relation->rd_indextuple,
4675+
Anum_pg_index_indexprs,
4676+
GetPgIndexDescriptor(),
4677+
&isnull);
4678+
Assert(!isnull);
4679+
exprsString=TextDatumGetCString(exprsDatum);
4680+
rawExprs= (List*)stringToNode(exprsString);
4681+
pfree(exprsString);
4682+
4683+
/* Construct null Consts; the typlen and typbyval are arbitrary. */
4684+
result=NIL;
4685+
foreach(lc,rawExprs)
4686+
{
4687+
Node*rawExpr= (Node*)lfirst(lc);
4688+
4689+
result=lappend(result,
4690+
makeConst(exprType(rawExpr),
4691+
exprTypmod(rawExpr),
4692+
exprCollation(rawExpr),
4693+
1,
4694+
(Datum)0,
4695+
true,
4696+
true));
4697+
}
4698+
4699+
returnresult;
4700+
}
4701+
46504702
/*
46514703
* RelationGetIndexPredicate -- get the index predicate for an index
46524704
*

‎src/include/catalog/index.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ extern void index_drop(Oid indexId, bool concurrent);
9191

9292
externIndexInfo*BuildIndexInfo(Relationindex);
9393

94+
externIndexInfo*BuildDummyIndexInfo(Relationindex);
95+
9496
externboolCompareIndexInfo(IndexInfo*info1,IndexInfo*info2,
9597
Oid*collations1,Oid*collations2,
9698
Oid*opfamilies1,Oid*opfamilies2,

‎src/include/utils/relcache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ extern OidRelationGetOidIndex(Relation relation);
4949
externOidRelationGetPrimaryKeyIndex(Relationrelation);
5050
externOidRelationGetReplicaIndex(Relationrelation);
5151
externList*RelationGetIndexExpressions(Relationrelation);
52+
externList*RelationGetDummyIndexExpressions(Relationrelation);
5253
externList*RelationGetIndexPredicate(Relationrelation);
5354

5455
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