Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lYEFl-0004J8-TO for pgsql-hackers@arkaria.postgresql.org; Sun, 18 Apr 2021 20:42:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lYEFk-0000gX-QJ for pgsql-hackers@arkaria.postgresql.org; Sun, 18 Apr 2021 20:42:48 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lYEFj-0000gQ-2w for pgsql-hackers@lists.postgresql.org; Sun, 18 Apr 2021 20:42:48 +0000 Received: from [66.111.4.26] (helo=out2-smtp.messagingengine.com) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lYEFg-0005Pt-BT for pgsql-hackers@lists.postgresql.org; Sun, 18 Apr 2021 20:42:46 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 7738E5C006A; Sun, 18 Apr 2021 16:42:32 -0400 (EDT) Received: from imap2 ([10.202.2.52]) by compute5.internal (MEProxy); Sun, 18 Apr 2021 16:42:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=compiler.org; h= mime-version:message-id:in-reply-to:references:date:from:to:cc :subject:content-type; s=fm1; bh=pkM00DCYewgMX1Kq1az/bXRISYp3m7g Wl7DfqeLnUSU=; b=LsTWZHECmIEFIjxxjdyH1FP1etjcAx1h/E4PTtcm6Rh8H1u 0S0ZbKD/jZfWwV2Xqdl+AOHcZzbX5/f3RuToNNbzBIMgq1aZvQaiMvcEGOPQot/s VkOPTrQKG74i7a+Wrb4x46NTvsv423g1mJMt6d+uBvUjc2ciQwVYJ/g7oUPhtRxI oZsKRbDDec2ENtwEHDhMkK67kHOfFyx7gGZ81CsAl5yUp1gf+PxVyjD+8+fO7Phd HPEyrXSet/8bM9VX0hiFezk7sTgqiRZCFA1QXDTAsd8evOttAe+qRf7cFZXpMF6f /gZupSvTTOIY26JWm/R+T28G4CdB3W4JhnvEtyQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=pkM00D CYewgMX1Kq1az/bXRISYp3m7gWl7DfqeLnUSU=; b=QUKb0qa7V6JZJW1S3PDXz0 gAyukKk2n6BBhCQKUGtlkZulB2ZztF8oywQgk/Z57gqQEUeDuIX4VgZTO/MG0cxB BCjFBcoJH7+Yzr7FnrotXZ+oFs2saigT6pKfhsRoBnBXgiKdxdAPzIX58e1KEffu JnkfAulaHOtmJA2nJf+Cc0kqykqvO9uflv5XEn7nmJKbYTOk9fTEpUOXv60RYfM0 reBihJbEyraOx8a42o0QJoikIcGSMKdWLG/4/bdgRFRyq1zqdEdGVdI9N8571wjh 9OAUq7J3d9/AOccv2xLan3UBWCNm9bIy1RCiPp14brFEXedC3duaUvikaEe/o4Bw == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvddtvddgvddvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfgjfhffhffvufgtsegrtd erreerredtnecuhfhrohhmpedflfhovghlucflrggtohgsshhonhdfuceojhhovghlsegt ohhmphhilhgvrhdrohhrgheqnecuggftrfgrthhtvghrnheptdeuudfftdefffejuddtte fhvdetheejheejieetgfduieffffefgffggedvheefnecuvehluhhsthgvrhfuihiivgep tdenucfrrghrrghmpehmrghilhhfrhhomhepjhhovghlsegtohhmphhilhgvrhdrohhrgh X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 9A55AA00079; Sun, 18 Apr 2021 16:42:31 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.5.0-alpha0-273-g8500d2492d-fm-20210323.002-g8500d249 Mime-Version: 1.0 Message-Id: <1eccf300-7d93-4355-9687-34b9ec57f3fc@www.fastmail.com> In-Reply-To: <140747.1618776847@sss.pgh.pa.us> References: <797aff54-b49b-4914-9ff9-aa42564a4d7d@www.fastmail.com> <140747.1618776847@sss.pgh.pa.us> Date: Sun, 18 Apr 2021 22:42:11 +0200 From: "Joel Jacobson" To: "Tom Lane" Cc: pgsql-hackers@lists.postgresql.org Subject: Re: Planning time grows exponentially with levels of nested views Content-Type: multipart/alternative; boundary=95499a06af354d3f95e1264e8fc6a2b1 X-Host-Lookup-Failed: Reverse DNS lookup failed for 66.111.4.26 (deferred) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --95499a06af354d3f95e1264e8fc6a2b1 Content-Type: text/plain On Sun, Apr 18, 2021, at 22:14, Tom Lane wrote: > "Joel Jacobson" > 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 --95499a06af354d3f95e1264e8fc6a2b1 Content-Type: text/html Content-Transfer-Encoding: quoted-printable
On Sun, Apr 18,= 2021, at 22:14, Tom Lane wrote:
"Joel Jacobson" <joel@compiler.org> writes:
> I assumed t= he 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 ini= tially has RTEs for v2 itself
plus v1.  When we flatt= en v1 into v2, v2 acquires the RTEs from v1,
namely v1 its= elf 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 s= tacking
makes for O(N^2) work overall just in manipulating= the rtable.

We can't get rid of these rtab= le entries altogether, since all of them
represent table p= rivilege checks that the executor will need to do.
It occu= rs to me though that we don't need the rte->subquery trees anymore
once those are flattened, so maybe we could do something lik= e 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.
Pl= anning time grows much slower now:

EXPLAIN = ANALYZE SELECT * FROM v64;
- Planning Time: 14.981 ms
<= /div>
+ Planning Time: 2.802 ms

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

EXP= LAIN ANALYZE SELECT * FROM v256;
- Planning Time: 1594.809= ms
+ Planning Time: 46.709 ms

Very nice.

/Joel
--95499a06af354d3f95e1264e8fc6a2b1--