pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: brent_despain@selinc.com
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #13530: sort receives "unexpected out-of-memory situation during sort"
Date: Tue, 04 Aug 2015 15:43:04 -0400
Message-ID: <15011.1438717384@sss.pgh.pa.us> (raw)
In-Reply-To: <OF3C210022.1E6B6368-ON88257E97.0059E2AE-87257E97.005C0096@selinc.com>
References: <20150731220618.2987.1731@wrigleys.postgresql.org>
	<20722.1438402315@sss.pgh.pa.us>
	<6977.1438451753@sss.pgh.pa.us>
	<OF3C210022.1E6B6368-ON88257E97.0059E2AE-87257E97.005C0096@selinc.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

brent_despain@selinc.com writes:
> The error is from tuplestore.c in the grow_memtuples() function.  It 
> probably applies to tuplesort.c as well.

> WARNING: orig_memtupsize: 1024
> WARNING: orig_memtuples_size: 4104
> WARNING: orig_availMem: 2544
> WARNING: newmemtupsize: 1025
> WARNING: new_memtuples_size: 8200
> WARNING: new_availMem: -1552

Ohhh ... I was supposing that this was happening in tuplesort.c.
The issue cannot arise there because sizeof(SortTuple) is at least
16 bytes, so with even the minimum memtupsize of 1024, we are
requesting a chunk of at least 16K; which is more than is needed
to shove palloc into its separate-chunk allocation path, which
has constant chunk allocation overhead as this code expects.

But in tuplestore.c, the array elements are just "void *".  On a 32-bit
machine that's small enough that the very first allocation is a standard
palloc chunk not a separate chunk.  So then the allocation overhead *does*
increase at the first enlargement, and if you're close to exhausting
work_mem then LACKMEM can happen due to that.  The new code that made
sorting run closer to full memory utilization may have increased the odds
of this, but it did not create the problem.

So you need both a 32-bit machine and fairly small work_mem to trigger
this failure; that explains why I could not reproduce it here (I was
using a 64-bit machine).

> I see a few options.
> - Don't check LACKMEM here.  It hardly get checked anywhere else.

Not workable.  If we ignore LACKMEM here, we may have enlarged the array
so much that even dumping all the tuples to disk would not restore
positive availMem; this is the "space management algorithm will go nuts"
hazard referred to in the comment.  (The new array-sizing logic might've
reduced the odds of this, but I doubt it would be safe to assume it can't
happen.)

> - Don't use growth_ratio if the new repalloc request will be <= 8kB.
> - When new repalloc is <= 8kB over allocate allowedMem, increase 
> newmemtupsize to fully use new chunk size.

AFAICS neither of these will fix the problem, because the issue is that
the chunk overhead will increase as soon as we enlarge the initial array
request.

I think the best fix really is to increase the initial array size
so that it's at least large enough to force palloc to do what we want.
As a quick hack you could just s/1024/2048/ in tuplestore_begin_common,
but what would be more maintainable in the long run is for the memory
allocation code to export a #define for its "large chunk" threshold,
and make the initial allocation here depend on that.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (7+ messages)  latest in thread

Message-ID: <15011.1438717384@sss.pgh.pa.us>
Permalink:  ../15011.1438717384@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/15011.1438717384@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, brent_despain@selinc.com
  Subject: Re: BUG #13530: sort receives "unexpected out-of-memory situation during sort"
  In-Reply-To: <15011.1438717384@sss.pgh.pa.us>
  References: <20150731220618.2987.1731@wrigleys.postgresql.org> <20722.1438402315@sss.pgh.pa.us> <6977.1438451753@sss.pgh.pa.us> <OF3C210022.1E6B6368-ON88257E97.0059E2AE-87257E97.005C0096@selinc.com> <15011.1438717384@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox