pl/pgsql function not working

Started by Tom Jenkinsalmost 24 years ago7 messagesgeneral
Jump to latest
#1Tom Jenkins
tjenkins@devis.com

hello all,
i hope somebody can shed some light on a problem i'm having with
pl/pgsql. I need to loop over all my tables and clear out the field
lastaccess (it was improperly defined as time instead of timestamp)

DROP FUNCTION clear_lastaccess();
CREATE FUNCTION clear_lastaccess() RETURNS bool AS '
DECLARE
obj RECORD;
BEGIN
FOR obj IN SELECT relname FROM pg_class WHERE relkind IN (''r'') AND
relname !~ ''^pg_'' LOOP
RAISE NOTICE ''update % set lastaccess = NULL;'', obj.relname;
PERFORM ''update % set lastaccess = NULL;'', obj.relname ;
END LOOP;
RETURN true;
END;
'LANGUAGE 'plpgsql';

running this function with:
SELECT clear_lastaccess();
results in the update statements getting printed but the updates are
never processed.

looking at my pg log shows the SELECT relname query but no UPDATEs

what do i have wrong?

--

Tom Jenkins
Development InfoStructure
http://www.devis.com

#2Richard Huxton
dev@archonet.com
In reply to: Tom Jenkins (#1)
Re: pl/pgsql function not working

On Wednesday 19 Jun 2002 2:25 pm, Tom Jenkins wrote:

hello all,
i hope somebody can shed some light on a problem i'm having with
pl/pgsql.

PERFORM ''update % set lastaccess = NULL;'', obj.relname ;

Are you sure this shouldn't be EXECUTE (build the sql-string normally first)

- Richard Huxton

#3Tom Jenkins
tjenkins@devis.com
In reply to: Richard Huxton (#2)
Re: pl/pgsql function not working

On Wed, 2002-06-19 at 10:09, Richard Huxton wrote:

On Wednesday 19 Jun 2002 2:25 pm, Tom Jenkins wrote:

hello all,
i hope somebody can shed some light on a problem i'm having with
pl/pgsql.

PERFORM ''update % set lastaccess = NULL;'', obj.relname ;

Are you sure this shouldn't be EXECUTE (build the sql-string normally first)

hrmmm.. replacing PERFORM with EXECUTE results in an error

NOTICE: Error occurred while executing PL/pgSQL function
clear_lastaccess
NOTICE: line 6 at execute statement
ERROR: query "SELECT 'update % set lastaccess = NULL;', $1 " returned
2 columns

--

Tom Jenkins
Development InfoStructure
http://www.devis.com

#4Masaru Sugawara
rk73@sea.plala.or.jp
In reply to: Tom Jenkins (#1)
Re: pl/pgsql function not working

On 19 Jun 2002 09:25:11 -0400
Tom Jenkins <tjenkins@devis.com> wrote:

hello all,
i hope somebody can shed some light on a problem i'm having with
pl/pgsql. I need to loop over all my tables and clear out the field
lastaccess (it was improperly defined as time instead of timestamp)

DROP FUNCTION clear_lastaccess();
CREATE FUNCTION clear_lastaccess() RETURNS bool AS '
DECLARE
obj RECORD;
BEGIN
FOR obj IN SELECT relname FROM pg_class WHERE relkind IN (''r'') AND
relname !~ ''^pg_'' LOOP
RAISE NOTICE ''update % set lastaccess = NULL;'', obj.relname;
PERFORM ''update % set lastaccess = NULL;'', obj.relname ;

Try here instead of PERFORM.

EXECUTE ''update '' || obj.relname || '' set lastaccess = NULL;'';

END LOOP;
RETURN true;
END;
'LANGUAGE 'plpgsql';

running this function with:
SELECT clear_lastaccess();
results in the update statements getting printed but the updates are
never processed.

looking at my pg log shows the SELECT relname query but no UPDATEs

what do i have wrong?

Regards,
Masaru Sugawara

#5Darren Ferguson
darren@crystalballinc.com
In reply to: Richard Huxton (#2)
Re: pl/pgsql function not working

Perform doesn't work that way from my experience

Use the following

EXECUTE ''UPDATE ''||obj.relname||'' SET lastaccess = NULL'';

The % you are using is generally used in the RAISE NOTICE OR EXCEPTION
pieces

HTH

Darren Ferguson

On Wed, 19 Jun 2002, Richard Huxton wrote:

On Wednesday 19 Jun 2002 2:25 pm, Tom Jenkins wrote:

hello all,
i hope somebody can shed some light on a problem i'm having with
pl/pgsql.

PERFORM ''update % set lastaccess = NULL;'', obj.relname ;

Are you sure this shouldn't be EXECUTE (build the sql-string normally first)

- Richard Huxton

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

--
Darren Ferguson

#6Darren Ferguson
darren@crystalballinc.com
In reply to: Tom Jenkins (#3)
Re: pl/pgsql function not working

You can't use execute like that it discards the result unless you use it
in a FOR rec IN EXECUTE statement then you can get the results out.

EXECUTE ''UPDATE ''||obj.relname||'' set lastaccess = NULL;'';

This will work

Darren

On 19 Jun 2002, Tom Jenkins wrote:

On Wed, 2002-06-19 at 10:09, Richard Huxton wrote:

On Wednesday 19 Jun 2002 2:25 pm, Tom Jenkins wrote:

hello all,
i hope somebody can shed some light on a problem i'm having with
pl/pgsql.

PERFORM ''update % set lastaccess = NULL;'', obj.relname ;

Are you sure this shouldn't be EXECUTE (build the sql-string normally first)

hrmmm.. replacing PERFORM with EXECUTE results in an error

NOTICE: Error occurred while executing PL/pgSQL function
clear_lastaccess
NOTICE: line 6 at execute statement
ERROR: query "SELECT 'update % set lastaccess = NULL;', $1 " returned
2 columns

--
Darren Ferguson

#7Tom Jenkins
tjenkins@devis.com
In reply to: Masaru Sugawara (#4)
Re: pl/pgsql function not working

Masaru Sugawara, Darren Ferguson,

On Wed, 2002-06-19 at 11:35, Masaru Sugawara wrote:

Try here instead of PERFORM.

EXECUTE ''update '' || obj.relname || '' set lastaccess = NULL;'';

this works. thank you folks.

--

Tom Jenkins
Development InfoStructure
http://www.devis.com