Skip to main content

Posts

Showing posts with the label PL/C in PostgreSQL

OS Results From PostgreSQL

Hi , As of now, we are using "\i" or using "PL/SH" script for getting the OS command results from PostgreSQL. However, below is the one more option where we can get the required info. Step 1 ====== #include "postgres.h" #include "fmgr.h" #include <string.h> #include <unistd.h> #ifdef PG_MODULE_MAGIC PG_MODULE_MAGIC; #endif PG_FUNCTION_INFO_V1(shell_exec); Datum shell_exec(PG_FUNCTION_ARGS) {     text  *arg1 = PG_GETARG_TEXT_P(0);     char  *Command=VARDATA(arg1);     int32 result=system(Command);        PG_RETURN_TEXT_P(result); } Step 2 ======= -bash-3.2$ more Makefile PG_CONFIG = /opt/PostgresPlus/9.0AS/bin/pg_config MODULES = shell_exec PGXS := $(shell $(PG_CONFIG) --pgxs) include $(PGXS) Step 3 ====== make and .so file to $PGHOME/lib  or $PGHOME/lib/postgres/ Step 4 ====== postgres=# CREATE OR REPLACE FUNCTION shell_exec(text)...

PostgreSQL PL/C Functions

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),            VARDA...