Re: (Comment)Bug in CteScanNext

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Jim Nasby <Jim(dot)Nasby(at)BlueTreble(dot)com>
Cc: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: (Comment)Bug in CteScanNext
Date: 2016-09-06 17:00:45
Message-ID: 25798.1473181245@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Jim Nasby <Jim(dot)Nasby(at)BlueTreble(dot)com> writes:
> On 9/3/16 1:30 PM, Tom Lane wrote:
>> Or we could add something like "But first, we must deal with the special
>> case of reversing direction after reaching EOF."

> I'm working on that, but one thing isn't clear to me... why do we only
> skip past the last tuple if (!node->leader->eof_cte)? Even if we've hit
> the end of the underlying node, the tuplestore could still return data,
> and AFAICT we would still need to move past the last item in the
> tuplestore, no?

If tuplestore_ateof is true, then what presumably happened in the previous
call is that we fetched a row (moving forward) from the underlying plan,
appended it to the tuplestore, and returned it to the caller. If the
current call specifies moving backwards, we want to return the tuple
before that one, not the same one again.

Alternatively, if we'd already backed up some and gone forwards again (all
fetching from the tuplestore), then ateof means that the last call
returned the last row currently in the tuplestore. Again, we don't want
to return that row twice.

On the other hand, if eof_cte is true, then what happened on the last
call is that we tried to fetch forwards, reached EOF on the underlying
query, and returned NULL. In that case, a backwards fetch *should*
produce the last row in the tuplestore.

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Marko Tiikkaja 2016-09-06 17:03:42 Re: SELECT FOR UPDATE regression in 9.5
Previous Message Claudio Freire 2016-09-06 16:58:25 Re: Vacuum: allow usage of more than 1GB of work mem