PostgreSQL Source Code  git master
smgr.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * smgr.h
4  * storage manager switch public interface declarations.
5  *
6  *
7  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/storage/smgr.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef SMGR_H
15 #define SMGR_H
16 
17 #include "lib/ilist.h"
18 #include "storage/block.h"
19 #include "storage/relfilelocator.h"
20 
21 /*
22  * smgr.c maintains a table of SMgrRelation objects, which are essentially
23  * cached file handles. An SMgrRelation is created (if not already present)
24  * by smgropen(), and destroyed by smgrdestroy(). Note that neither of these
25  * operations imply I/O, they just create or destroy a hashtable entry. (But
26  * smgrdestroy() may release associated resources, such as OS-level file
27  * descriptors.)
28  *
29  * An SMgrRelation may be "pinned", to prevent it from being destroyed while
30  * it's in use. We use this to prevent pointers relcache to smgr from being
31  * invalidated. SMgrRelations that are not pinned are deleted at end of
32  * transaction.
33  */
34 typedef struct SMgrRelationData
35 {
36  /* rlocator is the hashtable lookup key, so it must be first! */
37  RelFileLocatorBackend smgr_rlocator; /* relation physical identifier */
38 
39  /*
40  * The following fields are reset to InvalidBlockNumber upon a cache flush
41  * event, and hold the last known size for each fork. This information is
42  * currently only reliable during recovery, since there is no cache
43  * invalidation for fork extension.
44  */
45  BlockNumber smgr_targblock; /* current insertion target block */
46  BlockNumber smgr_cached_nblocks[MAX_FORKNUM + 1]; /* last known size */
47 
48  /* additional public fields may someday exist here */
49 
50  /*
51  * Fields below here are intended to be private to smgr.c and its
52  * submodules. Do not touch them from elsewhere.
53  */
54  int smgr_which; /* storage manager selector */
55 
56  /*
57  * for md.c; per-fork arrays of the number of open segments
58  * (md_num_open_segs) and the segments themselves (md_seg_fds).
59  */
62 
63  /*
64  * Pinning support. If unpinned (ie. pincount == 0), 'node' is a list
65  * link in list of all unpinned SMgrRelations.
66  */
67  int pincount;
70 
72 
73 #define SmgrIsTemp(smgr) \
74  RelFileLocatorBackendIsTemp((smgr)->smgr_rlocator)
75 
76 extern void smgrinit(void);
77 extern SMgrRelation smgropen(RelFileLocator rlocator, ProcNumber backend);
78 extern bool smgrexists(SMgrRelation reln, ForkNumber forknum);
79 extern void smgrpin(SMgrRelation reln);
80 extern void smgrunpin(SMgrRelation reln);
81 extern void smgrclose(SMgrRelation reln);
82 extern void smgrdestroyall(void);
83 extern void smgrrelease(SMgrRelation reln);
84 extern void smgrreleaseall(void);
85 extern void smgrreleaserellocator(RelFileLocatorBackend rlocator);
86 extern void smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo);
87 extern void smgrdosyncall(SMgrRelation *rels, int nrels);
88 extern void smgrdounlinkall(SMgrRelation *rels, int nrels, bool isRedo);
89 extern void smgrextend(SMgrRelation reln, ForkNumber forknum,
90  BlockNumber blocknum, const void *buffer, bool skipFsync);
91 extern void smgrzeroextend(SMgrRelation reln, ForkNumber forknum,
92  BlockNumber blocknum, int nblocks, bool skipFsync);
93 extern bool smgrprefetch(SMgrRelation reln, ForkNumber forknum,
94  BlockNumber blocknum, int nblocks);
95 extern void smgrreadv(SMgrRelation reln, ForkNumber forknum,
96  BlockNumber blocknum,
97  void **buffers, BlockNumber nblocks);
98 extern void smgrwritev(SMgrRelation reln, ForkNumber forknum,
99  BlockNumber blocknum,
100  const void **buffers, BlockNumber nblocks,
101  bool skipFsync);
102 extern void smgrwriteback(SMgrRelation reln, ForkNumber forknum,
103  BlockNumber blocknum, BlockNumber nblocks);
104 extern BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum);
106 extern void smgrtruncate(SMgrRelation reln, ForkNumber *forknum,
107  int nforks, BlockNumber *nblocks);
108 extern void smgrimmedsync(SMgrRelation reln, ForkNumber forknum);
109 extern void smgrregistersync(SMgrRelation reln, ForkNumber forknum);
110 extern void AtEOXact_SMgr(void);
111 extern bool ProcessBarrierSmgrRelease(void);
112 
113 static inline void
114 smgrread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
115  void *buffer)
116 {
117  smgrreadv(reln, forknum, blocknum, &buffer, 1);
118 }
119 
120 static inline void
122  const void *buffer, bool skipFsync)
123 {
124  smgrwritev(reln, forknum, blocknum, &buffer, 1, skipFsync);
125 }
126 
127 #endif /* SMGR_H */
uint32 BlockNumber
Definition: block.h:31
int ProcNumber
Definition: procnumber.h:24
ForkNumber
Definition: relpath.h:56
#define MAX_FORKNUM
Definition: relpath.h:70
BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:655
void smgrwriteback(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
Definition: smgr.c:643
void smgrrelease(SMgrRelation reln)
Definition: smgr.c:300
SMgrRelation smgropen(RelFileLocator rlocator, ProcNumber backend)
Definition: smgr.c:198
void smgrinit(void)
Definition: smgr.c:154
void smgrdestroyall(void)
Definition: smgr.c:332
void smgrreleaseall(void)
Definition: smgr.c:353
void smgrpin(SMgrRelation reln)
Definition: smgr.c:250
void smgrunpin(SMgrRelation reln)
Definition: smgr.c:265
void smgrdosyncall(SMgrRelation *rels, int nrels)
Definition: smgr.c:426
void smgrimmedsync(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:789
void smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: smgr.c:411
BlockNumber smgrnblocks_cached(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:679
void smgrclose(SMgrRelation reln)
Definition: smgr.c:320
static void smgrwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
Definition: smgr.h:121
struct SMgrRelationData SMgrRelationData
void smgrzeroextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks, bool skipFsync)
Definition: smgr.c:560
bool ProcessBarrierSmgrRelease(void)
Definition: smgr.c:817
void AtEOXact_SMgr(void)
Definition: smgr.c:807
static void smgrread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void *buffer)
Definition: smgr.h:114
void smgrextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
Definition: smgr.c:535
void smgrtruncate(SMgrRelation reln, ForkNumber *forknum, int nforks, BlockNumber *nblocks)
Definition: smgr.c:703
void smgrwritev(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void **buffers, BlockNumber nblocks, bool skipFsync)
Definition: smgr.c:631
void smgrreleaserellocator(RelFileLocatorBackend rlocator)
Definition: smgr.c:379
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:398
SMgrRelationData * SMgrRelation
Definition: smgr.h:71
void smgrreadv(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void **buffers, BlockNumber nblocks)
Definition: smgr.c:600
void smgrdounlinkall(SMgrRelation *rels, int nrels, bool isRedo)
Definition: smgr.c:462
void smgrregistersync(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:757
bool smgrprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks)
Definition: smgr.c:585
int md_num_open_segs[MAX_FORKNUM+1]
Definition: smgr.h:60
BlockNumber smgr_targblock
Definition: smgr.h:45
BlockNumber smgr_cached_nblocks[MAX_FORKNUM+1]
Definition: smgr.h:46
struct _MdfdVec * md_seg_fds[MAX_FORKNUM+1]
Definition: smgr.h:61
RelFileLocatorBackend smgr_rlocator
Definition: smgr.h:37
dlist_node node
Definition: smgr.h:68
int smgr_which
Definition: smgr.h:54
int pincount
Definition: smgr.h:67
Definition: md.c:81