diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index b2239728cf..7207ae043e 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -1592,6 +1592,12 @@ inheritance_planner(PlannerInfo *root) */ if (subpaths == NIL) { + /* Set the empty result plan's targetlist appropriately. */ + List *tlist; + + tlist = preprocess_targetlist(root); + root->processed_tlist = tlist; + final_rel->reltarget = create_pathtarget(root, tlist); set_dummy_rel_pathlist(final_rel); return; } diff --git a/src/test/regress/expected/inherit.out b/src/test/regress/expected/inherit.out index f259d07535..2036afb34a 100644 --- a/src/test/regress/expected/inherit.out +++ b/src/test/regress/expected/inherit.out @@ -2033,3 +2033,30 @@ select min(a), max(a) from parted_minmax where b = '12345'; (1 row) drop table parted_minmax; +-- check that the generated targetlist is correct even if modifying +-- an empty set (all inheritance children excluded) +create table empty_modify_inh_tlist (a int); +create table empty_modify_inh_tlist1 () inherits (empty_modify_inh_tlist); +create or replace function test_empty_modify_inh_tlist (p boolean) returns setof empty_modify_inh_tlist + language 'plpgsql' as $body$ + begin + return query update empty_modify_inh_tlist set a = a + 1 where p returning *; + end; +$body$; +insert into empty_modify_inh_tlist values (1); +insert into empty_modify_inh_tlist1 values (2); +select * from test_empty_modify_inh_tlist(true); + a +--- + 2 + 3 +(2 rows) + +select * from test_empty_modify_inh_tlist(false); + a +--- +(0 rows) + +drop function test_empty_modify_inh_tlist; +drop table empty_modify_inh_tlist cascade; +NOTICE: drop cascades to table empty_modify_inh_tlist1 diff --git a/src/test/regress/sql/inherit.sql b/src/test/regress/sql/inherit.sql index 425052c1f4..78abd69077 100644 --- a/src/test/regress/sql/inherit.sql +++ b/src/test/regress/sql/inherit.sql @@ -711,3 +711,20 @@ insert into parted_minmax values (1,'12345'); explain (costs off) select min(a), max(a) from parted_minmax where b = '12345'; select min(a), max(a) from parted_minmax where b = '12345'; drop table parted_minmax; + +-- check that the generated targetlist is correct even if modifying +-- an empty set (all inheritance children excluded) +create table empty_modify_inh_tlist (a int); +create table empty_modify_inh_tlist1 () inherits (empty_modify_inh_tlist); +create or replace function test_empty_modify_inh_tlist (p boolean) returns setof empty_modify_inh_tlist + language 'plpgsql' as $body$ + begin + return query update empty_modify_inh_tlist set a = a + 1 where p returning *; + end; +$body$; +insert into empty_modify_inh_tlist values (1); +insert into empty_modify_inh_tlist1 values (2); +select * from test_empty_modify_inh_tlist(true); +select * from test_empty_modify_inh_tlist(false); +drop function test_empty_modify_inh_tlist; +drop table empty_modify_inh_tlist cascade;