Skip to content

Commit 132488b

Browse files
committed
Set ecxt_scantuple correctly for tuple routing.
In 2ac3ef7, we changed things so that it's possible for a different TupleTableSlot to be used for partitioned tables at successively lower levels. If we do end up changing the slot from the original, we must update ecxt_scantuple to point to the new one for partition key of the tuple to be computed correctly. Reported by Rajkumar Raghuwanshi. Patch by Amit Langote. Discussion: http://postgr.es/m/CAKcux6%3Dm1qyqB2k6cjniuMMrYXb75O-MB4qGQMu8zg-iGGLjDw%40mail.gmail.com
1 parent 27cdb34 commit 132488b

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

src/backend/catalog/partition.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,7 +1679,10 @@ get_partition_for_tuple(PartitionDispatch *pd,
16791679
bool isnull[PARTITION_MAX_KEYS];
16801680
int cur_offset,
16811681
cur_index;
1682-
int i;
1682+
int i,
1683+
result;
1684+
ExprContext *ecxt = GetPerTupleExprContext(estate);
1685+
TupleTableSlot *ecxt_scantuple_old = ecxt->ecxt_scantuple;
16831686

16841687
/* start with the root partitioned table */
16851688
parent = pd[0];
@@ -1708,7 +1711,15 @@ get_partition_for_tuple(PartitionDispatch *pd,
17081711
slot = myslot;
17091712
}
17101713

1711-
/* Extract partition key from tuple */
1714+
/*
1715+
* Extract partition key from tuple. Expression evaluation machinery
1716+
* that FormPartitionKeyDatum() invokes expects ecxt_scantuple to
1717+
* point to the correct tuple slot. The slot might have changed from
1718+
* what was used for the parent table if the table of the current
1719+
* partitioning level has different tuple descriptor from the parent.
1720+
* So update ecxt_scantuple accordingly.
1721+
*/
1722+
ecxt->ecxt_scantuple = slot;
17121723
FormPartitionKeyDatum(parent, slot, estate, values, isnull);
17131724

17141725
if (key->strategy == PARTITION_STRATEGY_RANGE)
@@ -1763,16 +1774,21 @@ get_partition_for_tuple(PartitionDispatch *pd,
17631774
*/
17641775
if (cur_index < 0)
17651776
{
1777+
result = -1;
17661778
*failed_at = RelationGetRelid(parent->reldesc);
1767-
return -1;
1779+
break;
17681780
}
1769-
else if (parent->indexes[cur_index] < 0)
1770-
parent = pd[-parent->indexes[cur_index]];
1771-
else
1781+
else if (parent->indexes[cur_index] >= 0)
1782+
{
1783+
result = parent->indexes[cur_index];
17721784
break;
1785+
}
1786+
else
1787+
parent = pd[-parent->indexes[cur_index]];
17731788
}
17741789

1775-
return parent->indexes[cur_index];
1790+
ecxt->ecxt_scantuple = ecxt_scantuple_old;
1791+
return result;
17761792
}
17771793

17781794
/*

src/backend/executor/execMain.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3192,9 +3192,7 @@ ExecFindPartition(ResultRelInfo *resultRelInfo, PartitionDispatch *pd,
31923192
{
31933193
int result;
31943194
Oid failed_at;
3195-
ExprContext *econtext = GetPerTupleExprContext(estate);
31963195

3197-
econtext->ecxt_scantuple = slot;
31983196
result = get_partition_for_tuple(pd, slot, estate, &failed_at);
31993197
if (result < 0)
32003198
{

src/test/regress/expected/insert.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ drop table part_ee_ff, part_gg2_2, part_gg2_1, part_gg2, part_gg1, part_gg;
320320
drop table part_aa_bb, part_cc_dd, part_null, list_parted;
321321
-- more tests for certain multi-level partitioning scenarios
322322
create table p (a int, b int) partition by range (a, b);
323-
create table p1 (b int, a int not null) partition by range (b);
323+
create table p1 (b int not null, a int not null) partition by range ((b+0));
324324
create table p11 (like p1);
325325
alter table p11 drop a;
326326
alter table p11 add a int;

src/test/regress/sql/insert.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ drop table part_aa_bb, part_cc_dd, part_null, list_parted;
193193

194194
-- more tests for certain multi-level partitioning scenarios
195195
create table p (a int, b int) partition by range (a, b);
196-
create table p1 (b int, a int not null) partition by range (b);
196+
create table p1 (b int not null, a int not null) partition by range ((b+0));
197197
create table p11 (like p1);
198198
alter table p11 drop a;
199199
alter table p11 add a int;

0 commit comments

Comments
 (0)