Skip to content

Commit 4cb658a

Browse files
committed
Refactor reloption handling for index AMs in-core
This reworks the reloption parsing and build of a couple of index AMs by creating new structures for each index AM's options. This split was already done for BRIN, GIN and GiST (which actually has a fillfactor parameter), but not for hash, B-tree and SPGiST which relied on StdRdOptions due to an overlap with the default option set. This saves a couple of bytes for rd_options in each relcache entry with indexes making use of relation options, and brings more consistency between all index AMs. While on it, add a couple of AssertMacro() calls to make sure that utility macros to grab values of reloptions are used with the expected index AM. Author: Nikolay Shaplov Reviewed-by: Amit Langote, Michael Paquier, Álvaro Herrera, Dent John Discussion: https://postgr.es/m/4127670.gFlpRb6XCm@x200m
1 parent 114541d commit 4cb658a

File tree

16 files changed

+110
-24
lines changed

16 files changed

+110
-24
lines changed

src/backend/access/common/reloptions.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "access/htup_details.h"
2424
#include "access/nbtree.h"
2525
#include "access/reloptions.h"
26-
#include "access/spgist.h"
26+
#include "access/spgist_private.h"
2727
#include "catalog/pg_type.h"
2828
#include "commands/defrem.h"
2929
#include "commands/tablespace.h"
@@ -1521,8 +1521,6 @@ default_reloptions(Datum reloptions, bool validate, relopt_kind kind)
15211521
offsetof(StdRdOptions, user_catalog_table)},
15221522
{"parallel_workers", RELOPT_TYPE_INT,
15231523
offsetof(StdRdOptions, parallel_workers)},
1524-
{"vacuum_cleanup_index_scale_factor", RELOPT_TYPE_REAL,
1525-
offsetof(StdRdOptions, vacuum_cleanup_index_scale_factor)},
15261524
{"vacuum_index_cleanup", RELOPT_TYPE_BOOL,
15271525
offsetof(StdRdOptions, vacuum_index_cleanup)},
15281526
{"vacuum_truncate", RELOPT_TYPE_BOOL,

src/backend/access/hash/hashpage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ _hash_init(Relation rel, double num_tuples, ForkNumber forkNum)
358358
data_width = sizeof(uint32);
359359
item_width = MAXALIGN(sizeof(IndexTupleData)) + MAXALIGN(data_width) +
360360
sizeof(ItemIdData); /* include the line pointer */
361-
ffactor = RelationGetTargetPageUsage(rel, HASH_DEFAULT_FILLFACTOR) / item_width;
361+
ffactor = HashGetTargetPageUsage(rel) / item_width;
362362
/* keep to a sane range */
363363
if (ffactor < 10)
364364
ffactor = 10;

src/backend/access/hash/hashutil.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,14 @@ _hash_checkpage(Relation rel, Buffer buf, int flags)
289289
bytea *
290290
hashoptions(Datum reloptions, bool validate)
291291
{
292-
return default_reloptions(reloptions, validate, RELOPT_KIND_HASH);
292+
static const relopt_parse_elt tab[] = {
293+
{"fillfactor", RELOPT_TYPE_INT, offsetof(HashOptions, fillfactor)},
294+
};
295+
296+
return (bytea *) build_reloptions(reloptions, validate,
297+
RELOPT_KIND_HASH,
298+
sizeof(HashOptions),
299+
tab, lengthof(tab));
293300
}
294301

295302
/*

src/backend/access/nbtree/nbtree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ _bt_vacuum_needs_cleanup(IndexVacuumInfo *info)
816816
}
817817
else
818818
{
819-
StdRdOptions *relopts;
819+
BTOptions *relopts;
820820
float8 cleanup_scale_factor;
821821
float8 prev_num_heap_tuples;
822822

@@ -827,7 +827,7 @@ _bt_vacuum_needs_cleanup(IndexVacuumInfo *info)
827827
* tuples exceeds vacuum_cleanup_index_scale_factor fraction of
828828
* original tuples count.
829829
*/
830-
relopts = (StdRdOptions *) info->index->rd_options;
830+
relopts = (BTOptions *) info->index->rd_options;
831831
cleanup_scale_factor = (relopts &&
832832
relopts->vacuum_cleanup_index_scale_factor >= 0)
833833
? relopts->vacuum_cleanup_index_scale_factor

src/backend/access/nbtree/nbtsort.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,8 +716,8 @@ _bt_pagestate(BTWriteState *wstate, uint32 level)
716716
if (level > 0)
717717
state->btps_full = (BLCKSZ * (100 - BTREE_NONLEAF_FILLFACTOR) / 100);
718718
else
719-
state->btps_full = RelationGetTargetPageFreeSpace(wstate->index,
720-
BTREE_DEFAULT_FILLFACTOR);
719+
state->btps_full = BTGetTargetPageFreeSpace(wstate->index);
720+
721721
/* no parent level, yet */
722722
state->btps_next = NULL;
723723

src/backend/access/nbtree/nbtsplitloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ _bt_findsplitloc(Relation rel,
167167

168168
/* Count up total space in data items before actually scanning 'em */
169169
olddataitemstotal = rightspace - (int) PageGetExactFreeSpace(page);
170-
leaffillfactor = RelationGetFillFactor(rel, BTREE_DEFAULT_FILLFACTOR);
170+
leaffillfactor = BTGetFillFactor(rel);
171171

172172
/* Passed-in newitemsz is MAXALIGNED but does not include line pointer */
173173
newitemsz += sizeof(ItemIdData);

src/backend/access/nbtree/nbtutils.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2014,7 +2014,18 @@ BTreeShmemInit(void)
20142014
bytea *
20152015
btoptions(Datum reloptions, bool validate)
20162016
{
2017-
return default_reloptions(reloptions, validate, RELOPT_KIND_BTREE);
2017+
static const relopt_parse_elt tab[] = {
2018+
{"fillfactor", RELOPT_TYPE_INT, offsetof(BTOptions, fillfactor)},
2019+
{"vacuum_cleanup_index_scale_factor", RELOPT_TYPE_REAL,
2020+
offsetof(BTOptions, vacuum_cleanup_index_scale_factor)}
2021+
2022+
};
2023+
2024+
return (bytea *) build_reloptions(reloptions, validate,
2025+
RELOPT_KIND_BTREE,
2026+
sizeof(BTOptions),
2027+
tab, lengthof(tab));
2028+
20182029
}
20192030

20202031
/*

src/backend/access/spgist/spgutils.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,7 @@ SpGistGetBuffer(Relation index, int flags, int needSpace, bool *isNew)
408408
* related to the ones already on it. But fillfactor mustn't cause an
409409
* error for requests that would otherwise be legal.
410410
*/
411-
needSpace += RelationGetTargetPageFreeSpace(index,
412-
SPGIST_DEFAULT_FILLFACTOR);
411+
needSpace += SpGistGetTargetPageFreeSpace(index);
413412
needSpace = Min(needSpace, SPGIST_PAGE_CAPACITY);
414413

415414
/* Get the cache entry for this flags setting */
@@ -586,7 +585,15 @@ SpGistInitMetapage(Page page)
586585
bytea *
587586
spgoptions(Datum reloptions, bool validate)
588587
{
589-
return default_reloptions(reloptions, validate, RELOPT_KIND_SPGIST);
588+
static const relopt_parse_elt tab[] = {
589+
{"fillfactor", RELOPT_TYPE_INT, offsetof(SpGistOptions, fillfactor)},
590+
};
591+
592+
return (bytea *) build_reloptions(reloptions, validate,
593+
RELOPT_KIND_SPGIST,
594+
sizeof(SpGistOptions),
595+
tab, lengthof(tab));
596+
590597
}
591598

592599
/*

src/include/access/brin.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,15 @@ typedef struct BrinStatsData
3737

3838
#define BRIN_DEFAULT_PAGES_PER_RANGE 128
3939
#define BrinGetPagesPerRange(relation) \
40-
((relation)->rd_options ? \
40+
(AssertMacro(relation->rd_rel->relkind == RELKIND_INDEX && \
41+
relation->rd_rel->relam == BRIN_AM_OID), \
42+
(relation)->rd_options ? \
4143
((BrinOptions *) (relation)->rd_options)->pagesPerRange : \
4244
BRIN_DEFAULT_PAGES_PER_RANGE)
4345
#define BrinGetAutoSummarize(relation) \
44-
((relation)->rd_options ? \
46+
(AssertMacro(relation->rd_rel->relkind == RELKIND_INDEX && \
47+
relation->rd_rel->relam == BRIN_AM_OID), \
48+
(relation)->rd_options ? \
4549
((BrinOptions *) (relation)->rd_options)->autosummarize : \
4650
false)
4751

src/include/access/gin_private.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "access/gin.h"
1515
#include "access/ginblock.h"
1616
#include "access/itup.h"
17+
#include "catalog/pg_am_d.h"
1718
#include "fmgr.h"
1819
#include "storage/bufmgr.h"
1920
#include "lib/rbtree.h"
@@ -30,10 +31,14 @@ typedef struct GinOptions
3031

3132
#define GIN_DEFAULT_USE_FASTUPDATE true
3233
#define GinGetUseFastUpdate(relation) \
33-
((relation)->rd_options ? \
34+
(AssertMacro(relation->rd_rel->relkind == RELKIND_INDEX && \
35+
relation->rd_rel->relam == GIN_AM_OID), \
36+
(relation)->rd_options ? \
3437
((GinOptions *) (relation)->rd_options)->useFastUpdate : GIN_DEFAULT_USE_FASTUPDATE)
3538
#define GinGetPendingListCleanupSize(relation) \
36-
((relation)->rd_options && \
39+
(AssertMacro(relation->rd_rel->relkind == RELKIND_INDEX && \
40+
relation->rd_rel->relam == GIN_AM_OID), \
41+
(relation)->rd_options && \
3742
((GinOptions *) (relation)->rd_options)->pendingListCleanupSize != -1 ? \
3843
((GinOptions *) (relation)->rd_options)->pendingListCleanupSize : \
3944
gin_pending_list_limit)

src/include/access/hash.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "access/amapi.h"
2121
#include "access/itup.h"
2222
#include "access/sdir.h"
23+
#include "catalog/pg_am_d.h"
2324
#include "lib/stringinfo.h"
2425
#include "storage/bufmgr.h"
2526
#include "storage/lockdefs.h"
@@ -263,6 +264,21 @@ typedef struct HashMetaPageData
263264

264265
typedef HashMetaPageData *HashMetaPage;
265266

267+
typedef struct HashOptions
268+
{
269+
int32 varlena_header_; /* varlena header (do not touch directly!) */
270+
int fillfactor; /* page fill factor in percent (0..100) */
271+
} HashOptions;
272+
273+
#define HashGetFillFactor(relation) \
274+
(AssertMacro(relation->rd_rel->relkind == RELKIND_INDEX && \
275+
relation->rd_rel->relam == HASH_AM_OID), \
276+
(relation)->rd_options ? \
277+
((HashOptions *) (relation)->rd_options)->fillfactor : \
278+
HASH_DEFAULT_FILLFACTOR)
279+
#define HashGetTargetPageUsage(relation) \
280+
(BLCKSZ * HashGetFillFactor(relation) / 100)
281+
266282
/*
267283
* Maximum size of a hash index item (it's okay to have only one per page)
268284
*/

src/include/access/nbtree.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "access/itup.h"
1919
#include "access/sdir.h"
2020
#include "access/xlogreader.h"
21+
#include "catalog/pg_am_d.h"
2122
#include "catalog/pg_index.h"
2223
#include "lib/stringinfo.h"
2324
#include "storage/bufmgr.h"
@@ -680,6 +681,23 @@ typedef BTScanOpaqueData *BTScanOpaque;
680681
#define SK_BT_DESC (INDOPTION_DESC << SK_BT_INDOPTION_SHIFT)
681682
#define SK_BT_NULLS_FIRST (INDOPTION_NULLS_FIRST << SK_BT_INDOPTION_SHIFT)
682683

684+
typedef struct BTOptions
685+
{
686+
int32 varlena_header_; /* varlena header (do not touch directly!) */
687+
int fillfactor; /* page fill factor in percent (0..100) */
688+
/* fraction of newly inserted tuples prior to trigger index cleanup */
689+
float8 vacuum_cleanup_index_scale_factor;
690+
} BTOptions;
691+
692+
#define BTGetFillFactor(relation) \
693+
(AssertMacro(relation->rd_rel->relkind == RELKIND_INDEX && \
694+
relation->rd_rel->relam == BTREE_AM_OID), \
695+
(relation)->rd_options ? \
696+
((BTOptions *) (relation)->rd_options)->fillfactor : \
697+
BTREE_DEFAULT_FILLFACTOR)
698+
#define BTGetTargetPageFreeSpace(relation) \
699+
(BLCKSZ * (100 - BTGetFillFactor(relation)) / 100)
700+
683701
/*
684702
* Constant definition for progress reporting. Phase numbers must match
685703
* btbuildphasename.

src/include/access/spgist.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
#include "lib/stringinfo.h"
2020

2121

22-
/* reloption parameters */
23-
#define SPGIST_MIN_FILLFACTOR 10
24-
#define SPGIST_DEFAULT_FILLFACTOR 80
25-
2622
/* SPGiST opclass support function numbers */
2723
#define SPGIST_CONFIG_PROC 1
2824
#define SPGIST_CHOOSE_PROC 2

src/include/access/spgist_private.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,29 @@
1616

1717
#include "access/itup.h"
1818
#include "access/spgist.h"
19+
#include "catalog/pg_am_d.h"
1920
#include "nodes/tidbitmap.h"
2021
#include "storage/buf.h"
2122
#include "utils/geo_decls.h"
2223
#include "utils/relcache.h"
2324

2425

26+
typedef struct SpGistOptions
27+
{
28+
int32 varlena_header_; /* varlena header (do not touch directly!) */
29+
int fillfactor; /* page fill factor in percent (0..100) */
30+
} SpGistOptions;
31+
32+
#define SpGistGetFillFactor(relation) \
33+
(AssertMacro(relation->rd_rel->relkind == RELKIND_INDEX && \
34+
relation->rd_rel->relam == SPGIST_AM_OID), \
35+
(relation)->rd_options ? \
36+
((SpGistOptions *) (relation)->rd_options)->fillfactor : \
37+
SPGIST_DEFAULT_FILLFACTOR)
38+
#define SpGistGetTargetPageFreeSpace(relation) \
39+
(BLCKSZ * (100 - SpGistGetFillFactor(relation)) / 100)
40+
41+
2542
/* Page numbers of fixed-location pages */
2643
#define SPGIST_METAPAGE_BLKNO (0) /* metapage */
2744
#define SPGIST_ROOT_BLKNO (1) /* root for normal entries */
@@ -423,6 +440,11 @@ typedef SpGistDeadTupleData *SpGistDeadTuple;
423440
#define GBUF_REQ_NULLS(flags) ((flags) & GBUF_NULLS)
424441

425442
/* spgutils.c */
443+
444+
/* reloption parameters */
445+
#define SPGIST_MIN_FILLFACTOR 10
446+
#define SPGIST_DEFAULT_FILLFACTOR 80
447+
426448
extern SpGistCache *spgGetCache(Relation index);
427449
extern void initSpGistState(SpGistState *state, Relation index);
428450
extern Buffer SpGistNewBuffer(Relation index);

src/include/utils/rel.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ typedef struct ForeignKeyCacheInfo
235235

236236
/*
237237
* StdRdOptions
238-
* Standard contents of rd_options for heaps and generic indexes.
238+
* Standard contents of rd_options for heaps.
239239
*
240240
* RelationGetFillFactor() and RelationGetTargetPageFreeSpace() can only
241241
* be applied to relations that use this format or a superset for
@@ -265,7 +265,6 @@ typedef struct StdRdOptions
265265
int32 vl_len_; /* varlena header (do not touch directly!) */
266266
int fillfactor; /* page fill factor in percent (0..100) */
267267
/* fraction of newly inserted tuples prior to trigger index cleanup */
268-
float8 vacuum_cleanup_index_scale_factor;
269268
int toast_tuple_target; /* target for tuple toasting */
270269
AutoVacOpts autovacuum; /* autovacuum-related options */
271270
bool user_catalog_table; /* use as an additional catalog relation */

src/tools/pgindent/typedefs.list

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ BTInsertStateData
171171
BTLeader
172172
BTMetaPageData
173173
BTOneVacInfo
174+
BTOptions
174175
BTPS_State
175176
BTPageOpaque
176177
BTPageOpaqueData
@@ -978,6 +979,7 @@ HashJoinTuple
978979
HashMemoryChunk
979980
HashMetaPage
980981
HashMetaPageData
982+
HashOptions
981983
HashPageOpaque
982984
HashPageOpaqueData
983985
HashPageStat
@@ -2270,6 +2272,7 @@ SpGistLeafTupleData
22702272
SpGistMetaPageData
22712273
SpGistNodeTuple
22722274
SpGistNodeTupleData
2275+
SpGistOptions
22732276
SpGistPageOpaque
22742277
SpGistPageOpaqueData
22752278
SpGistScanOpaque

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy