Skip to content

Instantly share code, notes, and snippets.

@segasai
Created May 26, 2022 21:48
Show Gist options
  • Save segasai/12488c591ff7dd495ec78b42aef80631 to your computer and use it in GitHub Desktop.
Save segasai/12488c591ff7dd495ec78b42aef80631 to your computer and use it in GitHub Desktop.
#include "postgres.h"
#include "catalog/pg_type.h"
#include "utils/lsyscache.h"
#include "fmgr.h"
#include "utils/rangetypes.h"
#include "utils/multirangetypes.h"
PG_MODULE_MAGIC;
Datum myfun(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(myfun);
Datum myfun(PG_FUNCTION_ARGS)
{
int range_count=2;
RangeType* ranges[range_count];
TypeCacheEntry *rangetyp;
int typooid = INT8RANGEOID;
RangeBound lower;
RangeBound upper;
MultirangeType *mr;
//Oid mltrngtypid = get_fn_expr_rettype(fcinfo->flinfo);
//elog(WARNING, "%d",mltrngtypid);
//cache=get_range_io_data(fcinfo,INT8RANGEOID, IOFunc_input);
rangetyp = range_get_typcache(fcinfo, INT8RANGEOID);
lower.lower=true;
upper.lower=false;
lower.infinite=false;
upper.infinite=false;
lower.inclusive=true;
upper.inclusive=true;
for (int i=0;i<range_count;i++)
{
lower.val = Int64GetDatum(10*i);
upper.val = Int64GetDatum(10*i+5);
ranges[i] = make_range(rangetyp, &lower, &upper, false);
}
//MultirangeIOData *cache = get_multirange_io_data(fcinfo, typoid, IOFunc_input);
//rangetyp = cache->typcache->rngtype;
rangetyp = multirange_get_typcache(fcinfo, INT8MULTIRANGEOID);
mr = make_multirange(INT8MULTIRANGEOID, rangetyp->rngtype, range_count, ranges);
PG_RETURN_MULTIRANGE_P(mr);
// https://github.com/postgres/postgres/blob/07d683b54af854098cc559d4b8640905f9efa0ea/src/backend/utils/adt/multirangetypes.c#L641
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment