From: | jian he <jian(dot)universality(at)gmail(dot)com> |
---|---|
To: | pgsql-general list <pgsql-general(at)lists(dot)postgresql(dot)org> |
Subject: | 38.10.6. Composite-Type Arguments C-language function code demo works for int, but not for numeric. |
Date: | 2023-02-01 10:36:59 |
Message-ID: | CACJufxHK=90nZ-U7ekwS7jrss6h-pw9VSVCK-Jq7Jmx8iLL-1A@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Hi.
source:
https://www.postgresql.org/docs/current/xfunc-c.html#XFUNC-C-BASETYPE
(38.10.6. Composite-Type Arguments)
column "salary" int data type works fine. But it does not work if the
"salary" column data type is numeric.
/*
rm funcs.so && rm funcs.o
gcc -I/home/jian/postgres/pg16/include/postgresql/server -fPIC -c funcs.c
gcc -shared -o funcs.so funcs.o
*/
#include "postgres.h"
#include "executor/executor.h"
#include "utils/numeric.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(c_overaid);
Datum
c_overaid(PG_FUNCTION_ARGS)
{
HeapTupleHeader t = PG_GETARG_HEAPTUPLEHEADER(0);
Numeric limit = PG_GETARG_NUMERIC_COPY(1);
bool isnull;
Datum salary;
salary = GetAttributeByName(t,"salary",&isnull);
if(isnull)
PG_RETURN_BOOL(false);
/* alternatively, we might prefer to do PG_RETURN_NULL() for null
salary */
PG_RETURN_BOOL(DatumGetNumericCopy(salary) > limit);
}
---------------------------------------------------------------------------
then quit the session and reconnect the session.
drop table emp cascade;
create table emp(empid bigint, salary numeric, name text);
insert into emp values(1,1000,'a1');
insert into emp values(2,1100,'a2');
insert into emp values(3,1200,'a3');
CREATE OR REPLACE FUNCTION c_overaid(emp, numeric) returns boolean AS
'/home/jian/pg_ext/misc/funcs','c_overaid' LANGUAGE C STRICT;
select name, c_overaid(emp,numeric '1110') as over_paid from emp;
it returns
> name | over_paid
> ------+-----------
> a1 | t
> a2 | t
> a3 | t
> (3 rows)
>
I also tried PG_GETARG_NUMERIC,DatumGetNumeric(salary).
From | Date | Subject | |
---|---|---|---|
Next Message | Erik Wienhold | 2023-02-01 11:54:18 | Re: database postgres not found |
Previous Message | Matthias Apitz | 2023-02-01 10:33:19 | database postgres not found |