2011-12-07 06:18:38 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* sortsupport.c
|
|
|
|
* Support routines for accelerated sorting.
|
|
|
|
*
|
|
|
|
*
|
2019-01-02 18:44:25 +01:00
|
|
|
* Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
|
2011-12-07 06:18:38 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* src/backend/utils/sort/sortsupport.c
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2014-08-06 22:06:06 +02:00
|
|
|
#include "access/nbtree.h"
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 01:36:59 +01:00
|
|
|
#include "catalog/pg_am.h"
|
2011-12-07 06:18:38 +01:00
|
|
|
#include "fmgr.h"
|
|
|
|
#include "utils/lsyscache.h"
|
2014-11-07 21:50:09 +01:00
|
|
|
#include "utils/rel.h"
|
2011-12-07 06:18:38 +01:00
|
|
|
#include "utils/sortsupport.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Info needed to use an old-style comparison function as a sort comparator */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
FunctionCallInfoData fcinfo; /* reusable callinfo structure */
|
2012-06-10 21:20:04 +02:00
|
|
|
FmgrInfo flinfo; /* lookup data for comparison function */
|
2011-12-07 06:18:38 +01:00
|
|
|
} SortShimExtra;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shim function for calling an old-style comparator
|
|
|
|
*
|
|
|
|
* This is essentially an inlined version of FunctionCall2Coll(), except
|
|
|
|
* we assume that the FunctionCallInfoData was already mostly set up by
|
|
|
|
* PrepareSortSupportComparisonShim.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
comparison_shim(Datum x, Datum y, SortSupport ssup)
|
|
|
|
{
|
|
|
|
SortShimExtra *extra = (SortShimExtra *) ssup->ssup_extra;
|
|
|
|
Datum result;
|
|
|
|
|
|
|
|
extra->fcinfo.arg[0] = x;
|
|
|
|
extra->fcinfo.arg[1] = y;
|
|
|
|
|
|
|
|
/* just for paranoia's sake, we reset isnull each time */
|
|
|
|
extra->fcinfo.isnull = false;
|
|
|
|
|
|
|
|
result = FunctionCallInvoke(&extra->fcinfo);
|
|
|
|
|
|
|
|
/* Check for null result, since caller is clearly not expecting one */
|
|
|
|
if (extra->fcinfo.isnull)
|
|
|
|
elog(ERROR, "function %u returned NULL", extra->flinfo.fn_oid);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up a shim function to allow use of an old-style btree comparison
|
|
|
|
* function as if it were a sort support comparator.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
PrepareSortSupportComparisonShim(Oid cmpFunc, SortSupport ssup)
|
|
|
|
{
|
2012-06-10 21:20:04 +02:00
|
|
|
SortShimExtra *extra;
|
2011-12-07 06:18:38 +01:00
|
|
|
|
|
|
|
extra = (SortShimExtra *) MemoryContextAlloc(ssup->ssup_cxt,
|
|
|
|
sizeof(SortShimExtra));
|
|
|
|
|
|
|
|
/* Lookup the comparison function */
|
|
|
|
fmgr_info_cxt(cmpFunc, &extra->flinfo, ssup->ssup_cxt);
|
|
|
|
|
|
|
|
/* We can initialize the callinfo just once and re-use it */
|
|
|
|
InitFunctionCallInfoData(extra->fcinfo, &extra->flinfo, 2,
|
|
|
|
ssup->ssup_collation, NULL, NULL);
|
|
|
|
extra->fcinfo.argnull[0] = false;
|
|
|
|
extra->fcinfo.argnull[1] = false;
|
|
|
|
|
|
|
|
ssup->ssup_extra = extra;
|
|
|
|
ssup->comparator = comparison_shim;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-11-07 21:50:09 +01:00
|
|
|
* Look up and call sortsupport function to setup SortSupport comparator;
|
|
|
|
* or if no such function exists or it declines to set up the appropriate
|
|
|
|
* state, prepare a suitable shim.
|
2011-12-07 06:18:38 +01:00
|
|
|
*/
|
2014-11-07 21:50:09 +01:00
|
|
|
static void
|
|
|
|
FinishSortSupportFunction(Oid opfamily, Oid opcintype, SortSupport ssup)
|
2011-12-07 06:18:38 +01:00
|
|
|
{
|
2014-08-06 22:06:06 +02:00
|
|
|
Oid sortSupportFunction;
|
2011-12-07 06:18:38 +01:00
|
|
|
|
2014-08-06 22:06:06 +02:00
|
|
|
/* Look for a sort support function */
|
|
|
|
sortSupportFunction = get_opfamily_proc(opfamily, opcintype, opcintype,
|
|
|
|
BTSORTSUPPORT_PROC);
|
|
|
|
if (OidIsValid(sortSupportFunction))
|
2011-12-07 06:18:38 +01:00
|
|
|
{
|
2014-08-06 22:06:06 +02:00
|
|
|
/*
|
2015-05-24 03:35:49 +02:00
|
|
|
* The sort support function can provide a comparator, but it can also
|
|
|
|
* choose not to so (e.g. based on the selected collation).
|
2014-08-06 22:06:06 +02:00
|
|
|
*/
|
|
|
|
OidFunctionCall1(sortSupportFunction, PointerGetDatum(ssup));
|
2011-12-07 06:18:38 +01:00
|
|
|
}
|
2014-08-06 22:06:06 +02:00
|
|
|
|
|
|
|
if (ssup->comparator == NULL)
|
2011-12-07 06:18:38 +01:00
|
|
|
{
|
2014-08-06 22:06:06 +02:00
|
|
|
Oid sortFunction;
|
|
|
|
|
|
|
|
sortFunction = get_opfamily_proc(opfamily, opcintype, opcintype,
|
|
|
|
BTORDER_PROC);
|
|
|
|
|
|
|
|
if (!OidIsValid(sortFunction))
|
|
|
|
elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
|
|
|
|
BTORDER_PROC, opcintype, opcintype, opfamily);
|
|
|
|
|
2011-12-07 06:18:38 +01:00
|
|
|
/* We'll use a shim to call the old-style btree comparator */
|
|
|
|
PrepareSortSupportComparisonShim(sortFunction, ssup);
|
|
|
|
}
|
|
|
|
}
|
2014-11-07 21:50:09 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill in SortSupport given an ordering operator (btree "<" or ">" operator).
|
|
|
|
*
|
|
|
|
* Caller must previously have zeroed the SortSupportData structure and then
|
|
|
|
* filled in ssup_cxt, ssup_collation, and ssup_nulls_first. This will fill
|
|
|
|
* in ssup_reverse as well as the comparator function pointer.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup)
|
|
|
|
{
|
|
|
|
Oid opfamily;
|
|
|
|
Oid opcintype;
|
|
|
|
int16 strategy;
|
|
|
|
|
|
|
|
Assert(ssup->comparator == NULL);
|
|
|
|
|
|
|
|
/* Find the operator in pg_amop */
|
|
|
|
if (!get_ordering_op_properties(orderingOp, &opfamily, &opcintype,
|
|
|
|
&strategy))
|
|
|
|
elog(ERROR, "operator %u is not a valid ordering operator",
|
|
|
|
orderingOp);
|
|
|
|
ssup->ssup_reverse = (strategy == BTGreaterStrategyNumber);
|
|
|
|
|
|
|
|
FinishSortSupportFunction(opfamily, opcintype, ssup);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill in SortSupport given an index relation, attribute, and strategy.
|
|
|
|
*
|
|
|
|
* Caller must previously have zeroed the SortSupportData structure and then
|
|
|
|
* filled in ssup_cxt, ssup_attno, ssup_collation, and ssup_nulls_first. This
|
|
|
|
* will fill in ssup_reverse (based on the supplied strategy), as well as the
|
|
|
|
* comparator function pointer.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
PrepareSortSupportFromIndexRel(Relation indexRel, int16 strategy,
|
|
|
|
SortSupport ssup)
|
|
|
|
{
|
|
|
|
Oid opfamily = indexRel->rd_opfamily[ssup->ssup_attno - 1];
|
|
|
|
Oid opcintype = indexRel->rd_opcintype[ssup->ssup_attno - 1];
|
|
|
|
|
|
|
|
Assert(ssup->comparator == NULL);
|
|
|
|
|
|
|
|
if (indexRel->rd_rel->relam != BTREE_AM_OID)
|
|
|
|
elog(ERROR, "unexpected non-btree AM: %u", indexRel->rd_rel->relam);
|
|
|
|
if (strategy != BTGreaterStrategyNumber &&
|
|
|
|
strategy != BTLessStrategyNumber)
|
|
|
|
elog(ERROR, "unexpected sort support strategy: %d", strategy);
|
|
|
|
ssup->ssup_reverse = (strategy == BTGreaterStrategyNumber);
|
|
|
|
|
|
|
|
FinishSortSupportFunction(opfamily, opcintype, ssup);
|
|
|
|
}
|