Hi, I noticed that the BufferDesc structure contains the following field and a comment :
/* * Buffer's index number (from 0). The field never changes after * initialization, so does not need locking. */ int buf_id; Actually, the comment is wrong, because we are using negative ids for local buffers (i.e. "buf_id" field contains values -1, -2, and so on). We have a comment about it in localbuf.c : /* * negative to indicate local buffer. This is tricky: shared buffers * start with 0. We have to start with -2. (Note that the routine * BufferDescriptorGetBuffer adds 1 to buf_id so our first buffer id * is -1.) */ Maybe we should place this note in buf_internals.h? Anyone who wants to know what this field means will look at the comment in buf_internals.h and be misled. I have attached a patch with my suggestion. What do you think? -- Best regards, Daniil Davydov
From e4f81b6d92034343f02d65f0ecd63ba5a389d625 Mon Sep 17 00:00:00 2001 From: Daniil Davidov <[email protected]> Date: Tue, 20 Jan 2026 13:32:39 +0700 Subject: [PATCH] Fix comment in BufferDesc struct --- src/backend/storage/buffer/localbuf.c | 3 +-- src/include/storage/buf_internals.h | 5 +++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/backend/storage/buffer/localbuf.c b/src/backend/storage/buffer/localbuf.c index 04a540379a2..ea67eca60c2 100644 --- a/src/backend/storage/buffer/localbuf.c +++ b/src/backend/storage/buffer/localbuf.c @@ -761,8 +761,7 @@ InitLocalBuffers(void) BufferDesc *buf = GetLocalBufferDescriptor(i); /* - * negative to indicate local buffer. This is tricky: shared buffers - * start with 0. We have to start with -2. (Note that the routine + * Negative to indicate local buffer. (Note that the routine * BufferDescriptorGetBuffer adds 1 to buf_id so our first buffer id * is -1.) */ diff --git a/src/include/storage/buf_internals.h b/src/include/storage/buf_internals.h index 27f12502d19..a04ac63ce77 100644 --- a/src/include/storage/buf_internals.h +++ b/src/include/storage/buf_internals.h @@ -333,8 +333,9 @@ typedef struct BufferDesc BufferTag tag; /* - * Buffer's index number (from 0). The field never changes after - * initialization, so does not need locking. + * Buffer's index number : positive value (from 0) for shared buffers and + * negative value (from -1) for local buffers. The field never changes + * after initialization, so does not need locking. */ int buf_id; -- 2.43.0
