pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: bashtanov@imap.cc
To: pgsql-bugs@postgresql.org
Subject: BUG #14642: Excessive sorting node appears in the plan
Date: Thu, 04 May 2017 12:29:23 +0000
Message-ID: <20170504122923.24366.50190@wrigleys.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

The following bug has been logged on the website:

Bug reference:      14642
Logged by:          Alexey Bashtanov
Email address:      bashtanov@imap.cc
PostgreSQL version: 9.6.2
Operating system:   Ubuntu Linux 14.04
Description:        

Hello,

Under certain circumstances, an excessive sorting node appears in the
plan.
It happens when at the same time:
1) There is a merge join node with a join by two equality clauses (say,
`innr.p=outr.p AND innr.q=outr.q`);
2) One of the children, say the inner one, comes as a `SELECT p, q FROM ..
WHERE p = 0 GROUP BY p, q` or `GROUP BY q`, doesn't matter.
3) It uses sort+groupAgg, not hashAgg.

Then the result of the grouping, being already sorted by p (which is always
1), and q, gets sorted again for the merge join.

It looks like the grouping subplan is aware of p=1 and simplifies its
pathkeys from [(innr.p, 1), (innr.q)] to [(innr.q)]. However, looks like it
does not share this kind of knowledge with the code that plans the merge
join, so the merge join keeps requiring the data coming in [innr.p, innr.q]
order.

Do blackbox-style subplans, such as subqueries with grouping, share only the
resulting stream order info with the enclosing query, not the equivalence
classes?

This is the example:
---
set enable_hashjoin to off;
set enable_hashagg to off;
set enable_nestloop to off;

explain 
select * 
from (
	select outr%2 o2,
		outr%5 o5
	from generate_series(1, 10) outr
	order by o2,
		  o5
) o
join (
	select innr%2 i2,
		innr%5 i5
	from generate_series(1, 10) innr
	where innr%2 = 0 
	group by innr%2,
		  innr%5
) i on o.o2 = i.i2
   and o.o5 = i.i5;
---

I tried 9.4.11, 9.6.2, and master versions.

Best regards,
  Alexey Bashtanov



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs


view thread (3+ messages)  latest in thread

Message-ID: <20170504122923.24366.50190@wrigleys.postgresql.org>
Permalink:  ../20170504122923.24366.50190@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/20170504122923.24366.50190@wrigleys.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: bashtanov@imap.cc
  Subject: Re: BUG #14642: Excessive sorting node appears in the plan
  In-Reply-To: <20170504122923.24366.50190@wrigleys.postgresql.org>
  References: <20170504122923.24366.50190@wrigleys.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox