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
|
2004-08-29 07:07:03 +02:00
|
|
|
* strategy.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
*
|
2008-01-01 20:46:01 +01:00
|
|
|
* Portions Copyright (c) 1996-2008, 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
|
|
|
*
|
2008-06-19 02:46:06 +02:00
|
|
|
* $PostgreSQL: pgsql/src/include/storage/buf_internals.h,v 1.97 2008/06/19 00:46:06 alvherre 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
|
|
|
|
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "storage/buf.h"
|
2001-09-29 06:02:27 +02:00
|
|
|
#include "storage/lwlock.h"
|
2005-02-04 00:29:19 +01:00
|
|
|
#include "storage/shmem.h"
|
2008-06-12 11:12:31 +02:00
|
|
|
#include "storage/smgr.h"
|
2005-03-04 21:21:07 +01:00
|
|
|
#include "storage/spin.h"
|
2008-06-19 02:46:06 +02:00
|
|
|
#include "utils/relcache.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
|
2005-03-04 21:21:07 +01:00
|
|
|
*
|
|
|
|
* Note: TAG_VALID essentially means that there is a buffer hashtable
|
|
|
|
* entry associated with the buffer's tag.
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
2004-08-29 07:07:03 +02:00
|
|
|
#define BM_DIRTY (1 << 0) /* data needs writing */
|
|
|
|
#define BM_VALID (1 << 1) /* data is valid */
|
2005-03-04 21:21:07 +01:00
|
|
|
#define BM_TAG_VALID (1 << 2) /* tag is assigned */
|
2005-10-15 04:49:52 +02:00
|
|
|
#define BM_IO_IN_PROGRESS (1 << 3) /* read or write in progress */
|
2005-03-04 21:21:07 +01:00
|
|
|
#define BM_IO_ERROR (1 << 4) /* previous I/O failed */
|
2005-10-15 04:49:52 +02:00
|
|
|
#define BM_JUST_DIRTIED (1 << 5) /* dirtied since write started */
|
|
|
|
#define BM_PIN_COUNT_WAITER (1 << 6) /* have waiter for sole pin */
|
2007-06-28 02:02:40 +02:00
|
|
|
#define BM_CHECKPOINT_NEEDED (1 << 7) /* must write for checkpoint */
|
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
|
|
|
|
2005-03-04 21:21:07 +01:00
|
|
|
/*
|
|
|
|
* The maximum allowed value of usage_count represents a tradeoff between
|
|
|
|
* accuracy and speed of the clock-sweep buffer management algorithm. A
|
|
|
|
* large value (comparable to NBuffers) would approximate LRU semantics.
|
|
|
|
* But it can take as many as BM_MAX_USAGE_COUNT+1 complete cycles of
|
|
|
|
* clock sweeps to find a free buffer, so in practice we don't want the
|
|
|
|
* value to be very large.
|
|
|
|
*/
|
|
|
|
#define BM_MAX_USAGE_COUNT 5
|
|
|
|
|
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
|
2005-02-04 00:29:19 +01:00
|
|
|
* block, without reference to pg_class or pg_tablespace entries. 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) \
|
|
|
|
( \
|
2004-06-18 08:14:31 +02:00
|
|
|
(a).rnode.spcNode = InvalidOid, \
|
|
|
|
(a).rnode.dbNode = InvalidOid, \
|
2004-04-21 20:06:30 +02:00
|
|
|
(a).rnode.relNode = InvalidOid, \
|
|
|
|
(a).blockNum = InvalidBlockNumber \
|
1998-06-15 20:40:05 +02:00
|
|
|
)
|
1996-08-28 03:59:28 +02:00
|
|
|
|
2008-06-12 11:12:31 +02:00
|
|
|
#define INIT_BUFFERTAG(a,xx_rnode,xx_blockNum) \
|
1998-06-15 20:40:05 +02:00
|
|
|
( \
|
2008-06-12 11:12:31 +02:00
|
|
|
(a).rnode = (xx_rnode), \
|
2004-04-21 20:06:30 +02:00
|
|
|
(a).blockNum = (xx_blockNum) \
|
1998-06-15 20:40:05 +02:00
|
|
|
)
|
|
|
|
|
2003-11-13 15:57:15 +01:00
|
|
|
#define BUFFERTAGS_EQUAL(a,b) \
|
|
|
|
( \
|
2004-04-21 20:06:30 +02:00
|
|
|
RelFileNodeEquals((a).rnode, (b).rnode) && \
|
|
|
|
(a).blockNum == (b).blockNum \
|
2003-11-13 15:57:15 +01:00
|
|
|
)
|
|
|
|
|
2006-07-23 05:07:58 +02:00
|
|
|
/*
|
|
|
|
* The shared buffer mapping table is partitioned to reduce contention.
|
|
|
|
* To determine which partition lock a given tag requires, compute the tag's
|
|
|
|
* hash code with BufTableHashCode(), then apply BufMappingPartitionLock().
|
|
|
|
* NB: NUM_BUFFER_PARTITIONS must be a power of 2!
|
|
|
|
*/
|
|
|
|
#define BufTableHashPartition(hashcode) \
|
|
|
|
((hashcode) % NUM_BUFFER_PARTITIONS)
|
|
|
|
#define BufMappingPartitionLock(hashcode) \
|
|
|
|
((LWLockId) (FirstBufMappingLock + BufTableHashPartition(hashcode)))
|
|
|
|
|
1996-08-28 03:59:28 +02:00
|
|
|
/*
|
2004-10-16 20:05:07 +02:00
|
|
|
* BufferDesc -- shared descriptor/state data for a single shared buffer.
|
2005-03-04 21:21:07 +01:00
|
|
|
*
|
|
|
|
* Note: buf_hdr_lock must be held to examine or change the tag, flags,
|
2005-05-19 23:35:48 +02:00
|
|
|
* usage_count, refcount, or wait_backend_pid fields. buf_id field never
|
2005-10-15 04:49:52 +02:00
|
|
|
* changes after initialization, so does not need locking. freeNext is
|
2005-03-04 21:21:07 +01:00
|
|
|
* protected by the BufFreelistLock not buf_hdr_lock. The LWLocks can take
|
2005-10-15 04:49:52 +02:00
|
|
|
* care of themselves. The buf_hdr_lock is *not* used to control access to
|
2005-03-04 21:21:07 +01:00
|
|
|
* the data in the buffer!
|
|
|
|
*
|
|
|
|
* An exception is that if we have the buffer pinned, its tag can't change
|
|
|
|
* underneath us, so we can examine the tag without locking the spinlock.
|
|
|
|
* Also, in places we do one-time reads of the flags without bothering to
|
|
|
|
* lock the spinlock; this is generally for situations where we don't expect
|
|
|
|
* the flag bit being tested to be changing.
|
|
|
|
*
|
|
|
|
* We can't physically remove items from a disk page if another backend has
|
|
|
|
* the buffer pinned. Hence, a backend may need to wait for all other pins
|
2005-10-15 04:49:52 +02:00
|
|
|
* to go away. This is signaled by storing its own PID into
|
2005-05-19 23:35:48 +02:00
|
|
|
* wait_backend_pid and setting flag bit BM_PIN_COUNT_WAITER. At present,
|
2005-03-04 21:21:07 +01:00
|
|
|
* there can be only one such waiter per buffer.
|
|
|
|
*
|
|
|
|
* We use this same struct for local buffer headers, but the lock fields
|
|
|
|
* are not used and not all of the flag bits are useful either.
|
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
|
|
|
{
|
2005-03-04 21:21:07 +01:00
|
|
|
BufferTag tag; /* ID of page contained in buffer */
|
1999-11-21 20:56:12 +01:00
|
|
|
BufFlags flags; /* see bit definitions above */
|
2005-03-04 21:21:07 +01:00
|
|
|
uint16 usage_count; /* usage counter for clock sweep code */
|
2001-07-06 23:04:26 +02:00
|
|
|
unsigned refcount; /* # of backends holding pins on buffer */
|
2005-10-15 04:49:52 +02:00
|
|
|
int wait_backend_pid; /* backend PID of pin-count waiter */
|
2005-03-04 21:21:07 +01:00
|
|
|
|
|
|
|
slock_t buf_hdr_lock; /* protects the above fields */
|
|
|
|
|
|
|
|
int buf_id; /* buffer's index number (from 0) */
|
|
|
|
int freeNext; /* link in freelist chain */
|
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 */
|
2005-03-04 21:21:07 +01:00
|
|
|
LWLockId content_lock; /* to lock access to buffer contents */
|
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)
|
|
|
|
|
2005-03-04 21:21:07 +01:00
|
|
|
/*
|
|
|
|
* The freeNext field is either the index of the next freelist entry,
|
|
|
|
* or one of these special values:
|
|
|
|
*/
|
|
|
|
#define FREENEXT_END_OF_LIST (-1)
|
|
|
|
#define FREENEXT_NOT_IN_LIST (-2)
|
2004-01-15 17:14:26 +01:00
|
|
|
|
2005-03-04 21:21:07 +01:00
|
|
|
/*
|
2005-12-29 19:08:05 +01:00
|
|
|
* Macros for acquiring/releasing a shared buffer header's spinlock.
|
|
|
|
* Do not apply these to local buffers!
|
2005-10-12 18:45:14 +02:00
|
|
|
*
|
|
|
|
* Note: as a general coding rule, if you are using these then you probably
|
2005-12-29 19:08:05 +01:00
|
|
|
* need to be using a volatile-qualified pointer to the buffer header, to
|
2005-10-12 18:45:14 +02:00
|
|
|
* ensure that the compiler doesn't rearrange accesses to the header to
|
|
|
|
* occur before or after the spinlock is acquired/released.
|
2005-03-04 21:21:07 +01:00
|
|
|
*/
|
2005-12-29 19:08:05 +01:00
|
|
|
#define LockBufHdr(bufHdr) SpinLockAcquire(&(bufHdr)->buf_hdr_lock)
|
|
|
|
#define UnlockBufHdr(bufHdr) SpinLockRelease(&(bufHdr)->buf_hdr_lock)
|
2005-03-04 21:21:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* in buf_init.c */
|
2007-07-25 14:22:54 +02:00
|
|
|
extern PGDLLIMPORT BufferDesc *BufferDescriptors;
|
2004-04-20 01:27:17 +02:00
|
|
|
|
2005-02-04 00:29:19 +01:00
|
|
|
/* in localbuf.c */
|
|
|
|
extern BufferDesc *LocalBufferDescriptors;
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2005-03-04 21:21:07 +01:00
|
|
|
/* event 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
|
|
|
/*
|
2005-02-04 00:29:19 +01: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 */
|
2007-05-30 22:12:03 +02:00
|
|
|
extern volatile BufferDesc *StrategyGetBuffer(BufferAccessStrategy strategy,
|
2007-11-15 22:14:46 +01:00
|
|
|
bool *lock_held);
|
2007-05-30 22:12:03 +02:00
|
|
|
extern void StrategyFreeBuffer(volatile BufferDesc *buf);
|
|
|
|
extern bool StrategyRejectBuffer(BufferAccessStrategy strategy,
|
2007-11-15 22:14:46 +01:00
|
|
|
volatile BufferDesc *buf);
|
2007-05-30 22:12:03 +02:00
|
|
|
|
2007-09-25 22:03:38 +02:00
|
|
|
extern int StrategySyncStart(uint32 *complete_passes, uint32 *num_buf_alloc);
|
2005-08-21 01:26:37 +02:00
|
|
|
extern Size StrategyShmemSize(void);
|
2003-11-13 15:57:15 +01:00
|
|
|
extern void StrategyInitialize(bool init);
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
/* buf_table.c */
|
2005-10-15 04:49:52 +02:00
|
|
|
extern Size BufTableShmemSize(int size);
|
2003-11-13 15:57:15 +01:00
|
|
|
extern void InitBufTable(int size);
|
2006-07-23 05:07:58 +02:00
|
|
|
extern uint32 BufTableHashCode(BufferTag *tagPtr);
|
|
|
|
extern int BufTableLookup(BufferTag *tagPtr, uint32 hashcode);
|
|
|
|
extern int BufTableInsert(BufferTag *tagPtr, uint32 hashcode, int buf_id);
|
|
|
|
extern void BufTableDelete(BufferTag *tagPtr, uint32 hashcode);
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
/* localbuf.c */
|
2008-06-12 11:12:31 +02:00
|
|
|
extern BufferDesc *LocalBufferAlloc(SMgrRelation reln, BlockNumber blockNum,
|
2000-04-12 19:17:23 +02:00
|
|
|
bool *foundPtr);
|
2006-04-01 01:32:07 +02:00
|
|
|
extern void MarkLocalBufferDirty(Buffer buffer);
|
2005-11-17 18:42:02 +01:00
|
|
|
extern void DropRelFileNodeLocalBuffers(RelFileNode rnode,
|
2005-11-22 19:17:34 +01:00
|
|
|
BlockNumber firstDelBlock);
|
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 */
|