Skip to content

Commit 967e8a3

Browse files
committed
Fix nodeUnique to behave correctly when reversing direction after reaching
either end of subplan results. This prevents misbehavior of cursors on SELECT DISTINCT ... queries. Per bug report 1-Feb-02.
1 parent 6ba8af9 commit 967e8a3

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/backend/executor/nodeUnique.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/executor/nodeUnique.c,v 1.37 2003/01/10 23:54:24 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/executor/nodeUnique.c,v 1.38 2003/02/02 19:08:57 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -56,6 +56,11 @@ ExecUnique(UniqueState *node)
5656
/*
5757
* now loop, returning only non-duplicate tuples. We assume that the
5858
* tuples arrive in sorted order so we can detect duplicates easily.
59+
*
60+
* We return the first tuple from each group of duplicates (or the
61+
* last tuple of each group, when moving backwards). At either end
62+
* of the subplan, clear priorTuple so that we correctly return the
63+
* first/last tuple when reversing direction.
5964
*/
6065
for (;;)
6166
{
@@ -64,10 +69,16 @@ ExecUnique(UniqueState *node)
6469
*/
6570
slot = ExecProcNode(outerPlan);
6671
if (TupIsNull(slot))
72+
{
73+
/* end of subplan; reset in case we change direction */
74+
if (node->priorTuple != NULL)
75+
heap_freetuple(node->priorTuple);
76+
node->priorTuple = NULL;
6777
return NULL;
78+
}
6879

6980
/*
70-
* Always return the first tuple from the subplan.
81+
* Always return the first/last tuple from the subplan.
7182
*/
7283
if (node->priorTuple == NULL)
7384
break;

0 commit comments

Comments
 (0)