Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

425
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda