Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1heHzq-0004Rv-RI for pgsql-general@arkaria.postgresql.org; Fri, 21 Jun 2019 11:46:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1heHzo-0002PY-Ma for pgsql-general@arkaria.postgresql.org; Fri, 21 Jun 2019 11:46:20 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1heHzo-0002PR-BZ for pgsql-general@lists.postgresql.org; Fri, 21 Jun 2019 11:46:20 +0000 Received: from smtp03.optiver.com ([195.246.114.70]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1heHzh-0007Ke-6I for pgsql-general@postgresql.org; Fri, 21 Jun 2019 11:46:19 +0000 Received: from pps.filterd (smtp03.optiver.com [127.0.0.1]) by smtp03.optiver.com (8.16.0.27/8.16.0.27) with SMTP id x5LBf2jY016408 for ; Fri, 21 Jun 2019 13:46:11 +0200 Received: from opammb0561.comp.optiver.com (opammb0561.comp.optiver.com [10.131.160.1]) by smtp03.optiver.com with ESMTP id 2t4p94kvqd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Fri, 21 Jun 2019 13:46:11 +0200 Received: from opammb0561.comp.optiver.com (10.131.160.1) by opammb0561.comp.optiver.com (10.131.160.1) with Microsoft SMTP Server (TLS) id 15.0.1263.5; Fri, 21 Jun 2019 13:46:11 +0200 Received: from opammb0561.comp.optiver.com ([fe80::4c35:85e9:1255:ee9f]) by opammb0561.comp.optiver.com ([fe80::4c35:85e9:1255:ee9f%17]) with mapi id 15.00.1263.000; Fri, 21 Jun 2019 13:46:11 +0200 From: Floris Van Nee To: "pgsql-general@postgresql.org" Subject: 'too many range table entries' error with partitioned tables and aggregations Thread-Topic: 'too many range table entries' error with partitioned tables and aggregations Thread-Index: AQHVKCW+r7jSrKkCpkiT2j5QWEQqGg== Date: Fri, 21 Jun 2019 11:46:10 +0000 Message-ID: <1561117570484.53082@Optiver.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.131.160.182] x-tm-as-product-ver: SMEX-11.0.0.1284-8.100.1062-24676.002 x-tm-as-result: No--7.159400-0.000000-31 x-tm-as-user-approved-sender: Yes x-tm-as-user-blocked-sender: No Content-Type: multipart/alternative; boundary="_000_156111757048453082Optivercom_" MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-06-21_08:,, signatures=0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --_000_156111757048453082Optivercom_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi all, Recently we hit a strange 'too many range table entries' error for some par= ticular queries in our database. Some googling revealed that it's an uncomm= on error usually occurring due to strange database design [1]. However, I f= eel like the case in which it occurs this time, although uncommon, doesn't = look that weird in itself. I've produced a simple example that reproduces o= n both PG11 and PG12 for me. It involves creating a partitioned table and t= hen selecting from it from within a subquery and a lot of aggregations. Note that this is just a simple minimal example that triggers it. We create= one regular table and one partitioned table with 1024 partitions. A query = that a) selects from the regular table b) lateral joins the partitioned table to look up just one single row+colum= n, referencing some column from the first table and doing this inside some = subplan (coalesce/ case when/ etc.) c) does a lot of aggregations - in this simple case it just aggregated the = same column multiple times, but imagine this is a table with a lot of diffe= rent columns that we want to sum/avg etc. In this case, the planner generates a plan with one SubPlan per aggregate (= see below for EXPLAIN output). Each SubPlan initially has to create a range= table entry for every partition. So if we do 10 aggregations with 1024 par= titions, we get 10240 entries in our range table. You can imagine it's pret= ty easy to hit the 65k limit on range table entries like this. Of course, a= ll but one get pruned away due to partition pruning, but this happens too l= ate for clauses such as now() as in this example. For PG11, this error even= occurs when pruning for static timestamps, but on PG12 the particular case= for static timestamps is fixed due to the recent work on partition pruning= at planning time. I was wondering if developers here are aware of this issue. In the previous= threads that I read about this issue, it always looked like a far-fetched = scenario that was very difficult to hit. This one (although still not trivi= al?) seems to be potentially more common. In any case it looks inefficient = to me that SubPlans are chosen here if they lead to so many references in t= he range table? I understand a lot of this code probably comes from a time = before partitioning, where it's nearly impossible to hit these kind of issu= es, but we might see more of this in the future. I'm not really looking for a particular solution to my problem here (we can= rewrite our queries to just avoid these SubPlans for now, so that's not a = problem). I just wanted to share my findings to see if the community indeed= finds it strange that examples like this yield errors and if they should b= e fixed, or if users should work around them on their own by rewriting thei= r queries not to use SubPlans for partitioned tables. -- create example tables. p1 as a regular table, q1 as a partitioned table = with 1024 partitions drop table if exists p1; create table p1 ( ts timestamptz, p int ); drop table if exists q1; create table q1 ( ts timestamptz, q int ) partition by range(ts); do $an$ DECLARE i int; begin for i in select * from generate_series(1, 1024) loop execute format($f$ create table q1_%s partition of q1 for values from= ('%s') to ('%s') $f$, i, '2015-04-04'::date - i, '2015-04-04'::date - i + = 1); end loop; end; $an$; -- this yields the error. removing one aggregation runs fine explain select sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), su= m(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), su= m(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), su= m(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), su= m(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), su= m(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), su= m(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), su= m(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), su= m(p*q) from p1 cross join lateral ( select coalesce((select q from q1 where ts =3D now() and ts > p1.ts limi= t 1), 1) q ) _ ; Example EXPLAIN output if we just do 2 sum(p*q) aggregations instead of 64 = aggregations: Aggregate (cost=3D62223.20..62223.21 rows=3D1 width=3D16) -> Seq Scan on p1 (cost=3D0.00..30.40 rows=3D2040 width=3D12) SubPlan 2 -> Limit (cost=3D0.00..15.24 rows=3D1 width=3D4) -> Append (cost=3D0.00..46812.16 rows=3D3072 width=3D4) Subplans Removed: 1023 -> Seq Scan on q1_1024 q1_1024_1 (cost=3D0.00..45.70 rows= =3D3 width=3D4) Filter: ((ts > p1.ts) AND (ts =3D now())) SubPlan 1 -> Limit (cost=3D0.00..15.24 rows=3D1 width=3D4) -> Append (cost=3D0.00..46812.16 rows=3D3072 width=3D4) Subplans Removed: 1023 -> Seq Scan on q1_1024 (cost=3D0.00..45.70 rows=3D3 width= =3D4) Filter: ((ts > p1.ts) AND (ts =3D now())) -Floris [1] https://www.postgresql.org/message-id/flat/20180625181410.fg5xejq4qsbnr= yl2%40alap3.anarazel.de#5756b3df32a3cc6184baba5eb9dd4174? --_000_156111757048453082Optivercom_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable

Hi all,


Recently we hit a strange 'too many range table entries' error for = some particular queries in our database. Some googling revealed that it's a= n uncommon error usually occurring due to strange database design [1].= However, I feel like the case in which it occurs this time, although uncommon, doesn't look that weird in itsel= f. I've produced a simple example that reproduces on both PG11 and PG12 for= me. It involves creating a partitioned table and then selecting from it fr= om within a subquery and a lot of aggregations.


Note that this is just a simple minimal example that triggers it. W= e create one regular table and one partitioned table with 1024 partitions. = A query that

a) selects from the regular table

