Line data Source code
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 in 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 : */
60 : int md_num_open_segs[MAX_FORKNUM + 1];
61 : struct _MdfdVec *md_seg_fds[MAX_FORKNUM + 1];
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;
68 : dlist_node node;
69 : } SMgrRelationData;
70 :
71 : typedef SMgrRelationData *SMgrRelation;
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 uint32 smgrmaxcombine(SMgrRelation reln, ForkNumber forknum,
96 : BlockNumber blocknum);
97 : extern void smgrreadv(SMgrRelation reln, ForkNumber forknum,
98 : BlockNumber blocknum,
99 : void **buffers, BlockNumber nblocks);
100 : extern void smgrwritev(SMgrRelation reln, ForkNumber forknum,
101 : BlockNumber blocknum,
102 : const void **buffers, BlockNumber nblocks,
103 : bool skipFsync);
104 : extern void smgrwriteback(SMgrRelation reln, ForkNumber forknum,
105 : BlockNumber blocknum, BlockNumber nblocks);
106 : extern BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum);
107 : extern BlockNumber smgrnblocks_cached(SMgrRelation reln, ForkNumber forknum);
108 : extern void smgrtruncate(SMgrRelation reln, ForkNumber *forknum,
109 : int nforks, BlockNumber *nblocks);
110 : extern void smgrimmedsync(SMgrRelation reln, ForkNumber forknum);
111 : extern void smgrregistersync(SMgrRelation reln, ForkNumber forknum);
112 : extern void AtEOXact_SMgr(void);
113 : extern bool ProcessBarrierSmgrRelease(void);
114 :
115 : static inline void
116 1196 : smgrread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
117 : void *buffer)
118 : {
119 1196 : smgrreadv(reln, forknum, blocknum, &buffer, 1);
120 1196 : }
121 :
122 : static inline void
123 944666 : smgrwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
124 : const void *buffer, bool skipFsync)
125 : {
126 944666 : smgrwritev(reln, forknum, blocknum, &buffer, 1, skipFsync);
127 944666 : }
128 :
129 : #endif /* SMGR_H */
|