OSDN Git Service

I had to change buffer tag: now RelFileNode is used instead of
[pg-rex/syncrep.git] / src / backend / storage / buffer / localbuf.c
1 /*-------------------------------------------------------------------------
2  *
3  * localbuf.c
4  *        local buffer manager. Fast buffer manager for temporary tables
5  *        or special cases when the operation is not visible to other backends.
6  *
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.
13  *
14  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
15  * Portions Copyright (c) 1994-5, Regents of the University of California
16  *
17  *
18  * IDENTIFICATION
19  *        $Header: /cvsroot/pgsql/src/backend/storage/buffer/localbuf.c,v 1.31 2000/10/18 05:50:15 vadim Exp $
20  *
21  *-------------------------------------------------------------------------
22  */
23 #include <sys/types.h>
24 #include <sys/file.h>
25 #include <math.h>
26 #include <signal.h>
27
28 #include "postgres.h"
29
30 #include "executor/execdebug.h"
31 #include "storage/smgr.h"
32 #include "utils/relcache.h"
33
34 extern long int LocalBufferFlushCount;
35
36 int                     NLocBuffer = 64;
37 BufferDesc *LocalBufferDescriptors = NULL;
38 long       *LocalRefCount = NULL;
39
40 static int      nextFreeLocalBuf = 0;
41
42 /*#define LBDEBUG*/
43
44 /*
45  * LocalBufferAlloc -
46  *        allocate a local buffer. We do round robin allocation for now.
47  */
48 BufferDesc *
49 LocalBufferAlloc(Relation reln, BlockNumber blockNum, bool *foundPtr)
50 {
51         int                     i;
52         BufferDesc *bufHdr = (BufferDesc *) NULL;
53
54         if (blockNum == P_NEW)
55         {
56                 blockNum = reln->rd_nblocks;
57                 reln->rd_nblocks++;
58         }
59
60         /* a low tech search for now -- not optimized for scans */
61         for (i = 0; i < NLocBuffer; i++)
62         {
63                 if (LocalBufferDescriptors[i].tag.rnode.relNode == 
64                         reln->rd_node.relNode &&
65                         LocalBufferDescriptors[i].tag.blockNum == blockNum)
66                 {
67
68 #ifdef LBDEBUG
69                         fprintf(stderr, "LB ALLOC (%u,%d) %d\n",
70                                         RelationGetRelid(reln), blockNum, -i - 1);
71 #endif
72                         LocalRefCount[i]++;
73                         *foundPtr = TRUE;
74                         return &LocalBufferDescriptors[i];
75                 }
76         }
77
78 #ifdef LBDEBUG
79         fprintf(stderr, "LB ALLOC (%u,%d) %d\n",
80                         RelationGetRelid(reln), blockNum, -nextFreeLocalBuf - 1);
81 #endif
82
83         /* need to get a new buffer (round robin for now) */
84         for (i = 0; i < NLocBuffer; i++)
85         {
86                 int                     b = (nextFreeLocalBuf + i) % NLocBuffer;
87
88                 if (LocalRefCount[b] == 0)
89                 {
90                         bufHdr = &LocalBufferDescriptors[b];
91                         LocalRefCount[b]++;
92                         nextFreeLocalBuf = (b + 1) % NLocBuffer;
93                         break;
94                 }
95         }
96         if (bufHdr == NULL)
97                 elog(ERROR, "no empty local buffer.");
98
99         /*
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!
103          */
104         if (bufHdr->flags & BM_DIRTY)
105         {
106                 Relation        bufrel = RelationIdCacheGetRelation(bufHdr->relId.relId);
107
108                 Assert(bufrel != NULL);
109
110                 /* flush this page */
111                 smgrwrite(DEFAULT_SMGR, bufrel, bufHdr->tag.blockNum,
112                                   (char *) MAKE_PTR(bufHdr->data));
113                 LocalBufferFlushCount++;
114
115                 /*
116                  * drop relcache refcount incremented by
117                  * RelationIdCacheGetRelation
118                  */
119                 RelationDecrementReferenceCount(bufrel);
120         }
121
122         /*
123          * it's all ours now.
124          *
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.
127          */
128         bufHdr->tag.rnode = reln->rd_node;
129         bufHdr->tag.blockNum = blockNum;
130         bufHdr->relId = reln->rd_lockInfo.lockRelId;
131         bufHdr->flags &= ~BM_DIRTY;
132
133         /*
134          * lazy memory allocation. (see MAKE_PTR for why we need to do
135          * MAKE_OFFSET.)
136          */
137         if (bufHdr->data == (SHMEM_OFFSET) 0)
138         {
139                 char       *data = (char *) malloc(BLCKSZ);
140
141                 bufHdr->data = MAKE_OFFSET(data);
142         }
143
144         *foundPtr = FALSE;
145         return bufHdr;
146 }
147
148 /*
149  * WriteLocalBuffer -
150  *        writes out a local buffer
151  */
152 int
153 WriteLocalBuffer(Buffer buffer, bool release)
154 {
155         int                     bufid;
156
157         Assert(BufferIsLocal(buffer));
158
159 #ifdef LBDEBUG
160         fprintf(stderr, "LB WRITE %d\n", buffer);
161 #endif
162
163         bufid = -(buffer + 1);
164         LocalBufferDescriptors[bufid].flags |= BM_DIRTY;
165
166         if (release)
167         {
168                 Assert(LocalRefCount[bufid] > 0);
169                 LocalRefCount[bufid]--;
170         }
171
172         return true;
173 }
174
175 /*
176  * FlushLocalBuffer -
177  *        flushes a local buffer
178  */
179 int
180 FlushLocalBuffer(Buffer buffer, bool release)
181 {
182         int                     bufid;
183         Relation        bufrel;
184         BufferDesc *bufHdr;
185
186         Assert(BufferIsLocal(buffer));
187
188 #ifdef LBDEBUG
189         fprintf(stderr, "LB FLUSH %d\n", buffer);
190 #endif
191
192         bufid = -(buffer + 1);
193         bufHdr = &LocalBufferDescriptors[bufid];
194         bufHdr->flags &= ~BM_DIRTY;
195         bufrel = RelationIdCacheGetRelation(bufHdr->relId.relId);
196
197         Assert(bufrel != NULL);
198         smgrflush(DEFAULT_SMGR, bufrel, bufHdr->tag.blockNum,
199                           (char *) MAKE_PTR(bufHdr->data));
200         LocalBufferFlushCount++;
201
202         /* drop relcache refcount incremented by RelationIdCacheGetRelation */
203         RelationDecrementReferenceCount(bufrel);
204
205         if (release)
206         {
207                 Assert(LocalRefCount[bufid] > 0);
208                 LocalRefCount[bufid]--;
209         }
210
211         return true;
212 }
213
214 /*
215  * InitLocalBuffer -
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.
219  */
220 void
221 InitLocalBuffer(void)
222 {
223         int                     i;
224
225         /*
226          * these aren't going away. I'm not gonna use palloc.
227          */
228         LocalBufferDescriptors =
229                 (BufferDesc *) malloc(sizeof(BufferDesc) * NLocBuffer);
230         MemSet(LocalBufferDescriptors, 0, sizeof(BufferDesc) * NLocBuffer);
231         nextFreeLocalBuf = 0;
232
233         for (i = 0; i < NLocBuffer; i++)
234         {
235                 BufferDesc *buf = &LocalBufferDescriptors[i];
236
237                 /*
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
241                  * buffer id is -1.)
242                  */
243                 buf->buf_id = -i - 2;
244         }
245
246         LocalRefCount = (long *) malloc(sizeof(long) * NLocBuffer);
247         MemSet(LocalRefCount, 0, sizeof(long) * NLocBuffer);
248 }
249
250 /*
251  * LocalBufferSync -
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.
255  */
256 void
257 LocalBufferSync(void)
258 {
259         int                     i;
260
261         for (i = 0; i < NLocBuffer; i++)
262         {
263                 BufferDesc *buf = &LocalBufferDescriptors[i];
264                 Relation        bufrel;
265
266                 if (buf->flags & BM_DIRTY)
267                 {
268 #ifdef LBDEBUG
269                         fprintf(stderr, "LB SYNC %d\n", -i - 1);
270 #endif
271                         bufrel = RelationIdCacheGetRelation(buf->relId.relId);
272
273                         Assert(bufrel != NULL);
274
275                         smgrwrite(DEFAULT_SMGR, bufrel, buf->tag.blockNum,
276                                           (char *) MAKE_PTR(buf->data));
277                         LocalBufferFlushCount++;
278
279                         /* drop relcache refcount from RelationIdCacheGetRelation */
280                         RelationDecrementReferenceCount(bufrel);
281
282                         buf->relId.relId = InvalidOid;
283                         buf->flags &= ~BM_DIRTY;
284                 }
285         }
286
287         MemSet(LocalRefCount, 0, sizeof(long) * NLocBuffer);
288         nextFreeLocalBuf = 0;
289 }
290
291 void
292 ResetLocalBufferPool(void)
293 {
294         int                     i;
295
296         for (i = 0; i < NLocBuffer; i++)
297         {
298                 BufferDesc *buf = &LocalBufferDescriptors[i];
299
300                 buf->tag.rnode.relNode = InvalidOid;
301                 buf->flags &= ~BM_DIRTY;
302                 buf->buf_id = -i - 2;
303         }
304
305         MemSet(LocalRefCount, 0, sizeof(long) * NLocBuffer);
306         nextFreeLocalBuf = 0;
307 }