b) lateral joins the partitioned table to look up just one single row= 3;column, referencing some column from the first table and doing this insid= e some subplan (coalesce/ case when/ etc.)

c) does a lot of aggregations - in this simple case it just aggregated t= he same column multiple times, but imagine this is a table with a lot of di= fferent columns that we want to sum/avg etc.


In this case, the planner generates a plan with one SubPlan per aggregat= e (see below for EXPLAIN output). Each SubPlan initially has to create a ra= nge table entry for every partition. So if we do 10 aggregations with 1024 = partitions, we get 10240 entries in our range table. You can imagine it's pretty easy to hit the 65k limit = on range table entries like this. Of course, all but one get pruned aw= ay due to partition pruning, but this happens too late for clauses such as = now() as in this example. For PG11, this error even occurs when pruning for static timestamps, but on PG12 the part= icular case for static timestamps is fixed due to the recent = ;work on partition pruning at planning time.


I was wondering if developers here are aware of this issue. In the previ= ous threads that I read about this issue, it always looked like a far-fetch= ed scenario that was very difficult to hit. This one (although still not tr= ivial?) seems to be potentially more common. In any case it looks inefficient to me that SubPlans are= chosen here if they lead to so many references in the range tabl= e? I understand a lot of this code probably comes from a time before partit= ioning, where it's nearly impossible to hit these kind of issues, but we might see more of this in the future.


