failures with tuplesort and ordered set aggregates (due to 5cefbf5a6c44)

From: Tomas Vondra <tomas(dot)vondra(at)2ndquadrant(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: failures with tuplesort and ordered set aggregates (due to 5cefbf5a6c44)
Date: 2015-02-20 19:58:54
Message-ID: 54E791FE.5000000@2ndquadrant.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

while reviewing one of the 'abbreviated keys' patches, I noticed that
the ordered set aggregates are broken when using tuplesort with multiple
runs.

ISTM this got broken by 5cefbf5a6c4466ac6b1cc2a4316b4eba9108c802:

Don't use abbreviated keys for the final merge pass.

When we write tuples out to disk and read them back in, the
abbreviated keys become non-abbreviated, because the readtup
routines don't know anything about abbreviation. But without
this fix, the rest of the code still thinks the
abbreviation-aware compartor should be used, so chaos ensues.

Triggering it is quite simple:

CREATE TABLE stuff AS SELECT random()::text AS randtext
FROM generate_series(1,1000000);
ANALYZE stuff;

SET work_mem = '1MB';

SELECT percentile_disc(0) WITHIN GROUP (ORDER BY randtext)
FROM stuff;

which fails like this:

----------------------------------------------------------------------
Program received signal SIGSEGV, Segmentation fault.
0x0000000000931bf6 in mergeruns (state=0x1dd3b58) at tuplesort.c:2175
2175 if (state->sortKeys->abbrev_converter)
(gdb) print state
$1 = (Tuplesortstate *) 0x1dd3b58
(gdb) print state->sortKeys
$2 = (SortSupport) 0x0
(gdb) print state->onlyKey
$3 = (SortSupport) 0x1dd3d70
----------------------------------------------------------------------
(gdb) bt
#0 0x0000000000931bf6 in mergeruns (state=0x1dd3b58) at tuplesort.c:2175
#1 0x00000000009309c1 in tuplesort_performsort (state=0x1dd3b58) at
tuplesort.c:1563
#2 0x0000000000864038 in percentile_disc_final (fcinfo=0x7fff2c9f4bc0)
at orderedsetaggs.c:443
#3 0x0000000000664f23 in finalize_aggregate (aggstate=0x1dcbc40,
peraggstate=0x1dcdb38, pergroupstate=0x1dce350, resultVal=0x1dcd308,
resultIsNull=0x1dcd328 "") at nodeAgg.c:865
#4 0x0000000000665826 in agg_retrieve_direct (aggstate=0x1dcbc40) at
nodeAgg.c:1295
#5 0x000000000066551a in ExecAgg (node=0x1dcbc40) at nodeAgg.c:1119
----------------------------------------------------------------------

This seems to happen because ordered_set_startup() calls
tuplesort_begin_datum() when (use_tuples == true), which only sets
'onlyKey' and leaves (sortKeys == NULL). So 'mergeruns' fails because it
does not expect that.

--
Tomas Vondra http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Eisentraut 2015-02-20 20:01:52 Re: Combining Aggregates
Previous Message Tom Lane 2015-02-20 19:41:41 Re: Precedence of standard comparison operators