Buglet in "Sort Method" explain output in degenerate case

From: Gregory Stark <stark(at)enterprisedb(dot)com>
To: PostgreSQL-development Hackers <pgsql-patches(at)postgresql(dot)org>
Subject: Buglet in "Sort Method" explain output in degenerate case
Date: 2007-09-01 17:28:24
Message-ID: 87tzqejnwn.fsf@oxford.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-patches


I noticed a small bug in the "Sort Method" code:

postgres=# explain analyze select * from test order by random() limit 1;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------
Limit (cost=21.50..21.50 rows=1 width=4) (actual time=3.649..3.651 rows=1 loops=1)
-> Sort (cost=21.50..24.00 rows=1000 width=4) (actual time=3.646..3.646 rows=1 loops=1)
Sort Key: (random())
Sort Method: quicksort Memory: 17kB
-> Seq Scan on test (cost=0.00..16.50 rows=1000 width=4) (actual time=0.021..1.707 rows=1000 loops=1)
Total runtime: 3.704 ms
(6 rows)

It's printing "quicksort" even though it used a heap. This happens because we
don't bother deheapifying a singleton heap so the boundUsed flag never gets
set. The patch below just moves setting that flag to when the heap is made
instead of when it's deheapified.

One could make the argument that we should distinguish the noop sort from
quicksort or the half-hearted singleton heapsort from the full heapsort but
that seems like gilding. But distinguishing between heapsort and quicksort is
important since it really could be either depending on how many inputs there
were.

Index: src/backend/utils/sort/tuplesort.c
===================================================================
RCS file: /home/stark/src/REPOSITORY/pgsql/src/backend/utils/sort/tuplesort.c,v
retrieving revision 1.77
diff -u -r1.77 tuplesort.c
--- src/backend/utils/sort/tuplesort.c 7 Jun 2007 19:19:57 -0000 1.77
+++ src/backend/utils/sort/tuplesort.c 1 Sep 2007 17:17:25 -0000
@@ -2247,6 +2247,7 @@
}

Assert(state->memtupcount == state->bound);
+ state->boundUsed = true;
state->status = TSS_BOUNDED;
}

@@ -2284,7 +2285,6 @@
REVERSEDIRECTION(state);

state->status = TSS_SORTEDINMEM;
- state->boundUsed = true;
}

/*

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com

Responses

Browse pgsql-patches by date

  From Date Subject
Next Message Tom Lane 2007-09-01 18:19:53 Re: Buglet in "Sort Method" explain output in degenerate case
Previous Message Brendan Jurd 2007-09-01 17:19:20 Linkage for escape strings