1 /*-------------------------------------------------------------------------
4 * POSTGRES buffer manager definitions.
7 * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/include/storage/bufmgr.h
12 *-------------------------------------------------------------------------
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"
25 /* Possible arguments for GetAccessStrategy() */
26 typedef enum BufferAccessStrategyType
28 BAS_NORMAL, /* Normal random access */
29 BAS_BULKREAD, /* Large read-only scan (hint bit updates are
31 BAS_BULKWRITE, /* Large multi-block write (e.g. COPY IN) */
32 BAS_VACUUM /* VACUUM */
33 } BufferAccessStrategyType;
35 /* Possible modes for ReadBufferExtended() */
38 RBM_NORMAL, /* Normal read */
39 RBM_ZERO, /* Don't read from disk, caller will
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 */
46 /* in globals.c ... this duplicates miscadmin.h */
47 extern PGDLLIMPORT int NBuffers;
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;
57 extern PGDLLIMPORT char *BufferBlocks;
58 extern PGDLLIMPORT int32 *PrivateRefCount;
61 extern PGDLLIMPORT int NLocBuffer;
62 extern PGDLLIMPORT Block *LocalBufferBlockPointers;
63 extern PGDLLIMPORT int32 *LocalRefCount;
65 /* special block number for ReadBuffer() */
66 #define P_NEW InvalidBlockNumber /* grow the file to get a new page */
69 * Buffer content lock modes (mode argument for LockBuffer())
71 #define BUFFER_LOCK_UNLOCK 0
72 #define BUFFER_LOCK_SHARE 1
73 #define BUFFER_LOCK_EXCLUSIVE 2
76 * These routines are beaten on quite heavily, hence the macroization.
81 * True iff the given buffer number is valid (either as a shared
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.
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.
98 #define BufferIsValid(bufnum) \
100 AssertMacro((bufnum) <= NBuffers && (bufnum) >= -NLocBuffer), \
101 (bufnum) != InvalidBuffer \
106 * True iff the buffer is pinned (also checks for valid buffer number).
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.
111 #define BufferIsPinned(bufnum) \
113 !BufferIsValid(bufnum) ? \
116 BufferIsLocal(bufnum) ? \
117 (LocalRefCount[-(bufnum) - 1] > 0) \
119 (PrivateRefCount[(bufnum) - 1] > 0) \
124 * Returns a reference to a disk page image associated with a buffer.
127 * Assumes buffer is valid.
129 #define BufferGetBlock(buffer) \
131 AssertMacro(BufferIsValid(buffer)), \
132 BufferIsLocal(buffer) ? \
133 LocalBufferBlockPointers[-(buffer) - 1] \
135 (Block) (BufferBlocks + ((Size) ((buffer) - 1)) * BLCKSZ) \
140 * Returns the page size within a buffer.
143 * Assumes buffer is valid.
145 * The buffer can be a raw disk block and need not contain a valid
146 * (formatted) disk page.
148 /* XXX should dig out of buffer descriptor */
149 #define BufferGetPageSize(buffer) \
151 AssertMacro(BufferIsValid(buffer)), \
157 * Returns the page associated with a buffer.
159 #define BufferGetPage(buffer) ((Page)BufferGetBlock(buffer))
162 * prototypes for functions in bufmgr.c
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);
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,
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);
196 #define RelationGetNumberOfBlocks(reln) \
197 RelationGetNumberOfBlocksInFork(reln, MAIN_FORKNUM)
199 extern bool BufferIsPermanent(Buffer buffer);
200 extern XLogRecPtr BufferGetLSNAtomic(Buffer buffer);
203 extern void PrintPinnedBufs(void);
205 extern Size BufferShmemSize(void);
206 extern void BufferGetTag(Buffer buffer, RelFileNode *rnode,
207 ForkNumber *forknum, BlockNumber *blknum);
209 extern void MarkBufferDirtyHint(Buffer buffer, bool buffer_std);
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);
218 extern void AbortBufferIO(void);
220 extern void BufmgrCommit(void);
221 extern bool BgBufferSync(void);
223 extern void AtProcExit_LocalBuffers(void);
226 extern BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype);
227 extern void FreeAccessStrategy(BufferAccessStrategy strategy);