1 /*-------------------------------------------------------------------------
4 * local buffer manager. Fast buffer manager for temporary tables
5 * or special cases when the operation is not visible to other backends.
7 * When a relation is being created, the descriptor will have rd_islocal
8 * set to indicate that the local buffer manager should be used. During
9 * the same transaction the relation is being created, any inserts or
10 * selects from the newly created relation will use the local buffer
11 * pool. rd_islocal is reset at the end of a transaction (commit/abort).
12 * This is useful for queries like SELECT INTO TABLE and create index.
14 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
15 * Portions Copyright (c) 1994-5, Regents of the University of California
19 * $Header: /cvsroot/pgsql/src/backend/storage/buffer/localbuf.c,v 1.31 2000/10/18 05:50:15 vadim Exp $
21 *-------------------------------------------------------------------------
23 #include <sys/types.h>
30 #include "executor/execdebug.h"
31 #include "storage/smgr.h"
32 #include "utils/relcache.h"
34 extern long int LocalBufferFlushCount;
37 BufferDesc *LocalBufferDescriptors = NULL;
38 long *LocalRefCount = NULL;
40 static int nextFreeLocalBuf = 0;
46 * allocate a local buffer. We do round robin allocation for now.
49 LocalBufferAlloc(Relation reln, BlockNumber blockNum, bool *foundPtr)
52 BufferDesc *bufHdr = (BufferDesc *) NULL;
54 if (blockNum == P_NEW)
56 blockNum = reln->rd_nblocks;
60 /* a low tech search for now -- not optimized for scans */
61 for (i = 0; i < NLocBuffer; i++)
63 if (LocalBufferDescriptors[i].tag.rnode.relNode ==
64 reln->rd_node.relNode &&
65 LocalBufferDescriptors[i].tag.blockNum == blockNum)
69 fprintf(stderr, "LB ALLOC (%u,%d) %d\n",
70 RelationGetRelid(reln), blockNum, -i - 1);
74 return &LocalBufferDescriptors[i];
79 fprintf(stderr, "LB ALLOC (%u,%d) %d\n",
80 RelationGetRelid(reln), blockNum, -nextFreeLocalBuf - 1);
83 /* need to get a new buffer (round robin for now) */
84 for (i = 0; i < NLocBuffer; i++)
86 int b = (nextFreeLocalBuf + i) % NLocBuffer;
88 if (LocalRefCount[b] == 0)
90 bufHdr = &LocalBufferDescriptors[b];
92 nextFreeLocalBuf = (b + 1) % NLocBuffer;
97 elog(ERROR, "no empty local buffer.");
100 * this buffer is not referenced but it might still be dirty (the last
101 * transaction to touch it doesn't need its contents but has not
102 * flushed it). if that's the case, write it out before reusing it!
104 if (bufHdr->flags & BM_DIRTY)
106 Relation bufrel = RelationIdCacheGetRelation(bufHdr->relId.relId);
108 Assert(bufrel != NULL);
110 /* flush this page */
111 smgrwrite(DEFAULT_SMGR, bufrel, bufHdr->tag.blockNum,
112 (char *) MAKE_PTR(bufHdr->data));
113 LocalBufferFlushCount++;
116 * drop relcache refcount incremented by
117 * RelationIdCacheGetRelation
119 RelationDecrementReferenceCount(bufrel);
125 * We need not in tblNode currently but will in future I think,
126 * when we'll give up rel->rd_fd to fmgr cache.
128 bufHdr->tag.rnode = reln->rd_node;
129 bufHdr->tag.blockNum = blockNum;
130 bufHdr->relId = reln->rd_lockInfo.lockRelId;
131 bufHdr->flags &= ~BM_DIRTY;
134 * lazy memory allocation. (see MAKE_PTR for why we need to do
137 if (bufHdr->data == (SHMEM_OFFSET) 0)
139 char *data = (char *) malloc(BLCKSZ);
141 bufHdr->data = MAKE_OFFSET(data);
150 * writes out a local buffer
153 WriteLocalBuffer(Buffer buffer, bool release)
157 Assert(BufferIsLocal(buffer));
160 fprintf(stderr, "LB WRITE %d\n", buffer);
163 bufid = -(buffer + 1);
164 LocalBufferDescriptors[bufid].flags |= BM_DIRTY;
168 Assert(LocalRefCount[bufid] > 0);
169 LocalRefCount[bufid]--;
177 * flushes a local buffer
180 FlushLocalBuffer(Buffer buffer, bool release)
186 Assert(BufferIsLocal(buffer));
189 fprintf(stderr, "LB FLUSH %d\n", buffer);
192 bufid = -(buffer + 1);
193 bufHdr = &LocalBufferDescriptors[bufid];
194 bufHdr->flags &= ~BM_DIRTY;
195 bufrel = RelationIdCacheGetRelation(bufHdr->relId.relId);
197 Assert(bufrel != NULL);
198 smgrflush(DEFAULT_SMGR, bufrel, bufHdr->tag.blockNum,
199 (char *) MAKE_PTR(bufHdr->data));
200 LocalBufferFlushCount++;
202 /* drop relcache refcount incremented by RelationIdCacheGetRelation */
203 RelationDecrementReferenceCount(bufrel);
207 Assert(LocalRefCount[bufid] > 0);
208 LocalRefCount[bufid]--;
216 * init the local buffer cache. Since most queries (esp. multi-user ones)
217 * don't involve local buffers, we delay allocating memory for actual the
218 * buffer until we need it.
221 InitLocalBuffer(void)
226 * these aren't going away. I'm not gonna use palloc.
228 LocalBufferDescriptors =
229 (BufferDesc *) malloc(sizeof(BufferDesc) * NLocBuffer);
230 MemSet(LocalBufferDescriptors, 0, sizeof(BufferDesc) * NLocBuffer);
231 nextFreeLocalBuf = 0;
233 for (i = 0; i < NLocBuffer; i++)
235 BufferDesc *buf = &LocalBufferDescriptors[i];
238 * negative to indicate local buffer. This is tricky: shared
239 * buffers start with 0. We have to start with -2. (Note that the
240 * routine BufferDescriptorGetBuffer adds 1 to buf_id so our first
243 buf->buf_id = -i - 2;
246 LocalRefCount = (long *) malloc(sizeof(long) * NLocBuffer);
247 MemSet(LocalRefCount, 0, sizeof(long) * NLocBuffer);
252 * flush all dirty buffers in the local buffer cache. Since the buffer
253 * cache is only used for keeping relations visible during a transaction,
254 * we will not need these buffers again.
257 LocalBufferSync(void)
261 for (i = 0; i < NLocBuffer; i++)
263 BufferDesc *buf = &LocalBufferDescriptors[i];
266 if (buf->flags & BM_DIRTY)
269 fprintf(stderr, "LB SYNC %d\n", -i - 1);
271 bufrel = RelationIdCacheGetRelation(buf->relId.relId);
273 Assert(bufrel != NULL);
275 smgrwrite(DEFAULT_SMGR, bufrel, buf->tag.blockNum,
276 (char *) MAKE_PTR(buf->data));
277 LocalBufferFlushCount++;
279 /* drop relcache refcount from RelationIdCacheGetRelation */
280 RelationDecrementReferenceCount(bufrel);
282 buf->relId.relId = InvalidOid;
283 buf->flags &= ~BM_DIRTY;
287 MemSet(LocalRefCount, 0, sizeof(long) * NLocBuffer);
288 nextFreeLocalBuf = 0;
292 ResetLocalBufferPool(void)
296 for (i = 0; i < NLocBuffer; i++)
298 BufferDesc *buf = &LocalBufferDescriptors[i];
300 buf->tag.rnode.relNode = InvalidOid;
301 buf->flags &= ~BM_DIRTY;
302 buf->buf_id = -i - 2;
305 MemSet(LocalRefCount, 0, sizeof(long) * NLocBuffer);
306 nextFreeLocalBuf = 0;