Thread: sql query cursor problem

sql query cursor problem

From
Risko Peter
Date:
============================================================================
                        POSTGRESQL BUG REPORT TEMPLATE
============================================================================


Your name        : Peter RISKO
Your email address    : rpetike@freemail.hu


System Configuration
---------------------
  Architecture (example: Intel Pentium)      : Intel Pentium III

  Operating System (example: Linux 2.0.26 ELF)     : Linux 2.4.3

  PostgreSQL version (example: PostgreSQL-7.0):   PostgreSQL-7.1.1

  Compiler used (example:  gcc 2.8.0)        : gcc 2.95.2


Please enter a FULL description of your problem:
------------------------------------------------

Dear list members,
  We are developing an application that uses Postgresql as its dbms.
It seems we've found a bug in PostgreSQL handling of cursored queries.
  I assume that it's legal to do 'fetch backward all in xxx' and then
'fetch 1 in xxx' afterwards. If it is not legal in Postgresql, the rest
of this mail is irrelevant. (I have been using Postgresql for quite a
long time now, and I experienced that you cannot 'fall off' of neither
ends of a query result by moving the cursor.)

Please describe a way to repeat the problem.   Please try to provide a
concise reproducible example, if at all possible:
----------------------------------------------------------------------

So the bug appears when I execute the following steps:

1.
in psql shell: => create database test;
               => create user tester with password 'tester';
2.
in the os's shell: $ psql -d test -U tester -f ss3.sql
                   $ psql -d test -U tester
3.
in psql shell: => \i test5a.sql
               => fetch 1 in lionhart;
               => fetch -1 in lionhart;
               => fetch 1 in lionhart;
     ??? last fetch should return a row, but it doesn't seem like...

Attached files:
  ss3.sql : The dumped content of my database
  test5a.sql : The query we would use


If you know how this problem might be fixed, list the solution below:
---------------------------------------------------------------------

Re: sql query cursor problem

From
Tom Lane
Date:
Risko Peter <risko.peter@mhc.hu> writes:
> It seems we've found a bug in PostgreSQL handling of cursored queries.
>   I assume that it's legal to do 'fetch backward all in xxx' and then
> 'fetch 1 in xxx' afterwards.

Backwards scan doesn't work right for any but the simplest sorts of
queries (no joins, no grouping or aggregation, probably some other
issues).  It'd be nice to fix it, but it's not very high on anyone's
priority list.  There are workarounds available, one of the simplest
being to specify ORDER BY --- the result of a sort step *can* be scanned
backwards, IIRC.  Use EXPLAIN to make sure your query has a Sort at
the top level.

            regards, tom lane