PostgreSQL Source Code  git master
sortsupport.c File Reference
#include "postgres.h"
#include "access/nbtree.h"
#include "catalog/pg_am.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
 

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)
 

Function Documentation

◆ comparison_shim()

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

Definition at line 42 of file sortsupport.c.

References FunctionCallInfoData::arg, elog, ERROR, SortShimExtra::fcinfo, SortShimExtra::flinfo, FmgrInfo::fn_oid, FunctionCallInvoke, FunctionCallInfoData::isnull, and SortSupportData::ssup_extra.

Referenced by PrepareSortSupportComparisonShim().

43 {
44  SortShimExtra *extra = (SortShimExtra *) ssup->ssup_extra;
45  Datum result;
46 
47  extra->fcinfo.arg[0] = x;
48  extra->fcinfo.arg[1] = y;
49 
50  /* just for paranoia's sake, we reset isnull each time */
51  extra->fcinfo.isnull = false;
52 
53  result = FunctionCallInvoke(&extra->fcinfo);
54 
55  /* Check for null result, since caller is clearly not expecting one */
56  if (extra->fcinfo.isnull)
57  elog(ERROR, "function %u returned NULL", extra->flinfo.fn_oid);
58 
59  return result;
60 }
#define ERROR
Definition: elog.h:43
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:137
void * ssup_extra
Definition: sortsupport.h:87
uintptr_t Datum
Definition: postgres.h:372
FunctionCallInfoData fcinfo
Definition: sortsupport.c:29
Oid fn_oid
Definition: fmgr.h:59
Datum arg[FUNC_MAX_ARGS]
Definition: fmgr.h:85
FmgrInfo flinfo
Definition: sortsupport.c:30
#define elog
Definition: elog.h:219

◆ FinishSortSupportFunction()

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

Definition at line 93 of file sortsupport.c.

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

Referenced by PrepareSortSupportFromIndexRel(), and PrepareSortSupportFromOrderingOp().

94 {
95  Oid sortSupportFunction;
96 
97  /* Look for a sort support function */
98  sortSupportFunction = get_opfamily_proc(opfamily, opcintype, opcintype,
100  if (OidIsValid(sortSupportFunction))
101  {
102  /*
103  * The sort support function can provide a comparator, but it can also
104  * choose not to so (e.g. based on the selected collation).
105  */
106  OidFunctionCall1(sortSupportFunction, PointerGetDatum(ssup));
107  }
108 
109  if (ssup->comparator == NULL)
110  {
111  Oid sortFunction;
112 
113  sortFunction = get_opfamily_proc(opfamily, opcintype, opcintype,
114  BTORDER_PROC);
115 
116  if (!OidIsValid(sortFunction))
117  elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
118  BTORDER_PROC, opcintype, opcintype, opfamily);
119 
120  /* We'll use a shim to call the old-style btree comparator */
121  PrepareSortSupportComparisonShim(sortFunction, ssup);
122  }
123 }
#define BTORDER_PROC
Definition: nbtree.h:229
#define BTSORTSUPPORT_PROC
Definition: nbtree.h:230
void PrepareSortSupportComparisonShim(Oid cmpFunc, SortSupport ssup)
Definition: sortsupport.c:67
#define PointerGetDatum(X)
Definition: postgres.h:562
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:576
#define ERROR
Definition: elog.h:43
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:623
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:107
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:744
#define elog
Definition: elog.h:219

◆ PrepareSortSupportComparisonShim()

void PrepareSortSupportComparisonShim ( Oid  cmpFunc,
SortSupport  ssup 
)

Definition at line 67 of file sortsupport.c.

References FunctionCallInfoData::argnull, SortSupportData::comparator, comparison_shim(), SortShimExtra::fcinfo, SortShimExtra::flinfo, fmgr_info_cxt(), InitFunctionCallInfoData, MemoryContextAlloc(), SortSupportData::ssup_collation, SortSupportData::ssup_cxt, and SortSupportData::ssup_extra.

Referenced by ApplySortAbbrevFullComparator(), FinishSortSupportFunction(), and MJExamineQuals().

