Commit af5ced9c authored by Tom Lane's avatar Tom Lane

Further work on connecting the free space map (which is still just a

stub) into the rest of the system.  Adopt a cleaner approach to preventing
deadlock in concurrent heap_updates: allow RelationGetBufferForTuple to
select any page of the rel, and put the onus on it to lock both buffers
in a consistent order.  Remove no-longer-needed isExtend hack from
API of ReleaseAndReadBuffer.
parent 0eab92c0
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/access/heap/heapam.c,v 1.120 2001/06/27 23:31:38 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/access/heap/heapam.c,v 1.121 2001/06/29 21:08:23 tgl Exp $
* *
* *
* INTERFACE ROUTINES * INTERFACE ROUTINES
...@@ -202,8 +202,7 @@ heapgettup(Relation relation, ...@@ -202,8 +202,7 @@ heapgettup(Relation relation,
*buffer = ReleaseAndReadBuffer(*buffer, *buffer = ReleaseAndReadBuffer(*buffer,
relation, relation,
ItemPointerGetBlockNumber(tid), ItemPointerGetBlockNumber(tid));
false);
if (!BufferIsValid(*buffer)) if (!BufferIsValid(*buffer))
elog(ERROR, "heapgettup: failed ReadBuffer"); elog(ERROR, "heapgettup: failed ReadBuffer");
...@@ -238,8 +237,7 @@ heapgettup(Relation relation, ...@@ -238,8 +237,7 @@ heapgettup(Relation relation,
*buffer = ReleaseAndReadBuffer(*buffer, *buffer = ReleaseAndReadBuffer(*buffer,
relation, relation,
page, page);
false);
if (!BufferIsValid(*buffer)) if (!BufferIsValid(*buffer))
elog(ERROR, "heapgettup: failed ReadBuffer"); elog(ERROR, "heapgettup: failed ReadBuffer");
...@@ -280,8 +278,7 @@ heapgettup(Relation relation, ...@@ -280,8 +278,7 @@ heapgettup(Relation relation,
*buffer = ReleaseAndReadBuffer(*buffer, *buffer = ReleaseAndReadBuffer(*buffer,
relation, relation,
page, page);
false);
if (!BufferIsValid(*buffer)) if (!BufferIsValid(*buffer))
elog(ERROR, "heapgettup: failed ReadBuffer"); elog(ERROR, "heapgettup: failed ReadBuffer");
...@@ -374,8 +371,7 @@ heapgettup(Relation relation, ...@@ -374,8 +371,7 @@ heapgettup(Relation relation,
*buffer = ReleaseAndReadBuffer(*buffer, *buffer = ReleaseAndReadBuffer(*buffer,
relation, relation,
page, page);
false);
if (!BufferIsValid(*buffer)) if (!BufferIsValid(*buffer))
elog(ERROR, "heapgettup: failed ReadBuffer"); elog(ERROR, "heapgettup: failed ReadBuffer");
...@@ -1088,8 +1084,8 @@ heap_insert(Relation relation, HeapTuple tup) ...@@ -1088,8 +1084,8 @@ heap_insert(Relation relation, HeapTuple tup)
heap_tuple_toast_attrs(relation, tup, NULL); heap_tuple_toast_attrs(relation, tup, NULL);
#endif #endif
/* Find buffer for this tuple */ /* Find buffer to insert this tuple into */
buffer = RelationGetBufferForTuple(relation, tup->t_len, 0); buffer = RelationGetBufferForTuple(relation, tup->t_len, InvalidBuffer);
/* NO ELOG(ERROR) from here till changes are logged */ /* NO ELOG(ERROR) from here till changes are logged */
START_CRIT_SECTION(); START_CRIT_SECTION();
...@@ -1501,18 +1497,16 @@ l2: ...@@ -1501,18 +1497,16 @@ l2:
* buffer locks on both old and new pages. To avoid deadlock against * buffer locks on both old and new pages. To avoid deadlock against
* some other backend trying to get the same two locks in the other * some other backend trying to get the same two locks in the other
* order, we must be consistent about the order we get the locks in. * order, we must be consistent about the order we get the locks in.
* We use the rule "lock the higher-numbered page of the relation * We use the rule "lock the lower-numbered page of the relation
* first". To implement this, we must do RelationGetBufferForTuple * first". To implement this, we must do RelationGetBufferForTuple
* while not holding the lock on the old page, and we must tell it * while not holding the lock on the old page, and we must rely on it
* to give us a page beyond the old page. * to get the locks on both pages in the correct order.
*/ */
if (newtupsize > pagefree) if (newtupsize > pagefree)
{ {
/* Assume there's no chance to put newtup on same page. */ /* Assume there's no chance to put newtup on same page. */
newbuf = RelationGetBufferForTuple(relation, newtup->t_len, newbuf = RelationGetBufferForTuple(relation, newtup->t_len,
BufferGetBlockNumber(buffer) + 1); buffer);
/* Now reacquire lock on old tuple's page. */
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
} }
else else
{ {
...@@ -1529,8 +1523,7 @@ l2: ...@@ -1529,8 +1523,7 @@ l2:
*/ */
LockBuffer(buffer, BUFFER_LOCK_UNLOCK); LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
newbuf = RelationGetBufferForTuple(relation, newtup->t_len, newbuf = RelationGetBufferForTuple(relation, newtup->t_len,
BufferGetBlockNumber(buffer) + 1); buffer);
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
} }
else else
{ {
...@@ -1550,7 +1543,8 @@ l2: ...@@ -1550,7 +1543,8 @@ l2:
/* /*
* At this point newbuf and buffer are both pinned and locked, * At this point newbuf and buffer are both pinned and locked,
* and newbuf has enough space for the new tuple. * and newbuf has enough space for the new tuple. If they are
* the same buffer, only one pin is held.
*/ */
/* NO ELOG(ERROR) from here till changes are logged */ /* NO ELOG(ERROR) from here till changes are logged */
......
This diff is collapsed.
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Header: /cvsroot/pgsql/src/backend/access/transam/xlogutils.c,v 1.15 2001/03/22 03:59:18 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/access/transam/xlogutils.c,v 1.16 2001/06/29 21:08:24 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -171,6 +171,7 @@ XLogOpenLogRelation(void) ...@@ -171,6 +171,7 @@ XLogOpenLogRelation(void)
sprintf(RelationGetPhysicalRelationName(logRelation), "pg_log"); sprintf(RelationGetPhysicalRelationName(logRelation), "pg_log");
logRelation->rd_node.tblNode = InvalidOid; logRelation->rd_node.tblNode = InvalidOid;
logRelation->rd_node.relNode = RelOid_pg_log; logRelation->rd_node.relNode = RelOid_pg_log;
logRelation->rd_targblock = InvalidBlockNumber;
logRelation->rd_fd = -1; logRelation->rd_fd = -1;
logRelation->rd_fd = smgropen(DEFAULT_SMGR, logRelation, false); logRelation->rd_fd = smgropen(DEFAULT_SMGR, logRelation, false);
if (logRelation->rd_fd < 0) if (logRelation->rd_fd < 0)
...@@ -384,6 +385,7 @@ XLogOpenRelation(bool redo, RmgrId rmid, RelFileNode rnode) ...@@ -384,6 +385,7 @@ XLogOpenRelation(bool redo, RmgrId rmid, RelFileNode rnode)
hentry->rdesc = res; hentry->rdesc = res;
res->reldata.rd_targblock = InvalidBlockNumber;
res->reldata.rd_fd = -1; res->reldata.rd_fd = -1;
res->reldata.rd_fd = smgropen(DEFAULT_SMGR, &(res->reldata), res->reldata.rd_fd = smgropen(DEFAULT_SMGR, &(res->reldata),
true /* allow failure */ ); true /* allow failure */ );
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.169 2001/06/27 23:31:38 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.170 2001/06/29 21:08:24 tgl Exp $
* *
* *
* INTERFACE ROUTINES * INTERFACE ROUTINES
...@@ -39,7 +39,6 @@ ...@@ -39,7 +39,6 @@
#include "catalog/pg_attrdef.h" #include "catalog/pg_attrdef.h"
#include "catalog/pg_inherits.h" #include "catalog/pg_inherits.h"
#include "catalog/pg_index.h" #include "catalog/pg_index.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_relcheck.h" #include "catalog/pg_relcheck.h"
#include "catalog/pg_statistic.h" #include "catalog/pg_statistic.h"
#include "catalog/pg_type.h" #include "catalog/pg_type.h"
...@@ -50,8 +49,6 @@ ...@@ -50,8 +49,6 @@
#include "optimizer/planmain.h" #include "optimizer/planmain.h"
#include "optimizer/prep.h" #include "optimizer/prep.h"
#include "optimizer/var.h" #include "optimizer/var.h"
#include "nodes/makefuncs.h"
#include "parser/parse_clause.h"
#include "parser/parse_expr.h" #include "parser/parse_expr.h"
#include "parser/parse_relation.h" #include "parser/parse_relation.h"
#include "parser/parse_target.h" #include "parser/parse_target.h"
...@@ -59,7 +56,6 @@ ...@@ -59,7 +56,6 @@
#include "rewrite/rewriteRemove.h" #include "rewrite/rewriteRemove.h"
#include "storage/smgr.h" #include "storage/smgr.h"
#include "utils/builtins.h" #include "utils/builtins.h"
#include "utils/catcache.h"
#include "utils/fmgroids.h" #include "utils/fmgroids.h"
#include "utils/lsyscache.h" #include "utils/lsyscache.h"
#include "utils/relcache.h" #include "utils/relcache.h"
...@@ -197,25 +193,18 @@ heap_create(char *relname, ...@@ -197,25 +193,18 @@ heap_create(char *relname,
static unsigned int uniqueId = 0; static unsigned int uniqueId = 0;
Oid relid; Oid relid;
Relation rel;
bool nailme = false;
int natts = tupDesc->natts;
int i;
MemoryContext oldcxt;
Oid tblNode = MyDatabaseId; Oid tblNode = MyDatabaseId;
bool nailme = false;
Relation rel;
/* /*
* sanity checks * sanity checks
*/ */
AssertArg(natts > 0);
if (relname && !allow_system_table_mods && if (relname && !allow_system_table_mods &&
IsSystemRelationName(relname) && IsNormalProcessingMode()) IsSystemRelationName(relname) && IsNormalProcessingMode())
{
elog(ERROR, "Illegal class name '%s'" elog(ERROR, "Illegal class name '%s'"
"\n\tThe 'pg_' name prefix is reserved for system catalogs", "\n\tThe 'pg_' name prefix is reserved for system catalogs",
relname); relname);
}
/* /*
* Real ugly stuff to assign the proper relid in the relation * Real ugly stuff to assign the proper relid in the relation
...@@ -276,7 +265,6 @@ heap_create(char *relname, ...@@ -276,7 +265,6 @@ heap_create(char *relname,
if (istemp) if (istemp)
{ {
/* /*
* replace relname of caller with a unique name for a temp * replace relname of caller with a unique name for a temp
* relation * relation
...@@ -286,60 +274,11 @@ heap_create(char *relname, ...@@ -286,60 +274,11 @@ heap_create(char *relname,
} }
/* /*
* switch to the cache context to create the relcache entry. * build the relcache entry.
*/
if (!CacheMemoryContext)
CreateCacheMemoryContext();
oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
/*
* allocate a new relation descriptor.
*/ */
rel = (Relation) palloc(sizeof(RelationData)); rel = RelationBuildLocalRelation(relname, tupDesc,
MemSet((char *) rel, 0, sizeof(RelationData)); relid, tblNode,
rel->rd_fd = -1; /* physical file is not open */ nailme);
RelationSetReferenceCount(rel, 1);
/*
* create a new tuple descriptor from the one passed in
*/
rel->rd_att = CreateTupleDescCopyConstr(tupDesc);
/*
* nail the reldesc if this is a bootstrap create reln and we may need
* it in the cache later on in the bootstrap process so we don't ever
* want it kicked out. e.g. pg_attribute!!!
*/
if (nailme)
rel->rd_isnailed = true;
/*
* initialize the fields of our new relation descriptor
*/
rel->rd_rel = (Form_pg_class) palloc(sizeof *rel->rd_rel);
MemSet((char *) rel->rd_rel, 0, sizeof *rel->rd_rel);
strcpy(RelationGetPhysicalRelationName(rel), relname);
rel->rd_rel->relkind = RELKIND_UNCATALOGED;
rel->rd_rel->relnatts = natts;
rel->rd_rel->reltype = InvalidOid;
if (tupDesc->constr)
rel->rd_rel->relchecks = tupDesc->constr->num_check;
for (i = 0; i < natts; i++)
rel->rd_att->attrs[i]->attrelid = relid;
RelationGetRelid(rel) = relid;
rel->rd_node.tblNode = tblNode;
rel->rd_node.relNode = relid;
rel->rd_rel->relfilenode = relid;
/*
* done building relcache entry.
*/
MemoryContextSwitchTo(oldcxt);
/* /*
* have the storage manager create the relation. * have the storage manager create the relation.
...@@ -347,8 +286,6 @@ heap_create(char *relname, ...@@ -347,8 +286,6 @@ heap_create(char *relname,
if (storage_create) if (storage_create)
heap_storage_create(rel); heap_storage_create(rel);
RelationRegisterRelation(rel);
return rel; return rel;
} }
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/sequence.c,v 1.60 2001/06/23 00:07:34 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/commands/sequence.c,v 1.61 2001/06/29 21:08:24 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -168,21 +168,26 @@ DefineSequence(CreateSeqStmt *seq) ...@@ -168,21 +168,26 @@ DefineSequence(CreateSeqStmt *seq)
DefineRelation(stmt, RELKIND_SEQUENCE); DefineRelation(stmt, RELKIND_SEQUENCE);
rel = heap_openr(seq->seqname, AccessExclusiveLock); rel = heap_openr(seq->seqname, AccessExclusiveLock);
tupDesc = RelationGetDescr(rel); tupDesc = RelationGetDescr(rel);
Assert(RelationGetNumberOfBlocks(rel) == 0); /* Initialize first page of relation with special magic number */
buf = ReadBuffer(rel, P_NEW); buf = ReadBuffer(rel, P_NEW);
if (!BufferIsValid(buf)) if (!BufferIsValid(buf))
elog(ERROR, "DefineSequence: ReadBuffer failed"); elog(ERROR, "DefineSequence: ReadBuffer failed");
Assert(BufferGetBlockNumber(buf) == 0);
page = (PageHeader) BufferGetPage(buf); page = (PageHeader) BufferGetPage(buf);
PageInit((Page) page, BufferGetPageSize(buf), sizeof(sequence_magic)); PageInit((Page) page, BufferGetPageSize(buf), sizeof(sequence_magic));
sm = (sequence_magic *) PageGetSpecialPointer(page); sm = (sequence_magic *) PageGetSpecialPointer(page);
sm->magic = SEQ_MAGIC; sm->magic = SEQ_MAGIC;
/* hack: ensure heap_insert will insert on the just-created page */
rel->rd_targblock = 0;
/* Now - form & insert sequence tuple */ /* Now - form & insert sequence tuple */
tuple = heap_formtuple(tupDesc, value, null); tuple = heap_formtuple(tupDesc, value, null);
heap_insert(rel, tuple); heap_insert(rel, tuple);
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/bufmgr.c,v 1.113 2001/06/22 19:16:22 wieck Exp $ * $Header: /cvsroot/pgsql/src/backend/storage/buffer/bufmgr.c,v 1.114 2001/06/29 21:08:24 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -91,7 +91,7 @@ extern void AbortBufferIO(void); ...@@ -91,7 +91,7 @@ extern void AbortBufferIO(void);
#define BUFFER_IS_BROKEN(buf) ((buf->flags & BM_IO_ERROR) && !(buf->flags & BM_DIRTY)) #define BUFFER_IS_BROKEN(buf) ((buf->flags & BM_IO_ERROR) && !(buf->flags & BM_DIRTY))
static Buffer ReadBufferInternal(Relation reln, BlockNumber blockNum, static Buffer ReadBufferInternal(Relation reln, BlockNumber blockNum,
bool isExtend, bool bufferLockHeld); bool bufferLockHeld);
static BufferDesc *BufferAlloc(Relation reln, BlockNumber blockNum, static BufferDesc *BufferAlloc(Relation reln, BlockNumber blockNum,
bool *foundPtr); bool *foundPtr);
static int ReleaseBufferWithBufferLock(Buffer buffer); static int ReleaseBufferWithBufferLock(Buffer buffer);
...@@ -103,7 +103,9 @@ void PrintBufferDescs(void); ...@@ -103,7 +103,9 @@ void PrintBufferDescs(void);
* ReadBuffer -- returns a buffer containing the requested * ReadBuffer -- returns a buffer containing the requested
* block of the requested relation. If the blknum * block of the requested relation. If the blknum
* requested is P_NEW, extend the relation file and * requested is P_NEW, extend the relation file and
* allocate a new block. * allocate a new block. (Caller is responsible for
* ensuring that only one backend tries to extend a
* relation at the same time!)
* *
* Returns: the buffer number for the buffer containing * Returns: the buffer number for the buffer containing
* the block read, or NULL on an error. If successful, * the block read, or NULL on an error. If successful,
...@@ -111,6 +113,8 @@ void PrintBufferDescs(void); ...@@ -111,6 +113,8 @@ void PrintBufferDescs(void);
* *
* Assume when this function is called, that reln has been * Assume when this function is called, that reln has been
* opened already. * opened already.
*
* Note: a side effect of a P_NEW call is to update reln->rd_nblocks.
*/ */
#undef ReadBuffer /* conflicts with macro when BUFMGR_DEBUG #undef ReadBuffer /* conflicts with macro when BUFMGR_DEBUG
...@@ -122,28 +126,26 @@ void PrintBufferDescs(void); ...@@ -122,28 +126,26 @@ void PrintBufferDescs(void);
Buffer Buffer
ReadBuffer(Relation reln, BlockNumber blockNum) ReadBuffer(Relation reln, BlockNumber blockNum)
{ {
return ReadBufferInternal(reln, blockNum, false, false); return ReadBufferInternal(reln, blockNum, false);
} }
/* /*
* ReadBufferInternal -- internal version of ReadBuffer with more options * ReadBufferInternal -- internal version of ReadBuffer with more options
* *
* isExtend: if true, assume that we are extending the file and the caller
* is passing the current EOF block number (ie, caller already called
* smgrnblocks()).
*
* bufferLockHeld: if true, caller already acquired the bufmgr spinlock. * bufferLockHeld: if true, caller already acquired the bufmgr spinlock.
* (This is assumed never to be true if dealing with a local buffer!) * (This is assumed never to be true if dealing with a local buffer!)
*/ */
static Buffer static Buffer
ReadBufferInternal(Relation reln, BlockNumber blockNum, ReadBufferInternal(Relation reln, BlockNumber blockNum,
bool isExtend, bool bufferLockHeld) bool bufferLockHeld)
{ {
BufferDesc *bufHdr; BufferDesc *bufHdr;
int status; int status;
bool found; bool found;
bool isExtend;
bool isLocalBuf; bool isLocalBuf;
isExtend = (blockNum == P_NEW);
isLocalBuf = reln->rd_myxactonly; isLocalBuf = reln->rd_myxactonly;
if (isLocalBuf) if (isLocalBuf)
...@@ -151,11 +153,10 @@ ReadBufferInternal(Relation reln, BlockNumber blockNum, ...@@ -151,11 +153,10 @@ ReadBufferInternal(Relation reln, BlockNumber blockNum,
ReadLocalBufferCount++; ReadLocalBufferCount++;
pgstat_count_buffer_read(&reln->pgstat_info, reln); pgstat_count_buffer_read(&reln->pgstat_info, reln);
/* Substitute proper block number if caller asked for P_NEW */ /* Substitute proper block number if caller asked for P_NEW */
if (blockNum == P_NEW) if (isExtend)
{ {
blockNum = reln->rd_nblocks; blockNum = reln->rd_nblocks;
reln->rd_nblocks++; reln->rd_nblocks++;
isExtend = true;
} }
bufHdr = LocalBufferAlloc(reln, blockNum, &found); bufHdr = LocalBufferAlloc(reln, blockNum, &found);
if (found) if (found)
...@@ -169,10 +170,11 @@ ReadBufferInternal(Relation reln, BlockNumber blockNum, ...@@ -169,10 +170,11 @@ ReadBufferInternal(Relation reln, BlockNumber blockNum,
ReadBufferCount++; ReadBufferCount++;
pgstat_count_buffer_read(&reln->pgstat_info, reln); pgstat_count_buffer_read(&reln->pgstat_info, reln);
/* Substitute proper block number if caller asked for P_NEW */ /* Substitute proper block number if caller asked for P_NEW */
if (blockNum == P_NEW) if (isExtend)
{ {
blockNum = smgrnblocks(DEFAULT_SMGR, reln); /* must be sure we have accurate file length! */
isExtend = true; blockNum = reln->rd_nblocks = smgrnblocks(DEFAULT_SMGR, reln);
reln->rd_nblocks++;
} }
/* /*
* lookup the buffer. IO_IN_PROGRESS is set if the requested * lookup the buffer. IO_IN_PROGRESS is set if the requested
...@@ -649,26 +651,24 @@ WriteNoReleaseBuffer(Buffer buffer) ...@@ -649,26 +651,24 @@ WriteNoReleaseBuffer(Buffer buffer)
* ReleaseAndReadBuffer -- combine ReleaseBuffer() and ReadBuffer() * ReleaseAndReadBuffer -- combine ReleaseBuffer() and ReadBuffer()
* to save a spinlock release/acquire. * to save a spinlock release/acquire.
* *
* An additional frammish of this routine is that the caller may perform
* file extension (as if blockNum = P_NEW) by passing the actual current
* EOF block number as blockNum and setting isExtend true. This hack
* allows us to avoid calling smgrnblocks() again when the caller has
* already done it.
*
* Note: it is OK to pass buffer = InvalidBuffer, indicating that no old
* buffer actually needs to be released. This case is the same as ReadBuffer
* except for the isExtend option.
*
* Also, if the passed buffer is valid and already contains the desired block * Also, if the passed buffer is valid and already contains the desired block
* number, we simply return it without ever acquiring the spinlock at all. * number, we simply return it without ever acquiring the spinlock at all.
* Since the passed buffer must be pinned, it's OK to examine its block * Since the passed buffer must be pinned, it's OK to examine its block
* number without getting the lock first. * number without getting the lock first.
*
* Note: it is OK to pass buffer = InvalidBuffer, indicating that no old
* buffer actually needs to be released. This case is the same as ReadBuffer,
* but can save some tests in the caller.
*
* Also note: while it will work to call this routine with blockNum == P_NEW,
* it's best to avoid doing so, since that would result in calling
* smgrnblocks() while holding the bufmgr spinlock, hence some loss of
* concurrency.
*/ */
Buffer Buffer
ReleaseAndReadBuffer(Buffer buffer, ReleaseAndReadBuffer(Buffer buffer,
Relation relation, Relation relation,
BlockNumber blockNum, BlockNumber blockNum)
bool isExtend)
{ {
BufferDesc *bufHdr; BufferDesc *bufHdr;
...@@ -703,14 +703,12 @@ ReleaseAndReadBuffer(Buffer buffer, ...@@ -703,14 +703,12 @@ ReleaseAndReadBuffer(Buffer buffer,
AddBufferToFreelist(bufHdr); AddBufferToFreelist(bufHdr);
bufHdr->flags |= BM_FREE; bufHdr->flags |= BM_FREE;
} }
return ReadBufferInternal(relation, blockNum, return ReadBufferInternal(relation, blockNum, true);
isExtend, true);
} }
} }
} }
return ReadBufferInternal(relation, blockNum, return ReadBufferInternal(relation, blockNum, false);
isExtend, false);
} }
/* /*
...@@ -1738,14 +1736,13 @@ ReleaseAndReadBuffer_Debug(char *file, ...@@ -1738,14 +1736,13 @@ ReleaseAndReadBuffer_Debug(char *file,
int line, int line,
Buffer buffer, Buffer buffer,
Relation relation, Relation relation,
BlockNumber blockNum, BlockNumber blockNum)
bool isExtend)
{ {
bool bufferValid; bool bufferValid;
Buffer b; Buffer b;
bufferValid = BufferIsValid(buffer); bufferValid = BufferIsValid(buffer);
b = ReleaseAndReadBuffer(buffer, relation, blockNum, isExtend); b = ReleaseAndReadBuffer(buffer, relation, blockNum);
if (ShowPinTrace && bufferValid && BufferIsLocal(buffer) if (ShowPinTrace && bufferValid && BufferIsLocal(buffer)
&& is_userbuffer(buffer)) && is_userbuffer(buffer))
{ {
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/freespace/freespace.c,v 1.1 2001/06/27 23:31:39 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/storage/freespace/freespace.c,v 1.2 2001/06/29 21:08:24 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -161,10 +161,42 @@ FreeSpaceShmemSize(void) ...@@ -161,10 +161,42 @@ FreeSpaceShmemSize(void)
return size; return size;
} }
BlockNumber
GetPageWithFreeSpace(RelFileNode *rel, Size spaceNeeded)
{
return InvalidBlockNumber; /* stub */
}
void
RecordFreeSpace(RelFileNode *rel, BlockNumber page, Size spaceAvail)
{
/* stub */
}
BlockNumber
RecordAndGetPageWithFreeSpace(RelFileNode *rel,
BlockNumber oldPage,
Size oldSpaceAvail,
Size spaceNeeded)
{
return InvalidBlockNumber; /* stub */
}
void
MultiRecordFreeSpace(RelFileNode *rel,
BlockNumber minPage,
BlockNumber maxPage,
int nPages,
BlockNumber *pages,
Size *spaceAvail)
{
/* stub */
}
void void
FreeSpaceMapForgetRel(RelFileNode *rel) FreeSpaceMapForgetRel(RelFileNode *rel)
{ {
/* stub */
} }
...@@ -178,6 +210,7 @@ FreeSpaceMapForgetRel(RelFileNode *rel) ...@@ -178,6 +210,7 @@ FreeSpaceMapForgetRel(RelFileNode *rel)
void void
DumpFreeSpace(void) DumpFreeSpace(void)
{ {
/* stub */
} }
#endif /* FREESPACE_DEBUG */ #endif /* FREESPACE_DEBUG */
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/smgr/smgr.c,v 1.50 2001/06/27 23:31:39 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/storage/smgr/smgr.c,v 1.51 2001/06/29 21:08:24 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -437,7 +437,20 @@ smgrblindmarkdirty(int16 which, ...@@ -437,7 +437,20 @@ smgrblindmarkdirty(int16 which,
BlockNumber BlockNumber
smgrnblocks(int16 which, Relation reln) smgrnblocks(int16 which, Relation reln)
{ {
return (*(smgrsw[which].smgr_nblocks)) (reln); BlockNumber nblocks;
nblocks = (*(smgrsw[which].smgr_nblocks)) (reln);
/*
* NOTE: if a relation ever did grow to 2^32-1 blocks, this code would
* fail --- but that's a good thing, because it would stop us from
* extending the rel another block and having a block whose number
* actually is InvalidBlockNumber.
*/
if (nblocks == InvalidBlockNumber)
elog(ERROR, "cannot count blocks for %s: %m",
RelationGetRelationName(reln));
return nblocks;
} }
/* /*
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.140 2001/06/27 23:31:39 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.141 2001/06/29 21:08:25 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -1928,33 +1928,111 @@ RelationCacheAbortWalker(Relation *relationPtr, Datum dummy) ...@@ -1928,33 +1928,111 @@ RelationCacheAbortWalker(Relation *relationPtr, Datum dummy)
} }
/* /*
* RelationRegisterRelation - * RelationBuildLocalRelation
* register the Relation descriptor of a newly created relation * Build a relcache entry for an about-to-be-created relation,
* with the relation descriptor Cache. * and enter it into the relcache.
*/ */
void Relation
RelationRegisterRelation(Relation relation) RelationBuildLocalRelation(const char *relname,
TupleDesc tupDesc,
Oid relid, Oid dbid,
bool nailit)
{ {
Relation rel;
MemoryContext oldcxt; MemoryContext oldcxt;
int natts = tupDesc->natts;
int i;
RelationInitLockInfo(relation); AssertArg(natts > 0);
/*
* switch to the cache context to create the relcache entry.
*/
if (!CacheMemoryContext)
CreateCacheMemoryContext();
oldcxt = MemoryContextSwitchTo(CacheMemoryContext); oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
RelationCacheInsert(relation); /*
* allocate a new relation descriptor.
*/
rel = (Relation) palloc(sizeof(RelationData));
MemSet((char *) rel, 0, sizeof(RelationData));
rel->rd_targblock = InvalidBlockNumber;
/* make sure relation is marked as having no open file yet */
rel->rd_fd = -1;
RelationSetReferenceCount(rel, 1);
/*
* nail the reldesc if this is a bootstrap create reln and we may need
* it in the cache later on in the bootstrap process so we don't ever
* want it kicked out. e.g. pg_attribute!!!
*/
if (nailit)
rel->rd_isnailed = true;
/*
* create a new tuple descriptor from the one passed in
* (we do this to copy it into the cache context)
*/
rel->rd_att = CreateTupleDescCopyConstr(tupDesc);
/*
* initialize relation tuple form (caller may add/override data later)
*/
rel->rd_rel = (Form_pg_class) palloc(CLASS_TUPLE_SIZE);
MemSet((char *) rel->rd_rel, 0, CLASS_TUPLE_SIZE);
strcpy(RelationGetPhysicalRelationName(rel), relname);
rel->rd_rel->relkind = RELKIND_UNCATALOGED;
rel->rd_rel->relnatts = natts;
rel->rd_rel->reltype = InvalidOid;
if (tupDesc->constr)
rel->rd_rel->relchecks = tupDesc->constr->num_check;
/*
* Insert relation OID and database/tablespace ID into the right places.
* XXX currently we assume physical tblspace/relnode are same as logical
* dbid/reloid. Probably should pass an extra pair of parameters.
*/
rel->rd_rel->relisshared = (dbid == InvalidOid);
RelationGetRelid(rel) = relid;
for (i = 0; i < natts; i++)
rel->rd_att->attrs[i]->attrelid = relid;
RelationInitLockInfo(rel); /* see lmgr.c */
rel->rd_node.tblNode = dbid;
rel->rd_node.relNode = relid;
rel->rd_rel->relfilenode = relid;
/*
* Okay to insert into the relcache hash tables.
*/
RelationCacheInsert(rel);
/* /*
* we've just created the relation. It is invisible to anyone else * we've just created the relation. It is invisible to anyone else
* before the transaction is committed. Setting rd_myxactonly allows * before the transaction is committed. Setting rd_myxactonly allows
* us to use the local buffer manager for select/insert/etc before the * us to use the local buffer manager for select/insert/etc before the
* end of transaction. (We also need to keep track of relations * end of transaction. (We also need to keep track of relations
* created during a transaction and does the necessary clean up at the * created during a transaction and do the necessary clean up at the
* end of the transaction.) - ay 3/95 * end of the transaction.) - ay 3/95
*/ */
relation->rd_myxactonly = TRUE; rel->rd_myxactonly = true;
newlyCreatedRelns = lcons(relation, newlyCreatedRelns); newlyCreatedRelns = lcons(rel, newlyCreatedRelns);
/*
* done building relcache entry.
*/
MemoryContextSwitchTo(oldcxt); MemoryContextSwitchTo(oldcxt);
return rel;
} }
/* /*
...@@ -1972,14 +2050,18 @@ RelationPurgeLocalRelation(bool xactCommitted) ...@@ -1972,14 +2050,18 @@ RelationPurgeLocalRelation(bool xactCommitted)
List *l = newlyCreatedRelns; List *l = newlyCreatedRelns;
Relation reln = lfirst(l); Relation reln = lfirst(l);
newlyCreatedRelns = lnext(newlyCreatedRelns);
pfree(l);
Assert(reln != NULL && reln->rd_myxactonly); Assert(reln != NULL && reln->rd_myxactonly);
reln->rd_myxactonly = false; /* mark it not on list anymore */ reln->rd_myxactonly = false; /* mark it not on list anymore */
newlyCreatedRelns = lnext(newlyCreatedRelns); /*
pfree(l); * XXX while we clearly must throw out new Relation entries at
* xact abort, it's not clear why we need to do it at commit.
/* XXX is this step still needed? If so, why? */ * Could this be improved?
*/
if (!IsBootstrapProcessingMode()) if (!IsBootstrapProcessingMode())
RelationClearRelation(reln, false); RelationClearRelation(reln, false);
} }
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: hio.h,v 1.18 2001/05/16 22:35:12 tgl Exp $ * $Id: hio.h,v 1.19 2001/06/29 21:08:25 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -19,6 +19,6 @@ ...@@ -19,6 +19,6 @@
extern void RelationPutHeapTuple(Relation relation, Buffer buffer, extern void RelationPutHeapTuple(Relation relation, Buffer buffer,
HeapTuple tuple); HeapTuple tuple);
extern Buffer RelationGetBufferForTuple(Relation relation, Size len, extern Buffer RelationGetBufferForTuple(Relation relation, Size len,
BlockNumber minblocknum); Buffer otherBuffer);
#endif /* HIO_H */ #endif /* HIO_H */
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: bufmgr.h,v 1.52 2001/06/09 18:16:59 tgl Exp $ * $Id: bufmgr.h,v 1.53 2001/06/29 21:08:25 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -155,10 +155,11 @@ extern long *LocalRefCount; ...@@ -155,10 +155,11 @@ extern long *LocalRefCount;
* prototypes for functions in bufmgr.c * prototypes for functions in bufmgr.c
*/ */
extern Buffer ReadBuffer(Relation reln, BlockNumber blockNum); extern Buffer ReadBuffer(Relation reln, BlockNumber blockNum);
extern int ReleaseBuffer(Buffer buffer);
extern int WriteBuffer(Buffer buffer); extern int WriteBuffer(Buffer buffer);
extern int WriteNoReleaseBuffer(Buffer buffer); extern int WriteNoReleaseBuffer(Buffer buffer);
extern Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation, extern Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation,
BlockNumber blockNum, bool isExtend); BlockNumber blockNum);
extern int FlushBuffer(Buffer buffer, bool sync, bool release); extern int FlushBuffer(Buffer buffer, bool sync, bool release);
extern void InitBufferPool(void); extern void InitBufferPool(void);
...@@ -176,7 +177,6 @@ extern void DropRelFileNodeBuffers(RelFileNode rnode); ...@@ -176,7 +177,6 @@ extern void DropRelFileNodeBuffers(RelFileNode rnode);
extern void DropBuffers(Oid dbid); extern void DropBuffers(Oid dbid);
extern void PrintPinnedBufs(void); extern void PrintPinnedBufs(void);
extern int BufferShmemSize(void); extern int BufferShmemSize(void);
extern int ReleaseBuffer(Buffer buffer);
extern RelFileNode BufferGetFileNode(Buffer buffer); extern RelFileNode BufferGetFileNode(Buffer buffer);
extern void SetBufferCommitInfoNeedsSave(Buffer buffer); extern void SetBufferCommitInfoNeedsSave(Buffer buffer);
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: relcache.h,v 1.24 2001/01/24 19:43:29 momjian Exp $ * $Id: relcache.h,v 1.25 2001/06/29 21:08:25 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -27,7 +27,6 @@ extern Relation RelationNodeCacheGetRelation(RelFileNode rnode); ...@@ -27,7 +27,6 @@ extern Relation RelationNodeCacheGetRelation(RelFileNode rnode);
extern Relation RelationIdCacheGetRelation(Oid relationId); extern Relation RelationIdCacheGetRelation(Oid relationId);
extern void RelationClose(Relation relation); extern void RelationClose(Relation relation);
extern void RelationForgetRelation(Oid rid);
/* /*
* Routines to compute/retrieve additional cached information * Routines to compute/retrieve additional cached information
...@@ -40,14 +39,23 @@ extern List *RelationGetIndexList(Relation relation); ...@@ -40,14 +39,23 @@ extern List *RelationGetIndexList(Relation relation);
extern void RelationCacheInitialize(void); extern void RelationCacheInitialize(void);
extern void RelationCacheInitializePhase2(void); extern void RelationCacheInitializePhase2(void);
/*
* Routine to create a relcache entry for an about-to-be-created relation
*/
extern Relation RelationBuildLocalRelation(const char *relname,
TupleDesc tupDesc,
Oid relid, Oid dbid,
bool nailit);
/* /*
* Routines for flushing/rebuilding relcache entries in various scenarios * Routines for flushing/rebuilding relcache entries in various scenarios
*/ */
extern void RelationForgetRelation(Oid rid);
extern void RelationIdInvalidateRelationCacheByRelationId(Oid relationId); extern void RelationIdInvalidateRelationCacheByRelationId(Oid relationId);
extern void RelationCacheInvalidate(void); extern void RelationCacheInvalidate(void);
extern void RelationRegisterRelation(Relation relation);
extern void RelationPurgeLocalRelation(bool xactComitted); extern void RelationPurgeLocalRelation(bool xactComitted);
extern void RelationCacheAbort(void); extern void RelationCacheAbort(void);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment