bufmgr.h 5.0 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * bufmgr.h
4
 *	  POSTGRES buffer manager definitions.
5 6
 *
 *
B
Add:  
Bruce Momjian 已提交
7 8
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * Portions Copyright (c) 1994, Regents of the University of California
9
 *
V
WAL  
Vadim B. Mikheev 已提交
10
 * $Id: bufmgr.h,v 1.42 2000/10/28 16:21:00 vadim Exp $
11 12 13
 *
 *-------------------------------------------------------------------------
 */
14
#ifndef BUFMGR_H
15 16
#define BUFMGR_H

17
#include "storage/buf_internals.h"
V
WAL  
Vadim B. Mikheev 已提交
18
#include "access/xlogdefs.h"
19

20
typedef void *Block;
21 22

/* special pageno for bget */
23
#define P_NEW	InvalidBlockNumber		/* grow the file to get a new page */
24 25 26 27 28 29 30 31 32 33 34 35 36 37


/**********************************************************************

  the rest is function defns in the bufmgr that are externally callable

 **********************************************************************/

/*
 * These routines are beaten on quite heavily, hence the macroization.
 * See buf_internals.h for a related comment.
 */
#define BufferDescriptorGetBuffer(bdesc) ((bdesc)->buf_id + 1)

38
extern int	ShowPinTrace;
39

V
Vadim B. Mikheev 已提交
40 41 42
/*
 * Buffer context lock modes
 */
B
Bruce Momjian 已提交
43 44 45
#define BUFFER_LOCK_UNLOCK		0
#define BUFFER_LOCK_SHARE		1
#define BUFFER_LOCK_EXCLUSIVE	2
V
Vadim B. Mikheev 已提交
46

47 48 49 50 51 52 53 54 55 56 57
#define UnlockAndReleaseBuffer(buffer)	\
( \
	LockBuffer(buffer, BUFFER_LOCK_UNLOCK), \
	ReleaseBuffer(buffer) \
)

#define UnlockAndWriteBuffer(buffer)	\
( \
	LockBuffer(buffer, BUFFER_LOCK_UNLOCK), \
	WriteBuffer(buffer) \
)
V
Vadim B. Mikheev 已提交
58

59
/*
B
Bruce Momjian 已提交
60
 * BufferIsValid
61 62 63
 *		True iff the given buffer number is valid (either as a shared
 *		or local buffer).
 *
64 65 66
 * Note:
 *		BufferIsValid(InvalidBuffer) is False.
 *		BufferIsValid(UnknownBuffer) is False.
67 68 69 70 71 72
 *
 * Note: For a long time this was defined the same as BufferIsPinned,
 * that is it would say False if you didn't hold a pin on the buffer.
 * I believe this was bogus and served only to mask logic errors.
 * Code should always know whether it has a buffer reference,
 * independently of the pin state.
73 74
 */
#define BufferIsValid(bufnum) \
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
( \
	BufferIsLocal(bufnum) ? \
		((bufnum) >= -NLocBuffer) \
	: \
		(! BAD_BUFFER_ID(bufnum)) \
)

/*
 * BufferIsPinned
 *		True iff the buffer is pinned (also checks for valid buffer number).
 *
 *		NOTE: what we check here is that *this* backend holds a pin on
 *		the buffer.  We do not care whether some other backend does.
 */
#define BufferIsPinned(bufnum) \
90 91 92 93 94 95 96 97 98 99 100 101
( \
	BufferIsLocal(bufnum) ? \
		((bufnum) >= -NLocBuffer && LocalRefCount[-(bufnum) - 1] > 0) \
	: \
	( \
		BAD_BUFFER_ID(bufnum) ? \
			false \
		: \
			(PrivateRefCount[(bufnum) - 1] > 0) \
	) \
)

102
/*
103 104 105
 * IncrBufferRefCount
 *		Increment the pin count on a buffer that we have *already* pinned
 *		at least once.
106
 *
107 108 109
 *		This macro cannot be used on a buffer we do not have pinned,
 *		because it doesn't change the shared buffer state.  Therefore the
 *		Assert checks are for refcount > 0.  Someone got this wrong once...
110
 */
111 112 113 114
#define IncrBufferRefCount(buffer) \
( \
	BufferIsLocal(buffer) ? \
	( \
115 116 117
		(void) AssertMacro((buffer) >= -NLocBuffer), \
		(void) AssertMacro(LocalRefCount[-(buffer) - 1] > 0), \
		(void) LocalRefCount[-(buffer) - 1]++ \
118 119 120
	) \
	: \
	( \
121 122 123
		(void) AssertMacro(!BAD_BUFFER_ID(buffer)), \
		(void) AssertMacro(PrivateRefCount[(buffer) - 1] > 0), \
		(void) PrivateRefCount[(buffer) - 1]++ \
124 125 126 127
	) \
)

/*
B
Bruce Momjian 已提交
128
 * BufferGetBlock
129 130 131 132 133 134 135
 *		Returns a reference to a disk page image associated with a buffer.
 *
 * Note:
 *		Assumes buffer is valid.
 */
#define BufferGetBlock(buffer) \
( \
136
	AssertMacro(BufferIsValid(buffer)), \
137 138 139 140 141 142 143
	BufferIsLocal(buffer) ? \
		((Block) MAKE_PTR(LocalBufferDescriptors[-(buffer) - 1].data)) \
	: \
		((Block) MAKE_PTR(BufferDescriptors[(buffer) - 1].data)) \
)


144
/*
145
 * prototypes for functions in bufmgr.c
146
 */
147
extern Buffer RelationGetBufferWithBuffer(Relation relation,
148
							BlockNumber blockNumber, Buffer buffer);
149 150 151
extern Buffer ReadBuffer(Relation reln, BlockNumber blockNum);
extern int	WriteBuffer(Buffer buffer);
extern int	WriteNoReleaseBuffer(Buffer buffer);
152
extern Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation,
153
					 BlockNumber blockNum);
154
extern int	FlushBuffer(Buffer buffer, bool release);
155

156
extern void InitBufferPool(IPCKey key);
157
extern void PrintBufferUsage(FILE *statfp);
158
extern void ResetBufferUsage(void);
159
extern void ResetBufferPool(bool isCommit);
160
extern int	BufferPoolCheckLeak(void);
V
Vadim B. Mikheev 已提交
161
extern void FlushBufferPool(void);
162 163
extern BlockNumber BufferGetBlockNumber(Buffer buffer);
extern BlockNumber RelationGetNumberOfBlocks(Relation relation);
164
extern int	FlushRelationBuffers(Relation rel, BlockNumber firstDelBlock);
165
extern void ReleaseRelationBuffers(Relation rel);
166 167 168 169 170 171
extern void DropBuffers(Oid dbid);
extern void PrintPinnedBufs(void);
extern int	BufferShmemSize(void);
extern int	ReleaseBuffer(Buffer buffer);

extern void SetBufferCommitInfoNeedsSave(Buffer buffer);
172

V
Vadim B. Mikheev 已提交
173 174
extern void UnlockBuffers(void);
extern void LockBuffer(Buffer buffer, int mode);
H
 
Hiroshi Inoue 已提交
175
extern void AbortBufferIO(void);
V
Vadim B. Mikheev 已提交
176

177 178 179
extern bool BufferIsUpdatable(Buffer buffer);
extern void MarkBufferForCleanup(Buffer buffer, void (*CleanupFunc)(Buffer));

V
WAL  
Vadim B. Mikheev 已提交
180 181 182 183 184
#ifdef XLOG
extern void BufmgrCommit(void);
extern void BufferSync(void);
#endif

185
#endif