buf_internals.h 5.5 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * buf_internals.h
T
Tom Lane 已提交
4
 *	  Internal definitions for buffer manager.
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
 *
10
 * $Id: buf_internals.h,v 1.44 2000/11/28 23:27:57 tgl Exp $
11 12 13
 *
 *-------------------------------------------------------------------------
 */
14
#ifndef BUFMGR_INTERNALS_H
15 16
#define BUFMGR_INTERNALS_H

17
#include "storage/buf.h"
B
Bruce Momjian 已提交
18
#include "storage/lmgr.h"
19
#include "storage/s_lock.h"
20 21 22

/* Buf Mgr constants */
/* in bufmgr.c */
23 24 25 26 27
extern int	NBuffers;
extern int	Data_Descriptors;
extern int	Free_List_Descriptor;
extern int	Lookup_List_Descriptor;
extern int	Num_Descriptors;
28 29 30 31

/*
 * Flags for buffer descriptors
 */
32 33 34 35 36 37 38 39
#define BM_DIRTY				(1 << 0)
#define BM_PRIVATE				(1 << 1)
#define BM_VALID				(1 << 2)
#define BM_DELETED				(1 << 3)
#define BM_FREE					(1 << 4)
#define BM_IO_IN_PROGRESS		(1 << 5)
#define BM_IO_ERROR				(1 << 6)
#define BM_JUST_DIRTIED			(1 << 7)
40

41
typedef bits16 BufFlags;
42 43

/* long * so alignment will be correct */
44
typedef long **BufferBlock;
45

T
Tom Lane 已提交
46
typedef struct buftag
47
{
48
	RelFileNode	rnode;
49
	BlockNumber blockNum;		/* blknum relative to begin of reln */
T
Tom Lane 已提交
50
} BufferTag;
51

52 53
#define CLEAR_BUFFERTAG(a) \
( \
54 55
	(a)->rnode.tblNode = InvalidOid, \
	(a)->rnode.relNode = InvalidOid, \
56 57
	(a)->blockNum = InvalidBlockNumber \
)
58 59

#define INIT_BUFFERTAG(a,xx_reln,xx_blockNum) \
60
( \
61
	(a)->blockNum = (xx_blockNum), \
62
	(a)->rnode = (xx_reln)->rd_node \
63 64
)

65 66 67 68
/*
 * We don't need in this data any more but it allows more user
 * friendly error messages. Feel free to get rid of it
 * (and change a lot of places -:))
69 70 71
 */
typedef struct bufblindid
{
72 73 74
	char		dbname[NAMEDATALEN];	/* name of db in which buf belongs */
	char		relname[NAMEDATALEN];	/* name of reln */
}			BufferBlindId;
75

76
#define BAD_BUFFER_ID(bid) ((bid) < 1 || (bid) > NBuffers)
77 78 79
#define INVALID_DESCRIPTOR (-3)

/*
T
Tom Lane 已提交
80 81
 *	BufferDesc -- shared buffer cache metadata for a single
 *				  shared buffer descriptor.
82
 *
83 84 85 86 87 88 89
 *		We keep the name of the database and relation in which this
 *		buffer appears in order to avoid a catalog lookup on cache
 *		flush if we don't have the reldesc in the cache.  It is also
 *		possible that the relation to which this buffer belongs is
 *		not visible to all backends at the time that it gets flushed.
 *		Dbname, relname, dbid, and relid are enough to determine where
 *		to put the buffer, for all storage managers.
90
 */
T
Tom Lane 已提交
91
typedef struct sbufdesc
92
{
T
Tom Lane 已提交
93
	Buffer		freeNext;		/* links for freelist chain */
94 95
	Buffer		freePrev;
	SHMEM_OFFSET data;			/* pointer to data in buf pool */
96

97
	/* tag and id must be together for table lookup to work */
98 99
	BufferTag	tag;			/* file/block identifier */
	int			buf_id;			/* maps global desc to local desc */
100

T
Tom Lane 已提交
101
	BufFlags	flags;			/* see bit definitions above */
102
	unsigned	refcount;		/* # of times buffer is pinned */
103

104
	slock_t		io_in_progress_lock; /* to block for I/O to complete */
V
Vadim B. Mikheev 已提交
105
	slock_t		cntx_lock;		/* to lock access to page context */
106

V
Vadim B. Mikheev 已提交
107 108 109
	unsigned	r_locks;		/* # of shared locks */
	bool		ri_lock;		/* read-intent lock */
	bool		w_lock;			/* context exclusively locked */
110

V
WAL  
Vadim B. Mikheev 已提交
111 112 113 114
#ifdef XLOG
	bool		cntxDirty;		/* new way to mark block as dirty */
#endif

115
	BufferBlindId blind;		/* was used to support blind write */
116 117 118 119 120 121 122 123

	/*
	 * When we can't delete item from page (someone else has buffer pinned)
	 * we mark buffer for cleanup by specifying appropriate for buffer
	 * content cleanup function. Buffer will be cleaned up from release
	 * buffer functions.
	 */
	void		(*CleanupFunc)(Buffer);
T
Tom Lane 已提交
124
} BufferDesc;
125

