pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
plpgsql: can I use a variable in a DECLARE later whithin the DECLARE?
3+ messages / 2 participants
[nested] [flat]

Commits from this thread:
  2021-10-29 a2a731d6c9  Test and document the behavior of initialization cross-refs in plpgsql.
* plpgsql: can I use a variable in a DECLARE later whithin the DECLARE?
@ 2021-10-29 12:14 tomas@tuxteam.de
  2021-10-29 14:04 ` Re: plpgsql: can I use a variable in a DECLARE later whithin the DECLARE? Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 3+ messages in thread

From: tomas@tuxteam.de @ 2021-10-29 12:14 UTC (permalink / raw)
  To: pgsql-hackers

Hi,

In plpgsql, i'm trying to use a variable initialised in a DECLARE in
a later initialisation expression whithin the same DECLARE:

  DECLARE
    x int := 23;
    y int := x + 4
  BEGIN
   ...

Experiments suggest that it works, but the docs are silent about that.
Is this a good idea, or should I be more careful and do it in a nested
block?

(For Lispers: is that a let, or a let*? ;-)

Thanks & cheers
 - tomás

Attachments:

  signature.asc (application/pgp-signature, 197B) download

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

* Re: plpgsql: can I use a variable in a DECLARE later whithin the DECLARE?
  2021-10-29 12:14 plpgsql: can I use a variable in a DECLARE later whithin the DECLARE? tomas@tuxteam.de
@ 2021-10-29 14:04 ` Tom Lane <tgl@sss.pgh.pa.us>
  2021-10-29 14:50   ` Re: plpgsql: can I use a variable in a DECLARE later whithin the DECLARE? tomas@tuxteam.de
  0 siblings, 1 reply; 3+ messages in thread

From: Tom Lane @ 2021-10-29 14:04 UTC (permalink / raw)
  To: tomas@tuxteam.de; +Cc: pgsql-hackers

<tomas@tuxteam.de> writes:
> In plpgsql, i'm trying to use a variable initialised in a DECLARE in
> a later initialisation expression whithin the same DECLARE:

>   DECLARE
>     x int := 23;
>     y int := x + 4
>   BEGIN
>    ...

> Experiments suggest that it works, but the docs are silent about that.
> Is this a good idea, or should I be more careful and do it in a nested
> block?

I don't see any reason to foresee that it would break.

However, I notice that we don't actually have any regression tests
checking this.  scope_test() in plpgsql.sql checks an adjacent
question, but not exactly this one.  I'm a bit inclined now to go
add such a test.

			regards, tom lane





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

* Re: plpgsql: can I use a variable in a DECLARE later whithin the DECLARE?
  2021-10-29 12:14 plpgsql: can I use a variable in a DECLARE later whithin the DECLARE? tomas@tuxteam.de
  2021-10-29 14:04 ` Re: plpgsql: can I use a variable in a DECLARE later whithin the DECLARE? Tom Lane <tgl@sss.pgh.pa.us>
@ 2021-10-29 14:50   ` tomas@tuxteam.de
  0 siblings, 0 replies; 3+ messages in thread

From: tomas@tuxteam.de @ 2021-10-29 14:50 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-hackers

On Fri, Oct 29, 2021 at 10:04:31AM -0400, Tom Lane wrote:
> <tomas@tuxteam.de> writes:
> > In plpgsql, i'm trying to use a variable initialised in a DECLARE in
> > a later initialisation expression whithin the same DECLARE:
> 
> >   DECLARE
> >     x int := 23;
> >     y int := x + 4
> >   BEGIN
> >    ...
> 
> > Experiments suggest that it works, but the docs are silent about that.
> > Is this a good idea, or should I be more careful and do it in a nested
> > block?
> 
> I don't see any reason to foresee that it would break.

Thanks :)

> However, I notice that we don't actually have any regression tests
> checking this.  scope_test() in plpgsql.sql checks an adjacent
> question, but not exactly this one.  I'm a bit inclined now to go
> add such a test.

Even more thanks.

After a long hiatus I have the luck to play again with PostgreSQL.
Let me say: you folks rock, seriously.

Cheers
 - t

Attachments:

  signature.asc (application/pgp-signature, 197B) download

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


end of thread, other threads:[~2021-10-29 14:50 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-10-29 12:14 plpgsql: can I use a variable in a DECLARE later whithin the DECLARE? tomas@tuxteam.de
2021-10-29 14:04 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-10-29 14:50   ` tomas@tuxteam.de

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