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 1lsrjP-0000aN-DM for pgsql-performance@arkaria.postgresql.org; Mon, 14 Jun 2021 18:54:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lsrjO-0001pz-7T for pgsql-performance@arkaria.postgresql.org; Mon, 14 Jun 2021 18:54:42 +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 1lsqLK-00016T-BK for pgsql-performance@lists.postgresql.org; Mon, 14 Jun 2021 17:25:46 +0000 Received: from mail-qk1-x72e.google.com ([2607:f8b0:4864:20::72e]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lsqLI-0001UO-BU for pgsql-performance@lists.postgresql.org; Mon, 14 Jun 2021 17:25:46 +0000 Received: by mail-qk1-x72e.google.com with SMTP id 5so1156908qkc.8 for ; Mon, 14 Jun 2021 10:25:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=uKHg3KCyAisrRJHuCsz6zkEWd6/CdJDaBSqmjSrwXs4=; b=Lj3rRqKeXLoEI7m7vqFUe0tVvYvgFPJr8vo1L2rUI0CnBqUDDwgz5RrlSi1YHzq7D7 hS8TTPVu+VzJagZPANmHtYqwNKvasI+VpgQ8rnw7djstZrjSgwZ9JTD2XSCHiJKo5NRB fFkxHoLU6HrW5sAj0iBYfKid8hvJ1uwreEkPm1cEWk5wITjieEiXMYjZXcQ3bXYkyKFQ J3sG7XmjsqFHzZ+NWiTKkjVLCORDeQ559yUaqS+SaD/GuWuCFS7H3F4wREBJu/dFwI+w 7aqawC22qasz0nbrWGhIef40QW6BlG30+Vvqe0XXk/IS5jN8/QSK4vGKjfVbTF8qcbpL fR8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=uKHg3KCyAisrRJHuCsz6zkEWd6/CdJDaBSqmjSrwXs4=; b=HYkwfC3AlPhWRNdMbSdnquAWgSRAyxK7sesancQgNZDNDRg2LNiGZY1AT144adQg8o +qNH3gy++ilHQ3Ea7/gAvt6NhTqcg4MQ9+rHcwZv/zzyI8mEB+D+aV3pmwQ07v9WZo78 4HbvVVNv9J9fGq49HaCAshD+jhCerH1Ggy2sEBX8euTVseCk2v148YCblcOiLTDEZPXn 0diD+cc8UixrEpHER7b0ThyhZV7UOZX6/3bCGC29OI2DCo5+GVEZgvuteYlnGug4mfYc /AakwgKUXVPqiwlch7ZbZdPrUXzc07PO+m2d3/KjmStPrc0t6qsw0/MCD3ql8rakIX7V IwAQ== X-Gm-Message-State: AOAM532mAqGMWcTSohNkkPNe4waxA8oXiKQo8DTqeEdXIlps+25zp7ht DyHe9OUS/JcXeF5zta8TisQ= X-Google-Smtp-Source: ABdhPJy+2XFjZkVfrELjiSOu6qYllad2AX+hk9quSHlHDmY3ywbU2IxDDYE39KaBre32IkeIGz1NcA== X-Received: by 2002:a37:ac03:: with SMTP id e3mr17516171qkm.367.1623691542632; Mon, 14 Jun 2021 10:25:42 -0700 (PDT) Received: from ip-192-168-1-83.ec2.internal (190-21-45-1.baf.movistar.cl. [190.21.45.1]) by smtp.gmail.com with ESMTPSA id o63sm6863828qke.112.2021.06.14.10.25.41 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 14 Jun 2021 10:25:42 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.60.0.2.21\)) Subject: Re: Planning performance problem (67626.278ms) From: Manuel Weitzman In-Reply-To: <767433.1619015825@sss.pgh.pa.us> Date: Mon, 14 Jun 2021 13:25:38 -0400 Cc: David Rowley , Jeremy Schneider , "pgsql-performance@lists.postgresql.org" , Andres Freund Content-Transfer-Encoding: quoted-printable Message-Id: References: <6d58f68e-ed1a-c48e-1440-691554f6e1e7@9livesdata.com> <20190408144210.GG10080@telsasoft.com> <20190408232644.r6xixkeulsmomwfj@alap3.anarazel.de> <3f5e5673-06aa-bf7c-cdb7-0e053c45ba26@amazon.com> <767433.1619015825@sss.pgh.pa.us> To: Tom Lane X-Mailer: Apple Mail (2.3654.60.0.2.21) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > However, I'm skeptical that any problem actually remains in > real-world use cases. Hello Tom, We also had some issues with planning and get_actual_variable_range(). = We actually found some interesting behaviour that probably requires an eye = with better expertise in how the planner works. For the example being discussed you can add some joins into the equation = and planning times deteriorate quite a bit. I'll just skip posting the first executions as it is already established = that a subsequent one will be faster. create table b (b int primary key, a int references a(a)) with (autovacuum_enabled=3Doff); insert into a select x from generate_series(1,10000000) x; insert into b select x, x from generate_series(1,10000000) x; create index b_a_idx on b(a); analyze a, b; For our case a rollback of a bulk insert causes bloat on the index. begin; insert into a select x from generate_series(10000001,20000000) x; rollback; explain (analyze, buffers) select * from a join b on (b.a =3D a.a) where b.a in (1,100,10000,1000000,1000001); Planning: Buffers: shared hit=3D9 read=3D27329 Planning Time: 134.560 ms Execution Time: 0.100 ms I see a lot of buffers being read for some reason (wasn't this fixed?). = And times are slow too. But it get's worse with each join added to the = select. explain (analyze, buffers) select * from a join b b1 on (b1.a =3D a.a) join b b2 on (b2.a =3D a.a) where b1.a in (1,100,10000,1000000,1000001); Planning: Buffers: shared hit=3D38 read=3D81992 Planning Time: 312.826 ms Execution Time: 0.131 ms Just add a few more joins and it is a recipe for disaster. Apparently, the planner isn't reusing the data boundaries across = alternative plans. It would be nicer if the planner remembered each column = boundaries for later reuse (within the same planner execution). Another thing that worries me is that even the second run has faster = planning it is still way slower than the case without lots of bloat in the index. = And I don't think this is just an edge case. Rollbacks on bulk inserts can = be quite common, and joins are expected in a SQL database. We had downtime due to how the planner works on this case. Unfortunately setting more aggressive vacuum settings won't fix our problems. Most of = the read queries are being issued to a replica. When the issues with the = planner start happening, CPU usage on that node goes to 100% which interferes = with the replication process. This means the replica cannot get to a new checkpoint with a new live max value in the index nor can it delete the bloat that vacuum has = already cleaned on the leader server. Oh, by the way, we're running version 13.2 Regards, Manuel