aboutsummaryrefslogtreecommitdiff
path: root/src/backend
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2007-04-06 22:33:43 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2007-04-06 22:33:43 +0000
commitf02a82b6adad1af75499c4ac7221bbd94e3c4fbf (patch)
treed15b0a2a52d95e046bb3945f4487982943ed211e /src/backend
parent146c83c045625d6f0072dd96045ebbc54582be05 (diff)
downloadpostgresql-f02a82b6adad1af75499c4ac7221bbd94e3c4fbf.tar.gz
postgresql-f02a82b6adad1af75499c4ac7221bbd94e3c4fbf.zip
Make 'col IS NULL' clauses be indexable conditions.
Teodor Sigaev, with some kibitzing from Tom Lane.
Diffstat (limited to 'src/backend')
-rw-r--r--src/backend/access/common/scankey.c13
-rw-r--r--src/backend/access/gist/gistget.c58
-rw-r--r--src/backend/access/nbtree/nbtsearch.c4
-rw-r--r--src/backend/access/nbtree/nbtutils.c93
-rw-r--r--src/backend/executor/nodeIndexscan.c38
-rw-r--r--src/backend/optimizer/path/indxpath.c26
-rw-r--r--src/backend/optimizer/plan/createplan.c46
-rw-r--r--src/backend/optimizer/util/plancat.c3
-rw-r--r--src/backend/utils/adt/selfuncs.c39
9 files changed, 249 insertions, 71 deletions
diff --git a/src/backend/access/common/scankey.c b/src/backend/access/common/scankey.c
index a93c72e0932..21fccc23a2c 100644
--- a/src/backend/access/common/scankey.c
+++ b/src/backend/access/common/scankey.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/access/common/scankey.c,v 1.29 2007/01/05 22:19:21 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/access/common/scankey.c,v 1.30 2007/04/06 22:33:41 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -20,7 +20,8 @@
/*
* ScanKeyEntryInitialize
* Initializes a scan key entry given all the field values.
- * The target procedure is specified by OID.
+ * The target procedure is specified by OID (but can be invalid
+ * if SK_SEARCHNULL is set).
*
* Note: CurrentMemoryContext at call should be as long-lived as the ScanKey
* itself, because that's what will be used for any subsidiary info attached
@@ -40,7 +41,13 @@ ScanKeyEntryInitialize(ScanKey entry,
entry->sk_strategy = strategy;
entry->sk_subtype = subtype;
entry->sk_argument = argument;
- fmgr_info(procedure, &entry->sk_func);
+ if (RegProcedureIsValid(procedure))
+ fmgr_info(procedure, &entry->sk_func);
+ else
+ {
+ Assert(flags & SK_SEARCHNULL);
+ MemSet(&entry->sk_func, 0, sizeof(entry->sk_func));
+ }
}
/*
diff --git a/src/backend/access/gist/gistget.c b/src/backend/access/gist/gistget.c
index f1d2c777c2f..226812322aa 100644
--- a/src/backend/access/gist/gistget.c
+++ b/src/backend/access/gist/gistget.c
@@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/access/gist/gistget.c,v 1.64 2007/01/20 18:43:35 neilc Exp $
+ * $PostgreSQL: pgsql/src/backend/access/gist/gistget.c,v 1.65 2007/04/06 22:33:41 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -381,37 +381,45 @@ gistindex_keytest(IndexTuple tuple,
if (key->sk_flags & SK_ISNULL)
{
/*
- * is the compared-to datum NULL? on non-leaf page it's possible
- * to have nulls in childs :(
+ * On non-leaf page we can't conclude that child hasn't NULL
+ * values because of assumption in GiST: uinon (VAL, NULL) is VAL
+ * But if on non-leaf page key IS NULL then all childs
+ * has NULL.
*/
- if (isNull || !GistPageIsLeaf(p))
- return true;
- return false;
+ Assert( key->sk_flags & SK_SEARCHNULL );
+
+ if ( GistPageIsLeaf(p) && !isNull )
+ return false;
}
else if (isNull)
+ {
return false;
+ }
+ else
+ {
- gistdentryinit(giststate, key->sk_attno - 1, &de,
- datum, r, p, offset,
- FALSE, isNull);
+ gistdentryinit(giststate, key->sk_attno - 1, &de,
+ datum, r, p, offset,
+ FALSE, isNull);
- /*
- * Call the Consistent function to evaluate the test. The arguments
- * are the index datum (as a GISTENTRY*), the comparison datum, and
- * the comparison operator's strategy number and subtype from pg_amop.
- *
- * (Presently there's no need to pass the subtype since it'll always
- * be zero, but might as well pass it for possible future use.)
- */
- test = FunctionCall4(&key->sk_func,
- PointerGetDatum(&de),
- key->sk_argument,
- Int32GetDatum(key->sk_strategy),
- ObjectIdGetDatum(key->sk_subtype));
-
- if (!DatumGetBool(test))
- return false;
+ /*
+ * Call the Consistent function to evaluate the test. The arguments
+ * are the index datum (as a GISTENTRY*), the comparison datum, and
+ * the comparison operator's strategy number and subtype from pg_amop.
+ *
+ * (Presently there's no need to pass the subtype since it'll always
+ * be zero, but might as well pass it for possible future use.)
+ */
+ test = FunctionCall4(&key->sk_func,
+ PointerGetDatum(&de),
+ key->sk_argument,
+ Int32GetDatum(key->sk_strategy),
+ ObjectIdGetDatum(key->sk_subtype));
+
+ if (!DatumGetBool(test))
+ return false;
+ }
keySize--;
key++;
diff --git a/src/backend/access/nbtree/nbtsearch.c b/src/backend/access/nbtree/nbtsearch.c
index fc8b18a2e90..036a97a8d04 100644
--- a/src/backend/access/nbtree/nbtsearch.c
+++ b/src/backend/access/nbtree/nbtsearch.c
@@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.111 2007/01/09 02:14:10 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.112 2007/04/06 22:33:42 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -746,8 +746,6 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
*
* If goback = true, we will then step back one item, while if
* goback = false, we will start the scan on the located item.
- *
- * it's yet other place to add some code later for is(not)null ...
*----------
*/
switch (strat_total)
diff --git a/src/backend/access/nbtree/nbtutils.c b/src/backend/access/nbtree/nbtutils.c
index 9c227d7f6c6..b5e7686303e 100644
--- a/src/backend/access/nbtree/nbtutils.c
+++ b/src/backend/access/nbtree/nbtutils.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/access/nbtree/nbtutils.c,v 1.83 2007/03/30 00:12:59 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/access/nbtree/nbtutils.c,v 1.84 2007/04/06 22:33:42 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -264,12 +264,27 @@ _bt_preprocess_keys(IndexScanDesc scan)
if (numberOfKeys == 1)
{
/*
- * We don't use indices for 'A is null' and 'A is not null' currently
- * and 'A < = > <> NULL' will always fail - so qual is not OK if
- * comparison value is NULL. - vadim 03/21/97
+ * We treat all btree operators as strict (even if they're not so
+ * marked in pg_proc). This means that it is impossible for an
+ * operator condition with a NULL comparison constant to succeed,
+ * and we can reject it right away.
+ *
+ * However, we now also support "x IS NULL" clauses as search
+ * conditions, so in that case keep going. The planner has not
+ * filled in any particular strategy in this case, so set it to
+ * BTEqualStrategyNumber --- we can treat IS NULL as an equality
+ * operator for purposes of search strategy.
*/
if (cur->sk_flags & SK_ISNULL)
- so->qual_ok = false;
+ {
+ if (cur->sk_flags & SK_SEARCHNULL)
+ {
+ cur->sk_strategy = BTEqualStrategyNumber;
+ cur->sk_subtype = InvalidOid;
+ }
+ else
+ so->qual_ok = false;
+ }
_bt_mark_scankey_with_indoption(cur, indoption);
memcpy(outkeys, cur, sizeof(ScanKeyData));
so->numberOfKeys = 1;
@@ -303,17 +318,20 @@ _bt_preprocess_keys(IndexScanDesc scan)
{
if (i < numberOfKeys)
{
- /* See comments above: any NULL implies cannot match qual */
+ /* See comments above about NULLs and IS NULL handling. */
/* Note: we assume SK_ISNULL is never set in a row header key */
if (cur->sk_flags & SK_ISNULL)
{
- so->qual_ok = false;
-
- /*
- * Quit processing so we don't try to invoke comparison
- * routines on NULLs.
- */
- return;
+ if (cur->sk_flags & SK_SEARCHNULL)
+ {
+ cur->sk_strategy = BTEqualStrategyNumber;
+ cur->sk_subtype = InvalidOid;
+ }
+ else
+ {
+ so->qual_ok = false;
+ return;
+ }
}
}
@@ -344,6 +362,14 @@ _bt_preprocess_keys(IndexScanDesc scan)
if (!chk || j == (BTEqualStrategyNumber - 1))
continue;
+
+ /* IS NULL together with any other predicate must fail */
+ if (eq->sk_flags & SK_SEARCHNULL)
+ {
+ so->qual_ok = false;
+ return;
+ }
+
if (_bt_compare_scankey_args(scan, chk, eq, chk,
&test_result))
{
@@ -455,6 +481,23 @@ _bt_preprocess_keys(IndexScanDesc scan)
else
{
/* yup, keep only the more restrictive key */
+
+ /* if either arg is NULL, don't try to compare */
+ if ((cur->sk_flags | xform[j]->sk_flags) & SK_ISNULL)
+ {
+ /* at least one of them must be an IS NULL clause */
+ Assert(j == (BTEqualStrategyNumber - 1));
+ Assert((cur->sk_flags | xform[j]->sk_flags) & SK_SEARCHNULL);
+ /* if one is and one isn't, the search must fail */
+ if ((cur->sk_flags ^ xform[j]->sk_flags) & SK_SEARCHNULL)
+ {
+ so->qual_ok = false;
+ return;
+ }
+ /* we have duplicate IS NULL clauses, ignore the newer one */
+ continue;
+ }
+
if (_bt_compare_scankey_args(scan, cur, cur, xform[j],
&test_result))
{
@@ -798,11 +841,29 @@ _bt_checkkeys(IndexScanDesc scan,
tupdesc,
&isNull);
- /* btree doesn't support 'A is null' clauses, yet */
if (key->sk_flags & SK_ISNULL)
{
- /* we shouldn't get here, really; see _bt_preprocess_keys() */
- *continuescan = false;
+ /* Handle IS NULL tests */
+ Assert(key->sk_flags & SK_SEARCHNULL);
+
+ if (isNull)
+ continue; /* tuple satisfies this qual */
+
+ /*
+ * Tuple fails this qual. If it's a required qual for the current
+ * scan direction, then we can conclude no further tuples will
+ * pass, either.
+ */
+ if ((key->sk_flags & SK_BT_REQFWD) &&
+ ScanDirectionIsForward(dir))
+ *continuescan = false;
+ else if ((key->sk_flags & SK_BT_REQBKWD) &&
+ ScanDirectionIsBackward(dir))
+ *continuescan = false;
+
+ /*
+ * In any case, this indextuple doesn't match the qual.
+ */
return false;
}
diff --git a/src/backend/executor/nodeIndexscan.c b/src/backend/executor/nodeIndexscan.c
index 5f0a66f92bb..d9136de0035 100644
--- a/src/backend/executor/nodeIndexscan.c
+++ b/src/backend/executor/nodeIndexscan.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/executor/nodeIndexscan.c,v 1.120 2007/01/05 22:19:28 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/executor/nodeIndexscan.c,v 1.121 2007/04/06 22:33:42 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -599,7 +599,7 @@ ExecInitIndexScan(IndexScan *node, EState *estate, int eflags)
* The index quals are passed to the index AM in the form of a ScanKey array.
* This routine sets up the ScanKeys, fills in all constant fields of the
* ScanKeys, and prepares information about the keys that have non-constant
- * comparison values. We divide index qual expressions into four types:
+ * comparison values. We divide index qual expressions into five types:
*
* 1. Simple operator with constant comparison value ("indexkey op constant").
* For these, we just fill in a ScanKey containing the constant value.
@@ -620,6 +620,8 @@ ExecInitIndexScan(IndexScan *node, EState *estate, int eflags)
* (Note that we treat all array-expressions as requiring runtime evaluation,
* even if they happen to be constants.)
*
+ * 5. NullTest ("indexkey IS NULL"). We just fill in the ScanKey properly.
+ *
* Input params are:
*
* planstate: executor state node we are working for
@@ -956,6 +958,38 @@ ExecIndexBuildScanKeys(PlanState *planstate, Relation index,
opfuncid, /* reg proc to use */
(Datum) 0); /* constant */
}
+ else if (IsA(clause, NullTest))
+ {
+ /* indexkey IS NULL */
+ Assert(((NullTest *) clause)->nulltesttype == IS_NULL);
+
+ /*
+ * argument should be the index key Var, possibly relabeled
+ */
+ leftop = ((NullTest *) clause)->arg;
+
+ if (leftop && IsA(leftop, RelabelType))
+ leftop = ((RelabelType *) leftop)->arg;
+
+ Assert(leftop != NULL);
+
+ if (!(IsA(leftop, Var) &&
+ var_is_rel((Var *) leftop)))
+ elog(ERROR, "NullTest indexqual has wrong key");
+
+ varattno = ((Var *) leftop)->varattno;
+
+ /*
+ * initialize the scan key's fields appropriately
+ */
+ ScanKeyEntryInitialize(this_scan_key,
+ SK_ISNULL | SK_SEARCHNULL,
+ varattno, /* attribute number to scan */
+ strategy, /* op's strategy */
+ subtype, /* strategy subtype */
+ InvalidOid, /* no reg proc for this */
+ (Datum) 0); /* constant */
+ }
else
elog(ERROR, "unsupported indexqual type: %d",
(int) nodeTag(clause));
diff --git a/src/backend/optimizer/path/indxpath.c b/src/backend/optimizer/path/indxpath.c
index 7197658ae9b..176f2a66387 100644
--- a/src/backend/optimizer/path/indxpath.c
+++ b/src/backend/optimizer/path/indxpath.c
@@ -9,7 +9,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/optimizer/path/indxpath.c,v 1.218 2007/03/21 22:18:12 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/optimizer/path/indxpath.c,v 1.219 2007/04/06 22:33:42 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -1050,6 +1050,7 @@ match_clause_to_indexcol(IndexOptInfo *index,
* Clause must be a binary opclause, or possibly a ScalarArrayOpExpr
* (which is always binary, by definition). Or it could be a
* RowCompareExpr, which we pass off to match_rowcompare_to_indexcol().
+ * Or, if the index supports it, we can handle IS NULL clauses.
*/
if (is_opclause(clause))
{
@@ -1083,6 +1084,15 @@ match_clause_to_indexcol(IndexOptInfo *index,
(RowCompareExpr *) clause,
outer_relids);
}
+ else if (index->amsearchnulls && IsA(clause, NullTest))
+ {
+ NullTest *nt = (NullTest *) clause;
+
+ if (nt->nulltesttype == IS_NULL &&
+ match_index_to_operand((Node *) nt->arg, indexcol, index))
+ return true;
+ return false;
+ }
else
return false;
@@ -2102,8 +2112,8 @@ expand_indexqual_conditions(IndexOptInfo *index, List *clausegroups)
}
/*
- * Else it must be an opclause (usual case), ScalarArrayOp, or
- * RowCompare
+ * Else it must be an opclause (usual case), ScalarArrayOp,
+ * RowCompare, or NullTest
*/
if (is_opclause(clause))
{
@@ -2123,6 +2133,16 @@ expand_indexqual_conditions(IndexOptInfo *index, List *clausegroups)
index,
indexcol));
}
+ else if (IsA(clause, NullTest))
+ {
+ Assert(index->amsearchnulls);
+ resultquals = lappend(resultquals,
+ make_restrictinfo(clause,
+ true,
+ false,
+ false,
+ NULL));
+ }
else
elog(ERROR, "unsupported indexqual type: %d",
(int) nodeTag(clause));
diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c
index 6c4bde14213..9b9645faf28 100644
--- a/src/backend/optimizer/plan/createplan.c
+++ b/src/backend/optimizer/plan/createplan.c
@@ -10,7 +10,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.227 2007/02/25 17:44:01 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.228 2007/04/06 22:33:42 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -18,6 +18,7 @@
#include <limits.h>
+#include "access/skey.h"
#include "nodes/makefuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
@@ -1821,6 +1822,7 @@ fix_indexqual_references(List *indexquals, IndexPath *index_path,
Oid stratlefttype;
Oid stratrighttype;
bool recheck;
+ bool is_null_op = false;
Assert(IsA(rinfo, RestrictInfo));
@@ -1907,6 +1909,17 @@ fix_indexqual_references(List *indexquals, IndexPath *index_path,
&opfamily);
clause_op = saop->opno;
}
+ else if (IsA(clause, NullTest))
+ {
+ NullTest *nt = (NullTest *) clause;
+
+ Assert(nt->nulltesttype == IS_NULL);
+ nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
+ index,
+ &opfamily);
+ is_null_op = true;
+ clause_op = InvalidOid; /* keep compiler quiet */
+ }
else
{
elog(ERROR, "unsupported indexqual type: %d",
@@ -1916,16 +1929,27 @@ fix_indexqual_references(List *indexquals, IndexPath *index_path,
*fixed_indexquals = lappend(*fixed_indexquals, clause);
- /*
- * Look up the (possibly commuted) operator in the operator family to
- * get its strategy number and the recheck indicator. This also
- * double-checks that we found an operator matching the index.
- */
- get_op_opfamily_properties(clause_op, opfamily,
- &stratno,
- &stratlefttype,
- &stratrighttype,
- &recheck);
+ if (is_null_op)
+ {
+ /* IS NULL doesn't have a clause_op */
+ stratno = InvalidStrategy;
+ stratrighttype = InvalidOid;
+ /* We assume it's non-lossy ... might need more work someday */
+ recheck = false;
+ }
+ else
+ {
+ /*
+ * Look up the (possibly commuted) operator in the operator family
+ * to get its strategy number and the recheck indicator. This also
+ * double-checks that we found an operator matching the index.
+ */
+ get_op_opfamily_properties(clause_op, opfamily,
+ &stratno,
+ &stratlefttype,
+ &stratrighttype,
+ &recheck);
+ }
*indexstrategy = lappend_int(*indexstrategy, stratno);
*indexsubtype = lappend_oid(*indexsubtype, stratrighttype);
diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c
index e52943a675e..33b081ffffd 100644
--- a/src/backend/optimizer/util/plancat.c
+++ b/src/backend/optimizer/util/plancat.c
@@ -9,7 +9,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/optimizer/util/plancat.c,v 1.132 2007/01/20 23:13:01 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/optimizer/util/plancat.c,v 1.133 2007/04/06 22:33:42 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -187,6 +187,7 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
info->relam = indexRelation->rd_rel->relam;
info->amcostestimate = indexRelation->rd_am->amcostestimate;
info->amoptionalkey = indexRelation->rd_am->amoptionalkey;
+ info->amsearchnulls = indexRelation->rd_am->amsearchnulls;
/*
* Fetch the ordering operators associated with the index, if any.
diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c
index f596220d5a4..28e95a74eed 100644
--- a/src/backend/utils/adt/selfuncs.c
+++ b/src/backend/utils/adt/selfuncs.c
@@ -15,7 +15,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.231 2007/03/27 23:21:10 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.232 2007/04/06 22:33:42 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -4992,6 +4992,7 @@ btcostestimate(PG_FUNCTION_ARGS)
int indexcol;
bool eqQualHere;
bool found_saop;
+ bool found_null_op;
double num_sa_scans;
ListCell *l;
@@ -5016,6 +5017,7 @@ btcostestimate(PG_FUNCTION_ARGS)
indexcol = 0;
eqQualHere = false;
found_saop = false;
+ found_null_op = false;
num_sa_scans = 1;
foreach(l, indexQuals)
{
@@ -5025,6 +5027,7 @@ btcostestimate(PG_FUNCTION_ARGS)
*rightop;
Oid clause_op;
int op_strategy;
+ bool is_null_op = false;
Assert(IsA(rinfo, RestrictInfo));
clause = rinfo->clause;
@@ -5051,6 +5054,17 @@ btcostestimate(PG_FUNCTION_ARGS)
clause_op = saop->opno;
found_saop = true;
}
+ else if (IsA(clause, NullTest))
+ {
+ NullTest *nt = (NullTest *) clause;
+
+ Assert(nt->nulltesttype == IS_NULL);
+ leftop = (Node *) nt->arg;
+ rightop = NULL;
+ clause_op = InvalidOid;
+ found_null_op = true;
+ is_null_op = true;
+ }
else
{
elog(ERROR, "unsupported indexqual type: %d",
@@ -5088,11 +5102,20 @@ btcostestimate(PG_FUNCTION_ARGS)
break;
}
}
- op_strategy = get_op_opfamily_strategy(clause_op,
- index->opfamily[indexcol]);
- Assert(op_strategy != 0); /* not a member of opfamily?? */
- if (op_strategy == BTEqualStrategyNumber)
+ /* check for equality operator */
+ if (is_null_op)
+ {
+ /* IS NULL is like = for purposes of selectivity determination */
eqQualHere = true;
+ }
+ else
+ {
+ op_strategy = get_op_opfamily_strategy(clause_op,
+ index->opfamily[indexcol]);
+ Assert(op_strategy != 0); /* not a member of opfamily?? */
+ if (op_strategy == BTEqualStrategyNumber)
+ eqQualHere = true;
+ }
/* count up number of SA scans induced by indexBoundQuals only */
if (IsA(clause, ScalarArrayOpExpr))
{
@@ -5108,12 +5131,14 @@ btcostestimate(PG_FUNCTION_ARGS)
/*
* If index is unique and we found an '=' clause for each column, we can
* just assume numIndexTuples = 1 and skip the expensive
- * clauselist_selectivity calculations.
+ * clauselist_selectivity calculations. However, a ScalarArrayOp or
+ * NullTest invalidates that theory, even though it sets eqQualHere.
*/
if (index->unique &&
indexcol == index->ncolumns - 1 &&
eqQualHere &&
- !found_saop)
+ !found_saop &&
+ !found_null_op)
numIndexTuples = 1.0;
else
{