Bug in PL/pgSQL FOR cursor variant - Mailing list pgsql-bugs

From Heikki Linnakangas
Subject Bug in PL/pgSQL FOR cursor variant
Date
Msg-id 4C1F1D57.9060802@enterprisedb.com
Whole thread Raw
Responses Re: Bug in PL/pgSQL FOR cursor variant
List pgsql-bugs
postgres=# CREATE FUNCTION func() RETURNS VOID AS $$
   declare
     cur CURSOR IS SELECT generate_series(1,10) AS a;
     BEGIN
         FOR erec IN cur LOOP
             raise notice 'row %', erec.a ;
             IF (erec.a = 5) THEN CLOSE cur; END IF;
         END LOOP;
         RETURN;
     END;
$$ LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# SELECT func();
NOTICE:  row 1
NOTICE:  row 2
NOTICE:  row 3
NOTICE:  row 4
NOTICE:  row 5
server closed the connection unexpectedly
    This probably means the server terminated abnormally
    before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.

Reproducible on 8.4 and CVS HEAD, the "FOR cursor" statement didn't
exist on earlier versions.

The problem is that exec_stmt_forc keeps using a pointer to the Portal,
which becomes invalid if the cursor is closed in the middle. Patch
attached, will apply..

--
   Heikki Linnakangas
   EnterpriseDB   http://www.enterprisedb.com

Attachment

pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: BUG #5514: no relation entry for relid X
Next
From: Tatsuhito Kasahara
Date:
Subject: make_greater_string() does not return a string in some cases