1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* localbuf.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* local buffer manager. Fast buffer manager for temporary tables
|
|
|
|
* or special cases when the operation is not visible to other backends.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* When a relation is being created, the descriptor will have rd_islocal
|
|
|
|
* set to indicate that the local buffer manager should be used. During
|
|
|
|
* the same transaction the relation is being created, any inserts or
|
|
|
|
* selects from the newly created relation will use the local buffer
|
|
|
|
* pool. rd_islocal is reset at the end of a transaction (commit/abort).
|
|
|
|
* This is useful for queries like SELECT INTO TABLE and create index.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
|
|
|
|
* Portions Copyright (c) 1994-5, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2000-11-20 17:47:32 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/localbuf.c,v 1.35 2000/11/20 16:47:32 petere Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2000-10-28 18:21:00 +02:00
|
|
|
|
2000-11-20 17:47:32 +01:00
|
|
|
#include "postgres.h"
|
2000-10-28 18:21:00 +02:00
|
|
|
|
2000-11-20 17:47:32 +01:00
|
|
|
#ifdef XLOG
|
2000-10-28 18:21:00 +02:00
|
|
|
#include "xlog_localbuf.c"
|
|
|
|
#else
|
|
|
|
|
1997-07-28 02:57:08 +02:00
|
|
|
#include <sys/types.h>
|
1996-07-09 08:22:35 +02:00
|
|
|
#include <sys/file.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "executor/execdebug.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
|
|
|
extern long int LocalBufferFlushCount;
|
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
int NLocBuffer = 64;
|
|
|
|
BufferDesc *LocalBufferDescriptors = NULL;
|
|
|
|
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
|
|
|
|
|
|
|
/*#define LBDEBUG*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
if (blockNum == P_NEW)
|
|
|
|
{
|
|
|
|
blockNum = reln->rd_nblocks;
|
|
|
|
reln->rd_nblocks++;
|
|
|
|
}
|
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++)
|
|
|
|
{
|
2000-10-18 07:50:16 +02:00
|
|
|
if (LocalBufferDescriptors[i].tag.rnode.relNode ==
|
|
|
|
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
|
|
|
|
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
|
|
|
/*
|
|
|
|
* this buffer is not referenced but it might still be dirty (the last
|
|
|
|
* transaction to touch it doesn't need its contents but has not
|
|
|
|
* flushed it). if that's the case, write it out before reusing it!
|
|
|
|
*/
|
|
|
|
if (bufHdr->flags & BM_DIRTY)
|
|
|
|
{
|
2000-10-23 06:10:24 +02:00
|
|
|
Relation bufrel = RelationNodeCacheGetRelation(bufHdr->tag.rnode);
|
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
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
/*
|
|
|
|
* drop relcache refcount incremented by
|
|
|
|
* RelationIdCacheGetRelation
|
|
|
|
*/
|
1997-10-12 09:12:03 +02:00
|
|
|
RelationDecrementReferenceCount(bufrel);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* it's all ours now.
|
2000-10-18 07:50:16 +02:00
|
|
|
*
|
|
|
|
* We need not in tblNode currently but will in future I think,
|
|
|
|
* when we'll give up rel->rd_fd to fmgr cache.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2000-10-18 07:50:16 +02:00
|
|
|
bufHdr->tag.rnode = reln->rd_node;
|
1997-09-07 07:04:48 +02:00
|
|
|
bufHdr->tag.blockNum = blockNum;
|
|
|
|
bufHdr->flags &= ~BM_DIRTY;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lazy memory allocation. (see MAKE_PTR for why we need to do
|
|
|
|
* MAKE_OFFSET.)
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
|
|
|
bufHdr->data = MAKE_OFFSET(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
*foundPtr = FALSE;
|
|
|
|
return bufHdr;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* WriteLocalBuffer -
|
1997-09-07 07:04:48 +02:00
|
|
|
* writes out a local buffer
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
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
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return true;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FlushLocalBuffer -
|
1997-09-07 07:04:48 +02:00
|
|
|
* flushes a local buffer
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
int
|
2000-11-08 23:10:03 +01:00
|
|
|
FlushLocalBuffer(Buffer buffer, bool sync, bool release)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int bufid;
|
|
|
|
Relation bufrel;
|
|
|
|
BufferDesc *bufHdr;
|
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 FLUSH %d\n", buffer);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bufid = -(buffer + 1);
|
|
|
|
bufHdr = &LocalBufferDescriptors[bufid];
|
|
|
|
bufHdr->flags &= ~BM_DIRTY;
|
2000-10-23 06:10:24 +02:00
|
|
|
bufrel = RelationNodeCacheGetRelation(bufHdr->tag.rnode);
|
1997-09-07 07:04:48 +02:00
|
|
|
Assert(bufrel != NULL);
|
2000-11-08 23:10:03 +01:00
|
|
|
|
|
|
|
if (sync)
|
|
|
|
smgrflush(DEFAULT_SMGR, bufrel, bufHdr->tag.blockNum,
|
|
|
|
(char *) MAKE_PTR(bufHdr->data));
|
|
|
|
else
|
|
|
|
smgrwrite(DEFAULT_SMGR, bufrel, bufHdr->tag.blockNum,
|
|
|
|
(char *) MAKE_PTR(bufHdr->data));
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
LocalBufferFlushCount++;
|
1999-09-18 21:08:25 +02:00
|
|
|
|
2000-11-08 23:10:03 +01:00
|
|
|
/* drop relcache refcount incremented by RelationNodeCacheGetRelation */
|
1997-10-12 09:12:03 +02:00
|
|
|
RelationDecrementReferenceCount(bufrel);
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (release)
|
1999-09-24 02:25:33 +02:00
|
|
|
{
|
|
|
|
Assert(LocalRefCount[bufid] > 0);
|
1997-09-07 07:04:48 +02:00
|
|
|
LocalRefCount[bufid]--;
|
1999-09-24 02:25:33 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
return true;
|
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)
|
|
|
|
* don't involve local buffers, we delay allocating memory for actual the
|
|
|
|
* buffer until we need it.
|
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 =
|
|
|
|
(BufferDesc *) malloc(sizeof(BufferDesc) * NLocBuffer);
|
1997-09-18 22:22:58 +02:00
|
|
|
MemSet(LocalBufferDescriptors, 0, sizeof(BufferDesc) * NLocBuffer);
|
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
|
|
|
|
1999-02-03 22:18:02 +01:00
|
|
|
LocalRefCount = (long *) malloc(sizeof(long) * NLocBuffer);
|
1997-09-18 22:22:58 +02:00
|
|
|
MemSet(LocalRefCount, 0, sizeof(long) * NLocBuffer);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-10-28 18:21:00 +02:00
|
|
|
* LocalBufferSync
|
|
|
|
*
|
|
|
|
* Flush all dirty buffers in the local buffer cache at commit time.
|
|
|
|
* Since the buffer cache is only used for keeping relations visible
|
|
|
|
* during a transaction, we will not need these buffers again.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
void
|
1996-11-10 04:06:38 +01:00
|
|
|
LocalBufferSync(void)
|
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++)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
BufferDesc *buf = &LocalBufferDescriptors[i];
|
|
|
|
Relation bufrel;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (buf->flags & BM_DIRTY)
|
|
|
|
{
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef LBDEBUG
|
1997-09-07 07:04:48 +02:00
|
|
|
fprintf(stderr, "LB SYNC %d\n", -i - 1);
|
|
|
|
#endif
|
2000-10-23 06:10:24 +02:00
|
|
|
bufrel = RelationNodeCacheGetRelation(buf->tag.rnode);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
Assert(bufrel != NULL);
|
|
|
|
|
1997-11-21 19:12:58 +01:00
|
|
|
smgrwrite(DEFAULT_SMGR, bufrel, buf->tag.blockNum,
|
1997-09-07 07:04:48 +02:00
|
|
|
(char *) MAKE_PTR(buf->data));
|
|
|
|
LocalBufferFlushCount++;
|
1999-09-18 21:08:25 +02:00
|
|
|
|
|
|
|
/* drop relcache refcount from RelationIdCacheGetRelation */
|
1997-10-12 09:12:03 +02:00
|
|
|
RelationDecrementReferenceCount(bufrel);
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
buf->flags &= ~BM_DIRTY;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-18 22:22:58 +02:00
|
|
|
MemSet(LocalRefCount, 0, sizeof(long) * NLocBuffer);
|
1997-09-07 07:04:48 +02:00
|
|
|
nextFreeLocalBuf = 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1996-11-10 04:06:38 +01:00
|
|
|
ResetLocalBufferPool(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
|
|
|
for (i = 0; i < NLocBuffer; i++)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
BufferDesc *buf = &LocalBufferDescriptors[i];
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-10-18 07:50:16 +02:00
|
|
|
buf->tag.rnode.relNode = InvalidOid;
|
1997-09-07 07:04:48 +02:00
|
|
|
buf->flags &= ~BM_DIRTY;
|
|
|
|
buf->buf_id = -i - 2;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-18 22:22:58 +02:00
|
|
|
MemSet(LocalRefCount, 0, sizeof(long) * NLocBuffer);
|
1997-09-07 07:04:48 +02:00
|
|
|
nextFreeLocalBuf = 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2000-10-28 18:21:00 +02:00
|
|
|
|
|
|
|
#endif /* XLOG */
|