PostgreSQL Source Code  git master
sortsupport.c File Reference
#include "postgres.h"
#include "access/gist.h"
#include "access/nbtree.h"
#include "fmgr.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/sortsupport.h"
Include dependency graph for sortsupport.c:

Go to the source code of this file.

Data Structures

struct  SortShimExtra
 

Macros

#define SizeForSortShimExtra(nargs)   (offsetof(SortShimExtra, fcinfo) + SizeForFunctionCallInfo(nargs))
 

Functions

static int comparison_shim (Datum x, Datum y, SortSupport ssup)
 
void PrepareSortSupportComparisonShim (Oid cmpFunc, SortSupport ssup)
 
static void FinishSortSupportFunction (Oid opfamily, Oid opcintype, SortSupport ssup)
 
void PrepareSortSupportFromOrderingOp (Oid orderingOp, SortSupport ssup)
 
void PrepareSortSupportFromIndexRel (Relation indexRel, int16 strategy, SortSupport ssup)
 
void PrepareSortSupportFromGistIndexRel (Relation indexRel, SortSupport ssup)
 

Macro Definition Documentation

◆ SizeForSortShimExtra

#define SizeForSortShimExtra (   nargs)    (offsetof(SortShimExtra, fcinfo) + SizeForFunctionCallInfo(nargs))

Definition at line 33 of file sortsupport.c.

Function Documentation

◆ comparison_shim()

static int comparison_shim ( Datum  x,
Datum  y,
SortSupport  ssup 
)
static

Definition at line 43 of file sortsupport.c.

44 {
45  SortShimExtra *extra = (SortShimExtra *) ssup->ssup_extra;
46  Datum result;
47 
48  extra->fcinfo.args[0].value = x;
49  extra->fcinfo.args[1].value = y;
50 
51  /* just for paranoia's sake, we reset isnull each time */
52  extra->fcinfo.isnull = false;
53 
54  result = FunctionCallInvoke(&extra->fcinfo);
55 
56  /* Check for null result, since caller is clearly not expecting one */
57  if (extra->fcinfo.isnull)
58  elog(ERROR, "function %u returned NULL", extra->flinfo.fn_oid);
59 
60  return result;
61 }
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:172
int y
Definition: isn.c:71
int x
Definition: isn.c:70
uintptr_t Datum
Definition: postgres.h:64
Oid fn_oid
Definition: fmgr.h:59
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
Datum value
Definition: postgres.h:75
FmgrInfo flinfo
Definition: sortsupport.c:29
FunctionCallInfoBaseData fcinfo
Definition: sortsupport.c:30
void * ssup_extra
Definition: sortsupport.h:87

References FunctionCallInfoBaseData::args, elog, ERROR, SortShimExtra::fcinfo, SortShimExtra::flinfo, FmgrInfo::fn_oid, FunctionCallInvoke, FunctionCallInfoBaseData::isnull, SortSupportData::ssup_extra, NullableDatum::value, x, and y.

Referenced by PrepareSortSupportComparisonShim().

◆ FinishSortSupportFunction()

static void FinishSortSupportFunction ( Oid  opfamily,
Oid  opcintype,
SortSupport  ssup 
)
static

Definition at line 94 of file sortsupport.c.

95 {
96  Oid sortSupportFunction;
97 
98  /* Look for a sort support function */
99  sortSupportFunction = get_opfamily_proc(opfamily, opcintype, opcintype,
101  if (OidIsValid(sortSupportFunction))
102  {
103  /*
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).
106  */
107  OidFunctionCall1(sortSupportFunction, PointerGetDatum(ssup));
108  }
109 
110  if (ssup->comparator == NULL)
111  {
112  Oid sortFunction;
113 
114  sortFunction = get_opfamily_proc(opfamily, opcintype, opcintype,
115  BTORDER_PROC);
116 
117  if (!OidIsValid(sortFunction))
118  elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
119  BTORDER_PROC, opcintype, opcintype, opfamily);
120 
121  /* We'll use a shim to call the old-style btree comparator */
122  PrepareSortSupportComparisonShim(sortFunction, ssup);
123  }
124 }
#define OidIsValid(objectId)
Definition: c.h:754
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:679
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:796
#define BTORDER_PROC
Definition: nbtree.h:707
#define BTSORTSUPPORT_PROC
Definition: nbtree.h:708
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
unsigned int Oid
Definition: postgres_ext.h:31
void PrepareSortSupportComparisonShim(Oid cmpFunc, SortSupport ssup)
Definition: sortsupport.c:68
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:106

