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 1lYCdt-0000ak-1d for pgsql-general@arkaria.postgresql.org; Sun, 18 Apr 2021 18:59:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lYCdr-0001TC-Vy for pgsql-general@arkaria.postgresql.org; Sun, 18 Apr 2021 18:59:35 +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 1lYCdq-0001RP-An for pgsql-general@lists.postgresql.org; Sun, 18 Apr 2021 18:59:35 +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 1lYCdi-0004Sh-Fi for pgsql-general@postgresql.org; Sun, 18 Apr 2021 18:59:33 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 0F39E5C04D6 for ; Sun, 18 Apr 2021 14:59:15 -0400 (EDT) Received: from imap2 ([10.202.2.52]) by compute5.internal (MEProxy); Sun, 18 Apr 2021 14:59:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=compiler.org; h= mime-version:message-id:date:from:to:subject:content-type; s= fm1; bh=1ngeTlTsBhVE8QWgQt74zIZTWALvC2WKPqCw1m5+M1o=; b=eUvYMyZF qHZMVgNy6h1JBAtFpcBl5JAgFQacwCez0RNbz3qwU/XbgNP+LDrng42Rc4UdXYje HTr3kZqiK+Vi27bcBp3glUIXGJ7qY8/QFWQghOI3obLAR+P88pMCEI+E0QSa2PNy 4twpRbXHkmYvbYe4oNmXCoQilVkJi4H4YdcQVG+tfKjQUPe9Lf6hkJF8DGTHAeAX gKQcIhwJG4XeAtT5Lj06V9Ow6Y0MliUh1+AaeODlow8+TroCBlXIVY2fjbJLthxK MZyIzrW98niGon/QkWNflEjVNk1vqNTwbO229q2+Zr9sXUYzrXg7bqeRVNAUrfG5 0+NeyiQbzRv3cA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:message-id :mime-version:subject:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=1ngeTlTsBhVE8QWgQt74zIZTWALvC 2WKPqCw1m5+M1o=; b=Qj9KpgsZgzH+/D5loFDwT37aAPntu6uwOFmkhnm/EKeNT AaxYx9ifS4T978l1X7y59p7dWVBLWsd//+FsNyqTsexqRSdrNGlB2QnTkUlfX0Lb h9TTLkr7tDwbUJd9b3nW6WX5aeyBLA436114yLUU/WLSgwf6foQzIKGXzhpQoqIE rMl0AVy4Rl16jScFcN2LKjrP6Y2xmSKQNS3RWDSQ29aRqAqCNyYMhnF2tJ2ZPzuV TlpD03WNREKqasnDubgxMFFPfzFHXVTWL0AKJUigs7s1U5sT5hLe3GIef7gkAzxu +0yZcEWqW8Y7bnQ5QeNZsc1cYTIpHnGI7GoHDBwzg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvddtvddgtdefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfffhffvufgtsegrtderre erredtnecuhfhrohhmpedflfhovghlucflrggtohgsshhonhdfuceojhhovghlsegtohhm phhilhgvrhdrohhrgheqnecuggftrfgrthhtvghrnhepgefgvdfhgfefjedtleegteejje fhieegjedvhfehueetfeektdeiieefheffkeelnecuvehluhhsthgvrhfuihiivgeptden ucfrrghrrghmpehmrghilhhfrhhomhepjhhovghlsegtohhmphhilhgvrhdrohhrgh X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 48BA9A00079; Sun, 18 Apr 2021 14:59:14 -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: <797aff54-b49b-4914-9ff9-aa42564a4d7d@www.fastmail.com> Date: Sun, 18 Apr 2021 20:58:53 +0200 From: "Joel Jacobson" To: pgsql-general@postgresql.org Subject: Planning time grows exponentially with levels of nested views Content-Type: multipart/alternative; boundary=43ae6dc02bd2434f83db0967a969f2f5 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 --43ae6dc02bd2434f83db0967a969f2f5 Content-Type: text/plain Hi, I assumed the cost for each nested VIEW layer would grow linear, but my testing shows it appears to grow exponentially: CREATE TABLE foo (bar int); INSERT INTO foo (bar) VALUES (123); DO $_$ DECLARE BEGIN CREATE OR REPLACE VIEW v1 AS SELECT * FROM foo; FOR i IN 1..256 LOOP EXECUTE format ( $$ CREATE OR REPLACE VIEW v%s AS SELECT * FROM v%s $$, i+1, i ); END LOOP; END $_$; EXPLAIN ANALYZE SELECT * FROM foo; QUERY PLAN ------------------------------------------------------------------------------------------------- Seq Scan on foo (cost=0.00..35.50 rows=2550 width=4) (actual time=0.004..0.004 rows=1 loops=1) Planning Time: 0.117 ms Execution Time: 0.011 ms (3 rows) EXPLAIN ANALYZE SELECT * FROM v1; QUERY PLAN ------------------------------------------------------------------------------------------------- Seq Scan on foo (cost=0.00..35.50 rows=2550 width=4) (actual time=0.002..0.003 rows=1 loops=1) Planning Time: 0.019 ms Execution Time: 0.015 ms (3 rows) EXPLAIN ANALYZE SELECT * FROM v2; QUERY PLAN ------------------------------------------------------------------------------------------------- Seq Scan on foo (cost=0.00..35.50 rows=2550 width=4) (actual time=0.002..0.002 rows=1 loops=1) Planning Time: 0.018 ms Execution Time: 0.011 ms (3 rows) EXPLAIN ANALYZE SELECT * FROM v4; QUERY PLAN ------------------------------------------------------------------------------------------------- Seq Scan on foo (cost=0.00..35.50 rows=2550 width=4) (actual time=0.002..0.002 rows=1 loops=1) Planning Time: 0.030 ms Execution Time: 0.013 ms (3 rows) EXPLAIN ANALYZE SELECT * FROM v8; QUERY PLAN ------------------------------------------------------------------------------------------------- Seq Scan on foo (cost=0.00..35.50 rows=2550 width=4) (actual time=0.002..0.002 rows=1 loops=1) Planning Time: 0.061 ms Execution Time: 0.016 ms (3 rows) EXPLAIN ANALYZE SELECT * FROM v16; QUERY PLAN ------------------------------------------------------------------------------------------------- Seq Scan on foo (cost=0.00..35.50 rows=2550 width=4) (actual time=0.002..0.003 rows=1 loops=1) Planning Time: 0.347 ms Execution Time: 0.027 ms (3 rows) EXPLAIN ANALYZE SELECT * FROM v32; QUERY PLAN ------------------------------------------------------------------------------------------------- Seq Scan on foo (cost=0.00..35.50 rows=2550 width=4) (actual time=0.002..0.003 rows=1 loops=1) Planning Time: 2.096 ms Execution Time: 0.044 ms (3 rows) EXPLAIN ANALYZE SELECT * FROM v64; QUERY PLAN ------------------------------------------------------------------------------------------------- Seq Scan on foo (cost=0.00..35.50 rows=2550 width=4) (actual time=0.004..0.005 rows=1 loops=1) Planning Time: 14.981 ms Execution Time: 0.119 ms (3 rows) EXPLAIN ANALYZE SELECT * FROM v128; QUERY PLAN ------------------------------------------------------------------------------------------------- Seq Scan on foo (cost=0.00..35.50 rows=2550 width=4) (actual time=0.004..0.004 rows=1 loops=1) Planning Time: 109.407 ms Execution Time: 0.187 ms (3 rows) EXPLAIN ANALYZE SELECT * FROM v256; QUERY PLAN ------------------------------------------------------------------------------------------------- Seq Scan on foo (cost=0.00..35.50 rows=2550 width=4) (actual time=0.006..0.007 rows=1 loops=1) Planning Time: 1594.809 ms Execution Time: 0.531 ms (3 rows) --43ae6dc02bd2434f83db0967a969f2f5 Content-Type: text/html Content-Transfer-Encoding: quoted-printable
Hi,

