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

Commit25c7183

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 parentf71b22f commit25c7183

File tree

7 files changed

+132
-2
lines changed

7 files changed

+132
-2
lines changed

‎src/backend/catalog/heap.c

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

2798-
/* Fetch info needed for index_build */
2799-
indexInfo=BuildIndexInfo(currentIndex);
2798+
/*
2799+
* Fetch info needed for index_build. Since we know there are no
2800+
* tuples that actually need indexing, we can use a dummy IndexInfo.
2801+
* This is slightly cheaper to build, but the real point is to avoid
2802+
* possibly running user-defined code in index expressions or
2803+
* predicates. We might be getting invoked during ON COMMIT
2804+
* processing, and we don't want to run any such code then.
2805+
*/
2806+
indexInfo=BuildDummyIndexInfo(currentIndex);
28002807

28012808
/*
28022809
* Now truncate the actual file (and discard buffers).

‎src/backend/catalog/index.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,6 +1706,69 @@ BuildIndexInfo(Relation index)
17061706
returnii;
17071707
}
17081708

1709+
/* ----------------
1710+
*BuildDummyIndexInfo
1711+
*Construct a dummy IndexInfo record for an open index
1712+
*
1713+
* This differs from the real BuildIndexInfo in that it will never run any
1714+
* user-defined code that might exist in index expressions or predicates.
1715+
* Instead of the real index expressions, we return null constants that have
1716+
* the right types/typmods/collations. Predicates and exclusion clauses are
1717+
* just ignored. This is sufficient for the purpose of truncating an index,
1718+
* since we will not need to actually evaluate the expressions or predicates;
1719+
* the only thing that's likely to be done with the data is construction of
1720+
* a tupdesc describing the index's rowtype.
1721+
* ----------------
1722+
*/
1723+
IndexInfo*
1724+
BuildDummyIndexInfo(Relationindex)
1725+
{
1726+
IndexInfo*ii=makeNode(IndexInfo);
1727+
Form_pg_indexindexStruct=index->rd_index;
1728+
inti;
1729+
intnumKeys;
1730+
1731+
/* check the number of keys, and copy attr numbers into the IndexInfo */
1732+
numKeys=indexStruct->indnatts;
1733+
if (numKeys<1||numKeys>INDEX_MAX_KEYS)
1734+
elog(ERROR,"invalid indnatts %d for index %u",
1735+
numKeys,RelationGetRelid(index));
1736+
ii->ii_NumIndexAttrs=numKeys;
1737+
for (i=0;i<numKeys;i++)
1738+
ii->ii_KeyAttrNumbers[i]=indexStruct->indkey.values[i];
1739+
1740+
/* fetch dummy expressions for expressional indexes */
1741+
ii->ii_Expressions=RelationGetDummyIndexExpressions(index);
1742+
ii->ii_ExpressionsState=NIL;
1743+
1744+
/* pretend there is no predicate */
1745+
ii->ii_Predicate=NIL;
1746+
ii->ii_PredicateState=NULL;
1747+
1748+
/* We ignore the exclusion constraint if any */
1749+
ii->ii_ExclusionOps=NULL;
1750+
ii->ii_ExclusionProcs=NULL;
1751+
ii->ii_ExclusionStrats=NULL;
1752+
1753+
/* other info */
1754+
ii->ii_Unique=indexStruct->indisunique;
1755+
ii->ii_ReadyForInserts=IndexIsReady(indexStruct);
1756+
/* assume not doing speculative insertion for now */
1757+
ii->ii_UniqueOps=NULL;
1758+
ii->ii_UniqueProcs=NULL;
1759+
ii->ii_UniqueStrats=NULL;
1760+
1761+
/* initialize index-build state to default */
1762+
ii->ii_Concurrent= false;
1763+
ii->ii_BrokenHotChain= false;
1764+
1765+
/* set up for possible use by index AM */
1766+
ii->ii_AmCache=NULL;
1767+
ii->ii_Context=CurrentMemoryContext;
1768+
1769+
returnii;
1770+
}
1771+
17091772
/* ----------------
17101773
*BuildSpeculativeIndexInfo
17111774
*Add extra state to IndexInfo record

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include"commands/policy.h"
6666
#include"commands/trigger.h"
6767
#include"miscadmin.h"
68+
#include"nodes/makefuncs.h"
6869
#include"nodes/nodeFuncs.h"
6970
#include"optimizer/clauses.h"
7071
#include"optimizer/prep.h"
@@ -4816,6 +4817,57 @@ RelationGetIndexExpressions(Relation relation)
48164817
returnresult;
48174818
}
48184819

4820+
/*
4821+
* RelationGetDummyIndexExpressions -- get dummy expressions for an index
4822+
*
4823+
* Return a list of dummy expressions (just Const nodes) with the same
4824+
* types/typmods/collations as the index's real expressions. This is
4825+
* useful in situations where we don't want to run any user-defined code.
4826+
*/
4827+
List*
4828+
RelationGetDummyIndexExpressions(Relationrelation)
4829+
{
4830+
List*result;
4831+
DatumexprsDatum;
4832+
boolisnull;
4833+
char*exprsString;
4834+
List*rawExprs;
4835+
ListCell*lc;
4836+
4837+
/* Quick exit if there is nothing to do. */
4838+
if (relation->rd_indextuple==NULL||
4839+
heap_attisnull(relation->rd_indextuple,Anum_pg_index_indexprs))
4840+
returnNIL;
4841+
4842+
/* Extract raw node tree(s) from index tuple. */
4843+
exprsDatum=heap_getattr(relation->rd_indextuple,
4844+
Anum_pg_index_indexprs,
4845+
GetPgIndexDescriptor(),
4846+
&isnull);
4847+
Assert(!isnull);
4848+
exprsString=TextDatumGetCString(exprsDatum);
4849+
rawExprs= (List*)stringToNode(exprsString);
4850+
pfree(exprsString);
4851+
4852+
/* Construct null Consts; the typlen and typbyval are arbitrary. */
4853+
result=NIL;
4854+
foreach(lc,rawExprs)
4855+
{
4856+
Node*rawExpr= (Node*)lfirst(lc);
4857+
4858+
result=lappend(result,
4859+
makeConst(exprType(rawExpr),
4860+
exprTypmod(rawExpr),
4861+
exprCollation(rawExpr),
4862+
1,
4863+
(Datum)0,
4864+
true,
4865+
true));
4866+
}
4867+
4868+
returnresult;
4869+
}
4870+
48194871
/*
48204872
* RelationGetIndexPredicate -- get the index predicate for an index
48214873
*

‎src/include/catalog/index.h

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

8383
externIndexInfo*BuildIndexInfo(Relationindex);
8484

85+
externIndexInfo*BuildDummyIndexInfo(Relationindex);
86+
8587
externvoidBuildSpeculativeIndexInfo(Relationindex,IndexInfo*ii);
8688

8789
externvoidFormIndexDatum(IndexInfo*indexInfo,

‎src/include/utils/relcache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ extern OidRelationGetOidIndex(Relation relation);
4444
externOidRelationGetPrimaryKeyIndex(Relationrelation);
4545
externOidRelationGetReplicaIndex(Relationrelation);
4646
externList*RelationGetIndexExpressions(Relationrelation);
47+
externList*RelationGetDummyIndexExpressions(Relationrelation);
4748
externList*RelationGetIndexPredicate(Relationrelation);
4849

4950
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