V
Vadim B. Mikheev 已提交
126
/*
T
Tom Lane 已提交
127 128 129
 * Each backend has its own BufferLocks[] array holding flag bits
 * showing what locks it has set on each buffer.
 *
V
Vadim B. Mikheev 已提交
130 131
 * We have to free these locks in elog(ERROR)...
 */
B
Bruce Momjian 已提交
132 133 134 135
#define BL_IO_IN_PROGRESS	(1 << 0)	/* unimplemented */
#define BL_R_LOCK			(1 << 1)
#define BL_RI_LOCK			(1 << 2)
#define BL_W_LOCK			(1 << 3)
V
Vadim B. Mikheev 已提交
136

137
/*
138
 *	mao tracing buffer allocation
139 140 141 142 143
 */

/*#define BMTRACE*/
#ifdef BMTRACE

144 145
typedef struct _bmtrace
{
146 147 148 149 150 151
	int			bmt_pid;
	long		bmt_buf;
	long		bmt_dbid;
	long		bmt_relid;
	int			bmt_blkno;
	int			bmt_op;
152

153
#define BMT_NOTUSED		0
154
#define BMT_ALLOCFND	1
155 156
#define BMT_ALLOCNOTFND 2
#define BMT_DEALLOC		3
157

158
}			bmtrace;
159

160
#endif	 /* BMTRACE */
161 162


163
/*
164 165 166 167 168 169
 * Bufmgr Interface:
 */

/* Internal routines: only called by buf.c */

/*freelist.c*/
B
Bruce Momjian 已提交
170 171 172 173
extern void AddBufferToFreelist(BufferDesc *bf);
extern void PinBuffer(BufferDesc *buf);
extern void PinBuffer_Debug(char *file, int line, BufferDesc *buf);
extern void UnpinBuffer(BufferDesc *buf);
174
extern BufferDesc *GetFreeBuffer(void);
175
extern void InitFreeList(bool init);
176 177

/* buf_table.c */
178
extern void InitBufTable(void);
179
extern BufferDesc *BufTableLookup(BufferTag *tagPtr);
B
Bruce Momjian 已提交
180 181
extern bool BufTableDelete(BufferDesc *buf);
extern bool BufTableInsert(BufferDesc *buf);
182 183

/* bufmgr.c */
184 185
extern BufferDesc *BufferDescriptors;
extern BufferBlock BufferBlocks;
186
extern long *PrivateRefCount;
V
Vadim B. Mikheev 已提交
187
extern bits8 *BufferLocks;
188 189
extern BufferTag *BufferTagLastDirtied;
extern BufferBlindId *BufferBlindLastDirtied;
190
extern LockRelId *BufferRelidLastDirtied;
191
extern bool *BufferDirtiedByMe;
192
extern SPINLOCK BufMgrLock;
193 194

/* localbuf.c */
195
extern long *LocalRefCount;
196
extern BufferDesc *LocalBufferDescriptors;
197
extern int	NLocBuffer;
198

199
extern BufferDesc *LocalBufferAlloc(Relation reln, BlockNumber blockNum,
200
				 bool *foundPtr);
201
extern int	WriteLocalBuffer(Buffer buffer, bool release);
202
extern int	FlushLocalBuffer(Buffer buffer, bool sync, bool release);
203 204 205
extern void InitLocalBuffer(void);
extern void LocalBufferSync(void);
extern void ResetLocalBufferPool(void);
206

207
#endif	 /* BUFMGR_INTERNALS_H */