Header And Logo

| The world's most advanced open source database.

Data Structures | Defines | Functions

sortsupport.c File Reference

#include "postgres.h"
#include "fmgr.h"
#include "utils/lsyscache.h"
#include "utils/sortsupport.h"
Include dependency graph for sortsupport.c:

Go to the source code of this file.

Data Structures

struct  SortShimExtra




static int comparison_shim (Datum x, Datum y, SortSupport ssup)
void PrepareSortSupportComparisonShim (Oid cmpFunc, SortSupport ssup)
void PrepareSortSupportFromOrderingOp (Oid orderingOp, SortSupport ssup)

Define Documentation


Definition at line 19 of file sortsupport.c.

Function Documentation

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.

    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;

void PrepareSortSupportComparisonShim ( Oid  cmpFunc,
SortSupport  ssup 

Definition at line 67 of file sortsupport.c.

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

Referenced by MJExamineQuals(), and PrepareSortSupportFromOrderingOp().

    SortShimExtra *extra;

    extra = (SortShimExtra *) MemoryContextAlloc(ssup->ssup_cxt,

    /* 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;

void PrepareSortSupportFromOrderingOp ( Oid  orderingOp,
SortSupport  ssup 

Definition at line 95 of file sortsupport.c.

References Assert, SortSupportData::comparator, elog, ERROR, get_sort_function_for_ordering_op(), NULL, OidFunctionCall1, PointerGetDatum, PrepareSortSupportComparisonShim(), and SortSupportData::ssup_reverse.

Referenced by compute_scalar_stats(), ExecInitMergeAppend(), tuplesort_begin_datum(), and tuplesort_begin_heap().

    Oid         sortFunction;
    bool        issupport;

    if (!get_sort_function_for_ordering_op(orderingOp,
        elog(ERROR, "operator %u is not a valid ordering operator",

    if (issupport)
        /* The sort support function should provide a comparator */
        OidFunctionCall1(sortFunction, PointerGetDatum(ssup));
        Assert(ssup->comparator != NULL);
        /* We'll use a shim to call the old-style btree comparator */
        PrepareSortSupportComparisonShim(sortFunction, ssup);