Re: Avoid unncessary always true test (src/backend/storage/buffer/bufmgr.c)

From: Ranier Vilela <ranier(dot)vf(at)gmail(dot)com>
To: Karina Litskevich <litskevichkarina(at)gmail(dot)com>
Cc: Gurjeet Singh <gurjeet(at)singh(dot)im>, Richard Guo <guofenglinux(at)gmail(dot)com>, Kyotaro Horiguchi <horikyota(dot)ntt(at)gmail(dot)com>, michael(at)paquier(dot)xyz, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Avoid unncessary always true test (src/backend/storage/buffer/bufmgr.c)
Date: 2023-07-03 11:26:26
Message-ID: CAEudQAr78B8eme9rfzJsT__UOqBWW42csFwUM9ynuzhnCBceOw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Em sex., 30 de jun. de 2023 às 15:48, Karina Litskevich <
litskevichkarina(at)gmail(dot)com> escreveu:

> Hi,
>
> Good catch. Indeed, eb.rel shouldn't be NULL there and the tests should be
> unnecessary.
>
Thanks for the confirmation.

> However, it doesn't follow from the code of these functions.
> From what I can see eb.rel can be NULL in both of these functions. There is
> the following Assert in the beginning of the ExtendBufferedRelTo()
> function:
>
> Assert((eb.rel != NULL) != (eb.smgr != NULL));
>
> And ExtendBufferedRelShared() is only called from ExtendBufferedRelCommon()
> which can be called from ExtendBufferedRelTo() or ExtendBufferedRelBy()
> that
> also has the same Assert(). And none of these functions assigns eb.rel, so
> it can be NULL from the very beginning and it stays the same.
>
>
> And there is the following call in xlogutils.c, which is exactly the case
> when
> eb.rel is NULL:
>
> buffer = ExtendBufferedRelTo(EB_SMGR(smgr, RELPERSISTENCE_PERMANENT),
> forknum,
> NULL,
> EB_PERFORMING_RECOVERY |
> EB_SKIP_EXTENSION_LOCK,
> blkno + 1,
> mode);
>
EB_SMGR and EB_REL are macros for making new structs.
IMO these are buggy, once make new structs without initializing all fields.
Attached a patch to fix this and make more clear when rel or smgr is NULL.

>
>
>
> So as for me calling LockRelationForExtension() and
> UnlockRelationForExtension()
> without testing eb.rel first looks more like a bug here. However, they are
> never
> actually called with eb.rel=NULL because of the EB_* flags, so there is no
> bug
> here. I believe we should add Assert checking that when eb.rel is NULL,
> flags
> are such that we won't use eb.rel. And yes, we can remove unnecessary
> checks
> where the flags value guaranty us that eb.rel is not NULL.
>
Not against these Asserts, but It is very confusing and difficult to
understand them without some comment.

>
> And another minor observation. It seems to me that we don't need a "could
> have
> been closed while waiting for lock" in ExtendBufferedRelShared(), because I
> believe the comment above already explains why updating eb.smgr:
>
> * Note that another backend might have extended the relation by the time
> * we get the lock.
>
Ok, but the first comment still ambiguous, I think that could be:
"/* eb.smgr could have been closed while waiting for lock */"

best regards,
Ranier Vilela

Attachment Content-Type Size
0002-avoid-new-struct-with-undefined-fields-ExtendedBufferedWhat.patch application/octet-stream 764 bytes

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Heikki Linnakangas 2023-07-03 11:37:15 Re: Refactor ssl tests to avoid using internal PostgreSQL::Test::Cluster methods
Previous Message vignesh C 2023-07-03 11:19:40 Re: pg_decode_message vs skip_empty_xacts and xact_wrote_changes