From eb5c404b17752ca566947f12cb702438dcccdcb1 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Mon, 18 Sep 2017 16:36:28 -0400 Subject: [PATCH] Minor code-cleanliness improvements for btree. Make the btree page-flags test macros (P_ISLEAF and friends) return clean boolean values, rather than values that might not fit in a bool. Use them in a few places that were randomly referencing the flag bits directly. In passing, change access/nbtree/'s only direct use of BUFFER_LOCK_SHARE to BT_READ. (Some think we should go the other way, but as long as we have BT_READ/BT_WRITE, let's use them consistently.) Masahiko Sawada, reviewed by Doug Doole Discussion: https://postgr.es/m/CAD21AoBmWPeN=WBB5Jvyz_Nt3rmW1ebUyAnk3ZbJP3RMXALJog@mail.gmail.com --- contrib/amcheck/verify_nbtree.c | 4 ++-- contrib/pgstattuple/pgstattuple.c | 2 +- src/backend/access/nbtree/nbtpage.c | 6 +++--- src/backend/access/nbtree/nbtxlog.c | 4 ++-- src/include/access/nbtree.h | 16 ++++++++-------- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/contrib/amcheck/verify_nbtree.c b/contrib/amcheck/verify_nbtree.c index 9ae83dc839..868c14ec8f 100644 --- a/contrib/amcheck/verify_nbtree.c +++ b/contrib/amcheck/verify_nbtree.c @@ -1195,7 +1195,7 @@ palloc_btree_page(BtreeCheckState *state, BlockNumber blocknum) opaque = (BTPageOpaque) PageGetSpecialPointer(page); - if (opaque->btpo_flags & BTP_META && blocknum != BTREE_METAPAGE) + if (P_ISMETA(opaque) && blocknum != BTREE_METAPAGE) ereport(ERROR, (errcode(ERRCODE_INDEX_CORRUPTED), errmsg("invalid meta page found at block %u in index \"%s\"", @@ -1206,7 +1206,7 @@ palloc_btree_page(BtreeCheckState *state, BlockNumber blocknum) { BTMetaPageData *metad = BTPageGetMeta(page); - if (!(opaque->btpo_flags & BTP_META) || + if (!P_ISMETA(opaque) || metad->btm_magic != BTREE_MAGIC) ereport(ERROR, (errcode(ERRCODE_INDEX_CORRUPTED), diff --git a/contrib/pgstattuple/pgstattuple.c b/contrib/pgstattuple/pgstattuple.c index 7a91cc3468..7ca1bb24d2 100644 --- a/contrib/pgstattuple/pgstattuple.c +++ b/contrib/pgstattuple/pgstattuple.c @@ -416,7 +416,7 @@ pgstat_btree_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno, BTPageOpaque opaque; opaque = (BTPageOpaque) PageGetSpecialPointer(page); - if (opaque->btpo_flags & (BTP_DELETED | BTP_HALF_DEAD)) + if (P_IGNORE(opaque)) { /* recyclable page */ stat->free_space += BLCKSZ; diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c index 5c817b6510..10697e9e23 100644 --- a/src/backend/access/nbtree/nbtpage.c +++ b/src/backend/access/nbtree/nbtpage.c @@ -162,7 +162,7 @@ _bt_getroot(Relation rel, int access) metad = BTPageGetMeta(metapg); /* sanity-check the metapage */ - if (!(metaopaque->btpo_flags & BTP_META) || + if (!P_ISMETA(metaopaque) || metad->btm_magic != BTREE_MAGIC) ereport(ERROR, (errcode(ERRCODE_INDEX_CORRUPTED), @@ -365,7 +365,7 @@ _bt_gettrueroot(Relation rel) metaopaque = (BTPageOpaque) PageGetSpecialPointer(metapg); metad = BTPageGetMeta(metapg); - if (!(metaopaque->btpo_flags & BTP_META) || + if (!P_ISMETA(metaopaque) || metad->btm_magic != BTREE_MAGIC) ereport(ERROR, (errcode(ERRCODE_INDEX_CORRUPTED), @@ -452,7 +452,7 @@ _bt_getrootheight(Relation rel) metad = BTPageGetMeta(metapg); /* sanity-check the metapage */ - if (!(metaopaque->btpo_flags & BTP_META) || + if (!P_ISMETA(metaopaque) || metad->btm_magic != BTREE_MAGIC) ereport(ERROR, (errcode(ERRCODE_INDEX_CORRUPTED), diff --git a/src/backend/access/nbtree/nbtxlog.c b/src/backend/access/nbtree/nbtxlog.c index 3610c7c7e0..4afdf4736f 100644 --- a/src/backend/access/nbtree/nbtxlog.c +++ b/src/backend/access/nbtree/nbtxlog.c @@ -135,7 +135,7 @@ _bt_clear_incomplete_split(XLogReaderState *record, uint8 block_id) Page page = (Page) BufferGetPage(buf); BTPageOpaque pageop = (BTPageOpaque) PageGetSpecialPointer(page); - Assert((pageop->btpo_flags & BTP_INCOMPLETE_SPLIT) != 0); + Assert(P_INCOMPLETE_SPLIT(pageop)); pageop->btpo_flags &= ~BTP_INCOMPLETE_SPLIT; PageSetLSN(page, lsn); @@ -598,7 +598,7 @@ btree_xlog_delete_get_latestRemovedXid(XLogReaderState *record) UnlockReleaseBuffer(ibuffer); return InvalidTransactionId; } - LockBuffer(hbuffer, BUFFER_LOCK_SHARE); + LockBuffer(hbuffer, BT_READ); hpage = (Page) BufferGetPage(hbuffer); /* diff --git a/src/include/access/nbtree.h b/src/include/access/nbtree.h index e6abbec280..2d4c36d0b8 100644 --- a/src/include/access/nbtree.h +++ b/src/include/access/nbtree.h @@ -173,14 +173,14 @@ typedef struct BTMetaPageData */ #define P_LEFTMOST(opaque) ((opaque)->btpo_prev == P_NONE) #define P_RIGHTMOST(opaque) ((opaque)->btpo_next == P_NONE) -#define P_ISLEAF(opaque) ((opaque)->btpo_flags & BTP_LEAF) -#define P_ISROOT(opaque) ((opaque)->btpo_flags & BTP_ROOT) -#define P_ISDELETED(opaque) ((opaque)->btpo_flags & BTP_DELETED) -#define P_ISMETA(opaque) ((opaque)->btpo_flags & BTP_META) -#define P_ISHALFDEAD(opaque) ((opaque)->btpo_flags & BTP_HALF_DEAD) -#define P_IGNORE(opaque) ((opaque)->btpo_flags & (BTP_DELETED|BTP_HALF_DEAD)) -#define P_HAS_GARBAGE(opaque) ((opaque)->btpo_flags & BTP_HAS_GARBAGE) -#define P_INCOMPLETE_SPLIT(opaque) ((opaque)->btpo_flags & BTP_INCOMPLETE_SPLIT) +#define P_ISLEAF(opaque) (((opaque)->btpo_flags & BTP_LEAF) != 0) +#define P_ISROOT(opaque) (((opaque)->btpo_flags & BTP_ROOT) != 0) +#define P_ISDELETED(opaque) (((opaque)->btpo_flags & BTP_DELETED) != 0) +#define P_ISMETA(opaque) (((opaque)->btpo_flags & BTP_META) != 0) +#define P_ISHALFDEAD(opaque) (((opaque)->btpo_flags & BTP_HALF_DEAD) != 0) +#define P_IGNORE(opaque) (((opaque)->btpo_flags & (BTP_DELETED|BTP_HALF_DEAD)) != 0) +#define P_HAS_GARBAGE(opaque) (((opaque)->btpo_flags & BTP_HAS_GARBAGE) != 0) +#define P_INCOMPLETE_SPLIT(opaque) (((opaque)->btpo_flags & BTP_INCOMPLETE_SPLIT) != 0) /* * Lehman and Yao's algorithm requires a ``high key'' on every non-rightmost -- 2.40.0