1996-08-28 03:59:28 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* buf_internals.h
|
2004-01-15 17:14:26 +01:00
|
|
|
* Internal definitions for buffer manager and the buffer replacement
|
|
|
|
* strategy.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
*
|
2003-08-04 04:40:20 +02:00
|
|
|
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
2004-04-20 01:27:17 +02:00
|
|
|
* $PostgreSQL: pgsql/src/include/storage/buf_internals.h,v 1.69 2004/04/19 23:27:17 tgl Exp $
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
#ifndef BUFMGR_INTERNALS_H
|
1996-08-28 03:59:28 +02:00
|
|
|
#define BUFMGR_INTERNALS_H
|
|
|
|
|
2001-07-06 23:04:26 +02:00
|
|
|
#include "storage/backendid.h"
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "storage/buf.h"
|
1999-07-16 19:07:40 +02:00
|
|
|
#include "storage/lmgr.h"
|
2001-09-29 06:02:27 +02:00
|
|
|
#include "storage/lwlock.h"
|
1996-08-28 03:59:28 +02:00
|
|
|
|
2001-07-06 23:04:26 +02:00
|
|
|
|
1996-08-28 03:59:28 +02:00
|
|
|
/*
|
|
|
|
* Flags for buffer descriptors
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
#define BM_DIRTY (1 << 0)
|
2003-12-14 01:34:47 +01:00
|
|
|
#define BM_VALID (1 << 1)
|
|
|
|
#define BM_DELETED (1 << 2)
|
2004-02-12 16:06:56 +01:00
|
|
|
#define BM_IO_IN_PROGRESS (1 << 3)
|
|
|
|
#define BM_IO_ERROR (1 << 4)
|
|
|
|
#define BM_JUST_DIRTIED (1 << 5)
|
|
|
|
#define BM_PIN_COUNT_WAITER (1 << 6)
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
typedef bits16 BufFlags;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
2001-07-06 23:04:26 +02:00
|
|
|
/*
|
|
|
|
* Buffer tag identifies which disk block the buffer contains.
|
|
|
|
*
|
|
|
|
* Note: the BufferTag data must be sufficient to determine where to write the
|
|
|
|
* block, even during a "blind write" with no relcache entry. It's possible
|
|
|
|
* that the backend flushing the buffer doesn't even believe the relation is
|
|
|
|
* visible yet (its xact may have started before the xact that created the
|
|
|
|
* rel). The storage manager must be able to cope anyway.
|
2004-04-20 01:27:17 +02:00
|
|
|
*
|
|
|
|
* Note: if there's any pad bytes in the struct, INIT_BUFFERTAG will have
|
|
|
|
* to be fixed to zero them, since this struct is used as a hash key.
|
2001-07-06 23:04:26 +02:00
|
|
|
*/
|
1999-11-21 20:56:12 +01:00
|
|
|
typedef struct buftag
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2004-04-20 01:27:17 +02:00
|
|
|
RelFileNode rnode; /* physical relation identifier */
|
1997-09-08 04:41:22 +02:00
|
|
|
BlockNumber blockNum; /* blknum relative to begin of reln */
|
1999-11-21 20:56:12 +01:00
|
|
|
} BufferTag;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1998-06-15 20:40:05 +02:00
|
|
|
#define CLEAR_BUFFERTAG(a) \
|
|
|
|
( \
|
2000-10-18 07:50:16 +02:00
|
|
|
(a)->rnode.tblNode = InvalidOid, \
|
|
|
|
(a)->rnode.relNode = InvalidOid, \
|
1998-06-15 20:40:05 +02:00
|
|
|
(a)->blockNum = InvalidBlockNumber \
|
|
|
|
)
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
#define INIT_BUFFERTAG(a,xx_reln,xx_blockNum) \
|
1998-06-15 20:40:05 +02:00
|
|
|
( \
|
1999-09-18 21:08:25 +02:00
|
|
|
(a)->blockNum = (xx_blockNum), \
|
2000-10-18 07:50:16 +02:00
|
|
|
(a)->rnode = (xx_reln)->rd_node \
|
1998-06-15 20:40:05 +02:00
|
|
|
)
|
|
|
|
|
2003-11-13 15:57:15 +01:00
|
|
|
#define BUFFERTAGS_EQUAL(a,b) \
|
|
|
|
( \
|
|
|
|
(a)->rnode.tblNode == (b)->rnode.tblNode && \
|
|
|
|
(a)->rnode.relNode == (b)->rnode.relNode && \
|
|
|
|
(a)->blockNum == (b)->blockNum \
|
|
|
|
)
|
|
|
|
|
1996-08-28 03:59:28 +02:00
|
|
|
/*
|
1999-11-21 20:56:12 +01:00
|
|
|
* BufferDesc -- shared buffer cache metadata for a single
|
|
|
|
* shared buffer descriptor.
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
1999-11-21 20:56:12 +01:00
|
|
|
typedef struct sbufdesc
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-11-13 15:57:15 +01:00
|
|
|
Buffer bufNext; /* link in freelist chain */
|
1997-09-08 04:41:22 +02:00
|
|
|
SHMEM_OFFSET data; /* pointer to data in buf pool */
|
1996-08-28 03:59:28 +02:00
|
|
|
|
2004-04-20 01:27:17 +02:00
|
|
|
/* tag and id must be together for table lookup (still true?) */
|
1997-09-08 04:41:22 +02:00
|
|
|
BufferTag tag; /* file/block identifier */
|
2001-07-06 23:04:26 +02:00
|
|
|
int buf_id; /* buffer's index number (from 0) */
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1999-11-21 20:56:12 +01:00
|
|
|
BufFlags flags; /* see bit definitions above */
|
2001-07-06 23:04:26 +02:00
|
|
|
unsigned refcount; /* # of backends holding pins on buffer */
|
1996-08-28 03:59:28 +02:00
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockId io_in_progress_lock; /* to wait for I/O to complete */
|
|
|
|
LWLockId cntx_lock; /* to lock access to page context */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-10-28 18:21:00 +02:00
|
|
|
bool cntxDirty; /* new way to mark block as dirty */
|
|
|
|
|
2000-10-20 13:01:21 +02:00
|
|
|
/*
|
2001-10-25 07:50:21 +02:00
|
|
|
* We can't physically remove items from a disk page if another
|
|
|
|
* backend has the buffer pinned. Hence, a backend may need to wait
|
2004-04-20 01:27:17 +02:00
|
|
|
* for all other pins to go away. This is signaled by storing its own
|
2001-10-25 07:50:21 +02:00
|
|
|
* backend ID into wait_backend_id and setting flag bit
|
|
|
|
* BM_PIN_COUNT_WAITER. At present, there can be only one such waiter
|
|
|
|
* per buffer.
|
2000-10-20 13:01:21 +02:00
|
|
|
*/
|
2001-10-28 07:26:15 +01:00
|
|
|
BackendId wait_backend_id; /* backend ID of pin-count waiter */
|
1999-11-21 20:56:12 +01:00
|
|
|
} BufferDesc;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
2000-11-30 02:39:08 +01:00
|
|
|
#define BufferDescriptorGetBuffer(bdesc) ((bdesc)->buf_id + 1)
|
|
|
|
|
2003-11-13 15:57:15 +01:00
|
|
|
|
1998-12-15 13:47:01 +01:00
|
|
|
/*
|
1999-11-21 20:56:12 +01:00
|
|
|
* Each backend has its own BufferLocks[] array holding flag bits
|
|
|
|
* showing what locks it has set on each buffer.
|
|
|
|
*
|
2003-07-27 19:10:07 +02:00
|
|
|
* We have to free these locks during ereport(ERROR)...
|
1998-12-15 13:47:01 +01:00
|
|
|
*/
|
1999-05-25 18:15:34 +02:00
|
|
|
#define BL_IO_IN_PROGRESS (1 << 0) /* unimplemented */
|
2001-09-29 06:02:27 +02:00
|
|
|
#define BL_PIN_COUNT_LOCK (1 << 1)
|
1998-12-15 13:47:01 +01:00
|
|
|
|
2004-04-20 01:27:17 +02:00
|
|
|
/* entry for buffer lookup hashtable */
|
2001-10-01 07:36:17 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
2004-04-20 01:27:17 +02:00
|
|
|
BufferTag key; /* Tag of a disk page */
|
|
|
|
int id; /* CDB id of associated CDB */
|
2001-10-01 07:36:17 +02:00
|
|
|
} BufferLookupEnt;
|
|
|
|
|
2004-01-15 17:14:26 +01:00
|
|
|
/*
|
|
|
|
* Definitions for the buffer replacement strategy
|
|
|
|
*/
|
2004-04-20 01:27:17 +02:00
|
|
|
#define STRAT_LIST_UNUSED (-1)
|
2004-01-15 17:14:26 +01:00
|
|
|
#define STRAT_LIST_B1 0
|
|
|
|
#define STRAT_LIST_T1 1
|
|
|
|
#define STRAT_LIST_T2 2
|
|
|
|
#define STRAT_LIST_B2 3
|
|
|
|
#define STRAT_NUM_LISTS 4
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The Cache Directory Block (CDB) of the Adaptive Replacement Cache (ARC)
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
2004-04-20 01:27:17 +02:00
|
|
|
int prev; /* list links */
|
2004-01-15 17:14:26 +01:00
|
|
|
int next;
|
2004-04-20 01:27:17 +02:00
|
|
|
short list; /* ID of list it is currently in */
|
|
|
|
bool t1_vacuum; /* t => present only because of VACUUM */
|
2004-01-15 17:14:26 +01:00
|
|
|
TransactionId t1_xid; /* the xid this entry went onto T1 */
|
2004-04-20 01:27:17 +02:00
|
|
|
BufferTag buf_tag; /* page identifier */
|
|
|
|
int buf_id; /* currently assigned data buffer, or -1 */
|
2004-01-15 17:14:26 +01:00
|
|
|
} BufferStrategyCDB;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The shared ARC control information.
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int target_T1_size; /* What T1 size are we aiming for */
|
|
|
|
int listUnusedCDB; /* All unused StrategyCDB */
|
|
|
|
int listHead[STRAT_NUM_LISTS]; /* ARC lists B1, T1, T2 and B2 */
|
|
|
|
int listTail[STRAT_NUM_LISTS];
|
|
|
|
int listSize[STRAT_NUM_LISTS];
|
|
|
|
Buffer listFreeBuffers; /* List of unused buffers */
|
|
|
|
|
|
|
|
long num_lookup; /* Some hit statistics */
|
|
|
|
long num_hit[STRAT_NUM_LISTS];
|
|
|
|
time_t stat_report;
|
|
|
|
|
2004-04-20 01:27:17 +02:00
|
|
|
/* Array of CDB's starts here */
|
|
|
|
BufferStrategyCDB cdb[1]; /* VARIABLE SIZE ARRAY */
|
2004-01-15 17:14:26 +01:00
|
|
|
} BufferStrategyControl;
|
2004-04-20 01:27:17 +02:00
|
|
|
|
2004-01-15 17:14:26 +01:00
|
|
|
|
2002-08-06 04:36:35 +02:00
|
|
|
/* counters in buf_init.c */
|
2002-09-04 22:31:48 +02:00
|
|
|
extern long int ReadBufferCount;
|
|
|
|
extern long int ReadLocalBufferCount;
|
|
|
|
extern long int BufferHitCount;
|
|
|
|
extern long int LocalBufferHitCount;
|
|
|
|
extern long int BufferFlushCount;
|
|
|
|
extern long int LocalBufferFlushCount;
|
2002-08-06 04:36:35 +02:00
|
|
|
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1996-08-28 03:59:28 +02:00
|
|
|
* Bufmgr Interface:
|
|
|
|
*/
|
|
|
|
|
2004-04-20 01:27:17 +02:00
|
|
|
/* Internal routines: only called by bufmgr */
|
1996-08-28 03:59:28 +02:00
|
|
|
|
2004-04-20 01:27:17 +02:00
|
|
|
/* freelist.c */
|
|
|
|
extern BufferDesc *StrategyBufferLookup(BufferTag *tagPtr, bool recheck,
|
|
|
|
int *cdb_found_index);
|
|
|
|
extern BufferDesc *StrategyGetBuffer(int *cdb_replace_index);
|
|
|
|
extern void StrategyReplaceBuffer(BufferDesc *buf, BufferTag *newTag,
|
|
|
|
int cdb_found_index, int cdb_replace_index);
|
2003-11-13 15:57:15 +01:00
|
|
|
extern void StrategyInvalidateBuffer(BufferDesc *buf);
|
|
|
|
extern void StrategyHintVacuum(bool vacuum_active);
|
2004-04-20 01:27:17 +02:00
|
|
|
extern int StrategyDirtyBufferList(BufferDesc **buffers, BufferTag *buftags,
|
|
|
|
int max_buffers);
|
2003-11-13 15:57:15 +01:00
|
|
|
extern void StrategyInitialize(bool init);
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
/* buf_table.c */
|
2003-11-13 15:57:15 +01:00
|
|
|
extern void InitBufTable(int size);
|
|
|
|
extern int BufTableLookup(BufferTag *tagPtr);
|
2004-04-20 01:27:17 +02:00
|
|
|
extern void BufTableInsert(BufferTag *tagPtr, int cdb_id);
|
|
|
|
extern void BufTableDelete(BufferTag *tagPtr);
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
/* bufmgr.c */
|
1997-09-07 07:04:48 +02:00
|
|
|
extern BufferDesc *BufferDescriptors;
|
1998-12-15 13:47:01 +01:00
|
|
|
extern bits8 *BufferLocks;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
/* localbuf.c */
|
|
|
|
extern BufferDesc *LocalBufferDescriptors;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
extern BufferDesc *LocalBufferAlloc(Relation reln, BlockNumber blockNum,
|
2000-04-12 19:17:23 +02:00
|
|
|
bool *foundPtr);
|
2002-09-04 22:31:48 +02:00
|
|
|
extern void WriteLocalBuffer(Buffer buffer, bool release);
|
2002-08-06 04:36:35 +02:00
|
|
|
extern void AtEOXact_LocalBuffers(bool isCommit);
|
2001-10-28 07:26:15 +01:00
|
|
|
|
2001-11-05 18:46:40 +01:00
|
|
|
#endif /* BUFMGR_INTERNALS_H */
|