Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id D0A35E933E7 for ; Tue, 27 Dec 2011 22:09:41 -0400 (AST) Received: from m13-64.163.com ([220.181.13.64]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RfixW-0003rq-Kr for pgsql-bugs@postgresql.org; Wed, 28 Dec 2011 02:09:41 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=Received:Date:From:To:Message-ID:Subject: MIME-Version:Content-Type; bh=YOkCs53bkEPpHeWtJxwg81r6P2mpuMOXLQ PTjUmQ4m4=; b=UnAUBqDaKWb5r5fCJ1I2p0S1EVfRBx+NiIn8eRhKU1FkZQAFWG 4nLNsGkwPZwFwq/6uc6UTrU7pO8KSOA8iuYcorvj4ubaATuErq5OLw4XQREh0ufA HN7YwBv4FLVSpN1EIHNZP9ubKGF9526HlJGcj0rr12/KNEgkR9G/M3lSI= Received: from wcting163 ( [210.56.193.20] ) by ajax-webmail-wmsvr64 (Coremail) ; Wed, 28 Dec 2011 10:09:20 +0800 (CST) Date: Wed, 28 Dec 2011 10:09:20 +0800 (CST) From: wcting163 To: pgsql-bugs@postgresql.org Message-ID: <684bfc49.10560.134826dcb72.Coremail.wcting163@163.com> Subject: with hold cursor, cause function execute twice and wrong result MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_335638_1555192076.1325038160754" X-Originating-IP: [210.56.193.20] X-Priority: 1 X-Mailer: Coremail Webmail Server Version SP_ntes V3.5 build 111202(16086.4213.4207) Copyright (c) 2002-2011 www.mailtech.cn 163com X-CM-CTRLDATA: 11FdpmZvb3Rlcl9odG09MjMxNTo4MQ== X-CM-TRANSID: QMGowECJSEJQevpOLc0LAA--.45W X-CM-SenderInfo: pzfwx0jjrwjqqrwthudrp/1tbiGQwkuklzgi+YRQACsD X-Coremail-Antispam: 1U5529EdanIXcx71UUUUU7vcSsGvfC2KfnxnUU== X-Pg-Spam-Score: -3.2 (---) X-Archive-Number: 201112/232 X-Sequence-Number: 32043 ------=_Part_335638_1555192076.1325038160754 Content-Type: text/plain; charset=GBK Content-Transfer-Encoding: 7bit postgres=# select version(); version ------------------------------------------------------------------------------------------------------------------------ --- PostgreSQL 9.0alpha5 on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 4.1.2 20080704 (Asianux 3.0 4.1.2-44), 64-b it create table test_execute(id int,name varchar(40)); insert into test_execute values(1,'jack'); create or replace function p_test_execute() returns void as $$ begin raise notice 'hello world'; update test_execute set id=id*2; end; $$ LANGUAGE plpgsql; begin; declare JDBC_CURS_1 cursor with hold for select p_test_execute() from test_execute; fetch 50 from JDBC_CURS_1; NOTICE: hello world end; NOTICE: hello world COMMIT select * from test_execute; id | name ----+------ 4 | jack I expect id = 2, but it is **4** instead, The reason is that the function p_test_execute is executed twice, when *fetch*, it is first executed, and when transaction commit, because the cursor is a *holdable* cursor, it is executed again. I read the code, for holdable cursor, when commit, following call will execute: CommitHoldablePortals-->PersistHoldablePortal-->ExecutorRewind. Is *ExecutorRewind* necessary, is it the root of this bug? Does *ExecutorRewind* cause plan re-execute? ------=_Part_335638_1555192076.1325038160754 Content-Type: text/html; charset=GBK Content-Transfer-Encoding: 7bit
postgres=# select version();
                                                          version
------------------------------------------------------------------------------------------------------------------------
---
 PostgreSQL 9.0alpha5 on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 4.1.2 20080704 (Asianux 3.0 4.1.2-44), 64-b
it
 
create table test_execute(id int,name varchar(40));
insert into test_execute values(1,'jack');
create or replace function p_test_execute() returns void
as
$$
begin
        raise notice 'hello world';
        update test_execute set id=id*2;
end;
$$ LANGUAGE plpgsql;
 
begin;
declare JDBC_CURS_1 cursor with hold for select p_test_execute() from test_execute;
fetch 50 from JDBC_CURS_1;
NOTICE:  hello world
end;
NOTICE:  hello world
COMMIT
select * from test_execute;
 id | name
----+------
  4 | jack
 
I expect id = 2, but it is **4** instead,
 
The reason is that the function p_test_execute is executed twice, when *fetch*, it is first executed, and when transaction commit, because the cursor is a *holdable* cursor, it is executed again.
 
I read the code, for holdable cursor, when commit, following call will execute:
 CommitHoldablePortals-->PersistHoldablePortal-->ExecutorRewind.
 
Is *ExecutorRewind* necessary, is it the root of this bug?
Does *ExecutorRewind* cause plan re-execute?


------=_Part_335638_1555192076.1325038160754--