Hi ,
Here is my first PL/C Function. Find the Steps How to create and implement.
Step 1
======
-bash-3.2$ pwd
/opt/PostgresPlus/9.0AS
-bash-3.2$ more concat_text.c
#include "postgres.h"
#include "fmgr.h"
#include <string.h>
#include <unistd.h>
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
PG_FUNCTION_INFO_V1(concat_text);
Datum
concat_text(PG_FUNCTION_ARGS)
{
text *arg1 = PG_GETARG_TEXT_P(0);
text *arg2 = PG_GETARG_TEXT_P(1);
int32 new_text_size = VARSIZE(arg1) + VARSIZE(arg2) - VARHDRSZ;
text *new_text = (text *) palloc(new_text_size);
system("touch /tmp/table.txt");
SET_VARSIZE (new_text,new_text_size);
memcpy(VARDATA(new_text), VARDATA(arg1), VARSIZE(arg1) - VARHDRSZ);
memcpy(VARDATA(new_text) + (VARSIZE(arg1) - VARHDRSZ),
VARDATA(arg2), VARSIZE(arg2) - VARHDRSZ);
PG_RETURN_TEXT_P(new_text);
}
/opt/PostgresPlus/9.0AS
-bash-3.2$ more concat_text.c
#include "postgres.h"
#include "fmgr.h"
#include <string.h>
#include <unistd.h>
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
PG_FUNCTION_INFO_V1(concat_text);
Datum
concat_text(PG_FUNCTION_ARGS)
{
text *arg1 = PG_GETARG_TEXT_P(0);
text *arg2 = PG_GETARG_TEXT_P(1);
int32 new_text_size = VARSIZE(arg1) + VARSIZE(arg2) - VARHDRSZ;
text *new_text = (text *) palloc(new_text_size);
system("touch /tmp/table.txt");
SET_VARSIZE (new_text,new_text_size);
memcpy(VARDATA(new_text), VARDATA(arg1), VARSIZE(arg1) - VARHDRSZ);
memcpy(VARDATA(new_text) + (VARSIZE(arg1) - VARHDRSZ),
VARDATA(arg2), VARSIZE(arg2) - VARHDRSZ);
PG_RETURN_TEXT_P(new_text);
}
Step 2
=======
-bash-3.2$ more Makefile
PG_CONFIG = /opt/PostgresPlus/9.0AS/bin/pg_config
MODULES = concat_text
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
PG_CONFIG = /opt/PostgresPlus/9.0AS/bin/pg_config
MODULES = concat_text
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
Step 3
======
make and .so file to $PGHOME/lib or $PGHOME/lib/postgres/
Step 4
======
edb=# CREATE OR REPLACE FUNCTION concat_text(text, text) RETURNS text AS 'concat_text.so', 'concat_text' LANGUAGE C STRICT;
CREATE FUNCTION
CREATE FUNCTION
Step 5
======
edb=# select concat_text('Dinesh ','Kumar');
concat_text
--------------
Dinesh Kumar
(1 row)
concat_text
--------------
Dinesh Kumar
(1 row)
--Dinesh
Comments
Post a Comment