From d26559dbf356736923b26704ce76ca820ff3a2b0 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Fri, 4 May 2007 01:13:45 +0000 Subject: Teach tuplesort.c about "top N" sorting, in which only the first N tuples need be returned. We keep a heap of the current best N tuples and sift-up new tuples into it as we scan the input. For M input tuples this means only about M*log(N) comparisons instead of M*log(M), not to mention a lot less workspace when N is small --- avoiding spill-to-disk for large M is actually the most attractive thing about it. Patch includes planner and executor support for invoking this facility in ORDER BY ... LIMIT queries. Greg Stark, with some editorialization by moi. --- src/backend/utils/misc/guc.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'src/backend/utils/misc/guc.c') diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index a3a8f79a5d1..9f9d1e1bcbf 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -10,7 +10,7 @@ * Written by Peter Eisentraut . * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.389 2007/04/26 16:13:12 neilc Exp $ + * $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.390 2007/05/04 01:13:44 tgl Exp $ * *-------------------------------------------------------------------- */ @@ -108,6 +108,9 @@ extern bool fullPageWrites; #ifdef TRACE_SORT extern bool trace_sort; #endif +#ifdef DEBUG_BOUNDED_SORT +extern bool optimize_bounded_sort; +#endif #ifdef USE_SSL extern char *SSLCipherSuites; @@ -966,6 +969,20 @@ static struct config_bool ConfigureNamesBool[] = }, #endif +#ifdef DEBUG_BOUNDED_SORT + /* this is undocumented because not exposed in a standard build */ + { + { + "optimize_bounded_sort", PGC_USERSET, QUERY_TUNING_METHOD, + gettext_noop("Enable bounded sorting using heap sort."), + NULL, + GUC_NOT_IN_SAMPLE + }, + &optimize_bounded_sort, + true, NULL, NULL + }, +#endif + #ifdef WAL_DEBUG { {"wal_debug", PGC_SUSET, DEVELOPER_OPTIONS, @@ -1711,7 +1728,7 @@ static struct config_int ConfigureNamesInt[] = &server_version_num, PG_VERSION_NUM, PG_VERSION_NUM, PG_VERSION_NUM, NULL, NULL }, - + { {"log_temp_files", PGC_USERSET, LOGGING_WHAT, gettext_noop("Log the use of temporary files larger than this number of kilobytes."), @@ -2883,7 +2900,7 @@ InitializeGUCOptions(void) PGC_S_DEFAULT)) elog(FATAL, "failed to initialize %s to %d", conf->gen.name, conf->boot_val); - *conf->variable = conf->reset_val = conf->boot_val; + *conf->variable = conf->reset_val = conf->boot_val; break; } case PGC_REAL: @@ -2897,7 +2914,7 @@ InitializeGUCOptions(void) PGC_S_DEFAULT)) elog(FATAL, "failed to initialize %s to %g", conf->gen.name, conf->boot_val); - *conf->variable = conf->reset_val = conf->boot_val; + *conf->variable = conf->reset_val = conf->boot_val; break; } case PGC_STRING: -- cgit v1.2.3