Skip to content

Commit 86f6f8b

Browse files
committed
intruduce rowmark_add_tableoids_walker(), fix rowmark_add_tableoids()
1 parent f7e4267 commit 86f6f8b

File tree

3 files changed

+98
-50
lines changed

3 files changed

+98
-50
lines changed

expected/pg_pathman.out

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,3 +1768,41 @@ NOTICE: 100 rows copied from test_fkey_0
17681768
10
17691769
(1 row)
17701770

1771+
/* Check rowmarks */
1772+
SELECT create_hash_partitions('test_fkey', 'id', 5);
1773+
create_hash_partitions
1774+
------------------------
1775+
5
1776+
(1 row)
1777+
1778+
EXPLAIN (COSTS OFF)
1779+
SELECT * FROM test_fkey
1780+
WHERE id = (SELECT id FROM test_fkey ORDER BY id OFFSET 10 LIMIT 1 FOR UPDATE)
1781+
FOR SHARE;
1782+
QUERY PLAN
1783+
-----------------------------------------------------------------------
1784+
LockRows
1785+
InitPlan 1 (returns $1)
1786+
-> Limit
1787+
-> LockRows
1788+
-> Sort
1789+
Sort Key: test_fkey_0_1.id
1790+
-> Append
1791+
-> Seq Scan on test_fkey_0 test_fkey_0_1
1792+
-> Seq Scan on test_fkey_1 test_fkey_1_1
1793+
-> Seq Scan on test_fkey_2 test_fkey_2_1
1794+
-> Seq Scan on test_fkey_3 test_fkey_3_1
1795+
-> Seq Scan on test_fkey_4 test_fkey_4_1
1796+
-> Append
1797+
-> Seq Scan on test_fkey_0
1798+
Filter: (id = $1)
1799+
-> Seq Scan on test_fkey_1
1800+
Filter: (id = $1)
1801+
-> Seq Scan on test_fkey_2
1802+
Filter: (id = $1)
1803+
-> Seq Scan on test_fkey_3
1804+
Filter: (id = $1)
1805+
-> Seq Scan on test_fkey_4
1806+
Filter: (id = $1)
1807+
(23 rows)
1808+

sql/pg_pathman.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,3 +665,10 @@ SELECT create_hash_partitions('test_fkey', 'id', 10);
665665
INSERT INTO test_fkey VALUES(1, 'wrong');
666666
INSERT INTO test_fkey VALUES(1, 'test');
667667
SELECT drop_partitions('test_fkey');
668+
669+
/* Check rowmarks */
670+
SELECT create_hash_partitions('test_fkey', 'id', 5);
671+
EXPLAIN (COSTS OFF)
672+
SELECT * FROM test_fkey
673+
WHERE id = (SELECT id FROM test_fkey ORDER BY id OFFSET 10 LIMIT 1 FOR UPDATE)
674+
FOR SHARE;

src/utils.c

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static void change_varnos_in_restrinct_info(RestrictInfo *rinfo,
3939
static bool change_varno_walker(Node *node, change_varno_context *context);
4040
static List *get_tableoids_list(List *tlist);
4141
static void lock_rows_visitor(Plan *plan, void *context);
42+
static bool rowmark_add_tableoids_walker(Node *node, void *context);
4243

4344

4445
/*
@@ -492,6 +493,57 @@ plan_tree_walker(Plan *plan,
492493
visitor(plan, context);
493494
}
494495

496+
static bool
497+
rowmark_add_tableoids_walker(Node *node, void *context)
498+
{
499+
if (node == NULL)
500+
return false;
501+
502+
if (IsA(node, Query))
503+
{
504+
Query *parse = (Query *) node;
505+
ListCell *lc;
506+
507+
/* Generate 'tableoid' for partitioned table rowmark */
508+
foreach (lc, parse->rowMarks)
509+
{
510+
RowMarkClause *rc = (RowMarkClause *) lfirst(lc);
511+
Oid parent = getrelid(rc->rti, parse->rtable);
512+
Var *var;
513+
TargetEntry *tle;
514+
char resname[64];
515+
516+
/* Check that table is partitioned */
517+
if (!get_pathman_relation_info(parent))
518+
continue;
519+
520+
var = makeVar(rc->rti,
521+
TableOidAttributeNumber,
522+
OIDOID,
523+
-1,
524+
InvalidOid,
525+
0);
526+
527+
/* Use parent's Oid as TABLEOID_STR's key (%u) */
528+
snprintf(resname, sizeof(resname), TABLEOID_STR("%u"), parent);
529+
530+
tle = makeTargetEntry((Expr *) var,
531+
list_length(parse->targetList) + 1,
532+
pstrdup(resname),
533+
true);
534+
535+
/* There's no problem here since new attribute is junk */
536+
parse->targetList = lappend(parse->targetList, tle);
537+
}
538+
539+
return query_tree_walker((Query *) node,
540+
rowmark_add_tableoids_walker,
541+
NULL, 0);
542+
}
543+
544+
return expression_tree_walker(node, rowmark_add_tableoids_walker, NULL);
545+
}
546+
495547
/*
496548
* Add missing 'TABLEOID_STR%u' junk attributes for inherited partitions
497549
*
@@ -504,56 +556,7 @@ plan_tree_walker(Plan *plan,
504556
void
505557
rowmark_add_tableoids(Query *parse)
506558
{
507-
ListCell *lc;
508-
509-
check_stack_depth();
510-
511-
foreach(lc, parse->rtable)
512-
{
513-
RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
514-
515-
switch(rte->rtekind)
516-
{
517-
case RTE_SUBQUERY:
518-
rowmark_add_tableoids(rte->subquery);
519-
break;
520-
521-
default:
522-
break;
523-
}
524-
}
525-
526-
/* Generate 'tableoid' for partitioned table rowmark */
527-
foreach (lc, parse->rowMarks)
528-
{
529-
RowMarkClause *rc = (RowMarkClause *) lfirst(lc);
530-
Oid parent = getrelid(rc->rti, parse->rtable);
531-
Var *var;
532-
TargetEntry *tle;
533-
char resname[64];
534-
535-
/* Check that table is partitioned */
536-
if (!get_pathman_relation_info(parent))
537-
continue;
538-
539-
var = makeVar(rc->rti,
540-
TableOidAttributeNumber,
541-
OIDOID,
542-
-1,
543-
InvalidOid,
544-
0);
545-
546-
/* Use parent's Oid as TABLEOID_STR's key (%u) */
547-
snprintf(resname, sizeof(resname), TABLEOID_STR("%u"), parent);
548-
549-
tle = makeTargetEntry((Expr *) var,
550-
list_length(parse->targetList) + 1,
551-
pstrdup(resname),
552-
true);
553-
554-
/* There's no problem here since new attribute is junk */
555-
parse->targetList = lappend(parse->targetList, tle);
556-
}
559+
rowmark_add_tableoids_walker((Node *) parse, NULL);
557560
}
558561

559562
/*

0 commit comments

Comments
 (0)