Re: ReadRecentBuffer() is broken for local buffer

From: Thomas Munro <thomas(dot)munro(at)gmail(dot)com>
To: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: ReadRecentBuffer() is broken for local buffer
Date: 2022-07-24 21:35:36
Message-ID: CA+hUKGJrnLVdZtAtxyDiL5To4w_3S1zNUxQ9Dp5FHB55TwwHgg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, Jul 25, 2022 at 6:22 AM 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:

- bufHdr = GetBufferDescriptor(-recent_buffer - 1);
+ int b = -recent_buffer - 1;
+
+ bufHdr = GetLocalBufferDescriptor(b);

Ugh, right. Obviously this code path is not reached currently. I
added the local path for completeness but I didn't think of the idea
of testing it the way you suggested, hence thinko escaped into the
wild. That way of testing seems good and the patch indeed fixes the
problem.

- /* Bump local buffer's ref and usage counts. */
+ /*
+ * Bump buffer's ref and usage counts. This is equivalent of
+ * PinBuffer for a shared buffer.
+ */
+ if (LocalRefCount[b] == 0)
+ {
+ if (BUF_STATE_GET_USAGECOUNT(buf_state) < BM_MAX_USAGE_COUNT)
+ {
+ buf_state += BUF_USAGECOUNT_ONE;
+ pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state);
+ }
+ }
+ LocalRefCount[b]++;
ResourceOwnerRememberBuffer(CurrentResourceOwner, recent_buffer);
- LocalRefCount[-recent_buffer - 1]++;
- if (BUF_STATE_GET_USAGECOUNT(buf_state) < BM_MAX_USAGE_COUNT)
- pg_atomic_write_u32(&bufHdr->state,
- buf_state + BUF_USAGECOUNT_ONE);

+1, it makes sense to do it only if it wasn't pinned already, and it
really should look identical to the code in LocalBufferAlloc, and
perhaps the comment should even say so.

LGTM.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2022-07-24 21:50:59 Re: 012_subtransactions.pl vs clang -fsanitize=undefined
Previous Message Thomas Munro 2022-07-24 21:19:37 Re: 012_subtransactions.pl vs clang -fsanitize=undefined