Header And Logo

PostgreSQL
| The world's most advanced open source database.

bufmgr.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * bufmgr.h
00004  *    POSTGRES buffer manager definitions.
00005  *
00006  *
00007  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00008  * Portions Copyright (c) 1994, Regents of the University of California
00009  *
00010  * src/include/storage/bufmgr.h
00011  *
00012  *-------------------------------------------------------------------------
00013  */
00014 #ifndef BUFMGR_H
00015 #define BUFMGR_H
00016 
00017 #include "storage/block.h"
00018 #include "storage/buf.h"
00019 #include "storage/bufpage.h"
00020 #include "storage/relfilenode.h"
00021 #include "utils/relcache.h"
00022 
00023 typedef void *Block;
00024 
00025 /* Possible arguments for GetAccessStrategy() */
00026 typedef enum BufferAccessStrategyType
00027 {
00028     BAS_NORMAL,                 /* Normal random access */
00029     BAS_BULKREAD,               /* Large read-only scan (hint bit updates are
00030                                  * ok) */
00031     BAS_BULKWRITE,              /* Large multi-block write (e.g. COPY IN) */
00032     BAS_VACUUM                  /* VACUUM */
00033 } BufferAccessStrategyType;
00034 
00035 /* Possible modes for ReadBufferExtended() */
00036 typedef enum
00037 {
00038     RBM_NORMAL,                 /* Normal read */
00039     RBM_ZERO,                   /* Don't read from disk, caller will
00040                                  * initialize */
00041     RBM_ZERO_ON_ERROR           /* Read, but return an all-zeros page on error */
00042 } ReadBufferMode;
00043 
00044 /* in globals.c ... this duplicates miscadmin.h */
00045 extern PGDLLIMPORT int NBuffers;
00046 
00047 /* in bufmgr.c */
00048 extern bool zero_damaged_pages;
00049 extern int  bgwriter_lru_maxpages;
00050 extern double bgwriter_lru_multiplier;
00051 extern bool track_io_timing;
00052 extern int  target_prefetch_pages;
00053 
00054 /* in buf_init.c */
00055 extern PGDLLIMPORT char *BufferBlocks;
00056 extern PGDLLIMPORT int32 *PrivateRefCount;
00057 
00058 /* in localbuf.c */
00059 extern PGDLLIMPORT int NLocBuffer;
00060 extern PGDLLIMPORT Block *LocalBufferBlockPointers;
00061 extern PGDLLIMPORT int32 *LocalRefCount;
00062 
00063 /* special block number for ReadBuffer() */
00064 #define P_NEW   InvalidBlockNumber      /* grow the file to get a new page */
00065 
00066 /*
00067  * Buffer content lock modes (mode argument for LockBuffer())
00068  */
00069 #define BUFFER_LOCK_UNLOCK      0
00070 #define BUFFER_LOCK_SHARE       1
00071 #define BUFFER_LOCK_EXCLUSIVE   2
00072 
00073 /*
00074  * These routines are beaten on quite heavily, hence the macroization.
00075  */
00076 
00077 /*
00078  * BufferIsValid
00079  *      True iff the given buffer number is valid (either as a shared
00080  *      or local buffer).
00081  *
00082  * Note: For a long time this was defined the same as BufferIsPinned,
00083  * that is it would say False if you didn't hold a pin on the buffer.
00084  * I believe this was bogus and served only to mask logic errors.
00085  * Code should always know whether it has a buffer reference,
00086  * independently of the pin state.
00087  *
00088  * Note: For a further long time this was not quite the inverse of the
00089  * BufferIsInvalid() macro, in that it also did sanity checks to verify
00090  * that the buffer number was in range.  Most likely, this macro was
00091  * originally intended only to be used in assertions, but its use has
00092  * since expanded quite a bit, and the overhead of making those checks
00093  * even in non-assert-enabled builds can be significant.  Thus, we've
00094  * now demoted the range checks to assertions within the macro itself.
00095  */
00096 #define BufferIsValid(bufnum) \
00097 ( \
00098     AssertMacro((bufnum) <= NBuffers && (bufnum) >= -NLocBuffer), \
00099     (bufnum) != InvalidBuffer  \
00100 )
00101 
00102 /*
00103  * BufferIsPinned
00104  *      True iff the buffer is pinned (also checks for valid buffer number).
00105  *
00106  *      NOTE: what we check here is that *this* backend holds a pin on
00107  *      the buffer.  We do not care whether some other backend does.
00108  */
00109 #define BufferIsPinned(bufnum) \
00110 ( \
00111     !BufferIsValid(bufnum) ? \
00112         false \
00113     : \
00114         BufferIsLocal(bufnum) ? \
00115             (LocalRefCount[-(bufnum) - 1] > 0) \
00116         : \
00117             (PrivateRefCount[(bufnum) - 1] > 0) \
00118 )
00119 
00120 /*
00121  * BufferGetBlock
00122  *      Returns a reference to a disk page image associated with a buffer.
00123  *
00124  * Note:
00125  *      Assumes buffer is valid.
00126  */
00127 #define BufferGetBlock(buffer) \
00128 ( \
00129     AssertMacro(BufferIsValid(buffer)), \
00130     BufferIsLocal(buffer) ? \
00131         LocalBufferBlockPointers[-(buffer) - 1] \
00132     : \
00133         (Block) (BufferBlocks + ((Size) ((buffer) - 1)) * BLCKSZ) \
00134 )
00135 
00136 /*
00137  * BufferGetPageSize
00138  *      Returns the page size within a buffer.
00139  *
00140  * Notes:
00141  *      Assumes buffer is valid.
00142  *
00143  *      The buffer can be a raw disk block and need not contain a valid
00144  *      (formatted) disk page.
00145  */
00146 /* XXX should dig out of buffer descriptor */
00147 #define BufferGetPageSize(buffer) \
00148 ( \
00149     AssertMacro(BufferIsValid(buffer)), \
00150     (Size)BLCKSZ \
00151 )
00152 
00153 /*
00154  * BufferGetPage
00155  *      Returns the page associated with a buffer.
00156  */
00157 #define BufferGetPage(buffer) ((Page)BufferGetBlock(buffer))
00158 
00159 /*
00160  * prototypes for functions in bufmgr.c
00161  */
00162 extern void PrefetchBuffer(Relation reln, ForkNumber forkNum,
00163                BlockNumber blockNum);
00164 extern Buffer ReadBuffer(Relation reln, BlockNumber blockNum);
00165 extern Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum,
00166                    BlockNumber blockNum, ReadBufferMode mode,
00167                    BufferAccessStrategy strategy);
00168 extern Buffer ReadBufferWithoutRelcache(RelFileNode rnode,
00169                           ForkNumber forkNum, BlockNumber blockNum,
00170                           ReadBufferMode mode, BufferAccessStrategy strategy);
00171 extern void ReleaseBuffer(Buffer buffer);
00172 extern void UnlockReleaseBuffer(Buffer buffer);
00173 extern void MarkBufferDirty(Buffer buffer);
00174 extern void IncrBufferRefCount(Buffer buffer);
00175 extern Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation,
00176                      BlockNumber blockNum);
00177 
00178 extern void InitBufferPool(void);
00179 extern void InitBufferPoolAccess(void);
00180 extern void InitBufferPoolBackend(void);
00181 extern void AtEOXact_Buffers(bool isCommit);
00182 extern void PrintBufferLeakWarning(Buffer buffer);
00183 extern void CheckPointBuffers(int flags);
00184 extern BlockNumber BufferGetBlockNumber(Buffer buffer);
00185 extern BlockNumber RelationGetNumberOfBlocksInFork(Relation relation,
00186                                 ForkNumber forkNum);
00187 extern void FlushRelationBuffers(Relation rel);
00188 extern void FlushDatabaseBuffers(Oid dbid);
00189 extern void DropRelFileNodeBuffers(RelFileNodeBackend rnode,
00190                        ForkNumber forkNum, BlockNumber firstDelBlock);
00191 extern void DropRelFileNodesAllBuffers(RelFileNodeBackend *rnodes, int nnodes);
00192 extern void DropDatabaseBuffers(Oid dbid);
00193 
00194 #define RelationGetNumberOfBlocks(reln) \
00195     RelationGetNumberOfBlocksInFork(reln, MAIN_FORKNUM)
00196 
00197 extern bool BufferIsPermanent(Buffer buffer);
00198 extern XLogRecPtr BufferGetLSNAtomic(Buffer buffer);
00199 
00200 #ifdef NOT_USED
00201 extern void PrintPinnedBufs(void);
00202 #endif
00203 extern Size BufferShmemSize(void);
00204 extern void BufferGetTag(Buffer buffer, RelFileNode *rnode,
00205              ForkNumber *forknum, BlockNumber *blknum);
00206 
00207 extern void MarkBufferDirtyHint(Buffer buffer);
00208 
00209 extern void UnlockBuffers(void);
00210 extern void LockBuffer(Buffer buffer, int mode);
00211 extern bool ConditionalLockBuffer(Buffer buffer);
00212 extern void LockBufferForCleanup(Buffer buffer);
00213 extern bool ConditionalLockBufferForCleanup(Buffer buffer);
00214 extern bool HoldingBufferPinThatDelaysRecovery(void);
00215 
00216 extern void AbortBufferIO(void);
00217 
00218 extern void BufmgrCommit(void);
00219 extern bool BgBufferSync(void);
00220 
00221 extern void AtProcExit_LocalBuffers(void);
00222 
00223 /* in freelist.c */
00224 extern BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype);
00225 extern void FreeAccessStrategy(BufferAccessStrategy strategy);
00226 
00227 #endif