Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fqbz7-0005GZ-Hp for pgsql-bugs@arkaria.postgresql.org; Fri, 17 Aug 2018 10:28:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fqbz3-0005oM-K3 for pgsql-bugs@arkaria.postgresql.org; Fri, 17 Aug 2018 10:27:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fqbz3-0005oF-Dv for pgsql-bugs@lists.postgresql.org; Fri, 17 Aug 2018 10:27:57 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fqbyw-0002dS-Hi for pgsql-bugs@lists.postgresql.org; Fri, 17 Aug 2018 10:27:56 +0000 Received: from [192.168.127.1] (port=38960 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1fqbyt-000D99-VO; Fri, 17 Aug 2018 10:27:47 +0000 Received: from [127.0.0.1] (port=51189 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1fqbyt-0006b6-7B; Fri, 17 Aug 2018 10:27:47 +0000 From: Andrew Gierth To: pgsql-bugs@lists.postgresql.org, v.g.baranoff@gmail.com Subject: Re: BUG #15336: Wrong cursor's bacward fetch results in select with ALL(subquery) In-Reply-To: <153449812167.1304.1741624125628126322@wrigleys.postgresql.org> (PG Bug reporting form's message of "Fri, 17 Aug 2018 09:28:41 +0000") Message-ID: <87va892tf5.fsf@news-spur.riddles.org.uk> References: <153449812167.1304.1741624125628126322@wrigleys.postgresql.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Fri, 17 Aug 2018 11:27:46 +0100 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "PG" == PG Bug reporting form writes: PG> fetch backward 9 from "mycursor"; PG> commit; PG> The result set is wrong (9, 8, 7, 6, 5, 4, 3, 2, 1). PG> It seems that selection predicate is ignored. PG> This bug has been reproduced with PostgreSQL 9.6 and 10.0 I wonder if we have a contender here for the oldest reported bug in PG history; while I haven't tested anything older than 9.5, the incorrect logic seems to date back to the introduction of subqueries in 6.something. Here is a simpler test case: begin; declare foo cursor for select * from generate_series(1,3) i where i <> all (values (2)); fetch all from foo; -- returns the expected 2 rows fetch backward all from foo; -- assertion failure, or incorrect result The problem is that the scan direction is being set to "backward" in the EState, and as a result the subquery evaluation is run in the backward direction too, which obviously doesn't do anything sensible. The assertion failure is from the tuplestore code complaining about doing a backward fetch on a tuplestore not initialized for backward access. I'm really not sure yet what the correct fix is, though. -- Andrew (irc:RhodiumToad)