SPI return

From: Patrick Handja <patrick(dot)bungama(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: SPI return
Date: 2021-03-04 22:42:02
Message-ID: CAOKRWVD6cyuzmQ2-=CxS29o_QR_Z0-Btc581CeR=4kLhbi-00g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Greetings,

I have spent a couple of days working on separated lib in PostgreSql, and I
am facing some issues with the return of data using SPI (Server Programming
Interface).
I have this simple get_tuples function used in the main function foobar. My
example is very simple too (Using a previously created table a with a
single column and with some data):
SELECT foobar('select * from a');
I am not able to get the array returned by get_tuples function, and I am
thinking it's SPI_finish(). When I tried to print my array tuples itens
after SPI_finish(), It is not working.

####################
###### My Code ######
####################
static char**
get_tuples(char *command) {
int ret;
int8 rows;
char **tuples;

SPI_connect();
ret = SPI_exec(command, 0);
rows = SPI_processed;

tuples = palloc(sizeof(char*)*rows);

if (ret > 0 && SPI_tuptable != NULL) {
TupleDesc tupdesc = SPI_tuptable->tupdesc;
SPITupleTable *tuptable = SPI_tuptable;
uint64 j;

for (j = 0; j < rows; j++){
HeapTuple tuple = tuptable->vals[j];
int i;
for (i = 1; i <= tupdesc->natts; i++){
char *rowid;
rowid = SPI_getvalue(tuple, tupdesc, i);
tuples[j] = palloc(strlen(rowid)*sizeof(char));
tuples[j]= rowid;
}
}
}

// Printing my array to verify if I have all tuples, in fact I have all
of the
for (int i = 0; i < rows; ++i) {
elog(INFO, "Item: %s", *(tuples + i));
}

pfree(command);
SPI_finish();
return tuples;
}
####################
Datum
foobar (PG_FUNCTION_ARGS) {
char *command;
command = text_to_cstring(PG_GETARG_TEXT_PP(0));
get_tuples(command);

// *When I tried to do something like this, I am losing the connection
with the server (error)*
//elog(INFO, "*****: %s", *(get_tuples(command) + 1));
PG_RETURN_INT64(1);
}
####################
CREATE FUNCTION foobar(text) RETURNS int8
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
####################

regards,

*Andjasubu Bungama, Patrick *

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Ryan Lambert 2021-03-04 22:50:41 Re: Wired if-statement in gen_partprune_steps_internal
Previous Message Mark Dilger 2021-03-04 22:39:04 Re: pg_amcheck contrib application