1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* localbuf.c
|
2002-08-06 04:36:35 +02:00
|
|
|
* local buffer manager. Fast buffer manager for temporary tables,
|
|
|
|
* which never need to be WAL-logged or checkpointed, etc.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2002-06-20 22:29:54 +02:00
|
|
|
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994-5, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-11-30 20:06:37 +01:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2002-09-04 22:31:48 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/localbuf.c,v 1.46 2002/09/04 20:31:25 momjian Exp $
|
2000-11-30 20:06:37 +01:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2000-11-20 17:47:32 +01:00
|
|
|
#include "postgres.h"
|
2000-10-28 18:21:00 +02:00
|
|
|
|
2000-11-30 02:39:08 +01:00
|
|
|
#include "storage/buf_internals.h"
|
|
|
|
#include "storage/bufmgr.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "storage/smgr.h"
|
1996-11-08 07:02:30 +01:00
|
|
|
#include "utils/relcache.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-04-18 04:53:37 +02:00
|
|
|
|
2002-08-06 04:36:35 +02:00
|
|
|
/*#define LBDEBUG*/
|
|
|
|
|
|
|
|
/* should be a GUC parameter some day */
|
1997-09-08 04:41:22 +02:00
|
|
|
int NLocBuffer = 64;
|
2002-08-06 04:36:35 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
BufferDesc *LocalBufferDescriptors = NULL;
|
2000-11-30 02:39:08 +01:00
|
|
|
Block *LocalBufferBlockPointers = NULL;
|
1997-09-08 04:41:22 +02:00
|
|
|
long *LocalRefCount = NULL;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
static int nextFreeLocalBuf = 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* LocalBufferAlloc -
|
1997-09-07 07:04:48 +02:00
|
|
|
* allocate a local buffer. We do round robin allocation for now.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
BufferDesc *
|
1997-09-08 23:56:23 +02:00
|
|
|
LocalBufferAlloc(Relation reln, BlockNumber blockNum, bool *foundPtr)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
|
|
|
BufferDesc *bufHdr = (BufferDesc *) NULL;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* a low tech search for now -- not optimized for scans */
|
|
|
|
for (i = 0; i < NLocBuffer; i++)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
if (LocalBufferDescriptors[i].tag.rnode.relNode ==
|
2000-10-18 07:50:16 +02:00
|
|
|
reln->rd_node.relNode &&
|
1997-09-07 07:04:48 +02:00
|
|
|
LocalBufferDescriptors[i].tag.blockNum == blockNum)
|
|
|
|
{
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef LBDEBUG
|
1999-05-10 02:46:32 +02:00
|
|
|
fprintf(stderr, "LB ALLOC (%u,%d) %d\n",
|
1998-08-19 04:04:17 +02:00
|
|
|
RelationGetRelid(reln), blockNum, -i - 1);
|
1997-09-07 07:04:48 +02:00
|
|
|
#endif
|
2002-08-06 04:36:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
LocalRefCount[i]++;
|
|
|
|
*foundPtr = TRUE;
|
|
|
|
return &LocalBufferDescriptors[i];
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef LBDEBUG
|
1999-05-10 02:46:32 +02:00
|
|
|
fprintf(stderr, "LB ALLOC (%u,%d) %d\n",
|
1998-08-19 04:04:17 +02:00
|
|
|
RelationGetRelid(reln), blockNum, -nextFreeLocalBuf - 1);
|
1997-09-07 07:04:48 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* need to get a new buffer (round robin for now) */
|
|
|
|
for (i = 0; i < NLocBuffer; i++)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int b = (nextFreeLocalBuf + i) % NLocBuffer;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (LocalRefCount[b] == 0)
|
|
|
|
{
|
|
|
|
bufHdr = &LocalBufferDescriptors[b];
|
|
|
|
LocalRefCount[b]++;
|
|
|
|
nextFreeLocalBuf = (b + 1) % NLocBuffer;
|
|
|
|
break;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
if (bufHdr == NULL)
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "no empty local buffer.");
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
2002-09-04 22:31:48 +02:00
|
|
|
* this buffer is not referenced but it might still be dirty. if
|
|
|
|
* that's the case, write it out before reusing it!
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2000-11-30 20:03:26 +01:00
|
|
|
if (bufHdr->flags & BM_DIRTY || bufHdr->cntxDirty)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-10-23 06:10:24 +02:00
|
|
|
Relation bufrel = RelationNodeCacheGetRelation(bufHdr->tag.rnode);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-08-06 04:36:35 +02:00
|
|
|
/*
|
|
|
|
* The relcache is not supposed to throw away temp rels, so this
|
|
|
|
* should always succeed.
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
Assert(bufrel != NULL);
|
|
|
|
|
|
|
|
/* flush this page */
|
1997-11-21 19:12:58 +01:00
|
|
|
smgrwrite(DEFAULT_SMGR, bufrel, bufHdr->tag.blockNum,
|
1997-09-07 07:04:48 +02:00
|
|
|
(char *) MAKE_PTR(bufHdr->data));
|
|
|
|
LocalBufferFlushCount++;
|
1999-09-18 21:08:25 +02:00
|
|
|
|
2002-05-03 19:42:11 +02:00
|
|
|
/* drop refcount incremented by RelationNodeCacheGetRelation */
|
1997-10-12 09:12:03 +02:00
|
|
|
RelationDecrementReferenceCount(bufrel);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-11-30 02:39:08 +01:00
|
|
|
* lazy memory allocation: allocate space on first use of a buffer.
|
2002-08-06 04:36:35 +02:00
|
|
|
*
|
2002-09-04 22:31:48 +02:00
|
|
|
* Note this path cannot be taken for a buffer that was previously in
|
|
|
|
* use, so it's okay to do it (and possibly error out) before marking
|
|
|
|
* the buffer as valid.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
if (bufHdr->data == (SHMEM_OFFSET) 0)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
char *data = (char *) malloc(BLCKSZ);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-11-30 02:39:08 +01:00
|
|
|
if (data == NULL)
|
2002-08-06 04:36:35 +02:00
|
|
|
elog(ERROR, "Out of memory in LocalBufferAlloc");
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-11-30 02:39:08 +01:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* This is a bit of a hack: bufHdr->data needs to be a shmem
|
|
|
|
* offset for consistency with the shared-buffer case, so make it
|
|
|
|
* one even though it's not really a valid shmem offset.
|
2000-11-30 02:39:08 +01:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
bufHdr->data = MAKE_OFFSET(data);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-11-30 02:39:08 +01:00
|
|
|
/*
|
|
|
|
* Set pointer for use by BufferGetBlock() macro.
|
|
|
|
*/
|
|
|
|
LocalBufferBlockPointers[-(bufHdr->buf_id + 2)] = (Block) data;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2002-08-06 04:36:35 +02:00
|
|
|
/*
|
|
|
|
* it's all ours now.
|
|
|
|
*
|
|
|
|
* We need not in tblNode currently but will in future I think, when
|
|
|
|
* we'll give up rel->rd_fd to fmgr cache.
|
|
|
|
*/
|
|
|
|
bufHdr->tag.rnode = reln->rd_node;
|
|
|
|
bufHdr->tag.blockNum = blockNum;
|
|
|
|
bufHdr->flags &= ~BM_DIRTY;
|
|
|
|
bufHdr->cntxDirty = false;
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
*foundPtr = FALSE;
|
|
|
|
return bufHdr;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* WriteLocalBuffer -
|
2002-08-06 04:36:35 +02:00
|
|
|
* writes out a local buffer (actually, just marks it dirty)
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2002-06-15 21:59:59 +02:00
|
|
|
void
|
1996-07-09 08:22:35 +02:00
|
|
|
WriteLocalBuffer(Buffer buffer, bool release)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int bufid;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
Assert(BufferIsLocal(buffer));
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
#ifdef LBDEBUG
|
1997-09-07 07:04:48 +02:00
|
|
|
fprintf(stderr, "LB WRITE %d\n", buffer);
|
|
|
|
#endif
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
bufid = -(buffer + 1);
|
|
|
|
LocalBufferDescriptors[bufid].flags |= BM_DIRTY;
|
|
|
|
|
|
|
|
if (release)
|
|
|
|
{
|
|
|
|
Assert(LocalRefCount[bufid] > 0);
|
|
|
|
LocalRefCount[bufid]--;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* InitLocalBuffer -
|
1997-09-07 07:04:48 +02:00
|
|
|
* init the local buffer cache. Since most queries (esp. multi-user ones)
|
2000-11-30 02:39:08 +01:00
|
|
|
* don't involve local buffers, we delay allocating actual memory for the
|
2002-08-06 04:36:35 +02:00
|
|
|
* buffers until we need them; just make the buffer headers here.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
void
|
1996-11-10 04:06:38 +01:00
|
|
|
InitLocalBuffer(void)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* these aren't going away. I'm not gonna use palloc.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
LocalBufferDescriptors =
|
2000-11-30 02:39:08 +01:00
|
|
|
(BufferDesc *) calloc(NLocBuffer, sizeof(BufferDesc));
|
|
|
|
LocalBufferBlockPointers = (Block *) calloc(NLocBuffer, sizeof(Block));
|
|
|
|
LocalRefCount = (long *) calloc(NLocBuffer, sizeof(long));
|
1997-09-07 07:04:48 +02:00
|
|
|
nextFreeLocalBuf = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < NLocBuffer; i++)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
BufferDesc *buf = &LocalBufferDescriptors[i];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* negative to indicate local buffer. This is tricky: shared
|
|
|
|
* buffers start with 0. We have to start with -2. (Note that the
|
|
|
|
* routine BufferDescriptorGetBuffer adds 1 to buf_id so our first
|
|
|
|
* buffer id is -1.)
|
|
|
|
*/
|
|
|
|
buf->buf_id = -i - 2;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-08-06 04:36:35 +02:00
|
|
|
* AtEOXact_LocalBuffers - clean up at end of transaction.
|
2000-11-30 20:03:26 +01:00
|
|
|
*
|
2002-08-06 04:36:35 +02:00
|
|
|
* This is just like AtEOXact_Buffers, but for local buffers.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
void
|
2002-08-06 04:36:35 +02:00
|
|
|
AtEOXact_LocalBuffers(bool isCommit)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
for (i = 0; i < NLocBuffer; i++)
|
|
|
|
{
|
2002-08-06 04:36:35 +02:00
|
|
|
if (LocalRefCount[i] != 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2002-08-06 04:36:35 +02:00
|
|
|
BufferDesc *buf = &(LocalBufferDescriptors[i]);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-08-06 04:36:35 +02:00
|
|
|
if (isCommit)
|
|
|
|
elog(WARNING,
|
|
|
|
"Local Buffer Leak: [%03d] (rel=%u/%u, blockNum=%u, flags=0x%x, refcount=%d %ld)",
|
|
|
|
i,
|
|
|
|
buf->tag.rnode.tblNode, buf->tag.rnode.relNode,
|
|
|
|
buf->tag.blockNum, buf->flags,
|
|
|
|
buf->refcount, LocalRefCount[i]);
|
1999-09-18 21:08:25 +02:00
|
|
|
|
2002-08-06 04:36:35 +02:00
|
|
|
LocalRefCount[i] = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|