Re: allowed variable names in functions?

Lists: pgsql-general
From: "A B" <gentosaker(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: allowed variable names in functions?
Date: 2008-06-30 10:12:21
Message-ID: dbbf25900806300312v37d7d5n4e354bf6cf3ba5c@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hello.
I suspect that in a plpgsql function

DECLARE
c2 REAL;
cadiv REAL;

works but

c2 REAL;
c2div REAL;

doesn't.

Is this true, and if so, what are the rules for the names in the
function? I use 8.1.


From: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>
To: "A B" <gentosaker(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: allowed variable names in functions?
Date: 2008-06-30 10:30:47
Message-ID: 162867790806300330g40d362c8xcd82fe1a5d6996ee@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hello

it works in my 8.1

postgres=# CREATE LANGUAGE plpgsql;
CREATE LANGUAGE
postgres=# create or replace function foo(a int) returns void as
$$declare c2 real; c2div real; begin c2div := 10.9; end; $$ language
plpgsql;
CREATE FUNCTION
postgres=# select foo(10);
foo
-----

(1 row)
Regards
Pavel Stehule

2008/6/30 A B <gentosaker(at)gmail(dot)com>:
> Hello.
> I suspect that in a plpgsql function
>
> DECLARE
> c2 REAL;
> cadiv REAL;
>
> works but
>
> c2 REAL;
> c2div REAL;
>
> doesn't.
>
> Is this true, and if so, what are the rules for the names in the
> function? I use 8.1.
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>


From: "A B" <gentosaker(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: allowed variable names in functions?
Date: 2008-06-30 10:38:40
Message-ID: dbbf25900806300338r2d65e7ccye68ad728fae1e389@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Then my assumption was wrong.
Here is the entire function and it fails with the names
c2,c2div,c3,c3div, but if names are changed, it works!
(by works I mean I get the "hello" lines printed) There is nothing
wrong with the select statement either, that works fine if I run it
stand-alone, or with the names of c2,c2div,c3,c3div changed.

CREATE OR REPLACE FUNCTION foo(pid_ INTEGER) RETURNS void AS $$
DECLARE
c2 REAL;
c2div REAL;
c3 REAL;
c3div REAL;
weights RECORD;
tmp RECORD;
retval RECORD;
t RECORD;
BEGIN
RAISE NOTICE 'starting...';
FOR tmp IN SELECT id,c2,c3 FROM Master WHERE pid=pid_ AND c3 !=0 LOOP
RAISE NOTICE 'hello %',tmp.id;
END LOOP;
RETURN;
END; $$ LANGUAGE plpgsql;

2008/6/30 Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>:
> Hello
>
> it works in my 8.1
>
> postgres=# CREATE LANGUAGE plpgsql;
> CREATE LANGUAGE
> postgres=# create or replace function foo(a int) returns void as
> $$declare c2 real; c2div real; begin c2div := 10.9; end; $$ language
> plpgsql;
> CREATE FUNCTION
> postgres=# select foo(10);
> foo
> -----
>
> (1 row)
> Regards
> Pavel Stehule
>
> 2008/6/30 A B <gentosaker(at)gmail(dot)com>:
>> Hello.
>> I suspect that in a plpgsql function
>>
>> DECLARE
>> c2 REAL;
>> cadiv REAL;
>>
>> works but
>>
>> c2 REAL;
>> c2div REAL;
>>
>> doesn't.
>>
>> Is this true, and if so, what are the rules for the names in the
>> function? I use 8.1.


From: "A B" <gentosaker(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: allowed variable names in functions?
Date: 2008-06-30 10:44:31
Message-ID: dbbf25900806300344o4cb6d6a9i9ba2beba8c668c60@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

By changing c2,c3,etc. to something else xc2,xc3, etc. it worked!
So was the problem that I refered to the same names in the SELECT statement?

2008/6/30 A B <gentosaker(at)gmail(dot)com>:
> Then my assumption was wrong.
> Here is the entire function and it fails with the names
> c2,c2div,c3,c3div, but if names are changed, it works!
> (by works I mean I get the "hello" lines printed) There is nothing
> wrong with the select statement either, that works fine if I run it
> stand-alone, or with the names of c2,c2div,c3,c3div changed.
>
> CREATE OR REPLACE FUNCTION foo(pid_ INTEGER) RETURNS void AS $$
> DECLARE
> c2 REAL;
> c2div REAL;
> c3 REAL;
> c3div REAL;
> BEGIN
> FOR tmp IN SELECT id,c2,c3 FROM Master WHERE pid=pid_ AND c3 !=0 LOOP


From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: allowed variable names in functions?
Date: 2008-06-30 10:48:04
Message-ID: 20080630104804.GF13270@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

am Mon, dem 30.06.2008, um 12:38:40 +0200 mailte A B folgendes:
> Then my assumption was wrong.
> Here is the entire function and it fails with the names
> c2,c2div,c3,c3div, but if names are changed, it works!
> (by works I mean I get the "hello" lines printed) There is nothing
> wrong with the select statement either, that works fine if I run it
> stand-alone, or with the names of c2,c2div,c3,c3div changed.
>
> CREATE OR REPLACE FUNCTION foo(pid_ INTEGER) RETURNS void AS $$
> DECLARE
> c2 REAL;
> c2div REAL;
> c3 REAL;
> c3div REAL;
> weights RECORD;
> tmp RECORD;
> retval RECORD;
> t RECORD;
> BEGIN
> RAISE NOTICE 'starting...';
> FOR tmp IN SELECT id,c2,c3 FROM Master WHERE pid=pid_ AND c3 !=0 LOOP
> RAISE NOTICE 'hello %',tmp.id;
> END LOOP;
> RETURN;
> END; $$ LANGUAGE plpgsql;

Don't use the same names for plpgsql-variables and for column names.

Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net


From: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>
To: "A B" <gentosaker(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: allowed variable names in functions?
Date: 2008-06-30 10:48:14
Message-ID: 162867790806300348s4f74149au10e18fa5001cff05@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

I see one big problem. You have colision between column and variable
names! When you has SQL inside function use prefix for variables or
use qualified names.

DECLARE a varchar;
BEGIN
FOR a IN SELECT a FROM ... -- is bug

you have to do
DELARE _a varchar;
BEGIN
FOR _a IN SELECT t.a FROM tab t ...

Regards
Pavel Stehule

2008/6/30 A B <gentosaker(at)gmail(dot)com>:
> Then my assumption was wrong.
> Here is the entire function and it fails with the names
> c2,c2div,c3,c3div, but if names are changed, it works!
> (by works I mean I get the "hello" lines printed) There is nothing
> wrong with the select statement either, that works fine if I run it
> stand-alone, or with the names of c2,c2div,c3,c3div changed.
>
> CREATE OR REPLACE FUNCTION foo(pid_ INTEGER) RETURNS void AS $$
> DECLARE
> c2 REAL;
> c2div REAL;
> c3 REAL;
> c3div REAL;
> weights RECORD;
> tmp RECORD;
> retval RECORD;
> t RECORD;
> BEGIN
> RAISE NOTICE 'starting...';
> FOR tmp IN SELECT id,c2,c3 FROM Master WHERE pid=pid_ AND c3 !=0 LOOP
> RAISE NOTICE 'hello %',tmp.id;
> END LOOP;
> RETURN;
> END; $$ LANGUAGE plpgsql;
>
>
> 2008/6/30 Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>:
>> Hello
>>
>> it works in my 8.1
>>
>> postgres=# CREATE LANGUAGE plpgsql;
>> CREATE LANGUAGE
>> postgres=# create or replace function foo(a int) returns void as
>> $$declare c2 real; c2div real; begin c2div := 10.9; end; $$ language
>> plpgsql;
>> CREATE FUNCTION
>> postgres=# select foo(10);
>> foo
>> -----
>>
>> (1 row)
>> Regards
>> Pavel Stehule
>>
>> 2008/6/30 A B <gentosaker(at)gmail(dot)com>:
>>> Hello.
>>> I suspect that in a plpgsql function
>>>
>>> DECLARE
>>> c2 REAL;
>>> cadiv REAL;
>>>
>>> works but
>>>
>>> c2 REAL;
>>> c2div REAL;
>>>
>>> doesn't.
>>>
>>> Is this true, and if so, what are the rules for the names in the
>>> function? I use 8.1.
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>


From: Karsten Hilbert <Karsten(dot)Hilbert(at)gmx(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: allowed variable names in functions?
Date: 2008-06-30 10:50:13
Message-ID: 20080630105013.GA3979@merkur.hilbert.loc
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Mon, Jun 30, 2008 at 12:44:31PM +0200, A B wrote:

> By changing c2,c3,etc. to something else xc2,xc3, etc. it worked!
> So was the problem that I refered to the same names in the SELECT statement?
Yes.

Karsten
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346


From: "A B" <gentosaker(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: allowed variable names in functions?
Date: 2008-06-30 10:50:39
Message-ID: dbbf25900806300350w51fa50ddidf7e4be41b19dc5b@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Thank you both! :-)


From: "A B" <gentosaker(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: allowed variable names in functions?
Date: 2008-06-30 11:01:19
Message-ID: dbbf25900806300401s7a5cab05w9a681a65fea3e5b3@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

But there not any problem with returning values with code like this

DECLARE
retval RECORD;

retval.c2 := ....
RETRUN NEXT retval;

if c2 is a field on some table?

2008/6/30 Karsten Hilbert <Karsten(dot)Hilbert(at)gmx(dot)net>:
> On Mon, Jun 30, 2008 at 12:44:31PM +0200, A B wrote:
>
>> By changing c2,c3,etc. to something else xc2,xc3, etc. it worked!
>> So was the problem that I refered to the same names in the SELECT statement?
> Yes.
>
> Karsten
> --
> GPG key ID E4071346 @ wwwkeys.pgp.net
> E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>


From: "A B" <gentosaker(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: allowed variable names in functions?
Date: 2008-06-30 11:06:59
Message-ID: dbbf25900806300406g620a39calb4b5521a5cdd36c3@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

> But there not any problem with returning values with code like this
>
> DECLARE
> retval RECORD;
>
> retval.c2 := ....
> RETRUN NEXT retval;
>
> if c2 is a field on some table?

Oh, that seems also to be problematic.


From: "Albe Laurenz" <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: "A B *EXTERN*" <gentosaker(at)gmail(dot)com>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: allowed variable names in functions?
Date: 2008-06-30 13:48:32
Message-ID: D960CB61B694CF459DCFB4B0128514C20244E034@exadv11.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

A B wrote:
> Here is the entire function and it fails with the names
> c2,c2div,c3,c3div, but if names are changed, it works!
> (by works I mean I get the "hello" lines printed) There is nothing
> wrong with the select statement either, that works fine if I run it
> stand-alone, or with the names of c2,c2div,c3,c3div changed.
>
> CREATE OR REPLACE FUNCTION foo(pid_ INTEGER) RETURNS void AS $$
> DECLARE
> c2 REAL;
> c2div REAL;
> c3 REAL;
> c3div REAL;
> weights RECORD;
> tmp RECORD;
> retval RECORD;
> t RECORD;
> BEGIN
> RAISE NOTICE 'starting...';
> FOR tmp IN SELECT id,c2,c3 FROM Master WHERE pid=pid_ AND c3 !=0 LOOP
> RAISE NOTICE 'hello %',tmp.id;
> END LOOP;
> RETURN;
> END; $$ LANGUAGE plpgsql;

That is because c2 and c3 in the SELECT statement are replaced with
the variables before the SQL statement is executed.

See the documentation:
http://www.postgresql.org/docs/current/static/plpgsql-implementation.html#PLPGSQL-VAR-SUBST

You should qualify the column names:

SELECT master.id, master.c2, master.c3 FROM master WHERE ... AND master.c3 <> 0

Yours,
Laurenz Albe