Skip to content

Commit 5efccc1

Browse files
committed
Avoid useless "x = ANY(ARRAY[])" test for empty partition list.
This arises in practice if the partition only admits NULL values. Jeevan Ladhe Discussion: https://postgr.es/m/CAOgcT0OChrN--uuqH6wG6Z8+nxnCWJ+2Q-uhnK4KOANdRRxuAw@mail.gmail.com
1 parent 00c5e51 commit 5efccc1

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

src/backend/catalog/partition.c

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,12 @@ get_qual_for_list(PartitionKey key, PartitionBoundSpec *spec)
13111311
List *arrelems = NIL;
13121312
bool list_has_null = false;
13131313

1314+
/*
1315+
* Only single-column list partitioning is supported, so we are worried
1316+
* only about the partition key with index 0.
1317+
*/
1318+
Assert(key->partnatts == 1);
1319+
13141320
/* Construct Var or expression representing the partition column */
13151321
if (key->partattrs[0] != 0)
13161322
keyCol = (Expr *) makeVar(1,
@@ -1333,20 +1339,28 @@ get_qual_for_list(PartitionKey key, PartitionBoundSpec *spec)
13331339
arrelems = lappend(arrelems, copyObject(val));
13341340
}
13351341

1336-
/* Construct an ArrayExpr for the non-null partition values */
1337-
arr = makeNode(ArrayExpr);
1338-
arr->array_typeid = !type_is_array(key->parttypid[0])
1339-
? get_array_type(key->parttypid[0])
1340-
: key->parttypid[0];
1341-
arr->array_collid = key->parttypcoll[0];
1342-
arr->element_typeid = key->parttypid[0];
1343-
arr->elements = arrelems;
1344-
arr->multidims = false;
1345-
arr->location = -1;
1346-
1347-
/* Generate the main expression, i.e., keyCol = ANY (arr) */
1348-
opexpr = make_partition_op_expr(key, 0, BTEqualStrategyNumber,
1349-
keyCol, (Expr *) arr);
1342+
if (arrelems)
1343+
{
1344+
/* Construct an ArrayExpr for the non-null partition values */
1345+
arr = makeNode(ArrayExpr);
1346+
arr->array_typeid = !type_is_array(key->parttypid[0])
1347+
? get_array_type(key->parttypid[0])
1348+
: key->parttypid[0];
1349+
arr->array_collid = key->parttypcoll[0];
1350+
arr->element_typeid = key->parttypid[0];
1351+
arr->elements = arrelems;
1352+
arr->multidims = false;
1353+
arr->location = -1;
1354+
1355+
/* Generate the main expression, i.e., keyCol = ANY (arr) */
1356+
opexpr = make_partition_op_expr(key, 0, BTEqualStrategyNumber,
1357+
keyCol, (Expr *) arr);
1358+
}
1359+
else
1360+
{
1361+
/* If there are no partition values, we don't need an = ANY expr */
1362+
opexpr = NULL;
1363+
}
13501364

13511365
if (!list_has_null)
13521366
{
@@ -1361,24 +1375,29 @@ get_qual_for_list(PartitionKey key, PartitionBoundSpec *spec)
13611375
nulltest->argisrow = false;
13621376
nulltest->location = -1;
13631377

1364-
result = list_make2(nulltest, opexpr);
1378+
result = opexpr ? list_make2(nulltest, opexpr) : list_make1(nulltest);
13651379
}
13661380
else
13671381
{
13681382
/*
13691383
* Gin up a "col IS NULL" test that will be OR'd with the main
13701384
* expression.
13711385
*/
1372-
Expr *or;
1373-
13741386
nulltest = makeNode(NullTest);
13751387
nulltest->arg = keyCol;
13761388
nulltest->nulltesttype = IS_NULL;
13771389
nulltest->argisrow = false;
13781390
nulltest->location = -1;
13791391

1380-
or = makeBoolExpr(OR_EXPR, list_make2(nulltest, opexpr), -1);
1381-
result = list_make1(or);
1392+
if (opexpr)
1393+
{
1394+
Expr *or;
1395+
1396+
or = makeBoolExpr(OR_EXPR, list_make2(nulltest, opexpr), -1);
1397+
result = list_make1(or);
1398+
}
1399+
else
1400+
result = list_make1(nulltest);
13821401
}
13831402

13841403
return result;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy