1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-03 20:02:46 +03:00

Create a "sort support" interface API for faster sorting.

This patch creates an API whereby a btree index opclass can optionally
provide non-SQL-callable support functions for sorting.  In the initial
patch, we only use this to provide a directly-callable comparator function,
which can be invoked with a bit less overhead than the traditional
SQL-callable comparator.  While that should be of value in itself, the real
reason for doing this is to provide a datatype-extensible framework for
more aggressive optimizations, as in Peter Geoghegan's recent work.

Robert Haas and Tom Lane
This commit is contained in:
Tom Lane
2011-12-07 00:18:38 -05:00
parent d2a662182e
commit c6e3ac11b6
30 changed files with 869 additions and 421 deletions

View File

@ -47,8 +47,8 @@
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/pg_rusage.h"
#include "utils/sortsupport.h"
#include "utils/syscache.h"
#include "utils/tuplesort.h"
#include "utils/timestamp.h"
#include "utils/tqual.h"
@ -1774,8 +1774,7 @@ typedef struct
typedef struct
{
FmgrInfo *cmpFn;
int cmpFlags;
SortSupport ssup;
int *tupnoLink;
} CompareScalarsContext;
@ -2222,9 +2221,7 @@ compute_scalar_stats(VacAttrStatsP stats,
bool is_varwidth = (!stats->attrtype->typbyval &&
stats->attrtype->typlen < 0);
double corr_xysum;
Oid cmpFn;
int cmpFlags;
FmgrInfo f_cmpfn;
SortSupportData ssup;
ScalarItem *values;
int values_cnt = 0;
int *tupnoLink;
@ -2238,8 +2235,13 @@ compute_scalar_stats(VacAttrStatsP stats,
tupnoLink = (int *) palloc(samplerows * sizeof(int));
track = (ScalarMCVItem *) palloc(num_mcv * sizeof(ScalarMCVItem));
SelectSortFunction(mystats->ltopr, false, &cmpFn, &cmpFlags);
fmgr_info(cmpFn, &f_cmpfn);
memset(&ssup, 0, sizeof(ssup));
ssup.ssup_cxt = CurrentMemoryContext;
/* We always use the default collation for statistics */
ssup.ssup_collation = DEFAULT_COLLATION_OID;
ssup.ssup_nulls_first = false;
PrepareSortSupportFromOrderingOp(mystats->ltopr, &ssup);
/* Initial scan to find sortable values */
for (i = 0; i < samplerows; i++)
@ -2307,8 +2309,7 @@ compute_scalar_stats(VacAttrStatsP stats,
CompareScalarsContext cxt;
/* Sort the collected values */
cxt.cmpFn = &f_cmpfn;
cxt.cmpFlags = cmpFlags;
cxt.ssup = &ssup;
cxt.tupnoLink = tupnoLink;
qsort_arg((void *) values, values_cnt, sizeof(ScalarItem),
compare_scalars, (void *) &cxt);
@ -2712,12 +2713,9 @@ compare_scalars(const void *a, const void *b, void *arg)
Datum db = ((const ScalarItem *) b)->value;
int tb = ((const ScalarItem *) b)->tupno;
CompareScalarsContext *cxt = (CompareScalarsContext *) arg;
int32 compare;
int compare;
/* We always use the default collation for statistics */
compare = ApplySortFunction(cxt->cmpFn, cxt->cmpFlags,
DEFAULT_COLLATION_OID,
da, false, db, false);
compare = ApplySortComparator(da, false, db, false, cxt->ssup);
if (compare != 0)
return compare;