Re: [GENERAL] Postgres 7.2 - Updating rows in cursor problem

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-hackers(at)postgreSQL(dot)org
Cc: Vaclav Kulakovsky <vaclav(dot)kulakovsky(at)definity(dot)cz>, Jan Wieck <JanWieck(at)Yahoo(dot)com>
Subject: Re: [GENERAL] Postgres 7.2 - Updating rows in cursor problem
Date: 2002-02-13 22:53:19
Message-ID: 1355.1013640799@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers pgsql-patches

I wrote:
> This is a bug in plgsql, or more precisely in SPI, I think. The FOR
> statement needs to restore its initial value of scanCommandId each time
> it resumes execution of the SELECT. Seems like that should be done down
> inside SPI. Comments?

More specifically, the problem is that plpgsql's FOR-over-a-select now
depends on a SPI cursor, and both SPI cursors and regular cursors are
broken in this regard. Observe the following misbehavior with a plain
cursor:

regression=# select * from foo;
f1 | f2
----+----
1 | 1
2 | 2
3 | 3
(3 rows)

regression=# begin;
BEGIN
regression=# declare c cursor for select * from foo;
SELECT
regression=# fetch 2 from c;
f1 | f2
----+----
1 | 1
2 | 2
(2 rows)

regression=# update foo set f2 = f2 + 1;
UPDATE 3
regression=# fetch all from c;
f1 | f2
----+----
1 | 2
2 | 3
3 | 4
(3 rows)

IMHO the cursor should not be able to see the rows inserted by the
subsequent UPDATE. (Certainly it should not return the updated versions
of rows it's already returned.) The SQL spec says that cursors declared
INSENSITIVE shall not observe changes made after they are opened --- and
it gives the implementation the option to make all cursors behave that
way. I think we should choose to do so.

I believe the correct fix for this is that Portal objects should store
the scanCommandId that was current when they were created, and restore
this scanCommandId whenever they are asked to run their plan. Comments?

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Eric Webber 2002-02-14 01:04:24 pgaccess failed, used to work
Previous Message Jeff Anto 2002-02-13 22:45:16 Re: insert within pl/pgsql procedures: NOVICE question ?

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Eisentraut 2002-02-14 00:12:57 Re: NAMEDATALEN Changes
Previous Message Rod Taylor 2002-02-13 22:34:05 Re: When and where to check for function permissions

Browse pgsql-patches by date

  From Date Subject
Next Message Rod Taylor 2002-02-14 01:13:26 Make equals sign optional in CREATE DATABASE WITH param = 'val'
Previous Message Tom Lane 2002-02-13 19:47:24 Re: [GENERAL] Postgres 7.2 - Updating rows in cursor problem