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 * Copyright (c) 1994-5, Regents of the University of California
18 * $Header: /cvsroot/pgsql/src/backend/storage/buffer/localbuf.c,v 1.22 1999/05/10 00:45:36 momjian Exp $
20 *-------------------------------------------------------------------------
22 #include <sys/types.h>
31 /* declarations split between these three files */
32 #include "storage/buf.h"
33 #include "storage/buf_internals.h"
34 #include "storage/bufmgr.h"
36 #include "storage/fd.h"
37 #include "storage/ipc.h"
38 #include "storage/shmem.h"
39 #include "storage/spin.h"
40 #include "storage/smgr.h"
41 #include "storage/lmgr.h"
42 #include "miscadmin.h"
43 #include "utils/builtins.h"
44 #include "utils/hsearch.h"
45 #include "utils/memutils.h"
46 #include "utils/relcache.h"
47 #include "executor/execdebug.h" /* for NDirectFileRead */
48 #include "catalog/catalog.h"
50 extern long int LocalBufferFlushCount;
53 BufferDesc *LocalBufferDescriptors = NULL;
54 long *LocalRefCount = NULL;
56 static int nextFreeLocalBuf = 0;
62 * allocate a local buffer. We do round robin allocation for now.
65 LocalBufferAlloc(Relation reln, BlockNumber blockNum, bool *foundPtr)
68 BufferDesc *bufHdr = (BufferDesc *) NULL;
70 if (blockNum == P_NEW)
72 blockNum = reln->rd_nblocks;
76 /* a low tech search for now -- not optimized for scans */
77 for (i = 0; i < NLocBuffer; i++)
79 if (LocalBufferDescriptors[i].tag.relId.relId == RelationGetRelid(reln) &&
80 LocalBufferDescriptors[i].tag.blockNum == blockNum)
84 fprintf(stderr, "LB ALLOC (%u,%d) %d\n",
85 RelationGetRelid(reln), blockNum, -i - 1);
89 return &LocalBufferDescriptors[i];
94 fprintf(stderr, "LB ALLOC (%u,%d) %d\n",
95 RelationGetRelid(reln), blockNum, -nextFreeLocalBuf - 1);
98 /* need to get a new buffer (round robin for now) */
99 for (i = 0; i < NLocBuffer; i++)
101 int b = (nextFreeLocalBuf + i) % NLocBuffer;
103 if (LocalRefCount[b] == 0)
105 bufHdr = &LocalBufferDescriptors[b];
107 nextFreeLocalBuf = (b + 1) % NLocBuffer;
112 elog(ERROR, "no empty local buffer.");
115 * this buffer is not referenced but it might still be dirty (the last
116 * transaction to touch it doesn't need its contents but has not
117 * flushed it). if that's the case, write it out before reusing it!
119 if (bufHdr->flags & BM_DIRTY)
121 Relation bufrel = RelationIdCacheGetRelation(bufHdr->tag.relId.relId);
123 Assert(bufrel != NULL);
125 /* flush this page */
126 smgrwrite(DEFAULT_SMGR, bufrel, bufHdr->tag.blockNum,
127 (char *) MAKE_PTR(bufHdr->data));
128 LocalBufferFlushCount++;
129 RelationDecrementReferenceCount(bufrel);
135 bufHdr->tag.relId.relId = RelationGetRelid(reln);
136 bufHdr->tag.blockNum = blockNum;
137 bufHdr->flags &= ~BM_DIRTY;
140 * lazy memory allocation. (see MAKE_PTR for why we need to do
143 if (bufHdr->data == (SHMEM_OFFSET) 0)
145 char *data = (char *) malloc(BLCKSZ);
147 bufHdr->data = MAKE_OFFSET(data);
156 * writes out a local buffer
159 WriteLocalBuffer(Buffer buffer, bool release)
163 Assert(BufferIsLocal(buffer));
166 fprintf(stderr, "LB WRITE %d\n", buffer);
169 bufid = -(buffer + 1);
170 LocalBufferDescriptors[bufid].flags |= BM_DIRTY;
174 Assert(LocalRefCount[bufid] > 0);
175 LocalRefCount[bufid]--;
183 * flushes a local buffer
186 FlushLocalBuffer(Buffer buffer, bool release)
192 Assert(BufferIsLocal(buffer));
195 fprintf(stderr, "LB FLUSH %d\n", buffer);
198 bufid = -(buffer + 1);
199 bufHdr = &LocalBufferDescriptors[bufid];
200 bufHdr->flags &= ~BM_DIRTY;
201 bufrel = RelationIdCacheGetRelation(bufHdr->tag.relId.relId);
203 Assert(bufrel != NULL);
204 smgrflush(DEFAULT_SMGR, bufrel, bufHdr->tag.blockNum,
205 (char *) MAKE_PTR(bufHdr->data));
206 LocalBufferFlushCount++;
207 RelationDecrementReferenceCount(bufrel);
209 Assert(LocalRefCount[bufid] > 0);
211 LocalRefCount[bufid]--;
218 * init the local buffer cache. Since most queries (esp. multi-user ones)
219 * don't involve local buffers, we delay allocating memory for actual the
220 * buffer until we need it.
223 InitLocalBuffer(void)
228 * these aren't going away. I'm not gonna use palloc.
230 LocalBufferDescriptors =
231 (BufferDesc *) malloc(sizeof(BufferDesc) * NLocBuffer);
232 MemSet(LocalBufferDescriptors, 0, sizeof(BufferDesc) * NLocBuffer);
233 nextFreeLocalBuf = 0;
235 for (i = 0; i < NLocBuffer; i++)
237 BufferDesc *buf = &LocalBufferDescriptors[i];
240 * negative to indicate local buffer. This is tricky: shared
241 * buffers start with 0. We have to start with -2. (Note that the
242 * routine BufferDescriptorGetBuffer adds 1 to buf_id so our first
245 buf->buf_id = -i - 2;
248 LocalRefCount = (long *) malloc(sizeof(long) * NLocBuffer);
249 MemSet(LocalRefCount, 0, sizeof(long) * NLocBuffer);
254 * flush all dirty buffers in the local buffer cache. Since the buffer
255 * cache is only used for keeping relations visible during a transaction,
256 * we will not need these buffers again.
259 LocalBufferSync(void)
263 for (i = 0; i < NLocBuffer; i++)
265 BufferDesc *buf = &LocalBufferDescriptors[i];
268 if (buf->flags & BM_DIRTY)
271 fprintf(stderr, "LB SYNC %d\n", -i - 1);
273 bufrel = RelationIdCacheGetRelation(buf->tag.relId.relId);
275 Assert(bufrel != NULL);
277 smgrwrite(DEFAULT_SMGR, bufrel, buf->tag.blockNum,
278 (char *) MAKE_PTR(buf->data));
279 LocalBufferFlushCount++;
280 RelationDecrementReferenceCount(bufrel);
282 buf->tag.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.relId.relId = InvalidOid;
301 buf->flags &= ~BM_DIRTY;
302 buf->buf_id = -i - 2;
305 MemSet(LocalRefCount, 0, sizeof(long) * NLocBuffer);
306 nextFreeLocalBuf = 0;