Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

426
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda