scan on inheritance parent with no children in current session

From: Ashutosh Bapat <ashutosh(dot)bapat(at)enterprisedb(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: scan on inheritance parent with no children in current session
Date: 2017-08-04 09:11:38
Message-ID: CAFjFpReWJr1yTkHU=OqiMBmcYCMoSW3VPR39RBuQ_ovwDFBT5Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi All,
Consider a parent table which has no child in the current session, but
has temporary children in other sessions.

Session 1
postgres=# create table parent (a int);
CREATE TABLE

Session 2:
postgres=# create temp table temp_child () inherits(parent);
CREATE TABLE

Before commit d3cc37f1d801a6b5cad9bf179274a8d767f1ee50. We would have
Seq Scan plan for scanning parent in session 1
postgres=# explain verbose select * from parent;
QUERY PLAN
-------------------------------------------------------------
Seq Scan on public.parent (cost=0.00..0.00 rows=1 width=4)
Output: parent.a
(2 rows)

In session 2, it would be an Append plan
postgres=# explain verbose select * from parent;
QUERY PLAN
------------------------------------------------------------------------------
Append (cost=0.00..35.50 rows=2551 width=4)
-> Seq Scan on public.parent (cost=0.00..0.00 rows=1 width=4)
Output: parent.a
-> Seq Scan on pg_temp_4.temp_child (cost=0.00..35.50 rows=2550 width=4)
Output: temp_child.a
(5 rows)

After that commit in session 1, we get an Append plan
postgres=# explain verbose select * from parent;
QUERY PLAN
-------------------------------------------------------------------
Append (cost=0.00..0.00 rows=1 width=4)
-> Seq Scan on public.parent (cost=0.00..0.00 rows=1 width=4)
Output: parent.a
(3 rows)

I don't think this is an intentional change. Here's patch to fix it.
The comment in the patch uses term "real child" in the context of
comments about temporary children from other session and the comment
at the end of the function where rte->inh is reset. May be we should
move the second comment before setting has_child in the patch and use
"real child" in the comment at the end to avoid repetition. But I want
to first check whether we want this fix or we can live with the Append
plan.

--
Best Wishes,
Ashutosh Bapat
EnterpriseDB Corporation
The Postgres Database Company

Attachment Content-Type Size
no_child_parent_scan.patch text/x-patch 1.4 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Rushabh Lathia 2017-08-04 09:31:21 Re: reload-through-the-top-parent switch the partition table
Previous Message Masahiko Sawada 2017-08-04 08:27:35 Small code improvement for btree