Recursive stored procedure in C.

From: Leif Jensen <leif(at)crysberg(dot)dk>
To: pgsql-general(at)postgresql(dot)org
Subject: Recursive stored procedure in C.
Date: 2005-07-14 11:28:10
Message-ID: Pine.LNX.4.58.0507141313350.6560@samba.crysberg.dk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


Hi all,

I am trying to make a stored procedure in C that is used as a trigger
on before/after insert/update on a certain table. This procedure might do
inserts/updates on the same table (recursively triggering itself). I have
made (pretty) sure that I'm not using 'global' variables in this module
and that I do an SPI_connect() the very first time I enter and an
SPI_finish() at the last exit.

This works to some point, but in some instances I get
SPI_ERROR_UNCONNECTED from an SPI_exec() call a few recursive layers down,
e.g. the following code yields:

ret = SPI_exec( sql_query, 0 ); => SPI_ERROR_UNCONNECTED
if( ret == SPI_ERROR_UNCONNECTED ) {
ret = SPI_finish(); => SPI_ERROR_UNCONNECTED
ret = SPI_connect(); => SPI_ERROR_CONNECT
ret = SPI_exec( sql_query, 0 ); => SPI_ERROR_UNCONNECTED
}

??? (This 'if' was mainly to try to figure out what was going on, since
I should not do an SPI_finish() at all until the very end.)

Is such recursivity at all possible ? What are the pitfalls ? Any good
examples out there?

Any suggestions at all will be much appreciated,

Leif

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Magnus Hagander 2005-07-14 11:37:56 Re: To Postgres or not
Previous Message Joachim Wieland 2005-07-14 08:41:51 Re: Transparent encryption in PostgreSQL?