References BTORDER_PROC, BTSORTSUPPORT_PROC, SortSupportData::comparator, elog, ERROR, get_opfamily_proc(), OidFunctionCall1, OidIsValid, PointerGetDatum(), and PrepareSortSupportComparisonShim().

Referenced by PrepareSortSupportFromIndexRel(), and PrepareSortSupportFromOrderingOp().

◆ PrepareSortSupportComparisonShim()

void PrepareSortSupportComparisonShim ( Oid  cmpFunc,
SortSupport  ssup 
)

Definition at line 68 of file sortsupport.c.

69 {
70  SortShimExtra *extra;
71 
72  extra = (SortShimExtra *) MemoryContextAlloc(ssup->ssup_cxt,
74 
75  /* Lookup the comparison function */
76  fmgr_info_cxt(cmpFunc, &extra->flinfo, ssup->ssup_cxt);
77 
78  /* We can initialize the callinfo just once and re-use it */
79  InitFunctionCallInfoData(extra->fcinfo, &extra->flinfo, 2,
80  ssup->ssup_collation, NULL, NULL);
81  extra->fcinfo.args[0].isnull = false;
82  extra->fcinfo.args[1].isnull = false;
83 
84  ssup->ssup_extra = extra;
86 }
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:137
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:150
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1181
static int comparison_shim(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.c:43
#define SizeForSortShimExtra(nargs)
Definition: sortsupport.c:33
bool isnull
Definition: postgres.h:77
MemoryContext ssup_cxt
Definition: sortsupport.h:66

References FunctionCallInfoBaseData::args, SortSupportData::comparator, comparison_shim(), SortShimExtra::fcinfo, SortShimExtra::flinfo, fmgr_info_cxt(), InitFunctionCallInfoData, NullableDatum::isnull, MemoryContextAlloc(), SizeForSortShimExtra, SortSupportData::ssup_collation, SortSupportData::ssup_cxt, and SortSupportData::ssup_extra.

Referenced by FinishSortSupportFunction(), and MJExamineQuals().

◆ PrepareSortSupportFromGistIndexRel()

void PrepareSortSupportFromGistIndexRel ( Relation  indexRel,
SortSupport  ssup 
)

Definition at line 188 of file sortsupport.c.

189 {
190  Oid opfamily = indexRel->rd_opfamily[ssup->ssup_attno - 1];
191  Oid opcintype = indexRel->rd_opcintype[ssup->ssup_attno - 1];
192  Oid sortSupportFunction;
193 
194  Assert(ssup->comparator == NULL);
195 
196  if (indexRel->rd_rel->relam != GIST_AM_OID)
197  elog(ERROR, "unexpected non-gist AM: %u", indexRel->rd_rel->relam);
198  ssup->ssup_reverse = false;
199 
200  /*
201  * Look up the sort support function. This is simpler than for B-tree
202  * indexes because we don't support the old-style btree comparators.
203  */
204  sortSupportFunction = get_opfamily_proc(opfamily, opcintype, opcintype,
206  if (!OidIsValid(sortSupportFunction))
207  elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
208  GIST_SORTSUPPORT_PROC, opcintype, opcintype, opfamily);
209  OidFunctionCall1(sortSupportFunction, PointerGetDatum(ssup));
210 }
#define Assert(condition)
Definition: c.h:837
#define GIST_SORTSUPPORT_PROC
Definition: gist.h:41
Oid * rd_opcintype
Definition: rel.h:208
Oid * rd_opfamily
Definition: rel.h:207
Form_pg_class rd_rel
Definition: rel.h:111
AttrNumber ssup_attno
Definition: sortsupport.h:81

References Assert, SortSupportData::comparator, elog, ERROR, get_opfamily_proc(), GIST_SORTSUPPORT_PROC, OidFunctionCall1, OidIsValid, PointerGetDatum(), RelationData::rd_opcintype, RelationData::rd_opfamily, RelationData::rd_rel, SortSupportData::ssup_attno, and SortSupportData::ssup_reverse.

Referenced by tuplesort_begin_index_gist().

◆ PrepareSortSupportFromIndexRel()

void PrepareSortSupportFromIndexRel ( Relation  indexRel,
int16  strategy,
SortSupport  ssup 
)

Definition at line 161 of file sortsupport.c.

163 {
164  Oid opfamily = indexRel->rd_opfamily[ssup->ssup_attno - 1];
165  Oid opcintype = indexRel->rd_opcintype[ssup->ssup_attno - 1];
166 
167  Assert(ssup->comparator == NULL);
168 
169  if (indexRel->rd_rel->relam != BTREE_AM_OID)
170  elog(ERROR, "unexpected non-btree AM: %u", indexRel->rd_rel->relam);
171  if (strategy != BTGreaterStrategyNumber &&
172  strategy != BTLessStrategyNumber)
173  elog(ERROR, "unexpected sort support strategy: %d", strategy);
174  ssup->ssup_reverse = (strategy == BTGreaterStrategyNumber);
175 
176  FinishSortSupportFunction(opfamily, opcintype, ssup);
177 }
static void FinishSortSupportFunction(Oid opfamily, Oid opcintype, SortSupport ssup)
Definition: sortsupport.c:94
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define BTLessStrategyNumber
Definition: stratnum.h:29

References Assert, BTGreaterStrategyNumber, BTLessStrategyNumber, SortSupportData::comparator, elog, ERROR, FinishSortSupportFunction(), RelationData::rd_opcintype, RelationData::rd_opfamily, RelationData::rd_rel, SortSupportData::ssup_attno, and SortSupportData::ssup_reverse.

Referenced by _bt_load(), tuplesort_begin_cluster(), and tuplesort_begin_index_btree().

◆ PrepareSortSupportFromOrderingOp()

void PrepareSortSupportFromOrderingOp ( Oid  orderingOp,
SortSupport  ssup 
)

Definition at line 134 of file sortsupport.c.

135 {
136  Oid opfamily;
137  Oid opcintype;
138  int16 strategy;
139 
140  Assert(ssup->comparator == NULL);
141 
142  /* Find the operator in pg_amop */
143  if (!get_ordering_op_properties(orderingOp, &opfamily, &opcintype,
144  &strategy))
145  elog(ERROR, "operator %u is not a valid ordering operator",
146  orderingOp);
147  ssup->ssup_reverse = (strategy == BTGreaterStrategyNumber);
148 
149  FinishSortSupportFunction(opfamily, opcintype, ssup);
150 }
signed short int16
Definition: c.h:481
bool get_ordering_op_properties(Oid opno, Oid *opfamily, Oid *opcintype, int16 *strategy)
Definition: lsyscache.c:207

References Assert, BTGreaterStrategyNumber, SortSupportData::comparator, elog, ERROR, FinishSortSupportFunction(), get_ordering_op_properties(), and SortSupportData::ssup_reverse.

Referenced by compute_scalar_stats(), ExecInitGatherMerge(), ExecInitIndexScan(), ExecInitMergeAppend(), multi_sort_add_dimension(), statext_mcv_serialize(), tuplesort_begin_datum(), and tuplesort_begin_heap().