1/*-------------------------------------------------------------------------
4 * Support routines for accelerated sorting.
7 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/utils/sort/sortsupport.c
13 *-------------------------------------------------------------------------
26/* Info needed to use an old-style comparison function as a sort comparator */
33 #define SizeForSortShimExtra(nargs) (offsetof(SortShimExtra, fcinfo) + SizeForFunctionCallInfo(nargs))
36 * Shim function for calling an old-style comparator
38 * This is essentially an inlined version of FunctionCall2Coll(), except
39 * we assume that the FunctionCallInfoBaseData was already mostly set up by
40 * PrepareSortSupportComparisonShim.
51 /* just for paranoia's sake, we reset isnull each time */
56 /* Check for null result, since caller is clearly not expecting one */
64 * Set up a shim function to allow use of an old-style btree comparison
65 * function as if it were a sort support comparator.
75 /* Lookup the comparison function */
78 /* We can initialize the callinfo just once and re-use it */
89 * Look up and call sortsupport function to setup SortSupport comparator;
90 * or if no such function exists or it declines to set up the appropriate
91 * state, prepare a suitable shim.
96 Oid sortSupportFunction;
98 /* Look for a sort support function */
104 * The sort support function can provide a comparator, but it can also
105 * choose not to so (e.g. based on the selected collation).
118 elog(
ERROR,
"missing support function %d(%u,%u) in opfamily %u",
121 /* We'll use a shim to call the old-style btree comparator */
127 * Fill in SortSupport given an ordering operator (btree "<" or ">" operator).
129 * Caller must previously have zeroed the SortSupportData structure and then
130 * filled in ssup_cxt, ssup_collation, and ssup_nulls_first. This will fill
131 * in ssup_reverse as well as the comparator function pointer.
142 /* Find the operator in pg_amop */
145 elog(
ERROR,
"operator %u is not a valid ordering operator",
153 * Fill in SortSupport given an index relation and attribute.
155 * Caller must previously have zeroed the SortSupportData structure and then
156 * filled in ssup_cxt, ssup_attno, ssup_collation, and ssup_nulls_first. This
157 * will fill in ssup_reverse (based on the supplied argument), as well as the
158 * comparator function pointer.
170 elog(
ERROR,
"unexpected non-amcanorder AM: %u", indexRel->
rd_rel->relam);
177 * Fill in SortSupport given a GiST index relation
179 * Caller must previously have zeroed the SortSupportData structure and then
180 * filled in ssup_cxt, ssup_attno, ssup_collation, and ssup_nulls_first. This
181 * will fill in ssup_reverse (always false for GiST index build), as well as
182 * the comparator function pointer.
189 Oid sortSupportFunction;
193 if (indexRel->
rd_rel->relam != GIST_AM_OID)
198 * Look up the sort support function. This is simpler than for B-tree
199 * indexes because we don't support the old-style btree comparators.
204 elog(
ERROR,
"missing support function %d(%u,%u) in opfamily %u",
#define OidIsValid(objectId)
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
#define OidFunctionCall1(functionId, arg1)
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
#define FunctionCallInvoke(fcinfo)
#define GIST_SORTSUPPORT_PROC
Assert(PointerIsAligned(start, uint64))
bool get_ordering_op_properties(Oid opno, Oid *opfamily, Oid *opcintype, CompareType *cmptype)
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
void * MemoryContextAlloc(MemoryContext context, Size size)
#define BTSORTSUPPORT_PROC
static Datum PointerGetDatum(const void *X)
static int32 DatumGetInt32(Datum X)
static int comparison_shim(Datum x, Datum y, SortSupport ssup)
void PrepareSortSupportFromGistIndexRel(Relation indexRel, SortSupport ssup)
static void FinishSortSupportFunction(Oid opfamily, Oid opcintype, SortSupport ssup)
void PrepareSortSupportComparisonShim(Oid cmpFunc, SortSupport ssup)
void PrepareSortSupportFromIndexRel(Relation indexRel, bool reverse, SortSupport ssup)
void PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup)
#define SizeForSortShimExtra(nargs)
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
struct IndexAmRoutine * rd_indam
int(* comparator)(Datum x, Datum y, SortSupport ssup)