pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
OUT parameter
5+ messages / 3 participants
[nested] [flat]

* OUT parameter
@ 2006-03-22 22:34 Daniel Caune <daniel.caune@ubisoft.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Daniel Caune @ 2006-03-22 22:34 UTC (permalink / raw)
  To: pgsql-sql

Hi,

Is there any suggestion against using OUT parameter for local
calculation such as using a local variable?

CREATE OR REPLACE FUNCTION foo(a IN int,
                               b1 OUT int,
                               b2 OUT int)
AS $$
BEGIN
  FOR (...) LOOP
    b1 = (...);
    b2 = (...);
  END LOOP;
END;
$$ LANGUAGE PLPGSQL;

or for some reasons (performance or whatever other details of
implementation), would it be preferable to use local variable and to
initialize the OUT parameters at the end?

CREATE OR REPLACE FUNCTION foo(a IN int,
                               b1 OUT int,
                               b2 OUT int)
AS $$
  V_b1 int;
  V_b2 int;
BEGIN
  FOR (...) LOOP
    V_b1 = (...);
    V_b2 = (...);
  END LOOP;

  b1 = V_b1;
  b2 = V_b2;
END;
$$ LANGUAGE PLPGSQL;

Thanks,


--
Daniel CAUNE
Ubisoft Online Technology
(514) 4090 2040 ext. 5418




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: OUT parameter
@ 2006-03-22 22:39 Owen Jacobson <ojacobson@osl.com>
  2006-03-23 02:47 ` Re: OUT parameter Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 5+ messages in thread

From: Owen Jacobson @ 2006-03-22 22:39 UTC (permalink / raw)
  To: pgsql-sql

Daniel Caune wrote:

> Is there any suggestion against using OUT parameter for local
> calculation such as using a local variable?
> 
> CREATE OR REPLACE FUNCTION foo(a IN int,
>                                b1 OUT int,
>                                b2 OUT int)
> AS $$
> BEGIN
>   FOR (...) LOOP
>     b1 = (...);
>     b2 = (...);
>   END LOOP;
> END;
> $$ LANGUAGE PLPGSQL;

I'd say there's no problem with this, PROVIDED you can ensure you'll never abort before completing the computation.  It's not a good idea to modify out parameters partway; programmers (myself included) have this nasty habit of assuming, rightly or wrongly, that a failed function call won't have destroyed the parameters.

If you can't ensure you'll always complete, use locals.

-Owen



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: OUT parameter
  2006-03-22 22:39 Re: OUT parameter Owen Jacobson <ojacobson@osl.com>
@ 2006-03-23 02:47 ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 5+ messages in thread

From: Tom Lane @ 2006-03-23 02:47 UTC (permalink / raw)
  To: Owen Jacobson <ojacobson@osl.com>; +Cc: pgsql-sql

> Daniel Caune wrote:
>> Is there any suggestion against using OUT parameter for local
>> calculation such as using a local variable?

In plpgsql (at least in the current implementation) an OUT parameter is
pretty much just a local variable, and so there's no efficiency argument
against using it as a temporary.  Whether you consider this good style
is a matter of opinion.

"Owen Jacobson" <ojacobson@osl.com> writes:
> I'd say there's no problem with this, PROVIDED you can ensure you'll
> never abort before completing the computation.

Not really an issue in Postgres: we do not support pass-by-reference
parameters and are unlikely to start doing so.  There isn't any way
that you can affect locals of a calling procedure before you return.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: OUT parameter
@ 2006-03-23 17:12 Owen Jacobson <ojacobson@osl.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Owen Jacobson @ 2006-03-23 17:12 UTC (permalink / raw)
  To: pgsql-sql

Tom Lane wrote:

> "Owen Jacobson" <ojacobson@osl.com> writes:
> > I'd say there's no problem with this, PROVIDED you can ensure you'll
> > never abort before completing the computation.
> 
> Not really an issue in Postgres: we do not support pass-by-reference
> parameters and are unlikely to start doing so.  There isn't any way
> that you can affect locals of a calling procedure before you return.

Then I've misunderstood the semantics of OUT and more importantly INOUT parameters.  Thanks for the correction; I'm reading Daniel Caune's notes on the docs now.

-Owen



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: OUT parameter
@ 2006-03-23 19:20 Daniel Caune <daniel.caune@ubisoft.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Daniel Caune @ 2006-03-23 19:20 UTC (permalink / raw)
  To: Owen Jacobson <ojacobson@osl.com>; pgsql-sql


> > "Owen Jacobson" <ojacobson@osl.com> writes:
> > > I'd say there's no problem with this, PROVIDED you can ensure
you'll
> > > never abort before completing the computation.
> >
> > Not really an issue in Postgres: we do not support pass-by-reference
> > parameters and are unlikely to start doing so.  There isn't any way
> > that you can affect locals of a calling procedure before you return.
> 
> Then I've misunderstood the semantics of OUT and more importantly
INOUT
> parameters.  Thanks for the correction; I'm reading Daniel Caune's
notes
> on the docs now.
> 
> -Owen

Funny!  I started that thread on OUT parameter; that's a kind of
circle... :-)

--
Daniel CAUNE
Ubisoft Online Technology
(514) 4090 2040 ext. 5418



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2006-03-23 19:20 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-03-22 22:34 OUT parameter Daniel Caune <daniel.caune@ubisoft.com>
2006-03-22 22:39 Re: OUT parameter Owen Jacobson <ojacobson@osl.com>
2006-03-23 02:47 ` Tom Lane <tgl@sss.pgh.pa.us>
2006-03-23 17:12 Re: OUT parameter Owen Jacobson <ojacobson@osl.com>
2006-03-23 19:20 Re: OUT parameter Daniel Caune <daniel.caune@ubisoft.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