68 {
69  SortShimExtra *extra;
70 
71  extra = (SortShimExtra *) MemoryContextAlloc(ssup->ssup_cxt,
72  sizeof(SortShimExtra));
73 
74  /* Lookup the comparison function */
75  fmgr_info_cxt(cmpFunc, &extra->flinfo, ssup->ssup_cxt);
76 
77  /* We can initialize the callinfo just once and re-use it */
78  InitFunctionCallInfoData(extra->fcinfo, &extra->flinfo, 2,
79  ssup->ssup_collation, NULL, NULL);
80  extra->fcinfo.argnull[0] = false;
81  extra->fcinfo.argnull[1] = false;
82 
83  ssup->ssup_extra = extra;
85 }
MemoryContext ssup_cxt
Definition: sortsupport.h:66
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:107
void * ssup_extra
Definition: sortsupport.h:87
bool argnull[FUNC_MAX_ARGS]
Definition: fmgr.h:86
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:132
static int comparison_shim(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.c:42
FunctionCallInfoData fcinfo
Definition: sortsupport.c:29
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:120
FmgrInfo flinfo
Definition: sortsupport.c:30
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:706

◆ PrepareSortSupportFromIndexRel()

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

Definition at line 160 of file sortsupport.c.

References Assert, BTGreaterStrategyNumber, BTLessStrategyNumber, BTREE_AM_OID, 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(), ApplySortAbbrevFullComparator(), tuplesort_begin_cluster(), and tuplesort_begin_index_btree().

162 {
163  Oid opfamily = indexRel->rd_opfamily[ssup->ssup_attno - 1];
164  Oid opcintype = indexRel->rd_opcintype[ssup->ssup_attno - 1];
165 
166  Assert(ssup->comparator == NULL);
167 
168  if (indexRel->rd_rel->relam != BTREE_AM_OID)
169  elog(ERROR, "unexpected non-btree AM: %u", indexRel->rd_rel->relam);
170  if (strategy != BTGreaterStrategyNumber &&
171  strategy != BTLessStrategyNumber)
172  elog(ERROR, "unexpected sort support strategy: %d", strategy);
173  ssup->ssup_reverse = (strategy == BTGreaterStrategyNumber);
174 
175  FinishSortSupportFunction(opfamily, opcintype, ssup);
176 }
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define BTREE_AM_OID
Definition: pg_am.h:70
Form_pg_class rd_rel
Definition: rel.h:114
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:107
Oid * rd_opfamily
Definition: rel.h:182
static void FinishSortSupportFunction(Oid opfamily, Oid opcintype, SortSupport ssup)
Definition: sortsupport.c:93
AttrNumber ssup_attno
Definition: sortsupport.h:81
#define Assert(condition)
Definition: c.h:670
#define elog
Definition: elog.h:219
Oid * rd_opcintype
Definition: rel.h:183
#define BTLessStrategyNumber
Definition: stratnum.h:29

◆ PrepareSortSupportFromOrderingOp()

void PrepareSortSupportFromOrderingOp ( Oid  orderingOp,
SortSupport  ssup 
)

Definition at line 133 of file sortsupport.c.

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

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

134 {
135  Oid opfamily;
136  Oid opcintype;
137  int16 strategy;
138 
139  Assert(ssup->comparator == NULL);
140 
141  /* Find the operator in pg_amop */
142  if (!get_ordering_op_properties(orderingOp, &opfamily, &opcintype,
143  &strategy))
144  elog(ERROR, "operator %u is not a valid ordering operator",
145  orderingOp);
146  ssup->ssup_reverse = (strategy == BTGreaterStrategyNumber);
147 
148  FinishSortSupportFunction(opfamily, opcintype, ssup);
149 }
signed short int16
Definition: c.h:283
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:107
static void FinishSortSupportFunction(Oid opfamily, Oid opcintype, SortSupport ssup)
Definition: sortsupport.c:93
#define Assert(condition)
Definition: c.h:670
bool get_ordering_op_properties(Oid opno, Oid *opfamily, Oid *opcintype, int16 *strategy)
Definition: lsyscache.c:204
#define elog
Definition: elog.h:219