1 /*-------------------------------------------------------------------------
4 * POSTGRES buffer manager definitions.
7 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
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_VACUUM
/* VACUUM */
32 } BufferAccessStrategyType
;
34 /* in globals.c ... this duplicates miscadmin.h */
35 extern PGDLLIMPORT
int NBuffers
;
38 extern bool zero_damaged_pages
;
39 extern int bgwriter_lru_maxpages
;
40 extern double bgwriter_lru_multiplier
;
43 extern PGDLLIMPORT
char *BufferBlocks
;
44 extern PGDLLIMPORT int32
*PrivateRefCount
;
47 extern PGDLLIMPORT
int NLocBuffer
;
48 extern PGDLLIMPORT Block
*LocalBufferBlockPointers
;
49 extern PGDLLIMPORT int32
*LocalRefCount
;
51 /* special block number for ReadBuffer() */
52 #define P_NEW InvalidBlockNumber /* grow the file to get a new page */
55 * Buffer content lock modes (mode argument for LockBuffer())
57 #define BUFFER_LOCK_UNLOCK 0
58 #define BUFFER_LOCK_SHARE 1
59 #define BUFFER_LOCK_EXCLUSIVE 2
62 * These routines are beaten on quite heavily, hence the macroization.
67 * True iff the given buffer number is valid (either as a shared
70 * This is not quite the inverse of the BufferIsInvalid() macro, since this
71 * adds sanity rangechecks on the buffer number.
73 * Note: For a long time this was defined the same as BufferIsPinned,
74 * that is it would say False if you didn't hold a pin on the buffer.
75 * I believe this was bogus and served only to mask logic errors.
76 * Code should always know whether it has a buffer reference,
77 * independently of the pin state.
79 #define BufferIsValid(bufnum) \
81 (bufnum) != InvalidBuffer && \
82 (bufnum) >= -NLocBuffer && \
83 (bufnum) <= NBuffers \
88 * True iff the buffer is pinned (also checks for valid buffer number).
90 * NOTE: what we check here is that *this* backend holds a pin on
91 * the buffer. We do not care whether some other backend does.
93 #define BufferIsPinned(bufnum) \
95 !BufferIsValid(bufnum) ? \
98 BufferIsLocal(bufnum) ? \
99 (LocalRefCount[-(bufnum) - 1] > 0) \
101 (PrivateRefCount[(bufnum) - 1] > 0) \
106 * Returns a reference to a disk page image associated with a buffer.
109 * Assumes buffer is valid.
111 #define BufferGetBlock(buffer) \
113 AssertMacro(BufferIsValid(buffer)), \
114 BufferIsLocal(buffer) ? \
115 LocalBufferBlockPointers[-(buffer) - 1] \
117 (Block) (BufferBlocks + ((Size) ((buffer) - 1)) * BLCKSZ) \
122 * Returns the page size within a buffer.
125 * Assumes buffer is valid.
127 * The buffer can be a raw disk block and need not contain a valid
128 * (formatted) disk page.
130 /* XXX should dig out of buffer descriptor */
131 #define BufferGetPageSize(buffer) \
133 AssertMacro(BufferIsValid(buffer)), \
139 * Returns the page associated with a buffer.
141 #define BufferGetPage(buffer) ((Page)BufferGetBlock(buffer))
144 * prototypes for functions in bufmgr.c
146 extern Buffer
ReadBuffer(Relation reln
, BlockNumber blockNum
);
147 extern Buffer
ReadBufferWithFork(Relation reln
, ForkNumber forkNum
, BlockNumber blockNum
);
148 extern Buffer
ReadBufferWithStrategy(Relation reln
, BlockNumber blockNum
,
149 BufferAccessStrategy strategy
);
150 extern Buffer
ReadOrZeroBuffer(Relation reln
, ForkNumber forkNum
,
151 BlockNumber blockNum
);
152 extern Buffer
ReadBufferWithoutRelcache(RelFileNode rnode
, bool isTemp
,
153 ForkNumber forkNum
, BlockNumber blockNum
, bool zeroPage
);
154 extern void ReleaseBuffer(Buffer buffer
);
155 extern void UnlockReleaseBuffer(Buffer buffer
);
156 extern void MarkBufferDirty(Buffer buffer
);
157 extern void IncrBufferRefCount(Buffer buffer
);
158 extern Buffer
ReleaseAndReadBuffer(Buffer buffer
, Relation relation
,
159 BlockNumber blockNum
);
161 extern void InitBufferPool(void);
162 extern void InitBufferPoolAccess(void);
163 extern void InitBufferPoolBackend(void);
164 extern char *ShowBufferUsage(void);
165 extern void ResetBufferUsage(void);
166 extern void AtEOXact_Buffers(bool isCommit
);
167 extern void PrintBufferLeakWarning(Buffer buffer
);
168 extern void CheckPointBuffers(int flags
);
169 extern BlockNumber
BufferGetBlockNumber(Buffer buffer
);
170 extern BlockNumber
RelationGetNumberOfBlocks(Relation relation
);
171 extern void RelationTruncate(Relation rel
, BlockNumber nblocks
);
172 extern void FlushRelationBuffers(Relation rel
);
173 extern void FlushDatabaseBuffers(Oid dbid
);
174 extern void DropRelFileNodeBuffers(RelFileNode rnode
, ForkNumber forkNum
,
175 bool istemp
, BlockNumber firstDelBlock
);
176 extern void DropDatabaseBuffers(Oid dbid
);
179 extern void PrintPinnedBufs(void);
181 extern Size
BufferShmemSize(void);
182 extern void BufferGetTag(Buffer buffer
, RelFileNode
*rnode
,
183 ForkNumber
*forknum
, BlockNumber
*blknum
);
185 extern void SetBufferCommitInfoNeedsSave(Buffer buffer
);
187 extern void UnlockBuffers(void);
188 extern void LockBuffer(Buffer buffer
, int mode
);
189 extern bool ConditionalLockBuffer(Buffer buffer
);
190 extern void LockBufferForCleanup(Buffer buffer
);
191 extern bool ConditionalLockBufferForCleanup(Buffer buffer
);
193 extern void AbortBufferIO(void);
195 extern void BufmgrCommit(void);
196 extern void BgBufferSync(void);
198 extern void AtProcExit_LocalBuffers(void);
201 extern BufferAccessStrategy
GetAccessStrategy(BufferAccessStrategyType btype
);
202 extern void FreeAccessStrategy(BufferAccessStrategy strategy
);