bufmgr.h 4.4 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * bufmgr.h
4
 *	  POSTGRES buffer manager definitions.
5 6 7 8
 *
 *
 * Copyright (c) 1994, Regents of the University of California
 *
B
Bruce Momjian 已提交
9
 * $Id: bufmgr.h,v 1.26 1999/05/25 16:14:39 momjian Exp $
10 11 12
 *
 *-------------------------------------------------------------------------
 */
13
#ifndef BUFMGR_H
14 15
#define BUFMGR_H

16
#include <stdio.h>
17 18 19 20

#include <storage/ipc.h>
#include <storage/block.h>
#include <storage/buf.h>
21
#include <storage/buf_internals.h>
22
#include <utils/rel.h>
23 24 25 26 27 28 29

/*
 * the maximum size of a disk block for any possible installation.
 *
 * in theory this could be anything, but in practice this is actually
 * limited to 2^13 bytes because we have limited ItemIdData.lp_off and
 * ItemIdData.lp_len to 13 bits (see itemid.h).
30 31 32 33
 *
 * limit is now 2^15.  Took four bits from ItemIdData.lp_flags and gave
 * two apiece to ItemIdData.lp_len and lp_off. darrenk 01/06/98
 *
34 35
 */

36
#define MAXBLCKSZ		32768
37

38
typedef void *Block;
39 40

/* special pageno for bget */
41
#define P_NEW	InvalidBlockNumber		/* grow the file to get a new page */
42

43
typedef bits16 BufferLock;
44 45 46 47 48 49 50 51 52 53 54 55 56 57

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

  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)

/*
B
Bruce Momjian 已提交
58
 * BufferIsPinned
59
 *		True iff the buffer is pinned (and therefore valid)
60 61
 *
 * Note:
62 63
 *		Smenatics are identical to BufferIsValid
 *		XXX - need to remove either one eventually.
64 65 66 67
 */
#define BufferIsPinned BufferIsValid


68
extern int	ShowPinTrace;
69

70 71 72
/*
 * BufferWriteModes (settable via SetBufferWriteMode)
 */
73 74
#define BUFFER_FLUSH_WRITE		0		/* immediate write */
#define BUFFER_LATE_WRITE		1		/* delayed write: mark as DIRTY */
75

V
Vadim B. Mikheev 已提交
76 77 78
/*
 * Buffer context lock modes
 */
B
Bruce Momjian 已提交
79 80 81
#define BUFFER_LOCK_UNLOCK		0
#define BUFFER_LOCK_SHARE		1
#define BUFFER_LOCK_EXCLUSIVE	2
V
Vadim B. Mikheev 已提交
82 83


84
/*
B
Bruce Momjian 已提交
85
 * BufferIsValid
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
 *		True iff the refcnt of the local buffer is > 0
 * Note:
 *		BufferIsValid(InvalidBuffer) is False.
 *		BufferIsValid(UnknownBuffer) is False.
 */
#define BufferIsValid(bufnum) \
( \
	BufferIsLocal(bufnum) ? \
		((bufnum) >= -NLocBuffer && LocalRefCount[-(bufnum) - 1] > 0) \
	: \
	( \
		BAD_BUFFER_ID(bufnum) ? \
			false \
		: \
			(PrivateRefCount[(bufnum) - 1] > 0) \
	) \
)

#define IncrBufferRefCount(buffer) \
( \
	BufferIsLocal(buffer) ? \
	( \
		(void)AssertMacro(LocalRefCount[-(buffer) - 1] >= 0), \
		(void)LocalRefCount[-(buffer) - 1]++ \
	) \
	: \
	( \
		(void)AssertMacro(!BAD_BUFFER_ID(buffer)), \
		(void)AssertMacro(PrivateRefCount[(buffer) - 1] >= 0), \
		(void)PrivateRefCount[(buffer) - 1]++ \
	) \
)

/*
B
Bruce Momjian 已提交
120
 * BufferGetBlock
121 122 123 124 125 126 127
 *		Returns a reference to a disk page image associated with a buffer.
 *
 * Note:
 *		Assumes buffer is valid.
 */
#define BufferGetBlock(buffer) \
( \
128
	AssertMacro(BufferIsValid(buffer)), \
129 130 131 132 133 134 135
	BufferIsLocal(buffer) ? \
		((Block) MAKE_PTR(LocalBufferDescriptors[-(buffer) - 1].data)) \
	: \
		((Block) MAKE_PTR(BufferDescriptors[(buffer) - 1].data)) \
)


136
/*
137
 * prototypes for functions in bufmgr.c
138
 */
139
extern Buffer RelationGetBufferWithBuffer(Relation relation,
140
							BlockNumber blockNumber, Buffer buffer);
141 142 143
extern Buffer ReadBuffer(Relation reln, BlockNumber blockNum);
extern int	WriteBuffer(Buffer buffer);
extern int	WriteNoReleaseBuffer(Buffer buffer);
144
extern Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation,
145 146
					 BlockNumber blockNum);

147
extern void InitBufferPool(IPCKey key);
148
extern void PrintBufferUsage(FILE *statfp);
149 150 151 152
extern void ResetBufferUsage(void);
extern void ResetBufferPool(void);
extern int	BufferPoolCheckLeak(void);
extern void FlushBufferPool(int StableMainMemoryFlag);
153 154
extern BlockNumber BufferGetBlockNumber(Buffer buffer);
extern BlockNumber RelationGetNumberOfBlocks(Relation relation);
155
extern void ReleaseRelationBuffers(Relation rel);
156 157 158 159 160 161 162 163 164
extern void DropBuffers(Oid dbid);
extern void PrintPinnedBufs(void);
extern int	BufferShmemSize(void);
extern int	ReleaseBuffer(Buffer buffer);

extern void BufferRefCountReset(int *refcountsave);
extern void BufferRefCountRestore(int *refcountsave);
extern int	SetBufferWriteMode(int mode);
extern void SetBufferCommitInfoNeedsSave(Buffer buffer);
165

V
Vadim B. Mikheev 已提交
166 167 168
extern void UnlockBuffers(void);
extern void LockBuffer(Buffer buffer, int mode);

169
#endif	 /* !defined(BufMgrIncluded) */