8000 Don't trust deferred-unique indexes for join removal. · jaylevitt/postgres@18661c6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 18661c6

Browse files
committed
Don't trust deferred-unique indexes for join removal.
The uniqueness condition might fail to hold intra-transaction, and assuming it does can give incorrect query results. Per report from Marti Raudsepp, though this is not his proposed patch. Back-patch to 9.0, where both these features were introduced. In the released branches, add the new IndexOptInfo field to the end of the struct, to try to minimize ABI breakage for third-party code that may be examining that struct.
1 parent 8e8ac08 commit 18661c6

File tree

5 files changed

+17
-4
lines changed

5 files changed

+17
-4
lines changed

src/backend/nodes/outfuncs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,6 +1764,7 @@ _outIndexOptInfo(StringInfo str, IndexOptInfo *node)
17641764
WRITE_NODE_FIELD(indpred);
17651765
WRITE_BOOL_FIELD(predOK);
17661766
WRITE_BOOL_FIELD(unique);
1767+
WRITE_BOOL_FIELD(immediate);
17671768
WRITE_BOOL_FIELD(hypothetical);
17681769
}
17691770

src/backend/optimizer/path/indxpath.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,10 +2179,11 @@ relation_has_unique_index_for(PlannerInfo *root, RelOptInfo *rel,
21792179
int c;
21802180

21812181
/*
2182-
* If the index is not unique or if it's a partial index that doesn't
2183-
* match the query, it's useless here.
2182+
* If the index is not unique, or not immediately enforced, or if it's
2183+
* a partial index that doesn't match the query, it's useless here.
21842184
*/
2185-
if (!ind->unique || (ind->indpred != NIL && !ind->predOK))
2185+
if (!ind->unique || !ind->immediate ||
2186+
(ind->indpred != NIL && !ind->predOK))
21862187
continue;
21872188

21882189
/*

src/backend/optimizer/util/plancat.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
316316
ChangeVarNodes((Node *) info->indpred, 1, varno, 0);
317317
info->predOK = false; /* set later in indxpath.c */
318318
info->unique = index->indisunique;
319+
info->immediate = index->indimmediate;
319320
info->hypothetical = false;
320321

321322
/*
@@ -973,6 +974,11 @@ join_selectivity(PlannerInfo *root,
973974
* Detect whether there is a unique index on the specified attribute
974975
* of the specified relation, thus allowing us to conclude that all
975976
* the (non-null) values of the attribute are distinct.
977+
*
978+
* This function does not check the index's indimmediate property, which
979+
* means that uniqueness may transiently fail to hold intra-transaction.
980+
* That's appropriate when we are making statistical estimates, but beware
981+
* of using this for any correctness proofs.
976982
*/
977983
bool
978984
has_unique_index(RelOptInfo *rel, AttrNumber attno)

src/backend/utils/adt/selfuncs.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4121,7 +4121,9 @@ get_join_variables(PlannerInfo *root, List *args, SpecialJoinInfo *sjinfo,
41214121
* commonly the same as the exposed type of the variable argument,
41224122
* but can be different in binary-compatible-type cases.
41234123
* isunique: TRUE if we were able to match the var to a unique index,
4124-
* implying its values are unique for this query.
4124+
* implying its values are unique for this query. (Caution: this
4125+
* should be trusted for statistical purposes only, since we do not
4126+
* check indimmediate.)
41254127
*
41264128
* Caller is responsible for doing ReleaseVariableStats() before exiting.
41274129
*/

src/include/nodes/relation.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,9 @@ typedef struct IndexOptInfo
491491
bool amsearchnulls; /* can AM search for NULL/NOT NULL entries? */
492492
bool amhasgettuple; /* does AM have amgettuple interface? */
493493
bool amhasgetbitmap; /* does AM have amgetbitmap interface? */
494+
495+
/* Added at end of struct to avoid ABI breakage in released branches */
496+
bool immediate; /* is uniqueness enforced immediately? */
494497
} IndexOptInfo;
495498

496499

0 commit comments

Comments
 (0)
0