Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 625EE9FB2F0 for ; Fri, 28 Sep 2007 14:53:41 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 01019-08 for ; Fri, 28 Sep 2007 14:50:39 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from bay0-omc2-s41.bay0.hotmail.com (bay0-omc2-s41.bay0.hotmail.com [65.54.246.177]) by postgresql.org (Postfix) with ESMTP id 19E289FB2F3 for ; Fri, 28 Sep 2007 14:50:37 -0300 (ADT) Received: from BAY126-W60 ([65.55.131.95]) by bay0-omc2-s41.bay0.hotmail.com with Microsoft SMTPSVC(6.0.3790.3959); Fri, 28 Sep 2007 10:50:32 -0700 Message-ID: Content-Type: multipart/alternative; boundary="_bcb8c679-43a3-43a5-aa41-6831d98c6f01_" X-Originating-IP: [64.235.97.59] From: Arctic Toucan To: Subject: OOM Errors as a result of table inheritance and a bad plan(?) Date: Fri, 28 Sep 2007 13:50:32 -0400 Importance: Normal MIME-Version: 1.0 X-OriginalArrivalTime: 28 Sep 2007 17:50:32.0732 (UTC) FILETIME=[10E995C0:01C801F8] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200709/472 X-Sequence-Number: 27017 --_bcb8c679-43a3-43a5-aa41-6831d98c6f01_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable In keeping with some of the recent threads regarding the planner... =20 I have a fair sized data warehouse in which I am trying to perform an aggre= gation, but getting OOM errors in Postgres(8.2.4). I believe the reason for the OOM is that Postgres is attempting to do a has= h aggregation, but it has grossly underestimated the rows resulting from th= e aggregation. The data in the database is very uniformly distributed so I don't believe t= hat the table stats are the cause of the problem.=20 This may be related to table inheritance, and can be demonstrated pretty ea= sily. =20 CREATE TABLE foo(a INT);ANALYZE foo; CREATE TABLE foo_1() INHERITS(foo);insert into foo_1 select generate_serie= s(1,100000);insert into foo_1 select generate_series(1,100000);insert into= foo_1 select generate_series(1,100000);ANALYZE foo_1; CREATE TABLE foo_2() INHERITS(foo);insert into foo_2 select generate_serie= s(1,100000);insert into foo_2 select generate_series(1,100000);insert into= foo_2 select generate_series(1,100000);ANALYZE foo_2; -- If I query a particular partition, the plan estimate for the hash aggreg= ate is good EXPLAIN ANALYZE SELECT a,COUNT(*) from foo_1 group by a; HashAggregate (cost=3D5822.00..7061.01 rows=3D99121 width=3D4) (actual ti= me=3D554.556..657.121 rows=3D100000 loops=3D1) -> Seq Scan on foo_1 (co= st=3D0.00..4322.00 rows=3D300000 width=3D4) (actual time=3D0.014..203.290 r= ows=3D300000 loops=3D1) Total runtime: 712.211 ms -- If I query the base table, the plan estimate for the hash aggregate is o= ff by several orders of magnitude EXPLAIN ANALYZE SELECT a,COUNT(*) from foo group by a; HashAggregate (cost=3D11686.10..11688.60 rows=3D200 width=3D4) (actual tim= e=3D1724.188..1826.630 rows=3D100000 loops=3D1) -> Append (cost=3D0.00.= .8675.40 rows=3D602140 width=3D4) (actual time=3D0.016..1045.134 rows=3D600= 000 loops=3D1) -> Seq Scan on foo (cost=3D0.00..31.40 rows=3D2140= width=3D4) (actual time=3D0.001..0.001 rows=3D0 loops=3D1) -> Seq= Scan on foo_1 foo (cost=3D0.00..4322.00 rows=3D300000 width=3D4) (actual = time=3D0.012..205.130 rows=3D300000 loops=3D1) -> Seq Scan on foo_= 2 foo (cost=3D0.00..4322.00 rows=3D300000 width=3D4) (actual time=3D0.011.= .203.542 rows=3D300000 loops=3D1) Total runtime: 1879.550 ms(6 rows) -- Is there something magical about the hash aggregate estimate of 200 rows= ? -- I can have 30,000 or 300,000 rows in each child partition table and mult= iple partition's with different values of "a" and yet it always come up wit= h 200. -- eg. create table foo_3() inherits(foo);insert into foo_3 select generate_series= (100000,300000);analyze foo_3; EXPLAIN ANALYZE SELECT a,COUNT(*) from foo group by a; QUERY PLAN ---------------------------------------------------------------------------= --------------------------------------------------- HashAggregate (cost=3D= 15568.12..15570.62 rows=3D200 width=3D4) (actual time=3D2386.813..2691.254 = rows=3D300000 loops=3D1) -> Append (cost=3D0.00..11557.41 rows=3D802141= width=3D4) (actual time=3D0.016..1403.121 rows=3D800001 loops=3D1) = -> Seq Scan on foo (cost=3D0.00..31.40 rows=3D2140 width=3D4) (actual ti= me=3D0.002..0.002 rows=3D0 loops=3D1) -> Seq Scan on foo_1 foo (c= ost=3D0.00..4322.00 rows=3D300000 width=3D4) (actual time=3D0.013..201.549 = rows=3D300000 loops=3D1) -> Seq Scan on foo_2 foo (cost=3D0.00..4= 322.00 rows=3D300000 width=3D4) (actual time=3D0.010..211.332 rows=3D300000= loops=3D1) -> Seq Scan on foo_3 foo (cost=3D0.00..2882.01 rows= =3D200001 width=3D4) (actual time=3D0.011..137.262 rows=3D200001 loops=3D1)= Total runtime: 2851.990 ms Is this a bug, or some subtlety of the Postgres query planner? =20 In my particular case, I am doing a join out to another small table as part= of the aggregation and using constraint exclusion on the partitions, but I= believe the cause of my problem is the same. =20 I am running on 64bit FreeBSD/Postgres 8.2.4 on a machine with 8GB of memor= y. =20 An explain of the query and resulting OOM diagnostics follow: =20 The aggregation will result in 5,000,000 rows, not 5,000.In the stats_dtl t= able there are 12 observations(hourly) for each customerThere are 125 diffe= rent policy_id and 25 different policy_group_id'sPolicy's and policy_groups= are even distributed across all customers =20 userquery Scan table_4760 (cost=3D2897243.60..2897418.60 rows=3D5000 widt= h=3D152) -> HashAggregate (cost=3D2897243.60..2897368.60 rows=3D5000 wi= dth=3D40) -> Hash Join (cost=3D7.81..2241002.00 rows=3D37499520 w= idth=3D40) Hash Cond: (public.customer_stats_dtl.policy_id = =3D policy.policy_id) -> Append (cost=3D0.00..1641001.87 ro= ws=3D59999232 width=3D40) -> Seq Scan on customer_stat= s_dtl (cost=3D0.00..22.45 rows=3D4 width=3D40) F= ilter: ((period_start >=3D '2007-09-08 20:00:00-04'::timestamp with time zo= ne) AND (period_start < '2007-09-09 08:00:00-04'::timestamp with time zone)= ) -> Seq Scan on customer_stats_dtl_027 customer_stats= _dtl (cost=3D0.00..1640979.42 rows=3D59999228 width=3D40) = Filter: ((period_start >=3D '2007-09-08 20:00:00-04'::timestamp w= ith time zone) AND (period_start < '2007-09-09 08:00:00-04'::timestamp with= time zone)) -> Hash (cost=3D6.25..6.25 rows=3D125 width=3D= 8) -> Seq Scan on policy (cost=3D0.00..6.25 rows=3D12= 5 width=3D8) TopMemoryContext: 268400 total in 32 block= s; 20912 free (43 chunks); 247488 usedunnamed prepared statement: 2097152 t= otal in 8 blocks; 721456 free (2 chunks); 1375696 usedTopTransactionContext= : 8192 total in 1 blocks; 7648 free (0 chunks); 544 usedSPI Plan: 3072 tota= l in 2 blocks; 1320 free (0 chunks); 1752 usedSPI Plan: 7168 total in 3 blo= cks; 1312 free (0 chunks); 5856 usedSPI Plan: 3072 total in 2 blocks; 1320 = free (0 chunks); 1752 usedSPI Plan: 3072 total in 2 blocks; 928 free (0 chu= nks); 2144 usedSPI Plan: 7168 total in 3 blocks; 3920 free (0 chunks); 3248= usedSPI Plan: 3072 total in 2 blocks; 1224 free (0 chunks); 1848 usedPL/Pg= SQL function context: 24576 total in 2 blocks; 12008 free (9 chunks); 12568= usedCFuncHash: 8192 total in 1 blocks; 1680 free (0 chunks); 6512 usedRend= ezvous variable hash: 8192 total in 1 blocks; 1680 free (0 chunks); 6512 us= edPLpgSQL function cache: 24224 total in 2 blocks; 3744 free (0 chunks); 20= 480 usedType information cache: 24576 total in 2 blocks; 11888 free (5 chun= ks); 12688 usedS_2: 1024 total in 1 blocks; 488 free (0 chunks); 536 usedS_= 1: 1024 total in 1 blocks; 488 free (0 chunks); 536 usedPrepared Queries: 2= 4576 total in 2 blocks; 11888 free (5 chunks); 12688 usedRecord information= cache: 24576 total in 2 blocks; 15984 free (5 chunks); 8592 usedMessageCon= text: 8192 total in 1 blocks; 7744 free (0 chunks); 448 usedOperator class = cache: 8192 total in 1 blocks; 1680 free (0 chunks); 6512 usedsmgr relation= table: 57344 total in 3 blocks; 21872 free (8 chunks); 35472 usedTransacti= onAbortContext: 32768 total in 1 blocks; 32736 free (0 chunks); 32 usedPort= al hash: 8192 total in 1 blocks; 1680 free (0 chunks); 6512 usedPortalMemor= y: 8192 total in 1 blocks; 7888 free (1 chunks); 304 usedPortalHeapMemory: = 1024 total in 1 blocks; 768 free (0 chunks); 256 usedExecutorState: 122880 = total in 4 blocks; 68728 free (2 chunks); 54152 usedHashTableContext: 0 tot= al in 0 blocks; 0 free (0 chunks); 0 usedHashBatchContext: 32888 total in 3= blocks; 14512 free (0 chunks); 18376 usedExprContext: 0 total in 0 blocks;= 0 free (0 chunks); 0 usedExprContext: 0 total in 0 blocks; 0 free (0 chunk= s); 0 usedExprContext: 8192 total in 1 blocks; 8160 free (0 chunks); 32 use= dExprContext: 0 total in 0 blocks; 0 free (0 chunks); 0 usedExprContext: 81= 92 total in 1 blocks; 8160 free (0 chunks); 32 usedAggContext: 1033887744 t= otal in 133 blocks; 688 free (0 chunks); 1033887056 used **Tu= pleHashTable: 566485040 total in 78 blocks; 2064896 free (304 chunks); 5644= 20144 used **ExprContext: 0 total in 0 blocks; 0 free (0 chunks); 0 = usedExprContext: 8192 total in 1 blocks; 8040 free (1 chunks); 152 usedExpr= Context: 0 total in 0 blocks; 0 free (0 chunks); 0 usedRelcache by OID: 573= 44 total in 3 blocks; 31024 free (6 chunks); 26320 usedCacheMemoryContext: = 4679720 total in 23 blocks; 404104 free (1 chunks); 4275616 used =20 If you have actually read this far, the following wrinkle is where I am cur= rently stuck. =20 I set ENABLE_HASHAGG=3DOFF and still get an OOM :-( In this situation Postgres runs for much longer before getting the OOM and = I believe it has actually written some records to the aggregationI base thi= s on seeing autovacuum kick off against the aggregation table after the que= ry fails, which may not be valid. userquery Scan table_4760 (cost=3D8771233.77..9521474.17 rows=3D5000 width= =3D152) -> GroupAggregate (cost=3D8771233.77..9521349.17 rows=3D5000 wid= th=3D40) -> Sort (cost=3D8771233.77..8864982.57 rows=3D37499520 wi= dth=3D40) Sort Key: public.customer_stats_dtl.user_id, policy.= policy_group_id -> Hash Join (cost=3D7.81..2241002.00 rows= =3D37499520 width=3D40) Hash Cond: (public.customer_stat= s_dtl.policy_id =3D policy.policy_id) -> Append (cost= =3D0.00..1641001.87 rows=3D59999232 width=3D40) ->= Seq Scan on customer_stats_dtl (cost=3D0.00..22.45 rows=3D4 width=3D40) = Filter: ((period_start >=3D '2007-09-08 20:0= 0:00-04'::timestamp with time zone) AND (period_start < '2007-09-09 08:00:0= 0-04'::timestamp with time zone)) -> Seq Scan on = customer_stats_dtl_027 customer_stats_dtl (cost=3D0.00..1640979.42 rows=3D= 59999228 width=3D40) Filter: ((period_start = >=3D '2007-09-08 20:00:00-04'::timestamp with time zone) AND (period_start = < '2007-09-09 08:00:00-04'::timestamp with time zone)) -= > Hash (cost=3D6.25..6.25 rows=3D125 width=3D8) = -> Seq Scan on policy (cost=3D0.00..6.25 rows=3D125 width=3D8) =20 TopMemoryContext: 874608 total in 106 blocks; 14240 free (45 chunks); 86036= 8 usedunnamed prepared statement: 2097152 total in 8 blocks; 720544 free (2= chunks); 1376608 usedTopTransactionContext: 8192 total in 1 blocks; 7648 f= ree (0 chunks); 544 usedAfterTriggerEvents: 758112256 total in 102 blocks; = 1792 free (8 chunks); 758110464 usedS_2: 1024 total in 1 blocks; 488 free (= 0 chunks); 536 usedType information cache: 24576 total in 2 blocks; 11888 f= ree (5 chunks); 12688 usedSPI Plan: 3072 total in 2 blocks; 1320 free (0 ch= unks); 1752 usedSPI Plan: 7168 total in 3 blocks; 1312 free (0 chunks); 585= 6 usedSPI Plan: 3072 total in 2 blocks; 1320 free (0 chunks); 1752 usedSPI = Plan: 3072 total in 2 blocks; 928 free (0 chunks); 2144 usedSPI Plan: 7168 = total in 3 blocks; 3920 free (0 chunks); 3248 usedSPI Plan: 3072 total in 2= blocks; 1224 free (0 chunks); 1848 usedPL/PgSQL function context: 24576 to= tal in 2 blocks; 12008 free (9 chunks); 12568 usedCFuncHash: 8192 total in = 1 blocks; 1680 free (0 chunks); 6512 usedRendezvous variable hash: 8192 tot= al in 1 blocks; 1680 free (0 chunks); 6512 usedPLpgSQL function cache: 2422= 4 total in 2 blocks; 3744 free (0 chunks); 20480 usedS_1: 1024 total in 1 b= locks; 488 free (0 chunks); 536 usedPrepared Queries: 24576 total in 2 bloc= ks; 11888 free (5 chunks); 12688 usedRecord information cache: 24576 total = in 2 blocks; 15984 free (5 chunks); 8592 usedMessageContext: 8192 total in = 1 blocks; 7744 free (0 chunks); 448 usedOperator class cache: 8192 total in= 1 blocks; 1680 free (0 chunks); 6512 usedsmgr relation table: 253952 total= in 5 blocks; 40912 free (16 chunks); 213040 usedTransactionAbortContext: 3= 2768 total in 1 blocks; 32736 free (0 chunks); 32 usedPortal hash: 8192 tot= al in 1 blocks; 1680 free (0 chunks); 6512 usedPortalMemory: 8192 total in = 1 blocks; 7888 free (1 chunks); 304 usedPortalHeapMemory: 1024 total in 1 b= locks; 768 free (0 chunks); 256 usedExecutorState: 122880 total in 4 blocks= ; 61720 free (15 chunks); 61160 usedExprContext: 8192 total in 1 blocks; 81= 60 free (0 chunks); 32 usedHashTableContext: 0 total in 0 blocks; 0 free (0= chunks); 0 usedHashBatchContext: 32888 total in 3 blocks; 14512 free (0 ch= unks); 18376 usedTupleSort: 822437960 total in 105 blocks; 424201336 free (= 5267294 chunks); 398236624 usedExprContext: 0 total in 0 blocks; 0 free (0 = chunks); 0 usedExprContext: 0 total in 0 blocks; 0 free (0 chunks); 0 usedE= xprContext: 8192 total in 1 blocks; 8160 free (0 chunks); 32 usedExprContex= t: 0 total in 0 blocks; 0 free (0 chunks); 0 usedExprContext: 8192 total in= 1 blocks; 8160 free (0 chunks); 32 usedAggContext: 8192 total in 1 blocks;= 8000 free (3 chunks); 192 usedExprContext: 8192 total in 1 blocks; 8000 fr= ee (0 chunks); 192 usedExprContext: 8192 total in 1 blocks; 8160 free (0 ch= unks); 32 usedExprContext: 8192 total in 1 blocks; 8040 free (0 chunks); 15= 2 usedRelcache by OID: 253952 total in 5 blocks; 117488 free (8 chunks); 13= 6464 usedCacheMemoryContext: 17262632 total in 25 blocks; 3573056 free (0 c= hunks); 13689576 used _________________________________________________________________ Explore the seven wonders of the world http://search.msn.com/results.aspx?q=3D7+wonders+world&mkt=3Den-US&form=3DQ= BRE= --_bcb8c679-43a3-43a5-aa41-6831d98c6f01_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable In keeping with some of the recent threads regard= ing the planner...
 
I have a fair sized data warehouse in which I am trying to perform an aggre= gation, but getting OOM errors in Postgres(8.2.4).
I believe the reason for the OOM is that Postgres is attempting to do a has= h aggregation, but it has grossly underestimated the rows resulting from th= e aggregation.
The data in the database is very uniformly distributed so I don't believe t= hat the table stats are the cause of the problem.
This may be related to table inheritance, and can be demonstrated pretty ea= sily.
 
CREATE TABLE foo(a INT);
ANALYZE foo;
CREATE TABLE foo_1() INHERITS(foo);
insert into foo_1 select  gener= ate_series(1,100000);
insert into foo_1 select  generate_series(1,1= 00000);
insert into foo_1 select  generate_series(1,100000);
ANA= LYZE foo_1;

CREATE TABLE foo_2() INHERITS(foo);
insert into foo_2 select  g= enerate_series(1,100000);
insert into foo_2 select  generate_series= (1,100000);
insert into foo_2 select  generate_series(1,100000);ANALYZE foo_2;
-- If I query a particular partition, the plan estimate for the hash aggreg= ate is good
EXPLAIN ANALYZE SELECT a,COUNT(*) from foo_1 group by a;
 HashAggregate  (cost=3D5822.00..7061.01 rows=3D99121 width=3D4) = (actual time=3D554.556..657.121 rows=3D100000 loops=3D1)
   -&= gt;  Seq Scan on foo_1  (cost=3D0.00..4322.00 rows=3D300000 width= =3D4) (actual time=3D0.014..203.290 rows=3D300000 loops=3D1)
 Total= runtime: 712.211 ms

-- If I query the base table, the plan estimate for the hash aggregate = is off by several orders of magnitude
EXPLAIN ANALYZE SELECT a,COUNT(*) from foo group by a;
HashAggregate  (cost=3D11686.10..11688.60 rows=3D200 width=3D4) (actua= l time=3D1724.188..1826.630 rows=3D100000 loops=3D1)
   ->&= nbsp; Append  (cost=3D0.00..8675.40 rows=3D602140 width=3D4) (actual t= ime=3D0.016..1045.134 rows=3D600000 loops=3D1)
    &= nbsp;    ->  Seq Scan on foo  (cost=3D0.00..31.= 40 rows=3D2140 width=3D4) (actual time=3D0.001..0.001 rows=3D0 loops=3D1)         ->  Seq Scan on = foo_1 foo  (cost=3D0.00..4322.00 rows=3D300000 width=3D4) (actual time= =3D0.012..205.130 rows=3D300000 loops=3D1)
     = ;    ->  Seq Scan on foo_2 foo  (cost=3D0.00..4= 322.00 rows=3D300000 width=3D4) (actual time=3D0.011..203.542 rows=3D300000= loops=3D1)
 Total runtime: 1879.550 ms
(6 rows)
-- Is there something magical about the hash aggregate estimate of 200 rows= ?
-- I can have 30,000 or 300,000 rows in each child partition table and mult= iple partition's with different values of "a" and yet it always come up wit= h 200.
-- eg.
create table foo_3() inherits(foo);
insert into foo_3 select generate_se= ries(100000,300000);
analyze foo_3;
EXPLAIN ANALYZE SELECT a,COUNT(*) from foo group by a;
            &nb= sp;            =             &nb= sp;            =         QUERY PLAN
---------------------------------------------------------------------------= ---------------------------------------------------
 HashAggregate&= nbsp; (cost=3D15568.12..15570.62 rows=3D200 width=3D4) (actual time=3D2386.= 813..2691.254 rows=3D300000 loops=3D1)
   ->  Append&n= bsp; (cost=3D0.00..11557.41 rows=3D802141 width=3D4) (actual time=3D0.016..= 1403.121 rows=3D800001 loops=3D1)
      &n= bsp;  ->  Seq Scan on foo  (cost=3D0.00..31.40 rows=3D214= 0 width=3D4) (actual time=3D0.002..0.002 rows=3D0 loops=3D1)
  = ;       ->  Seq Scan on foo_1 foo&nbs= p; (cost=3D0.00..4322.00 rows=3D300000 width=3D4) (actual time=3D0.013..201= .549 rows=3D300000 loops=3D1)
       =   ->  Seq Scan on foo_2 foo  (cost=3D0.00..4322.00 rows= =3D300000 width=3D4) (actual time=3D0.010..211.332 rows=3D300000 loops=3D1)=
         ->  Seq Scan o= n foo_3 foo  (cost=3D0.00..2882.01 rows=3D200001 width=3D4) (actual ti= me=3D0.011..137.262 rows=3D200001 loops=3D1)
 Total runtime: 2851.9= 90 ms

Is this a bug, or some subtlety of the Postgres query planner?
 
In my particular case, I am doing a join out to another small table as part= of the aggregation and using constraint exclusion on the partitions, but I= believe the cause of my problem is the same.
 
I am running on 64bit FreeBSD/Postgres 8.2.4 on a machine with 8GB of memor= y.
 
An explain of the query and resulting OOM diagnostics follow:
 
The aggregation will result in 5,000,000 rows, not 5,000.
In the stats_d= tl table there are 12 observations(hourly) for each customer
There are 1= 25 different policy_id and 25 different policy_group_id's
Policy's and p= olicy_groups are even distributed across all customers
 
 userquery Scan table_4760  (cost=3D2897243.60..2897418.60 rows= =3D5000 width=3D152)
   ->  HashAggregate  (cost= =3D2897243.60..2897368.60 rows=3D5000 width=3D40)
   &nbs= p;     ->  Hash Join  (cost=3D7.81..224100= 2.00 rows=3D37499520 width=3D40)
      &nb= sp;        Hash Cond: (public.customer_s= tats_dtl.policy_id =3D policy.policy_id)
     &= nbsp;         ->  Append&nb= sp; (cost=3D0.00..1641001.87 rows=3D59999232 width=3D40)
  &nb= sp;            =       ->  Seq Scan on customer_stats_dtl&n= bsp; (cost=3D0.00..22.45 rows=3D4 width=3D40)
    &n= bsp;            = ;          Filter: ((period_st= art >=3D '2007-09-08 20:00:00-04'::timestamp with time zone) AND (period= _start < '2007-09-09 08:00:00-04'::timestamp with time zone))
 &= nbsp;           &nbs= p;       ->  Seq Scan on customer_sta= ts_dtl_027 customer_stats_dtl  (cost=3D0.00..1640979.42 rows=3D5999922= 8 width=3D40)
         &nbs= p;            &= nbsp;    Filter: ((period_start >=3D '2007-09-08 20:00:00= -04'::timestamp with time zone) AND (period_start < '2007-09-09 08:00:00= -04'::timestamp with time zone))
      &nb= sp;        ->  Hash  (cost= =3D6.25..6.25 rows=3D125 width=3D8)
      =             &nb= sp;  ->  Seq Scan on policy  (cost=3D0.00..6.25 rows=3D12= 5 width=3D8)
          = ;          
TopMemory= Context: 268400 total in 32 blocks; 20912 free (43 chunks); 247488 used
= unnamed prepared statement: 2097152 total in 8 blocks; 721456 free (2 chunk= s); 1375696 used
TopTransactionContext: 8192 total in 1 blocks; 7648 fre= e (0 chunks); 544 used
SPI Plan: 3072 total in 2 blocks; 1320 free (0 ch= unks); 1752 used
SPI Plan: 7168 total in 3 blocks; 1312 free (0 chunks);= 5856 used
SPI Plan: 3072 total in 2 blocks; 1320 free (0 chunks); 1752 = used
SPI Plan: 3072 total in 2 blocks; 928 free (0 chunks); 2144 usedSPI Plan: 7168 total in 3 blocks; 3920 free (0 chunks); 3248 used
SPI P= lan: 3072 total in 2 blocks; 1224 free (0 chunks); 1848 used
PL/PgSQL fu= nction context: 24576 total in 2 blocks; 12008 free (9 chunks); 12568 used<= BR>CFuncHash: 8192 total in 1 blocks; 1680 free (0 chunks); 6512 used
Re= ndezvous variable hash: 8192 total in 1 blocks; 1680 free (0 chunks); 6512 = used
PLpgSQL function cache: 24224 total in 2 blocks; 3744 free (0 chunk= s); 20480 used
Type information cache: 24576 total in 2 blocks; 11888 fr= ee (5 chunks); 12688 used
S_2: 1024 total in 1 blocks; 488 free (0 chunk= s); 536 used
S_1: 1024 total in 1 blocks; 488 free (0 chunks); 536 used<= BR>Prepared Queries: 24576 total in 2 blocks; 11888 free (5 chunks); 12688 = used
Record information cache: 24576 total in 2 blocks; 15984 free (5 ch= unks); 8592 used
MessageContext: 8192 total in 1 blocks; 7744 free (0 ch= unks); 448 used
Operator class cache: 8192 total in 1 blocks; 1680 free = (0 chunks); 6512 used
smgr relation table: 57344 total in 3 blocks; 2187= 2 free (8 chunks); 35472 used
TransactionAbortContext: 32768 total in 1 = blocks; 32736 free (0 chunks); 32 used
Portal hash: 8192 total in 1 bloc= ks; 1680 free (0 chunks); 6512 used
PortalMemory: 8192 total in 1 blocks= ; 7888 free (1 chunks); 304 used
PortalHeapMemory: 1024 total in 1 block= s; 768 free (0 chunks); 256 used
ExecutorState: 122880 total in 4 blocks= ; 68728 free (2 chunks); 54152 used
HashTableContext: 0 total in 0 block= s; 0 free (0 chunks); 0 used
HashBatchContext: 32888 total in 3 blocks; = 14512 free (0 chunks); 18376 used
ExprContext: 0 total in 0 blocks; 0 fr= ee (0 chunks); 0 used
ExprContext: 0 total in 0 blocks; 0 free (0 chunks= ); 0 used
ExprContext: 8192 total in 1 blocks; 8160 free (0 chunks); 32 = used
ExprContext: 0 total in 0 blocks; 0 free (0 chunks); 0 used
Expr= Context: 8192 total in 1 blocks; 8160 free (0 chunks); 32 used
AggContex= t: 1033887744 total in 133 blocks; 688 free (0 chunks); 1033887056 used&nbs= p;            &= nbsp; **
TupleHashTable: 566485040 total in 78 blocks; 2064896 free (304= chunks); 564420144 used        **
Ex= prContext: 0 total in 0 blocks; 0 free (0 chunks); 0 used
ExprContext: 8= 192 total in 1 blocks; 8040 free (1 chunks); 152 used
ExprContext: 0 tot= al in 0 blocks; 0 free (0 chunks); 0 used
Relcache by OID: 57344 total i= n 3 blocks; 31024 free (6 chunks); 26320 used
CacheMemoryContext: 467972= 0 total in 23 blocks; 404104 free (1 chunks); 4275616 used
 
If you have actually read this far, the following wrinkle is where I am cur= rently stuck.
 
I set ENABLE_HASHAGG=3DOFF and still get an OOM :-(
In this situation Postgres runs for much longer before getting the OOM and = I believe it has actually written some records to the aggregation
I base= this on seeing autovacuum kick off against the aggregation table after the= query fails, which may not be valid.

userquery Scan table_4760  (cost=3D8771233.77..9521474.17 rows=3D5= 000 width=3D152)
  ->  GroupAggregate  (cost=3D8771233= .77..9521349.17 rows=3D5000 width=3D40)
     &n= bsp;  ->  Sort  (cost=3D8771233.77..8864982.57 rows=3D374= 99520 width=3D40)
         =      Sort Key: public.customer_stats_dtl.user_id, polic= y.policy_group_id
         =      ->  Hash Join  (cost=3D7.81..2241002.= 00 rows=3D37499520 width=3D40)
       = ;             H= ash Cond: (public.customer_stats_dtl.policy_id =3D policy.policy_id)
&nb= sp;            =        ->  Append  (cost=3D0.00.= .1641001.87 rows=3D59999232 width=3D40)
     &n= bsp;            = ;        ->  Seq Scan on custome= r_stats_dtl  (cost=3D0.00..22.45 rows=3D4 width=3D40)
  &= nbsp;           &nbs= p;            &= nbsp;    Filter: ((period_start >=3D '2007-09-08 20:00:00= -04'::timestamp with time zone) AND (period_start < '2007-09-09 08:00:00= -04'::timestamp with time zone))
      &nb= sp;            =        ->  Seq Scan on customer_stats= _dtl_027 customer_stats_dtl  (cost=3D0.00..1640979.42 rows=3D59999228 = width=3D40)
          =             &nb= sp;         Filter: ((period_start = >=3D '2007-09-08 20:00:00-04'::timestamp with time zone) AND (period_sta= rt < '2007-09-09 08:00:00-04'::timestamp with time zone))
  = ;            &n= bsp;     ->  Hash  (cost=3D6.25..6.25 rows= =3D125 width=3D8)
         =             &nb= sp;    ->  Seq Scan on policy  (cost=3D0.00..6.= 25 rows=3D125 width=3D8)
 
TopMemoryContext: 874608 total in 106 blocks; 14240 free (45 chunks); 86036= 8 used
unnamed prepared statement: 2097152 total in 8 blocks; 720544 fre= e (2 chunks); 1376608 used
TopTransactionContext: 8192 total in 1 blocks= ; 7648 free (0 chunks); 544 used
AfterTriggerEvents: 758112256 total in = 102 blocks; 1792 free (8 chunks); 758110464 used
S_2: 1024 total in 1 bl= ocks; 488 free (0 chunks); 536 used
Type information cache: 24576 total = in 2 blocks; 11888 free (5 chunks); 12688 used
SPI Plan: 3072 total in 2= blocks; 1320 free (0 chunks); 1752 used
SPI Plan: 7168 total in 3 block= s; 1312 free (0 chunks); 5856 used
SPI Plan: 3072 total in 2 blocks; 132= 0 free (0 chunks); 1752 used
SPI Plan: 3072 total in 2 blocks; 928 free = (0 chunks); 2144 used
SPI Plan: 7168 total in 3 blocks; 3920 free (0 chu= nks); 3248 used
SPI Plan: 3072 total in 2 blocks; 1224 free (0 chunks); = 1848 used
PL/PgSQL function context: 24576 total in 2 blocks; 12008 free= (9 chunks); 12568
 used
CFuncHash: 8192 total in 1 blocks; 1680= free (0 chunks); 6512 used
Rendezvous variable hash: 8192 total in 1 bl= ocks; 1680 free (0 chunks); 6512 used
PLpgSQL function cache: 24224 tota= l in 2 blocks; 3744 free (0 chunks); 20480 used
S_1: 1024 total in 1 blo= cks; 488 free (0 chunks); 536 used
Prepared Queries: 24576 total in 2 bl= ocks; 11888 free (5 chunks); 12688 used
Record information cache: 24576 = total in 2 blocks; 15984 free (5 chunks); 8592 used
MessageContext: 8192= total in 1 blocks; 7744 free (0 chunks); 448 used
Operator class cache:= 8192 total in 1 blocks; 1680 free (0 chunks); 6512 used
smgr relation t= able: 253952 total in 5 blocks; 40912 free (16 chunks); 213040 used
Tran= sactionAbortContext: 32768 total in 1 blocks; 32736 free (0 chunks); 32 use= d
Portal hash: 8192 total in 1 blocks; 1680 free (0 chunks); 6512 usedPortalMemory: 8192 total in 1 blocks; 7888 free (1 chunks); 304 used
P= ortalHeapMemory: 1024 total in 1 blocks; 768 free (0 chunks); 256 used
E= xecutorState: 122880 total in 4 blocks; 61720 free (15 chunks); 61160 used<= BR>ExprContext: 8192 total in 1 blocks; 8160 free (0 chunks); 32 used
Ha= shTableContext: 0 total in 0 blocks; 0 free (0 chunks); 0 used
HashBatch= Context: 32888 total in 3 blocks; 14512 free (0 chunks); 18376 used
Tupl= eSort: 822437960 total in 105 blocks; 424201336 free (5267294 chunks); 3982= 36624 used
ExprContext: 0 total in 0 blocks; 0 free (0 chunks); 0 usedExprContext: 0 total in 0 blocks; 0 free (0 chunks); 0 used
ExprContex= t: 8192 total in 1 blocks; 8160 free (0 chunks); 32 used
ExprContext: 0 = total in 0 blocks; 0 free (0 chunks); 0 used
ExprContext: 8192 total in = 1 blocks; 8160 free (0 chunks); 32 used
AggContext: 8192 total in 1 bloc= ks; 8000 free (3 chunks); 192 used
ExprContext: 8192 total in 1 blocks; = 8000 free (0 chunks); 192 used
ExprContext: 8192 total in 1 blocks; 8160= free (0 chunks); 32 used
ExprContext: 8192 total in 1 blocks; 8040 free= (0 chunks); 152 used
Relcache by OID: 253952 total in 5 blocks; 117488 = free (8 chunks); 136464 used
CacheMemoryContext: 17262632 total in 25 bl= ocks; 3573056 free (0 chunks); 13689576 used


Explore the sev= en wonders of the world Learn more! = --_bcb8c679-43a3-43a5-aa41-6831d98c6f01_--