pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joel Jacobson <joel@compiler.org>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-hackers@lists.postgresql.org
Subject: Re: Planning time grows exponentially with levels of nested views
Date: Sun, 18 Apr 2021 22:42:11 +0200
Message-ID: <1eccf300-7d93-4355-9687-34b9ec57f3fc@www.fastmail.com> (raw)
In-Reply-To: <140747.1618776847@sss.pgh.pa.us>
References: <797aff54-b49b-4914-9ff9-aa42564a4d7d@www.fastmail.com>
	<140747.1618776847@sss.pgh.pa.us>

On Sun, Apr 18, 2021, at 22:14, Tom Lane wrote:
> "Joel Jacobson" <joel@compiler.org <mailto:joel%40compiler.org>> writes:
> > I assumed the cost for each nested VIEW layer would grow linear,
> > but my testing shows it appears to grow exponentially:
> 
> I think it's impossible to avoid less-than-O(N^2) growth on this sort
> of case.  For example, the v2 subquery initially has RTEs for v2 itself
> plus v1.  When we flatten v1 into v2, v2 acquires the RTEs from v1,
> namely v1 itself plus foo.  Similarly, once vK-1 is pulled up into vK,
> there are going to be order-of-K entries in vK's rtable, and that stacking
> makes for O(N^2) work overall just in manipulating the rtable.
> 
> We can't get rid of these rtable entries altogether, since all of them
> represent table privilege checks that the executor will need to do.
> It occurs to me though that we don't need the rte->subquery trees anymore
> once those are flattened, so maybe we could do something like the
> attached.  For me, this reduces the slowdown in your example from
> O(N^3) to O(N^2).

Many thanks for explaining and the patch.

I've tested the patch successfully.
Planning time grows much slower now:

EXPLAIN ANALYZE SELECT * FROM v64;
- Planning Time: 14.981 ms
+ Planning Time: 2.802 ms

EXPLAIN ANALYZE SELECT * FROM v128;
- Planning Time: 109.407 ms
+ Planning Time: 11.595 ms

EXPLAIN ANALYZE SELECT * FROM v256;
- Planning Time: 1594.809 ms
+ Planning Time: 46.709 ms

Very nice.

/Joel

view thread (6+ messages)  latest in thread

Message-ID: <1eccf300-7d93-4355-9687-34b9ec57f3fc@www.fastmail.com>
Permalink:  ../1eccf300-7d93-4355-9687-34b9ec57f3fc@www.fastmail.com/
Also on:    postgresql.org/message-id/1eccf300-7d93-4355-9687-34b9ec57f3fc@www.fastmail.com

 · 

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-general@postgresql.org
  Cc: joel@compiler.org, tgl@sss.pgh.pa.us, pgsql-hackers@lists.postgresql.org
  Subject: Re: Planning time grows exponentially with levels of nested views
  In-Reply-To: <1eccf300-7d93-4355-9687-34b9ec57f3fc@www.fastmail.com>
  References: <797aff54-b49b-4914-9ff9-aa42564a4d7d@www.fastmail.com> <140747.1618776847@sss.pgh.pa.us> <1eccf300-7d93-4355-9687-34b9ec57f3fc@www.fastmail.com>

* 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