Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

424
Views
PostgreSQL: how to create a numrange in my own C function?

I'm writing a c function to make a range type on a float range. I find PostgreSQL allows using numrange(low, high) to create a nunrange, but I can't find the corresponding part in the C code. The only method I find(i hope it works) is

    TypeCacheEntry *cache=lookup_type_cache(3907,TYPECACHE_RANGE_INFO);
    RangeBound  lower;
    lower.val = Float8GetDatum(b.zmin);
    lower.infinite = false;
    lower.inclusive = false;
    lower.lower = true;
    RangeBound  upper;
    upper.val = Float8GetDatum(b.zmax);
    upper.infinite = false;
    upper.inclusive = false;
    upper.lower = true;
    return PointerGetDatum(range_serialize(cache, &lower,&upper,false));

However the solution seems ugly... Could anyone give me some instruction about how to return a numrange in a more beautiful way?

I use DirectFunctionCall like this following the instruction of @Laurenz Albe

Datum s1= DirectFunctionCall1(float8_numeric,Float8GetDatum(b.zmin));
Datum s2= DirectFunctionCall1(float8_numeric,Float8GetDatum(b.zmax));
return DirectFunctionCall2(range_constructor2,s1,s2);

And sadly got a SIGSIEV in range_get_typcache

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

First, find the name of the C function for the two-argument version of numrange:

SELECT prosrc
FROM pg_proc
WHERE proname = 'numrange'
  AND pronargs = 2;

       prosrc       
--------------------
 range_constructor2
(1 row)

Now find that function in the source:

> git grep range_constructor2

src/backend/commands/typecmds.c:    static const char *const prosrc[2] = {"range_constructor2",
src/backend/utils/adt/rangetypes.c:range_constructor2(PG_FUNCTION_ARGS)
src/include/catalog/pg_proc.dat:  proargtypes => 'int4 int4', prosrc => 'range_constructor2' },
src/include/catalog/pg_proc.dat:  proargtypes => 'numeric numeric', prosrc => 'range_constructor2' },
src/include/catalog/pg_proc.dat:  proargtypes => 'timestamp timestamp', prosrc => 'range_constructor2' },
src/include/catalog/pg_proc.dat:  proargtypes => 'timestamptz timestamptz', prosrc => 'range_constructor2' },
src/include/catalog/pg_proc.dat:  proargtypes => 'date date', prosrc => 'range_constructor2' },
src/include/catalog/pg_proc.dat:  proargtypes => 'int8 int8', prosrc => 'range_constructor2' },

So your function is in src/backend/utils/adt/rangetypes.c.

You should be able to call it with DirectFunctionCall2.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!