I assumed the cost for each nested VIEW layer would gr= ow linear,
but my testing shows it appears to grow exponen= tially:

CREATE TABLE foo (bar int);
INSERT INTO foo (bar) VALUES (123);

= DO $_$
DECLARE
BEGIN
CREATE OR= REPLACE VIEW v1 AS SELECT * FROM foo;
FOR i IN 1..256 LOO= P
  EXECUTE format
  (
    $$
      CR= EATE OR REPLACE VIEW v%s AS
     = SELECT * FROM v%s
    $$,
&n= bsp;   i+1,
    i
&= nbsp; );
END LOOP;
END
$_$;

EXPLAIN ANALYZE SELECT * FROM foo;
<= div>           &n= bsp;           &n= bsp;           &n= bsp;       QUERY PLAN
------= ------------------------------------------------------------------------= -------------------
Seq Scan on foo  (cost=3D0.00..35= .50 rows=3D2550 width=3D4) (actual time=3D0.004..0.004 rows=3D1 loops=3D= 1)
Planning Time: 0.117 ms
Execution Time: 0= .011 ms
(3 rows)

EXPLAIN ANAL= YZE SELECT * FROM v1;
      =             =             =             = QUERY PLAN
----------------------------------------------= ---------------------------------------------------
Seq Sc= an on foo  (cost=3D0.00..35.50 rows=3D2550 width=3D4) (actual time=3D= 0.002..0.003 rows=3D1 loops=3D1)
Planning Time: 0.019 ms
Execution Time: 0.015 ms
(3 rows)

EXPLAIN ANALYZE SELECT * FROM v2;
 =             =             =             =       QUERY PLAN
----------------= ------------------------------------------------------------------------= ---------
Seq Scan on foo  (cost=3D0.00..35.50 rows=3D= 2550 width=3D4) (actual time=3D0.002..0.002 rows=3D1 loops=3D1)
Planning Time: 0.018 ms
Execution Time: 0.011 ms
=
(3 rows)

EXPLAIN ANALYZE SELECT = * FROM v4;
        = ;            = ;            = ;           QUERY PLAN=
---------------------------------------------------------= ----------------------------------------
Seq Scan on foo&n= bsp; (cost=3D0.00..35.50 rows=3D2550 width=3D4) (actual time=3D0.002..0.= 002 rows=3D1 loops=3D1)
Planning Time: 0.030 ms
<= div>Execution Time: 0.013 ms
(3 rows)

EXPLAIN ANALYZE SELECT * FROM v8;
  &nb= sp;           &nb= sp;           &nb= sp;           &nb= sp;    QUERY PLAN
-------------------------= ------------------------------------------------------------------------=
Seq Scan on foo  (cost=3D0.00..35.50 rows=3D2550 wid= th=3D4) (actual time=3D0.002..0.002 rows=3D1 loops=3D1)
Pl= anning Time: 0.061 ms
Execution Time: 0.016 ms
(3 rows)

EXPLAIN ANALYZE SELECT * FROM v= 16;
         =             =             =           QUERY PLAN
----------------------------------------------------------------= ---------------------------------
Seq Scan on foo  (c= ost=3D0.00..35.50 rows=3D2550 width=3D4) (actual time=3D0.002..0.003 row= s=3D1 loops=3D1)
Planning Time: 0.347 ms
Exe= cution Time: 0.027 ms
(3 rows)

EXPLAIN ANALYZE SELECT * FROM v32;
   &nb= sp;           &nb= sp;           &nb= sp;           &nb= sp;   QUERY PLAN
-------------------------------= ------------------------------------------------------------------
Seq Scan on foo  (cost=3D0.00..35.50 rows=3D2550 width=3D4= ) (actual time=3D0.002..0.003 rows=3D1 loops=3D1)
Planning= Time: 2.096 ms
Execution Time: 0.044 ms
(3 = rows)

EXPLAIN ANALYZE SELECT * FROM v64;
          =             =             =          QUERY PLAN
----------------------------------------------------------------------= ---------------------------
Seq Scan on foo  (cost=3D= 0.00..35.50 rows=3D2550 width=3D4) (actual time=3D0.004..0.005 rows=3D1 = loops=3D1)
Planning Time: 14.981 ms
Executio= n Time: 0.119 ms
(3 rows)

EXP= LAIN ANALYZE SELECT * FROM v128;
    &= nbsp;           &= nbsp;           &= nbsp;           &= nbsp;  QUERY PLAN
-----------------------------------= --------------------------------------------------------------
=
Seq Scan on foo  (cost=3D0.00..35.50 rows=3D2550 width=3D4) (a= ctual time=3D0.004..0.004 rows=3D1 loops=3D1)
Planning Tim= e: 109.407 ms
Execution Time: 0.187 ms
(3 ro= ws)

EXPLAIN ANALYZE SELECT * FROM v256;
=
          &= nbsp;           &= nbsp;           &= nbsp;        QUERY PLAN
-----------------------------------------------------------------------= --------------------------
Seq Scan on foo  (cost=3D0= .00..35.50 rows=3D2550 width=3D4) (actual time=3D0.006..0.007 rows=3D1 l= oops=3D1)
Planning Time: 1594.809 ms
Executi= on Time: 0.531 ms
(3 rows)
--43ae6dc02bd2434f83db0967a969f2f5--