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.3 1996/11/08 05:58:16 momjian Exp $
20 *-------------------------------------------------------------------------
30 /* declarations split between these three files */
31 #include "storage/buf.h"
32 #include "storage/buf_internals.h"
33 #include "storage/bufmgr.h"
35 #include "storage/fd.h"
36 #include "storage/ipc.h"
37 #include "storage/shmem.h"
38 #include "storage/spin.h"
39 #include "storage/smgr.h"
40 #include "storage/lmgr.h"
41 #include "miscadmin.h"
42 #include "utils/builtins.h"
43 #include "utils/hsearch.h"
44 #include "utils/memutils.h"
45 #include "utils/relcache.h"
46 #include "executor/execdebug.h" /* for NDirectFileRead */
47 #include "catalog/catalog.h"
50 BufferDesc *LocalBufferDescriptors = NULL;
51 long *LocalRefCount = NULL;
53 static int nextFreeLocalBuf = 0;
59 * allocate a local buffer. We do round robin allocation for now.
62 LocalBufferAlloc(Relation reln, BlockNumber blockNum, bool *foundPtr)
65 BufferDesc *bufHdr = (BufferDesc *) NULL;
67 if (blockNum == P_NEW) {
68 blockNum = reln->rd_nblocks;
72 /* a low tech search for now -- not optimized for scans */
73 for (i=0; i < NLocBuffer; i++) {
74 if (LocalBufferDescriptors[i].tag.relId.relId == reln->rd_id &&
75 LocalBufferDescriptors[i].tag.blockNum == blockNum) {
78 fprintf(stderr, "LB ALLOC (%d,%d) %d\n",
79 reln->rd_id, blockNum, -i-1);
83 return &LocalBufferDescriptors[i];
88 fprintf(stderr, "LB ALLOC (%d,%d) %d\n",
89 reln->rd_id, blockNum, -nextFreeLocalBuf-1);
92 /* need to get a new buffer (round robin for now) */
93 for(i=0; i < NLocBuffer; i++) {
94 int b = (nextFreeLocalBuf + i) % NLocBuffer;
96 if (LocalRefCount[b]==0) {
97 bufHdr = &LocalBufferDescriptors[b];
99 nextFreeLocalBuf = (b + 1) % NLocBuffer;
104 elog(WARN, "no empty local buffer.");
107 * this buffer is not referenced but it might still be dirty (the
108 * last transaction to touch it doesn't need its contents but has
109 * not flushed it). if that's the case, write it out before
112 if (bufHdr->flags & BM_DIRTY) {
113 Relation bufrel = RelationIdCacheGetRelation(bufHdr->tag.relId.relId);
115 Assert(bufrel != NULL);
117 /* flush this page */
118 smgrwrite(bufrel->rd_rel->relsmgr, bufrel, bufHdr->tag.blockNum,
119 (char *) MAKE_PTR(bufHdr->data));
125 bufHdr->tag.relId.relId = reln->rd_id;
126 bufHdr->tag.blockNum = blockNum;
127 bufHdr->flags &= ~BM_DIRTY;
130 * lazy memory allocation. (see MAKE_PTR for why we need to do
133 if (bufHdr->data == (SHMEM_OFFSET)0) {
134 char *data = (char *)malloc(BLCKSZ);
136 bufHdr->data = MAKE_OFFSET(data);
145 * writes out a local buffer
148 WriteLocalBuffer(Buffer buffer, bool release)
152 Assert(BufferIsLocal(buffer));
155 fprintf(stderr, "LB WRITE %d\n", buffer);
158 bufid = - (buffer + 1);
159 LocalBufferDescriptors[bufid].flags |= BM_DIRTY;
162 Assert(LocalRefCount[bufid] > 0);
163 LocalRefCount[bufid]--;
171 * flushes a local buffer
174 FlushLocalBuffer(Buffer buffer)
180 Assert(BufferIsLocal(buffer));
183 fprintf(stderr, "LB FLUSH %d\n", buffer);
186 bufid = - (buffer + 1);
187 bufHdr = &LocalBufferDescriptors[bufid];
188 bufHdr->flags &= ~BM_DIRTY;
189 bufrel = RelationIdCacheGetRelation(bufHdr->tag.relId.relId);
191 Assert(bufrel != NULL);
192 smgrflush(bufrel->rd_rel->relsmgr, bufrel, bufHdr->tag.blockNum,
193 (char *) MAKE_PTR(bufHdr->data));
195 Assert(LocalRefCount[bufid] > 0);
196 LocalRefCount[bufid]--;
203 * init the local buffer cache. Since most queries (esp. multi-user ones)
204 * don't involve local buffers, we delay allocating memory for actual the
205 * buffer until we need it.
213 * these aren't going away. I'm not gonna use palloc.
215 LocalBufferDescriptors =
216 (BufferDesc *)malloc(sizeof(BufferDesc) * NLocBuffer);
217 memset(LocalBufferDescriptors, 0, sizeof(BufferDesc) * NLocBuffer);
218 nextFreeLocalBuf = 0;
220 for (i = 0; i < NLocBuffer; i++) {
221 BufferDesc *buf = &LocalBufferDescriptors[i];
224 * negative to indicate local buffer. This is tricky: shared buffers
225 * start with 0. We have to start with -2. (Note that the routine
226 * BufferDescriptorGetBuffer adds 1 to buf_id so our first buffer id
229 buf->buf_id = - i - 2;
233 (long *)malloc(sizeof(long) * NLocBuffer);
234 memset(LocalRefCount, 0, sizeof(long) * NLocBuffer);
239 * flush all dirty buffers in the local buffer cache. Since the buffer
240 * cache is only used for keeping relations visible during a transaction,
241 * we will not need these buffers again.
248 for (i = 0; i < NLocBuffer; i++) {
249 BufferDesc *buf = &LocalBufferDescriptors[i];
252 if (buf->flags & BM_DIRTY) {
254 fprintf(stderr, "LB SYNC %d\n", -i-1);
256 bufrel = RelationIdCacheGetRelation(buf->tag.relId.relId);
258 Assert(bufrel != NULL);
260 smgrwrite(bufrel->rd_rel->relsmgr, bufrel, buf->tag.blockNum,
261 (char *) MAKE_PTR(buf->data));
263 buf->tag.relId.relId = InvalidOid;
264 buf->flags &= ~BM_DIRTY;
268 memset(LocalRefCount, 0, sizeof(long) * NLocBuffer);
272 ResetLocalBufferPool()
276 memset(LocalBufferDescriptors, 0, sizeof(BufferDesc) * NLocBuffer);
277 nextFreeLocalBuf = 0;
279 for (i = 0; i < NLocBuffer; i++) {
280 BufferDesc *buf = &LocalBufferDescriptors[i];
282 /* just like InitLocalBuffer() */
283 buf->buf_id = - i - 2;
286 memset(LocalRefCount, 0, sizeof(long) * NLocBuffer);