2011-12-07 06:18:38 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* sortsupport.c
|
|
|
|
* Support routines for accelerated sorting.
|
|
|
|
*
|
|
|
|
*
|
2020-01-01 18:21:45 +01:00
|
|
|
* Portions Copyright (c) 1996-2020, 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
|
|
|
|
{
|
2012-06-10 21:20:04 +02:00
|
|
|
FmgrInfo flinfo; /* lookup data for comparison function */
|
Change function call information to be variable length.
Before this change FunctionCallInfoData, the struct arguments etc for
V1 function calls are stored in, always had space for
FUNC_MAX_ARGS/100 arguments, storing datums and their nullness in two
arrays. For nearly every function call 100 arguments is far more than
needed, therefore wasting memory. Arg and argnull being two separate
arrays also guarantees that to access a single argument, two
cachelines have to be touched.
Change the layout so there's a single variable-length array with pairs
of value / isnull. That drastically reduces memory consumption for
most function calls (on x86-64 a two argument function now uses
64bytes, previously 936 bytes), and makes it very likely that argument
value and its nullness are on the same cacheline.
Arguments are stored in a new NullableDatum struct, which, due to
padding, needs more memory per argument than before. But as usually
far fewer arguments are stored, and individual arguments are cheaper
to access, that's still a clear win. It's likely that there's other
places where conversion to NullableDatum arrays would make sense,
e.g. TupleTableSlots, but that's for another commit.
Because the function call information is now variable-length
allocations have to take the number of arguments into account. For
heap allocations that can be done with SizeForFunctionCallInfoData(),
for on-stack allocations there's a new LOCAL_FCINFO(name, nargs) macro
that helps to allocate an appropriately sized and aligned variable.
Some places with stack allocation function call information don't know
the number of arguments at compile time, and currently variably sized
stack allocations aren't allowed in postgres. Therefore allow for
FUNC_MAX_ARGS space in these cases. They're not that common, so for
now that seems acceptable.
Because of the need to allocate FunctionCallInfo of the appropriate
size, older extensions may need to update their code. To avoid subtle
breakages, the FunctionCallInfoData struct has been renamed to
FunctionCallInfoBaseData. Most code only references FunctionCallInfo,
so that shouldn't cause much collateral damage.
This change is also a prerequisite for more efficient expression JIT
compilation (by allocating the function call information on the stack,
allowing LLVM to optimize it away); previously the size of the call
information caused problems inside LLVM's optimizer.
Author: Andres Freund
Reviewed-By: Tom Lane
Discussion: https://postgr.es/m/20180605172952.x34m5uz6ju6enaem@alap3.anarazel.de
2019-01-26 23:17:52 +01:00
|
|
|
FunctionCallInfoBaseData fcinfo; /* reusable callinfo structure */
|
2011-12-07 06:18:38 +01:00
|
|
|
} SortShimExtra;
|
|
|
|
|
Change function call information to be variable length.
Before this change FunctionCallInfoData, the struct arguments etc for
V1 function calls are stored in, always had space for
FUNC_MAX_ARGS/100 arguments, storing datums and their nullness in two
arrays. For nearly every function call 100 arguments is far more than
needed, therefore wasting memory. Arg and argnull being two separate
arrays also guarantees that to access a single argument, two
cachelines have to be touched.
Change the layout so there's a single variable-length array with pairs
of value / isnull. That drastically reduces memory consumption for
most function calls (on x86-64 a two argument function now uses
64bytes, previously 936 bytes), and makes it very likely that argument
value and its nullness are on the same cacheline.
Arguments are stored in a new NullableDatum struct, which, due to
padding, needs more memory per argument than before. But as usually
far fewer arguments are stored, and individual arguments are cheaper
to access, that's still a clear win. It's likely that there's other
places where conversion to NullableDatum arrays would make sense,
e.g. TupleTableSlots, but that's for another commit.
Because the function call information is now variable-length
allocations have to take the number of arguments into account. For
heap allocations that can be done with SizeForFunctionCallInfoData(),
for on-stack allocations there's a new LOCAL_FCINFO(name, nargs) macro
that helps to allocate an appropriately sized and aligned variable.
Some places with stack allocation function call information don't know
the number of arguments at compile time, and currently variably sized
stack allocations aren't allowed in postgres. Therefore allow for
FUNC_MAX_ARGS space in these cases. They're not that common, so for
now that seems acceptable.
Because of the need to allocate FunctionCallInfo of the appropriate
size, older extensions may need to update their code. To avoid subtle
breakages, the FunctionCallInfoData struct has been renamed to
FunctionCallInfoBaseData. Most code only references FunctionCallInfo,
so that shouldn't cause much collateral damage.
This change is also a prerequisite for more efficient expression JIT
compilation (by allocating the function call information on the stack,
allowing LLVM to optimize it away); previously the size of the call
information caused problems inside LLVM's optimizer.
Author: Andres Freund
Reviewed-By: Tom Lane
Discussion: https://postgr.es/m/20180605172952.x34m5uz6ju6enaem@alap3.anarazel.de
2019-01-26 23:17:52 +01:00
|
|
|
#define SizeForSortShimExtra(nargs) (offsetof(SortShimExtra, fcinfo) + SizeForFunctionCallInfo(nargs))
|
2011-12-07 06:18:38 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Shim function for calling an old-style comparator
|
|
|
|
*
|
|
|
|
* This is essentially an inlined version of FunctionCall2Coll(), except
|
Change function call information to be variable length.
Before this change FunctionCallInfoData, the struct arguments etc for
V1 function calls are stored in, always had space for
FUNC_MAX_ARGS/100 arguments, storing datums and their nullness in two
arrays. For nearly every function call 100 arguments is far more than
needed, therefore wasting memory. Arg and argnull being two separate
arrays also guarantees that to access a single argument, two
cachelines have to be touched.
Change the layout so there's a single variable-length array with pairs
of value / isnull. That drastically reduces memory consumption for
most function calls (on x86-64 a two argument function now uses
64bytes, previously 936 bytes), and makes it very likely that argument
value and its nullness are on the same cacheline.
Arguments are stored in a new NullableDatum struct, which, due to
padding, needs more memory per argument than before. But as usually
far fewer arguments are stored, and individual arguments are cheaper
to access, that's still a clear win. It's likely that there's other
places where conversion to NullableDatum arrays would make sense,
e.g. TupleTableSlots, but that's for another commit.
Because the function call information is now variable-length
allocations have to take the number of arguments into account. For
heap allocations that can be done with SizeForFunctionCallInfoData(),
for on-stack allocations there's a new LOCAL_FCINFO(name, nargs) macro
that helps to allocate an appropriately sized and aligned variable.
Some places with stack allocation function call information don't know
the number of arguments at compile time, and currently variably sized
stack allocations aren't allowed in postgres. Therefore allow for
FUNC_MAX_ARGS space in these cases. They're not that common, so for
now that seems acceptable.
Because of the need to allocate FunctionCallInfo of the appropriate
size, older extensions may need to update their code. To avoid subtle
breakages, the FunctionCallInfoData struct has been renamed to
FunctionCallInfoBaseData. Most code only references FunctionCallInfo,
so that shouldn't cause much collateral damage.
This change is also a prerequisite for more efficient expression JIT
compilation (by allocating the function call information on the stack,
allowing LLVM to optimize it away); previously the size of the call
information caused problems inside LLVM's optimizer.
Author: Andres Freund
Reviewed-By: Tom Lane
Discussion: https://postgr.es/m/20180605172952.x34m5uz6ju6enaem@alap3.anarazel.de
2019-01-26 23:17:52 +01:00
|
|
|
* we assume that the FunctionCallInfoBaseData was already mostly set up by
|
2011-12-07 06:18:38 +01:00
|
|
|
* PrepareSortSupportComparisonShim.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
comparison_shim(Datum x, Datum y, SortSupport ssup)
|
|
|
|
{
|
|
|
|
SortShimExtra *extra = (SortShimExtra *) ssup->ssup_extra;
|
|
|
|
Datum result;
|
|
|
|
|
Change function call information to be variable length.
Before this change FunctionCallInfoData, the struct arguments etc for
V1 function calls are stored in, always had space for
FUNC_MAX_ARGS/100 arguments, storing datums and their nullness in two
arrays. For nearly every function call 100 arguments is far more than
needed, therefore wasting memory. Arg and argnull being two separate
arrays also guarantees that to access a single argument, two
cachelines have to be touched.
Change the layout so there's a single variable-length array with pairs
of value / isnull. That drastically reduces memory consumption for
most function calls (on x86-64 a two argument function now uses
64bytes, previously 936 bytes), and makes it very likely that argument
value and its nullness are on the same cacheline.
Arguments are stored in a new NullableDatum struct, which, due to
padding, needs more memory per argument than before. But as usually
far fewer arguments are stored, and individual arguments are cheaper
to access, that's still a clear win. It's likely that there's other
places where conversion to NullableDatum arrays would make sense,
e.g. TupleTableSlots, but that's for another commit.
Because the function call information is now variable-length
allocations have to take the number of arguments into account. For
heap allocations that can be done with SizeForFunctionCallInfoData(),
for on-stack allocations there's a new LOCAL_FCINFO(name, nargs) macro
that helps to allocate an appropriately sized and aligned variable.
Some places with stack allocation function call information don't know
the number of arguments at compile time, and currently variably sized
stack allocations aren't allowed in postgres. Therefore allow for
FUNC_MAX_ARGS space in these cases. They're not that common, so for
now that seems acceptable.
Because of the need to allocate FunctionCallInfo of the appropriate
size, older extensions may need to update their code. To avoid subtle
breakages, the FunctionCallInfoData struct has been renamed to
FunctionCallInfoBaseData. Most code only references FunctionCallInfo,
so that shouldn't cause much collateral damage.
This change is also a prerequisite for more efficient expression JIT
compilation (by allocating the function call information on the stack,
allowing LLVM to optimize it away); previously the size of the call
information caused problems inside LLVM's optimizer.
Author: Andres Freund
Reviewed-By: Tom Lane
Discussion: https://postgr.es/m/20180605172952.x34m5uz6ju6enaem@alap3.anarazel.de
2019-01-26 23:17:52 +01:00
|
|
|
extra->fcinfo.args[0].value = x;
|
|
|
|
extra->fcinfo.args[1].value = y;
|
2011-12-07 06:18:38 +01:00
|
|
|
|
|
|
|
/* 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,
|
Change function call information to be variable length.
Before this change FunctionCallInfoData, the struct arguments etc for
V1 function calls are stored in, always had space for
FUNC_MAX_ARGS/100 arguments, storing datums and their nullness in two
arrays. For nearly every function call 100 arguments is far more than
needed, therefore wasting memory. Arg and argnull being two separate
arrays also guarantees that to access a single argument, two
cachelines have to be touched.
Change the layout so there's a single variable-length array with pairs
of value / isnull. That drastically reduces memory consumption for
most function calls (on x86-64 a two argument function now uses
64bytes, previously 936 bytes), and makes it very likely that argument
value and its nullness are on the same cacheline.
Arguments are stored in a new NullableDatum struct, which, due to
padding, needs more memory per argument than before. But as usually
far fewer arguments are stored, and individual arguments are cheaper
to access, that's still a clear win. It's likely that there's other
places where conversion to NullableDatum arrays would make sense,
e.g. TupleTableSlots, but that's for another commit.
Because the function call information is now variable-length
allocations have to take the number of arguments into account. For
heap allocations that can be done with SizeForFunctionCallInfoData(),
for on-stack allocations there's a new LOCAL_FCINFO(name, nargs) macro
that helps to allocate an appropriately sized and aligned variable.
Some places with stack allocation function call information don't know
the number of arguments at compile time, and currently variably sized
stack allocations aren't allowed in postgres. Therefore allow for
FUNC_MAX_ARGS space in these cases. They're not that common, so for
now that seems acceptable.
Because of the need to allocate FunctionCallInfo of the appropriate
size, older extensions may need to update their code. To avoid subtle
breakages, the FunctionCallInfoData struct has been renamed to
FunctionCallInfoBaseData. Most code only references FunctionCallInfo,
so that shouldn't cause much collateral damage.
This change is also a prerequisite for more efficient expression JIT
compilation (by allocating the function call information on the stack,
allowing LLVM to optimize it away); previously the size of the call
information caused problems inside LLVM's optimizer.
Author: Andres Freund
Reviewed-By: Tom Lane
Discussion: https://postgr.es/m/20180605172952.x34m5uz6ju6enaem@alap3.anarazel.de
2019-01-26 23:17:52 +01:00
|
|
|
SizeForSortShimExtra(2));
|
2011-12-07 06:18:38 +01:00
|
|
|
|
|
|
|
/* 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);
|
Change function call information to be variable length.
Before this change FunctionCallInfoData, the struct arguments etc for
V1 function calls are stored in, always had space for
FUNC_MAX_ARGS/100 arguments, storing datums and their nullness in two
arrays. For nearly every function call 100 arguments is far more than
needed, therefore wasting memory. Arg and argnull being two separate
arrays also guarantees that to access a single argument, two
cachelines have to be touched.
Change the layout so there's a single variable-length array with pairs
of value / isnull. That drastically reduces memory consumption for
most function calls (on x86-64 a two argument function now uses
64bytes, previously 936 bytes), and makes it very likely that argument
value and its nullness are on the same cacheline.
Arguments are stored in a new NullableDatum struct, which, due to
padding, needs more memory per argument than before. But as usually
far fewer arguments are stored, and individual arguments are cheaper
to access, that's still a clear win. It's likely that there's other
places where conversion to NullableDatum arrays would make sense,
e.g. TupleTableSlots, but that's for another commit.
Because the function call information is now variable-length
allocations have to take the number of arguments into account. For
heap allocations that can be done with SizeForFunctionCallInfoData(),
for on-stack allocations there's a new LOCAL_FCINFO(name, nargs) macro
that helps to allocate an appropriately sized and aligned variable.
Some places with stack allocation function call information don't know
the number of arguments at compile time, and currently variably sized
stack allocations aren't allowed in postgres. Therefore allow for
FUNC_MAX_ARGS space in these cases. They're not that common, so for
now that seems acceptable.
Because of the need to allocate FunctionCallInfo of the appropriate
size, older extensions may need to update their code. To avoid subtle
breakages, the FunctionCallInfoData struct has been renamed to
FunctionCallInfoBaseData. Most code only references FunctionCallInfo,
so that shouldn't cause much collateral damage.
This change is also a prerequisite for more efficient expression JIT
compilation (by allocating the function call information on the stack,
allowing LLVM to optimize it away); previously the size of the call
information caused problems inside LLVM's optimizer.
Author: Andres Freund
Reviewed-By: Tom Lane
Discussion: https://postgr.es/m/20180605172952.x34m5uz6ju6enaem@alap3.anarazel.de
2019-01-26 23:17:52 +01:00
|
|
|
extra->fcinfo.args[0].isnull = false;
|
|
|
|
extra->fcinfo.args[1].isnull = false;
|
2011-12-07 06:18:38 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|