Skip to content

Commit f0a0c17

Browse files
committed
Refactor get_partition_for_tuple a bit.
Pending patches for both default partitioning and hash partitioning find the current coding pattern to be inconvenient. Change it so that we switch on the partitioning method first and then do whatever is needed. Amul Sul, reviewed by Jeevan Ladhe, with a few adjustments by me. Discussion: http://postgr.es/m/CAAJ_b97mTb=dG2pv6+1ougxEVZFVnZJajW+0QHj46mEE7WsoOQ@mail.gmail.com Discussion: http://postgr.es/m/CAOgcT0M37CAztEinpvjJc18EdHfm23fw0EG9-36Ya=+rEFUqaQ@mail.gmail.com
1 parent 3ca930f commit f0a0c17

File tree

1 file changed

+52
-48
lines changed

1 file changed

+52
-48
lines changed

src/backend/catalog/partition.c

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,10 +1920,7 @@ get_partition_for_tuple(PartitionDispatch *pd,
19201920
PartitionDispatch parent;
19211921
Datum values[PARTITION_MAX_KEYS];
19221922
bool isnull[PARTITION_MAX_KEYS];
1923-
int cur_offset,
1924-
cur_index;
1925-
int i,
1926-
result;
1923+
int result;
19271924
ExprContext *ecxt = GetPerTupleExprContext(estate);
19281925
TupleTableSlot *ecxt_scantuple_old = ecxt->ecxt_scantuple;
19291926

@@ -1935,6 +1932,7 @@ get_partition_for_tuple(PartitionDispatch *pd,
19351932
PartitionDesc partdesc = parent->partdesc;
19361933
TupleTableSlot *myslot = parent->tupslot;
19371934
TupleConversionMap *map = parent->tupmap;
1935+
int cur_index = -1;
19381936

19391937
if (myslot != NULL && map != NULL)
19401938
{
@@ -1966,61 +1964,67 @@ get_partition_for_tuple(PartitionDispatch *pd,
19661964
ecxt->ecxt_scantuple = slot;
19671965
FormPartitionKeyDatum(parent, slot, estate, values, isnull);
19681966

1969-
if (key->strategy == PARTITION_STRATEGY_RANGE)
1967+
/* Route as appropriate based on partitioning strategy. */
1968+
switch (key->strategy)
19701969
{
1971-
/*
1972-
* Since we cannot route tuples with NULL partition keys through a
1973-
* range-partitioned table, simply return that no partition exists
1974-
*/
1975-
for (i = 0; i < key->partnatts; i++)
1976-
{
1977-
if (isnull[i])
1970+
case PARTITION_STRATEGY_LIST:
1971+
1972+
if (isnull[0])
19781973
{
1979-
*failed_at = parent;
1980-
*failed_slot = slot;
1981-
result = -1;
1982-
goto error_exit;
1974+
if (partition_bound_accepts_nulls(partdesc->boundinfo))
1975+
cur_index = partdesc->boundinfo->null_index;
19831976
}
1984-
}
1985-
}
1986-
1987-
/*
1988-
* A null partition key is only acceptable if null-accepting list
1989-
* partition exists.
1990-
*/
1991-
cur_index = -1;
1992-
if (isnull[0] && partition_bound_accepts_nulls(partdesc->boundinfo))
1993-
cur_index = partdesc->boundinfo->null_index;
1994-
else if (!isnull[0])
1995-
{
1996-
/* Else bsearch in partdesc->boundinfo */
1997-
bool equal = false;
1998-
1999-
cur_offset = partition_bound_bsearch(key, partdesc->boundinfo,
2000-
values, false, &equal);
2001-
switch (key->strategy)
2002-
{
2003-
case PARTITION_STRATEGY_LIST:
1977+
else
1978+
{
1979+
bool equal = false;
1980+
int cur_offset;
1981+
1982+
cur_offset = partition_bound_bsearch(key,
1983+
partdesc->boundinfo,
1984+
values,
1985+
false,
1986+
&equal);
20041987
if (cur_offset >= 0 && equal)
20051988
cur_index = partdesc->boundinfo->indexes[cur_offset];
2006-
else
2007-
cur_index = -1;
2008-
break;
1989+
}
1990+
break;
1991+
1992+
case PARTITION_STRATEGY_RANGE:
1993+
{
1994+
bool equal = false;
1995+
int cur_offset;
1996+
int i;
1997+
1998+
/* No range includes NULL. */
1999+
for (i = 0; i < key->partnatts; i++)
2000+
{
2001+
if (isnull[i])
2002+
{
2003+
*failed_at = parent;
2004+
*failed_slot = slot;
2005+
result = -1;
2006+
goto error_exit;
2007+
}
2008+
}
20092009

2010-
case PARTITION_STRATEGY_RANGE:
2010+
cur_offset = partition_bound_bsearch(key,
2011+
partdesc->boundinfo,
2012+
values,
2013+
false,
2014+
&equal);
20112015

20122016
/*
2013-
* Offset returned is such that the bound at offset is
2014-
* found to be less or equal with the tuple. So, the bound
2015-
* at offset+1 would be the upper bound.
2017+
* The offset returned is such that the bound at cur_offset
2018+
* is less than or equal to the tuple value, so the bound
2019+
* at offset+1 is the upper bound.
20162020
*/
20172021
cur_index = partdesc->boundinfo->indexes[cur_offset + 1];
2018-
break;
2022+
}
2023+
break;
20192024

2020-
default:
2021-
elog(ERROR, "unexpected partition strategy: %d",
2022-
(int) key->strategy);
2023-
}
2025+
default:
2026+
elog(ERROR, "unexpected partition strategy: %d",
2027+
(int) key->strategy);
20242028
}
20252029

20262030
/*

0 commit comments

Comments
 (0)