pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #6551: PL/pgSQL: GET DIAGNOSTICS not working for first OUT parameter
4+ messages / 4 participants
[nested] [flat]

* BUG #6551: PL/pgSQL: GET DIAGNOSTICS not working for first OUT parameter
@ 2012-03-22 17:38 aburacze@gmail.com
  2012-03-22 17:53 ` Re: BUG #6551: PL/pgSQL: GET DIAGNOSTICS not working for first OUT parameter Kevin Grittner <Kevin.Grittner@wicourts.gov>
  2012-03-22 17:57 ` Re: BUG #6551: PL/pgSQL: GET DIAGNOSTICS not working for first OUT parameter Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 4+ messages in thread

From: aburacze@gmail.com @ 2012-03-22 17:38 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      6551
Logged by:          Adam Buraczewski
Email address:      aburacze@gmail.com
PostgreSQL version: 9.1.3
Operating system:   Linux (Fedora 16)
Description:        

Hi!

I have just found strange behaviour of PL/pgSQL in case of using OUT
parameters and GET DIAGNOSTICS var = ROW_COUNT (PostgreSQL 9.1.3). Here is a
self-contained example:

create table t (c integer);

create function p(out x1 integer, out x2 integer, out x3 integer)
as $$
begin
    insert into t values (1);
    get diagnostics x1 = row_count;
    insert into t values (2);
    get diagnostics x2 = row_count;
    insert into t values (3);
    get diagnostics x3 = row_count;
end;
$$ language plpgsql;

select * from p();

 x1 | x2 | x3 
----+----+----
    |  1 |  1

Why x1 is NULL instead of the value 1? I found a workaround: declare a
temporary variable inside the function then assign ROW_COUNT to that
variable and then assign temporary variable to the OUT parameter. It works
but probably is not the solution one could expect:

create function p_workaround(out x1 integer, out x2 integer, out x3
integer)
as $$
declare
    tmpvar integer;
begin
    insert into t values (1);
    get diagnostics tmpvar = row_count;
    x1 := tmpvar;
    insert into t values (2);
    get diagnostics x2 = row_count;
    insert into t values (3);
    get diagnostics x3 = row_count;
end;
$$ language plpgsql;

select * from p_workaround();

 x1 | x2 | x3 
----+----+----
  1 |  1 |  1

Best regards,
Adam




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

* Re: BUG #6551: PL/pgSQL: GET DIAGNOSTICS not working for first OUT parameter
  2012-03-22 17:38 BUG #6551: PL/pgSQL: GET DIAGNOSTICS not working for first OUT parameter aburacze@gmail.com
@ 2012-03-22 17:53 ` Kevin Grittner <Kevin.Grittner@wicourts.gov>
  1 sibling, 0 replies; 4+ messages in thread

From: Kevin Grittner @ 2012-03-22 17:53 UTC (permalink / raw)
  To: aburacze@gmail.com; pgsql-bugs

<aburacze@gmail.com> wrote:
 
>  x1 | x2 | x3 
> ----+----+----
>     |  1 |  1
 
To reduce the ambiguity about what value is coming from where, I
slightly modified the script before testing it against a recent HEAD
build:
 
create table t (c integer);

create function p(out x1 integer, out x2 integer, out x3 integer)
as $$
begin
    insert into t values (1),(2);
    get diagnostics x1 = row_count;
    insert into t values (3),(4),(5);
    get diagnostics x2 = row_count;
    insert into t values (6),(7),(8),(9),(10);
    get diagnostics x3 = row_count;
end;
$$ language plpgsql;

select * from p();
 
I got this:
 
 x1 | x2 | x3 
----+----+----
    |  3 |  5
(1 row)
 
-Kevin



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

* Re: BUG #6551: PL/pgSQL: GET DIAGNOSTICS not working for first OUT parameter
  2012-03-22 17:38 BUG #6551: PL/pgSQL: GET DIAGNOSTICS not working for first OUT parameter aburacze@gmail.com
@ 2012-03-22 17:57 ` Tom Lane <tgl@sss.pgh.pa.us>
  2012-03-22 19:16   ` Re: BUG #6551: PL/pgSQL: GET DIAGNOSTICS not working for first OUT parameter Adam Buraczewski <aburacze@gmail.com>
  1 sibling, 1 reply; 4+ messages in thread

From: Tom Lane @ 2012-03-22 17:57 UTC (permalink / raw)
  To: aburacze@gmail.com; +Cc: pgsql-bugs

aburacze@gmail.com writes:
> I have just found strange behaviour of PL/pgSQL in case of using OUT
> parameters and GET DIAGNOSTICS var = ROW_COUNT (PostgreSQL 9.1.3). Here is a
> self-contained example:

Wow, that's been broken since the beginning, apparently.  There's an
entirely bogus test in exec_stmt_getdiag():

		if (diag_item->target <= 0)
			continue;

It's not clear what the point of this is, since there is no case in
which the target variable should be invalid, but in any case the effect
is to ignore attempts to assign to any variable with dno zero --- which
will be the one first defined in the function, eg the first parameter
if any.  I guess I can see how this escaped detection for a long time,
but it's broken for sure.  Thanks for the report!

			regards, tom lane



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

* Re: BUG #6551: PL/pgSQL: GET DIAGNOSTICS not working for first OUT parameter
  2012-03-22 17:38 BUG #6551: PL/pgSQL: GET DIAGNOSTICS not working for first OUT parameter aburacze@gmail.com
  2012-03-22 17:57 ` Re: BUG #6551: PL/pgSQL: GET DIAGNOSTICS not working for first OUT parameter Tom Lane <tgl@sss.pgh.pa.us>
@ 2012-03-22 19:16   ` Adam Buraczewski <aburacze@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Adam Buraczewski @ 2012-03-22 19:16 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs

> Wow, that's been broken since the beginning, apparently.  There's an
> entirely bogus test in exec_stmt_getdiag():

Thanks for quick fix! :)

Best regards,

-- 
Adam Buraczewski



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


end of thread, other threads:[~2012-03-22 19:16 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2012-03-22 17:38 BUG #6551: PL/pgSQL: GET DIAGNOSTICS not working for first OUT parameter aburacze@gmail.com
2012-03-22 17:53 ` Kevin Grittner <Kevin.Grittner@wicourts.gov>
2012-03-22 17:57 ` Tom Lane <tgl@sss.pgh.pa.us>
2012-03-22 19:16   ` Adam Buraczewski <aburacze@gmail.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