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 1osj0e-00082g-9H for pgsql-general@arkaria.postgresql.org; Wed, 09 Nov 2022 11:12:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1osize-00058I-Ho for pgsql-general@arkaria.postgresql.org; Wed, 09 Nov 2022 11:11: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 1osize-000588-1I for pgsql-general@lists.postgresql.org; Wed, 09 Nov 2022 11:11:42 +0000 Received: from smtp59.i.mail.ru ([217.69.128.39]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1osizb-00049y-0x for pgsql-general@lists.postgresql.org; Wed, 09 Nov 2022 11:11:41 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=aeronavigator.ru; s=mailru; h=Content-Type:MIME-Version:Message-ID:Date:Subject:To:From:From:Subject:Content-Type:Content-Transfer-Encoding:To:Cc; bh=7zQAmY9IM8Do5PgB4otWLvh/8biBMHsywd6c3UCXODs=; t=1667992299;x=1668082299; b=kr9XYLdmi5U8iMCzQYzbEhdP2hhXyT+SQk4iS28RBd845APaRhv0VCzdZQyCKTmFZfe1Ez87lnpnLn/QWwXF8p1bmpxGsLmnCg9EDzJdpsosoGNuVNbWY2Ho/Ug7cOX2wlDamdvd6XWZNEHfrpur4TYJq84jkBFymtD85xBIaiQ=; Received: by smtp59.i.mail.ru with esmtpa (envelope-from ) id 1osizY-0003TD-K0 for pgsql-general@lists.postgresql.org; Wed, 09 Nov 2022 14:11:37 +0300 From: To: Subject: Unnecessary locks for partitioned tables Date: Wed, 9 Nov 2022 14:11:33 +0300 Message-ID: <012801d8f42c$06ea5af0$14bf10d0$@aeronavigator.ru> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0129_01D8F445.2C3792F0" X-Mailer: Microsoft Outlook 16.0 Thread-Index: Adj0KQzI/UYrRk7KRRO6A0aVBLdsxg== Content-Language: ru Authentication-Results: smtp59.i.mail.ru; auth=pass smtp.auth=n.kobzarev@aeronavigator.ru smtp.mailfrom=n.kobzarev@aeronavigator.ru X-Mailru-Src: smtp X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD967121363E257EE6D4D23F43E228E2190C0E39DE6E66E41E700894C459B0CD1B927018D7926C6525F6B2F3E250D8A3623D8962E2AB50516B467236096E73D0E14 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE771540F9ECFC94C4BEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637103BD9D8542B9C568638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D820C152FB2903ACF4BBC890FF1A4B2A5A117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCECADA55FE5B58BB7A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352091ADC097FE2C3A08E5D25F19253116ADD2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EED58E677C372A3F474DC33E588678F033D8FC6C240DEA7642DBF02ECDB25306B2B78CF848AE20165D0A6AB1C7CE11FEE3DF046C1992EC065E302FCEF25BFAB345C4224003CC836476E2F48590F00D11D6E2021AF6380DFAD1A18204E546F3947CB11811A4A51E3B096D1867E19FE1407959CC434672EE6371089D37D7C0E48F6C8AA50765F7900637C69D4C8FA58DAC3AEFF80C71ABB335746BA297DBC24807EABDAD6C7F3747799A X-C1DE0DAB: 0D63561A33F958A516B8585683F99B54CE6947EAFF8B0A9DD433E0268C4C601C4EAF44D9B582CE87C8A4C02DF684249CC203C45FEA855C8F X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D349F4E6BC39AAD02BBB25317D6B3F09CE76555E0DC34EBA2D44FF6F7AA43CFC5CD9E127CDC9FB297C11D7E09C32AA3244CD60A40AF4DFE2D39AEB4495B4360820FB038C9161EF167A13EB3F6AD6EA9203E X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojbL9S8ysBdXg+BgY9oyzMpKh+L31MnPJK X-Mailru-Sender: 657AADBC28A10CF51E40C46096C03D8F389DE82D53ECF36F2D9A31599372A4D0F8C75CEE1205CA859215EFA742CC5EB292D99EB8CC7091A74C2ED15A5AB7F0073B9C7E7614533D2A011BE29E39D2FEA10D4ABDE8C577C2ED X-Mras: Ok List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multipart message in MIME format. ------=_NextPart_000_0129_01D8F445.2C3792F0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Hello! Recently I`ve been pushing into life a new project and immediately experienced an Out of shared memory error while querying partitioned tables. Imagine a huge busy table that you want to split into hundreds of partitions by list. Partition key is a kind of surrogate key that can be calculated outside of database and can be provided as parameter. create table t (a int) partition by list (a); select format('create table %I partition of t for values in (%s)', 'p'||x, x) from generate_series(0,20) x Query is executed inside stored procedure or as a prepared statement by, let's say, Spring JPA. prepare s (int) as select * from t where a = $1; explain (analyze) execute s(0); At the beginning database (12,13,14, or 15 versions) will try to create a custom execution plan like this one: Query Text: prepare s (int) as select * from t where a = $1; Seq Scan on p0 t (cost=0.00..41.88 rows=13 width=4) (actual time=0.011..0.011 rows=0 loops=1) Filter: (a = 0) We have only one partition examined. But suddenly database decides to create more generic query plan to deal with all range of partitions. begin; set local plan_cache_mode = force_generic_plan; Now all the queries looks like: Query Text: prepare s (int) as select * from t where a = $1; Append (cost=0.00..880.74 rows=273 width=4) (actual time=0.014..0.015 rows=0 loops=1) Subplans Removed: 20 -> Seq Scan on p0 t_1 (cost=0.00..41.88 rows=13 width=4) (actual time=0.013..0.013 rows=0 loops=1) Filter: (a = $1) Here the most important part is a count of removed plans. Database realizes that all partitions can be skipped except interested one. But under load we start receiving errors: ERROR: out of shared memory Hint: You might need to increase max_locks_per_transaction. Ok, let`s increase max_locks_per_transaction, but why this type of query produces so much locks? Looks like DB issues locks for all the partitioned objects involved in query and ONLY AFTER THAT it does partition pruning. Here are locks : select relation::regclass, mode from pg_locks where pid = pg_backend_pid() and locktype = 'relation' and relation::regclass <> 'pg_locks'::regclass; "p14" "AccessShareLock" "p20" "AccessShareLock" "p17" "AccessShareLock" "pg_type_oid_index" "AccessShareLock" "p19" "AccessShareLock" ....... etc But for real life there are also indexes, keys, up to 5000 locks in my case for single select. Setting plan_cache_mode = force_custom_plan resolves issue and also makes DB more performant (*5 in my case, literally from 140 to 650 executions per second on my development pc). I`m calling to PG authors, is there any way to optimize prepared queries and minimize unnecessary locks in generic query plan? For instance, lock parent table, eliminate unnecessary partitions, lock remining objects. This may help much in any place where partition pruning possible. Meanwhile I`m using force_custom_plan - no more huge peaks, all queries are at the lowest seen execution time. As a drawback there must be an overhead to compile plans every time. Thanks, Nikolay ------=_NextPart_000_0129_01D8F445.2C3792F0 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

Hello!

 

Recently = I`ve been pushing into life a new project and immediately experienced an = Out of shared memory error while querying partitioned = tables.

 

Imagine a huge busy table that you want to split into = hundreds of partitions by list. Partition key is a kind of surrogate key = that can be calculated outside of database and can be provided as = parameter.

 

create table t (a int) partition by list = (a);

 

select format('create table %I partition of t for = values in (%s)', 'p'||x, x)

from = generate_series(0,20) x

 

Query is = executed inside stored procedure or as a prepared statement by, = let’s say, Spring JPA.

 

prepare s = (int) as select * from t where a =3D $1;

explain (analyze) execute s(0);

 

At the = beginning database (12,13,14, or 15 versions) will try to create a = custom execution plan like this one:

 

Query Text: = prepare s (int) as select * from t where a =3D $1;

Seq Scan on p0 t  (cost=3D0.00..41.88 rows=3D13 = width=3D4) (actual time=3D0.011..0.011 rows=3D0 = loops=3D1)

  Filter: (a =3D = 0)

         &= nbsp;           &n= bsp;           &nb= sp;           =

We have only one partition examined. = But suddenly database decides to create more generic query plan to deal = with all range of partitions.

 

begin;

set local = plan_cache_mode =3D force_generic_plan;

 

Now all the = queries looks like:

 

Query Text: = prepare s (int) as select * from t where a =3D $1;

Append  (cost=3D0.00..880.74 rows=3D273 = width=3D4) (actual time=3D0.014..0.015 rows=3D0 = loops=3D1)

  Subplans Removed: = 20

  ->  Seq Scan on p0 = t_1  (cost=3D0.00..41.88 rows=3D13 width=3D4) (actual = time=3D0.013..0.013 rows=3D0 loops=3D1)

        Filter: (a = =3D $1)

         &= nbsp;           &n= bsp;           &nb= sp;           &nbs= p;            = ;   

Here the most = important part is a count of removed plans. Database realizes that all = partitions can be skipped except interested one. But under load we start = receiving errors:

 

ERROR: out = of shared memory

  Hint: You = might need to increase max_locks_per_transaction.

 

Ok, let`s = increase max_locks_per_transaction, but why this type of query produces = so much locks? Looks like DB issues locks for all the partitioned = objects involved in query and ONLY AFTER THAT it does partition = pruning.

 

Here are locks :

 

select = relation::regclass, mode

from   pg_locks

where  pid =3D pg_backend_pid() = and

       locktype =3D = 'relation' and relation::regclass <> = 'pg_locks'::regclass;

         &= nbsp;       

 

"p14"     = "AccessShareLock"

"p20"     = "AccessShareLock"

"p17"     = "AccessShareLock"

"pg_type_oid_index"    &n= bsp;  "AccessShareLock"

"p19"     = "AccessShareLock"

....... = etc

 

But for real life there are also indexes, keys, up to = 5000 locks in my case for single select.

 

Setting = plan_cache_mode =3D force_custom_plan resolves issue and also makes DB = more performant (*5 in my case, literally from 140 to 650 executions per = second on my development pc).

 

I`m calling = to PG authors, is there any way to optimize prepared queries and = minimize unnecessary locks in generic query plan? For instance, lock = parent table, eliminate unnecessary partitions, lock remining objects. = This may help much in any place where partition pruning = possible.

 

Meanwhile I`m using force_custom_plan - no more huge = peaks, all queries are at the lowest seen execution time. As a drawback = there must be an overhead to compile plans every time.

 

Thanks,

Nikolay

------=_NextPart_000_0129_01D8F445.2C3792F0--