From 6e8a1a6717956eb86113fb73f2bc3d0a22c542c6 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Sat, 15 Jun 2002 19:59:59 +0000 Subject: [PATCH] WriteBuffer return value: >I'd vote for changing WriteBuffer to >return void, and have it elog() on bad argument. Manfred Koizar --- src/backend/commands/sequence.c | 8 ++- src/backend/storage/buffer/bufmgr.c | 76 +++++++++++++-------------- src/backend/storage/buffer/localbuf.c | 6 +-- src/include/storage/buf_internals.h | 4 +- src/include/storage/bufmgr.h | 6 +-- 5 files changed, 46 insertions(+), 54 deletions(-) diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c index 71a5a561de..736dde31bc 100644 --- a/src/backend/commands/sequence.c +++ b/src/backend/commands/sequence.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/sequence.c,v 1.80 2002/06/15 19:54:23 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/sequence.c,v 1.81 2002/06/15 19:59:59 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -468,8 +468,7 @@ nextval(PG_FUNCTION_ARGS) LockBuffer(buf, BUFFER_LOCK_UNLOCK); - if (WriteBuffer(buf) == STATUS_ERROR) - elog(ERROR, "%s.nextval: WriteBuffer failed", sequence->relname); + WriteBuffer(buf); relation_close(seqrel, NoLock); @@ -581,8 +580,7 @@ do_setval(RangeVar *sequence, int64 next, bool iscalled) LockBuffer(buf, BUFFER_LOCK_UNLOCK); - if (WriteBuffer(buf) == STATUS_ERROR) - elog(ERROR, "%s.setval: WriteBuffer failed", sequence->relname); + WriteBuffer(buf); relation_close(seqrel, NoLock); } diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 639f957826..768dd0fd7f 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/storage/buffer/bufmgr.c,v 1.124 2002/06/15 19:55:37 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/storage/buffer/bufmgr.c,v 1.125 2002/06/15 19:59:59 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -87,6 +87,7 @@ static int ReleaseBufferWithBufferLock(Buffer buffer); static int BufferReplace(BufferDesc *bufHdr); void PrintBufferDescs(void); +static void write_buffer(Buffer buffer, bool unpin); /* * ReadBuffer -- returns a buffer containing the requested @@ -557,6 +558,36 @@ BufferAlloc(Relation reln, return buf; } +/* + * write_buffer -- common functionality for + * WriteBuffer and WriteNoReleaseBuffer + */ +static void +write_buffer(Buffer buffer, bool release) +{ + BufferDesc *bufHdr; + + if (BufferIsLocal(buffer)) + { + WriteLocalBuffer(buffer, release); + return; + } + + if (BAD_BUFFER_ID(buffer)) + elog(ERROR, "write_buffer: bad buffer %d", buffer); + + bufHdr = &BufferDescriptors[buffer - 1]; + + LWLockAcquire(BufMgrLock, LW_EXCLUSIVE); + Assert(bufHdr->refcount > 0); + + bufHdr->flags |= (BM_DIRTY | BM_JUST_DIRTIED); + + if (release) + UnpinBuffer(bufHdr); + LWLockRelease(BufMgrLock); +} + /* * WriteBuffer * @@ -571,55 +602,20 @@ BufferAlloc(Relation reln, #undef WriteBuffer -int +void WriteBuffer(Buffer buffer) { - BufferDesc *bufHdr; - - if (BufferIsLocal(buffer)) - return WriteLocalBuffer(buffer, TRUE); - - if (BAD_BUFFER_ID(buffer)) - return FALSE; - - bufHdr = &BufferDescriptors[buffer - 1]; - - LWLockAcquire(BufMgrLock, LW_EXCLUSIVE); - Assert(bufHdr->refcount > 0); - - bufHdr->flags |= (BM_DIRTY | BM_JUST_DIRTIED); - - UnpinBuffer(bufHdr); - LWLockRelease(BufMgrLock); - - return TRUE; + write_buffer(buffer, true); } /* * WriteNoReleaseBuffer -- like WriteBuffer, but do not unpin the buffer * when the operation is complete. */ -int +void WriteNoReleaseBuffer(Buffer buffer) { - BufferDesc *bufHdr; - - if (BufferIsLocal(buffer)) - return WriteLocalBuffer(buffer, FALSE); - - if (BAD_BUFFER_ID(buffer)) - return STATUS_ERROR; - - bufHdr = &BufferDescriptors[buffer - 1]; - - LWLockAcquire(BufMgrLock, LW_EXCLUSIVE); - Assert(bufHdr->refcount > 0); - - bufHdr->flags |= (BM_DIRTY | BM_JUST_DIRTIED); - - LWLockRelease(BufMgrLock); - - return STATUS_OK; + write_buffer(buffer, false); } diff --git a/src/backend/storage/buffer/localbuf.c b/src/backend/storage/buffer/localbuf.c index 44446d30a4..64bebabad9 100644 --- a/src/backend/storage/buffer/localbuf.c +++ b/src/backend/storage/buffer/localbuf.c @@ -16,7 +16,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/storage/buffer/localbuf.c,v 1.42 2002/05/03 17:42:11 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/storage/buffer/localbuf.c,v 1.43 2002/06/15 19:59:59 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -155,7 +155,7 @@ LocalBufferAlloc(Relation reln, BlockNumber blockNum, bool *foundPtr) * WriteLocalBuffer - * writes out a local buffer */ -int +void WriteLocalBuffer(Buffer buffer, bool release) { int bufid; @@ -174,8 +174,6 @@ WriteLocalBuffer(Buffer buffer, bool release) Assert(LocalRefCount[bufid] > 0); LocalRefCount[bufid]--; } - - return true; } /* diff --git a/src/include/storage/buf_internals.h b/src/include/storage/buf_internals.h index 192895b65f..7cc4b9aed7 100644 --- a/src/include/storage/buf_internals.h +++ b/src/include/storage/buf_internals.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: buf_internals.h,v 1.55 2002/06/15 19:55:38 momjian Exp $ + * $Id: buf_internals.h,v 1.56 2002/06/15 19:59:59 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -176,7 +176,7 @@ extern BufferDesc *LocalBufferDescriptors; extern BufferDesc *LocalBufferAlloc(Relation reln, BlockNumber blockNum, bool *foundPtr); -extern int WriteLocalBuffer(Buffer buffer, bool release); +extern void WriteLocalBuffer(Buffer buffer, bool release); extern int FlushLocalBuffer(Buffer buffer, bool sync, bool release); extern void LocalBufferSync(void); extern void ResetLocalBufferPool(void); diff --git a/src/include/storage/bufmgr.h b/src/include/storage/bufmgr.h index 6069814edb..d1fd92658a 100644 --- a/src/include/storage/bufmgr.h +++ b/src/include/storage/bufmgr.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: bufmgr.h,v 1.58 2002/04/15 23:47:12 momjian Exp $ + * $Id: bufmgr.h,v 1.59 2002/06/15 19:59:59 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -148,8 +148,8 @@ extern long *LocalRefCount; */ extern Buffer ReadBuffer(Relation reln, BlockNumber blockNum); extern int ReleaseBuffer(Buffer buffer); -extern int WriteBuffer(Buffer buffer); -extern int WriteNoReleaseBuffer(Buffer buffer); +extern void WriteBuffer(Buffer buffer); +extern void WriteNoReleaseBuffer(Buffer buffer); extern Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation, BlockNumber blockNum); extern int FlushBuffer(Buffer buffer, bool sync, bool release);