I'm not really looking for a particular solution to my problem here (we = can rewrite our queries to just avoid these SubPlans for now, so that's not= a problem). I just wanted to share my findings to see if the community ind= eed finds it strange that examples like this yield errors and if they should be fixed, or if users should wor= k around them on their own by rewriting their queries not to use SubPlans f= or partitioned tables.



-- create example tables. p1 as a regular table, q1 as a partit= ioned table with 1024 partitions
drop table if exists p1;
create table p1 (
   ts timestamptz,
   p int
);

drop table if exists q1;
create table q1 (
   ts timestamptz,
   q int
) partition by range(ts);

do $an$
DECLARE
   i int;
begin
   for i in select * from generate_series(1, 1024) lo= op
      execute format($f$ create table q1_%s part= ition of q1 for values from ('%s') to ('%s') $f$, i, '2015-04-04'::date - i= , '2015-04-04'::date - i + 1);
   end loop;
end;
$an$;

-- this yields the error. removing one aggregation runs fine
explain
select
   sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), = sum(p*q), sum(p*q), sum(p*q),
   sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), = sum(p*q), sum(p*q), sum(p*q),
   sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), = sum(p*q), sum(p*q), sum(p*q),
   sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), = sum(p*q), sum(p*q), sum(p*q),
   sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), = sum(p*q), sum(p*q), sum(p*q),
   sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), = sum(p*q), sum(p*q), sum(p*q),
   sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), = sum(p*q), sum(p*q), sum(p*q),
   sum(p*q), sum(p*q), sum(p*q), sum(p*q), sum(p*q), = sum(p*q), sum(p*q), sum(p*q)
from p1
cross join lateral (
   select coalesce((select q from q1 where ts =3D now= () and ts > p1.ts limit 1), 1) q
) _
;

Example EXPLAIN output if we just do 2 sum(p*q) aggregations inst= ead of 64 aggregations:

Aggre= gate  (cost=3D62223.20..62223.21 rows=3D1 width=3D16)
 = ; ->  Seq Scan on p1  (cost=3D0.00..30.40 rows=3D2040 width=3D= 12)
 = ; SubPlan 2
 = ;   ->  Limit  (cost=3D0.00..15.24 rows=3D1 width=3D4)
 = ;         ->  Append  (cost=3D0.00..46812.= 16 rows=3D3072 width=3D4)
 = ;               Subplans Removed: 1023
 = ;               ->  Seq Scan on = q1_1024 q1_1024_1  (cost=3D0.00..45.70 rows=3D3 width=3D4)
 = ;                     Fil= ter: ((ts > p1.ts) AND (ts =3D now()))
 = ; SubPlan 1
 = ;   ->  Limit  (cost=3D0.00..15.24 rows=3D1 width=3D4)
 = ;         ->  Append  (cost=3D0.00..46812.= 16 rows=3D3072 width=3D4)
 = ;               Subplans Removed: 1023
 = ;               ->  Seq Scan on = q1_1024  (cost=3D0.00..45.70 rows=3D3 width=3D4)
 = ;                     Fil= ter: ((ts > p1.ts) AND (ts =3D now()))


-Floris


[1] https:= //www.postgresql.org/message-id/flat/20180625181410.fg5xejq4qsbnryl2%40alap= 3.anarazel.de#5756b3df32a3cc6184baba5eb9dd4174​



--_000_156111757048453082Optivercom_--