pg.ddx.io pgsql-interfaces@postgresql.org mailing list archive
help / color / mirror / Atom feedpersistent variables between cross-calls in C functions
5+ messages / 4 participants
[nested] [flat]
* persistent variables between cross-calls in C functions
@ 2003-03-21 19:53 ff <ff-@uol.com.br>
0 siblings, 1 reply; 5+ messages in thread
From: ff @ 2003-03-21 19:53 UTC (permalink / raw)
To: pgsql-interfaces
Dear all:
I could not find any way to store a value in a C function
between calls.
For example, the following function doesn't work as expected:
#include "postgres.h"
#include "fmgr.h"
PG_FUNCTION_INFO_V1(next_value);
Datum
next_value(PG_FUNCTION_ARGS)
{
static int n;
PG_RETURN_INT32(n);
n++;
}
select next_value();
will always return 0;
even if I declare n as a global variable outside the fucntion.
Thanks a lot.
Fabio Furia Silva
---
UOL, o melhor da Internet
http://www.uol.com.br/
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: persistent variables between cross-calls in C functions
@ 2003-03-22 05:20 Tom Lane <tgl@sss.pgh.pa.us>
parent: ff <ff-@uol.com.br>
0 siblings, 0 replies; 5+ messages in thread
From: Tom Lane @ 2003-03-22 05:20 UTC (permalink / raw)
To: ff <ff-@uol.com.br>; +Cc: pgsql-interfaces
"ff" <ff-@uol.com.br> writes:
> I could not find any way to store a value in a C function
> between calls.
A static variable such as you illustrated should work fine --- at least
for successive calls within a single session. It will not provide
communications across sessions. For that you'd need to use a file or
grab some shared memory (this is doable but bear in mind that it's a
very finite resource).
regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: persistent variables between cross-calls in C functions
@ 2003-03-23 20:14 ff <ff-@uol.com.br>
0 siblings, 0 replies; 5+ messages in thread
From: ff @ 2003-03-23 20:14 UTC (permalink / raw)
To: tgl@sss.pgh.pa.us; +Cc: pgsql-interfaces
> "ff" <ff-@uol.com.br> writes:
> > I could not find any way to store a value in a C function
> > between calls.
>
> A static variable such as you illustrated should work fine -
-- at least
> for successive calls within a single session. It will not p
rovide
> communications across sessions. For that you'd need to use
a file or
> grab some shared memory (this is doable but bear in mind tha
t it's a
> very finite resource).
>
> regards, tom lane
>
>
Thanks, Tom!
But it isn't working.
Even inside the same session (I do not want to provide
communications across sessions).
Is there any macro provided in any c header file that I
should use?
I am using postgresql version 7.3.2, on AIX 4.3 (and all
patches).
Thanks again.
---
UOL, o melhor da Internet
http://www.uol.com.br/
^ permalink raw reply [nested|flat] 5+ messages in thread
* Persistent variables between cross-calls in C functions
@ 2003-03-25 16:12 Fabio Furia Silva <ff-@uol.com.br>
0 siblings, 1 reply; 5+ messages in thread
From: Fabio Furia Silva @ 2003-03-25 16:12 UTC (permalink / raw)
To: pgsql-hackers@postgresql.org; pgsql-general@postgresql.org; pgsql-interfaces
Dear all:
I could not find any way to store a value in a C function between calls
inside the same session.
For example, the following function doesn't work as expected:
/********************************/
#include "postgres.h"
#include "fmgr.h"
PG_FUNCTION_INFO_V1(next_value);
Datum next_value(PG_FUNCTION_ARGS)
{
static int n;
PG_RETURN_INT32(n);
n++;
}
/********************************/
create function next_value()
returns integer as
'next.so' language C;
/********************************/
Calling
select next_value();
will always return 0,
even if I declare 'n' as a global variable outside the C function.
Thanks a lot.
Fabio Furia Silva
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Persistent variables between cross-calls in C functions
@ 2003-03-25 17:51 Stephan Szabo <sszabo@megazone23.bigpanda.com>
parent: Fabio Furia Silva <ff-@uol.com.br>
0 siblings, 0 replies; 5+ messages in thread
From: Stephan Szabo @ 2003-03-25 17:51 UTC (permalink / raw)
To: Fabio Furia Silva <ff-@uol.com.br>; +Cc: pgsql-hackers@postgresql.org; pgsql-general@postgresql.org; pgsql-interfaces
On Tue, 25 Mar 2003, Fabio Furia Silva wrote:
> For example, the following function doesn't work as expected:
>
> /********************************/
> #include "postgres.h"
> #include "fmgr.h"
>
> PG_FUNCTION_INFO_V1(next_value);
>
> Datum next_value(PG_FUNCTION_ARGS)
> {
> static int n;
> PG_RETURN_INT32(n);
> n++;
> }
>
> /********************************/
> create function next_value()
> returns integer as
> 'next.so' language C;
>
> /********************************/
> Calling
>
> select next_value();
>
> will always return 0,
> even if I declare 'n' as a global variable outside the C function.
Which seems right since the n++ is never executed (being after the return
statement). Does it do what you want if you increment first?
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2003-03-25 17:51 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-03-21 19:53 persistent variables between cross-calls in C functions ff <ff-@uol.com.br>
2003-03-22 05:20 ` Tom Lane <tgl@sss.pgh.pa.us>
2003-03-23 20:14 Re: persistent variables between cross-calls in C functions ff <ff-@uol.com.br>
2003-03-25 16:12 Persistent variables between cross-calls in C functions Fabio Furia Silva <ff-@uol.com.br>
2003-03-25 17:51 ` Re: Persistent variables between cross-calls in C functions Stephan Szabo <sszabo@megazone23.bigpanda.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox