Re: ReadRecentBuffer() is broken for local buffer

From: Zhang Mingli <zmlpostgres(at)gmail(dot)com>
To: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>, Thomas Munro <thomas(dot)munro(at)gmail(dot)com>
Subject: Re: ReadRecentBuffer() is broken for local buffer
Date: 2022-07-25 02:44:10
Message-ID: 84516E4B-4012-45D6-8F17-FAD14A4134CD@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


Nice catch, LGTM.

> On Jul 25, 2022, at 02:22, Heikki Linnakangas <hlinnaka(at)iki(dot)fi> wrote:
>
> ReadRecentBuffer() doesn't work for local buffers, i.e. for temp tables. The bug is pretty clear if you look at the code:
>
> if (BufferIsLocal(recent_buffer))
> {
> - bufHdr = GetBufferDescriptor(-recent_buffer - 1);
> + bufHdr = GetLocalBufferDescriptor(-recent_buffer - 1);
>
> The code after that looks suspicious, too. It increases the usage count even if the buffer was already pinned. That's different from what it does for a shared buffer, and different from LocalBufferAlloc(). That's pretty harmless, just causes the usage count to be bumped more frequently, but I don't think it was intentional. The ordering of bumping the usage count, the local ref count, and registration in the resource owner are different too. As far as I can see, that makes no difference, but I think we should keep this code as close as possible to similar code used elsewhere, unless there's a particular reason to differ.
>
> I propose the attached to fix those things.
>
> I tested this by adding this little snippet to a random place where we have just read a page with ReadBuffer:
>
> diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
> index aab8d6fa4e5..c4abdbc96dd 100644
> --- a/src/backend/access/heap/heapam.c
> +++ b/src/backend/access/heap/heapam.c
> @@ -403,6 +403,14 @@ heapgetpage(TableScanDesc sscan, BlockNumber page)
> RBM_NORMAL, scan->rs_strategy);
> scan->rs_cblock = page;
>
> + {
> + bool still_ok;
> +
> + still_ok = ReadRecentBuffer(scan->rs_base.rs_rd->rd_locator, MAIN_FORKNUM, page, scan->rs_cbuf);
> + Assert(still_ok);
> + ReleaseBuffer(scan->rs_cbuf);
> + }
> +
> if (!(scan->rs_base.rs_flags & SO_ALLOW_PAGEMODE))
> return;
>
> Without the fix, the assertion is fails quickly on "make check".
>
> - Heikki<0001-Fix-ReadRecentBuffer-for-local-buffers.patch>

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Richard Guo 2022-07-25 02:48:33 Re: Using each rel as both outer and inner for JOIN_ANTI
Previous Message Tom Lane 2022-07-25 02:40:16 Re: Remove useless arguments in ReadCheckpointRecord().