]> granicus.if.org Git - postgresql/blob - src/include/storage/bufmgr.h
pgindent run for 9.4
[postgresql] / src / include / storage / bufmgr.h
1 /*-------------------------------------------------------------------------
2  *
3  * bufmgr.h
4  *        POSTGRES buffer manager definitions.
5  *
6  *
7  * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/storage/bufmgr.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef BUFMGR_H
15 #define BUFMGR_H
16
17 #include "storage/block.h"
18 #include "storage/buf.h"
19 #include "storage/bufpage.h"
20 #include "storage/relfilenode.h"
21 #include "utils/relcache.h"
22
23 typedef void *Block;
24
25 /* Possible arguments for GetAccessStrategy() */
26 typedef enum BufferAccessStrategyType
27 {
28         BAS_NORMAL,                                     /* Normal random access */
29         BAS_BULKREAD,                           /* Large read-only scan (hint bit updates are
30                                                                  * ok) */
31         BAS_BULKWRITE,                          /* Large multi-block write (e.g. COPY IN) */
32         BAS_VACUUM                                      /* VACUUM */
33 } BufferAccessStrategyType;
34
35 /* Possible modes for ReadBufferExtended() */
36 typedef enum
37 {
38         RBM_NORMAL,                                     /* Normal read */
39         RBM_ZERO,                                       /* Don't read from disk, caller will
40                                                                  * initialize */
41         RBM_ZERO_ON_ERROR,                      /* Read, but return an all-zeros page on error */
42         RBM_NORMAL_NO_LOG                       /* Don't log page as invalid during WAL
43                                                                  * replay; otherwise same as RBM_NORMAL */
44 } ReadBufferMode;
45
46 /* in globals.c ... this duplicates miscadmin.h */
47 extern PGDLLIMPORT int NBuffers;
48
49 /* in bufmgr.c */
50 extern bool zero_damaged_pages;
51 extern int      bgwriter_lru_maxpages;
52 extern double bgwriter_lru_multiplier;
53 extern bool track_io_timing;
54 extern int      target_prefetch_pages;
55
56 /* in buf_init.c */
57 extern PGDLLIMPORT char *BufferBlocks;
58 extern PGDLLIMPORT int32 *PrivateRefCount;
59
60 /* in localbuf.c */
61 extern PGDLLIMPORT int NLocBuffer;
62 extern PGDLLIMPORT Block *LocalBufferBlockPointers;
63 extern PGDLLIMPORT int32 *LocalRefCount;
64
65 /* special block number for ReadBuffer() */
66 #define P_NEW   InvalidBlockNumber              /* grow the file to get a new page */
67
68 /*
69  * Buffer content lock modes (mode argument for LockBuffer())
70  */
71 #define BUFFER_LOCK_UNLOCK              0
72 #define BUFFER_LOCK_SHARE               1
73 #define BUFFER_LOCK_EXCLUSIVE   2
74
75 /*
76  * These routines are beaten on quite heavily, hence the macroization.
77  */
78
79 /*
80  * BufferIsValid
81  *              True iff the given buffer number is valid (either as a shared
82  *              or local buffer).
83  *
84  * Note: For a long time this was defined the same as BufferIsPinned,
85  * that is it would say False if you didn't hold a pin on the buffer.
86  * I believe this was bogus and served only to mask logic errors.
87  * Code should always know whether it has a buffer reference,
88  * independently of the pin state.
89  *
90  * Note: For a further long time this was not quite the inverse of the
91  * BufferIsInvalid() macro, in that it also did sanity checks to verify
92  * that the buffer number was in range.  Most likely, this macro was
93  * originally intended only to be used in assertions, but its use has
94  * since expanded quite a bit, and the overhead of making those checks
95  * even in non-assert-enabled builds can be significant.  Thus, we've
96  * now demoted the range checks to assertions within the macro itself.
97  */
98 #define BufferIsValid(bufnum) \
99 ( \
100         AssertMacro((bufnum) <= NBuffers && (bufnum) >= -NLocBuffer), \
101         (bufnum) != InvalidBuffer  \
102 )
103
104 /*
105  * BufferIsPinned
106  *              True iff the buffer is pinned (also checks for valid buffer number).
107  *
108  *              NOTE: what we check here is that *this* backend holds a pin on
109  *              the buffer.  We do not care whether some other backend does.
110  */
111 #define BufferIsPinned(bufnum) \
112 ( \
113         !BufferIsValid(bufnum) ? \
114                 false \
115         : \
116                 BufferIsLocal(bufnum) ? \
117                         (LocalRefCount[-(bufnum) - 1] > 0) \
118                 : \
119                         (PrivateRefCount[(bufnum) - 1] > 0) \
120 )
121
122 /*
123  * BufferGetBlock
124  *              Returns a reference to a disk page image associated with a buffer.
125  *
126  * Note:
127  *              Assumes buffer is valid.
128  */
129 #define BufferGetBlock(buffer) \
130 ( \
131         AssertMacro(BufferIsValid(buffer)), \
132         BufferIsLocal(buffer) ? \
133                 LocalBufferBlockPointers[-(buffer) - 1] \
134         : \
135                 (Block) (BufferBlocks + ((Size) ((buffer) - 1)) * BLCKSZ) \
136 )
137
138 /*
139  * BufferGetPageSize
140  *              Returns the page size within a buffer.
141  *
142  * Notes:
143  *              Assumes buffer is valid.
144  *
145  *              The buffer can be a raw disk block and need not contain a valid
146  *              (formatted) disk page.
147  */
148 /* XXX should dig out of buffer descriptor */
149 #define BufferGetPageSize(buffer) \
150 ( \
151         AssertMacro(BufferIsValid(buffer)), \
152         (Size)BLCKSZ \
153 )
154
155 /*
156  * BufferGetPage
157  *              Returns the page associated with a buffer.
158  */
159 #define BufferGetPage(buffer) ((Page)BufferGetBlock(buffer))
160
161 /*
162  * prototypes for functions in bufmgr.c
163  */
164 extern void PrefetchBuffer(Relation reln, ForkNumber forkNum,
165                            BlockNumber blockNum);
166 extern Buffer ReadBuffer(Relation reln, BlockNumber blockNum);
167 extern Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum,
168                                    BlockNumber blockNum, ReadBufferMode mode,
169                                    BufferAccessStrategy strategy);
170 extern Buffer ReadBufferWithoutRelcache(RelFileNode rnode,
171                                                   ForkNumber forkNum, BlockNumber blockNum,
172                                                   ReadBufferMode mode, BufferAccessStrategy strategy);
173 extern void ReleaseBuffer(Buffer buffer);
174 extern void UnlockReleaseBuffer(Buffer buffer);
175 extern void MarkBufferDirty(Buffer buffer);
176 extern void IncrBufferRefCount(Buffer buffer);
177 extern Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation,
178                                          BlockNumber blockNum);
179
180 extern void InitBufferPool(void);
181 extern void InitBufferPoolAccess(void);
182 extern void InitBufferPoolBackend(void);
183 extern void AtEOXact_Buffers(bool isCommit);
184 extern void PrintBufferLeakWarning(Buffer buffer);
185 extern void CheckPointBuffers(int flags);
186 extern BlockNumber BufferGetBlockNumber(Buffer buffer);
187 extern BlockNumber RelationGetNumberOfBlocksInFork(Relation relation,
188                                                                 ForkNumber forkNum);
189 extern void FlushRelationBuffers(Relation rel);
190 extern void FlushDatabaseBuffers(Oid dbid);
191 extern void DropRelFileNodeBuffers(RelFileNodeBackend rnode,
192                                            ForkNumber forkNum, BlockNumber firstDelBlock);
193 extern void DropRelFileNodesAllBuffers(RelFileNodeBackend *rnodes, int nnodes);
194 extern void DropDatabaseBuffers(Oid dbid);
195
196 #define RelationGetNumberOfBlocks(reln) \
197         RelationGetNumberOfBlocksInFork(reln, MAIN_FORKNUM)
198
199 extern bool BufferIsPermanent(Buffer buffer);
200 extern XLogRecPtr BufferGetLSNAtomic(Buffer buffer);
201
202 #ifdef NOT_USED
203 extern void PrintPinnedBufs(void);
204 #endif
205 extern Size BufferShmemSize(void);
206 extern void BufferGetTag(Buffer buffer, RelFileNode *rnode,
207                          ForkNumber *forknum, BlockNumber *blknum);
208
209 extern void MarkBufferDirtyHint(Buffer buffer, bool buffer_std);
210
211 extern void UnlockBuffers(void);
212 extern void LockBuffer(Buffer buffer, int mode);
213 extern bool ConditionalLockBuffer(Buffer buffer);
214 extern void LockBufferForCleanup(Buffer buffer);
215 extern bool ConditionalLockBufferForCleanup(Buffer buffer);
216 extern bool HoldingBufferPinThatDelaysRecovery(void);
217
218 extern void AbortBufferIO(void);
219
220 extern void BufmgrCommit(void);
221 extern bool BgBufferSync(void);
222
223 extern void AtProcExit_LocalBuffers(void);
224
225 /* in freelist.c */
226 extern BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype);
227 extern void FreeAccessStrategy(BufferAccessStrategy strategy);
228
229 #endif