Skip site navigation (1) Skip section navigation (2)

FOR-IN-EXECUTE: FOR does not replanned on each entry to the FOR loop

From: <Eugen(dot)Konkov(at)aldec(dot)com>
To: <pgsql-bugs(at)postgresql(dot)org>
Subject: FOR-IN-EXECUTE: FOR does not replanned on each entry to the FOR loop
Date: 2008-03-28 10:53:48
Message-ID: 036201c890c2$032a7b30$1200a8c0@kharkov.localhost (view raw or flat)
Thread:
Lists: pgsql-bugs
ID |name | parent_ID
-------------------------
1    a             NULL
2    b               1


CREATE or REPLACE FUNCTION "public"."get_relation"(
IN "par_fields" text,
IN "par_table" text,
IN "par_id" int4)
RETURNS SETOF "pg_catalog"."record" AS
$BODY$
DECLARE v_parent_ID integer;
DECLARE v_row record;
BEGIN

v_parent_ID:= par_id;

--WHILE (v_parent_ID IS NOT NULL) LOOP
-- EXECUTE 'SELECT parent_ID, name FROM akh_build WHERE ID = ' || 
quote_literal(v_parent_ID) INTO v_row;
-- return next v_row;
-- v_parent_ID= v_row.parent_ID;
--END LOOP;

FOR v_row IN EXECUTE 'SELECT parent_ID, name FROM akh_build WHERE ID = ' || 
quote_literal(v_parent_ID) LOOP
 return next v_row;
 v_parent_ID= v_row.parent_ID;
END LOOP;

return;

END;
$BODY$
LANGUAGE 'plpgsql' STRICT STABLE;

select * from get_relation( '', '', 2 ) as ( a int, b varchar )

FOR and WHILE results differ
WHILE returns two rows
but FOR returns only one.
and it seems that FOR does not replanned on each entry to the FOR loop
While docs says:
38.6.4. Looping Through Query Results
....
This is like the previous form, except that the source query is specified as 
a string expression, which is evaluated and replanned on each entry to the 
FOR loop 


Responses

pgsql-bugs by date

Next:From: Alvaro HerreraDate: 2008-03-28 12:00:15
Subject: Re: Problem identifying constraints which should not beinherited
Previous:From: Magnus HaganderDate: 2008-03-28 10:44:53
Subject: Re: 8.3 problems with sspi authentication

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group