Re: PLPGSQL and FOUND stange behaviour after EXECUTE

Lists: pgsql-bugs
From: Константин <beholder(at)mmska(dot)ru>
To: <pgsql-bugs(at)postgresql(dot)org>
Subject: PLPGSQL and FOUND stange behaviour after EXECUTE
Date: 2004-09-30 16:02:59
Message-ID: 000001c4a706$f56229b0$b632030a@kobzev
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

Hello,
FreeBSD, I386, Postgres 8.0.0 beta2

Trying issuing such a plpgsql function:
Create function test_fun (suffix char(4)) returns integer as $$
DECLARE
sql text;
BEGIN
sql := 'insert into tbl' || suffix::text || ' values (1,1)';
EXECUTE sql;
IF NOT FOUND THEN
RAISE NOTICE 'NOT INSERTED';
END IF;
return 1;
END;
$$ LANGUAGE plpgsql;
create table tbl_a (id integer,name integer);

db# select test_fun('_a');
NOTICE: NOT INSERTED
test_fun
----------
1
(1 row)

db# # select * from tbl_a;
id | name
----+------
1 | 1
(1 row)

When I trying monitoring the result with GET DIAGNOSTICS - all fine, but
with FOUND it doesn't work.

Thank You in advance.

Konstantin


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Константин <beholder(at)mmska(dot)ru>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: PLPGSQL and FOUND stange behaviour after EXECUTE
Date: 2004-09-30 16:26:02
Message-ID: 17803.1096561562@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

=?koi8-r?B?68/O09TBztTJzg==?= <beholder(at)mmska(dot)ru> writes:
> sql := 'insert into tbl' || suffix::text || ' values (1,1)';
> EXECUTE sql;
> IF NOT FOUND THEN
> RAISE NOTICE 'NOT INSERTED';
> END IF;

EXECUTE does not set the FOUND flag. See
http://developer.postgresql.org/docs/postgres/plpgsql-statements.html#PLPGSQL-STATEMENTS-DIAGNOSTICS

regards, tom lane


From: Neil Conway <neilc(at)samurai(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Константин <beholder(at)mmska(dot)ru>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: PLPGSQL and FOUND stange behaviour after EXECUTE
Date: 2004-10-01 01:03:17
Message-ID: 1096592597.25277.443.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

On Fri, 2004-10-01 at 02:26, Tom Lane wrote:
> EXECUTE does not set the FOUND flag.

Is there a good reason for this behavior?

-Neil


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Neil Conway <neilc(at)samurai(dot)com>
Cc: Константин <beholder(at)mmska(dot)ru>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: PLPGSQL and FOUND stange behaviour after EXECUTE
Date: 2004-10-01 03:39:01
Message-ID: 4595.1096601941@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

Neil Conway <neilc(at)samurai(dot)com> writes:
> On Fri, 2004-10-01 at 02:26, Tom Lane wrote:
>> EXECUTE does not set the FOUND flag.

> Is there a good reason for this behavior?

Possibly not. Can EXECUTE determine how the executed statement would
have set the flag? Should we assume that the function doing the EXECUTE
knows exactly what it's executing and what the implications on FOUND
ought to be?

regards, tom lane


From: Neil Conway <neilc(at)samurai(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Константин <beholder(at)mmska(dot)ru>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: PLPGSQL and FOUND stange behaviour after EXECUTE
Date: 2004-10-01 05:07:34
Message-ID: 1096607254.9055.10.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

On Fri, 2004-10-01 at 13:39, Tom Lane wrote:
> Possibly not. Can EXECUTE determine how the executed statement would
> have set the flag?

At the moment, EXECUTE just feeds the string it finds to spi_execute().
We could probably hack it to figure out how to modify FOUND, but I think
it would be ugly. One way to fix this would be to reimplement EXECUTE to
be essentially `eval': it would take an arbitrary string and execute it
as a PL/pgSQL statement. That would fix the FOUND problem, and also give
us EXECUTE INTO in one fell swoop. (Rather than reimplementing EXECUTE,
we might want to add this functionality as a new command -- "EVAL" might
be a good name for it.)

> Should we assume that the function doing the EXECUTE
> knows exactly what it's executing and what the implications on FOUND
> ought to be?

I think it's reasonable to assume that the application developer knows
this much.

-Neil


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Neil Conway <neilc(at)samurai(dot)com>
Cc: Константин <beholder(at)mmska(dot)ru>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: PLPGSQL and FOUND stange behaviour after EXECUTE
Date: 2004-10-01 05:14:18
Message-ID: 5221.1096607658@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

Neil Conway <neilc(at)samurai(dot)com> writes:
> ... One way to fix this would be to reimplement EXECUTE to
> be essentially `eval': it would take an arbitrary string and execute it
> as a PL/pgSQL statement. That would fix the FOUND problem, and also give
> us EXECUTE INTO in one fell swoop.

Yeah, this has been on my to-do list for awhile...

> (Rather than reimplementing EXECUTE, we might want to add this
> functionality as a new command -- "EVAL" might be a good name for it.)

That would give cover for the inevitable backward-compatibility
arguments anyway. One question here is whether Oracle's PL/SQL has a
precedent, and if so which way does it point?

regards, tom lane


From: Neil Conway <neilc(at)samurai(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Константин <beholder(at)mmska(dot)ru>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: PLPGSQL and FOUND stange behaviour after EXECUTE
Date: 2004-10-04 07:19:11
Message-ID: 4160F96F.3060809@samurai.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

Tom Lane wrote:
> Yeah, this has been on my to-do list for awhile...

Ah, ok. Is this something you want to handle, or should I take a look?

> One question here is whether Oracle's PL/SQL has a
> precedent, and if so which way does it point?

I did some limited testing of this, and it appears that PL/SQL's EXECUTE
IMMEDIATE modifies SQL%FOUND.

-Neil


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Neil Conway <neilc(at)samurai(dot)com>
Cc: Константин <beholder(at)mmska(dot)ru>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: PLPGSQL and FOUND stange behaviour after EXECUTE
Date: 2004-10-04 13:54:26
Message-ID: 20422.1096898066@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

Neil Conway <neilc(at)samurai(dot)com> writes:
> Tom Lane wrote:
>> Yeah, this has been on my to-do list for awhile...

> Ah, ok. Is this something you want to handle, or should I take a look?

Well, it's not *high* on my to-do list; feel free to take a look.

>> One question here is whether Oracle's PL/SQL has a
>> precedent, and if so which way does it point?

> I did some limited testing of this, and it appears that PL/SQL's EXECUTE
> IMMEDIATE modifies SQL%FOUND.

Hm, okay, then we should probably think about doing so too.

If the EXECUTE executes something that's not
SELECT/INSERT/UPDATE/DELETE, should it clear FOUND? Or leave it alone?

regards, tom lane