OSDN Git Service

Change error messages to oids come out as %u and not %d. Change has no
[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  * Copyright (c) 1994-5, Regents of the University of California
15  *
16  *
17  * IDENTIFICATION
18  *        $Header: /cvsroot/pgsql/src/backend/storage/buffer/localbuf.c,v 1.22 1999/05/10 00:45:36 momjian Exp $
19  *
20  *-------------------------------------------------------------------------
21  */
22 #include <sys/types.h>
23 #include <sys/file.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <math.h>
27 #include <signal.h>
28
29 #include "postgres.h"
30
31 /* declarations split between these three files */
32 #include "storage/buf.h"
33 #include "storage/buf_internals.h"
34 #include "storage/bufmgr.h"
35
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"
49
50 extern long int LocalBufferFlushCount;
51
52 int                     NLocBuffer = 64;
53 BufferDesc *LocalBufferDescriptors = NULL;
54 long       *LocalRefCount = NULL;
55
56 static int      nextFreeLocalBuf = 0;
57
58 /*#define LBDEBUG*/
59
60 /*
61  * LocalBufferAlloc -
62  *        allocate a local buffer. We do round robin allocation for now.
63  */
64 BufferDesc *
65 LocalBufferAlloc(Relation reln, BlockNumber blockNum, bool *foundPtr)
66 {
67         int                     i;
68         BufferDesc *bufHdr = (BufferDesc *) NULL;
69
70         if (blockNum == P_NEW)
71         {
72                 blockNum = reln->rd_nblocks;
73                 reln->rd_nblocks++;
74         }
75
76         /* a low tech search for now -- not optimized for scans */
77         for (i = 0; i < NLocBuffer; i++)
78         {
79                 if (LocalBufferDescriptors[i].tag.relId.relId == RelationGetRelid(reln) &&
80                         LocalBufferDescriptors[i].tag.blockNum == blockNum)
81                 {
82
83 #ifdef LBDEBUG
84                         fprintf(stderr, "LB ALLOC (%u,%d) %d\n",
85                                         RelationGetRelid(reln), blockNum, -i - 1);
86 #endif
87                         LocalRefCount[i]++;
88                         *foundPtr = TRUE;
89                         return &LocalBufferDescriptors[i];
90                 }
91         }
92
93 #ifdef LBDEBUG
94         fprintf(stderr, "LB ALLOC (%u,%d) %d\n",
95                         RelationGetRelid(reln), blockNum, -nextFreeLocalBuf - 1);
96 #endif
97
98         /* need to get a new buffer (round robin for now) */
99         for (i = 0; i < NLocBuffer; i++)
100         {
101                 int                     b = (nextFreeLocalBuf + i) % NLocBuffer;
102
103                 if (LocalRefCount[b] == 0)
104                 {
105                         bufHdr = &LocalBufferDescriptors[b];
106                         LocalRefCount[b]++;
107                         nextFreeLocalBuf = (b + 1) % NLocBuffer;
108                         break;
109                 }
110         }
111         if (bufHdr == NULL)
112                 elog(ERROR, "no empty local buffer.");
113
114         /*
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!
118          */
119         if (bufHdr->flags & BM_DIRTY)
120         {
121                 Relation        bufrel = RelationIdCacheGetRelation(bufHdr->tag.relId.relId);
122
123                 Assert(bufrel != NULL);
124
125                 /* flush this page */
126                 smgrwrite(DEFAULT_SMGR, bufrel, bufHdr->tag.blockNum,
127                                   (char *) MAKE_PTR(bufHdr->data));
128                 LocalBufferFlushCount++;
129                 RelationDecrementReferenceCount(bufrel);
130         }
131
132         /*
133          * it's all ours now.
134          */
135         bufHdr->tag.relId.relId = RelationGetRelid(reln);
136         bufHdr->tag.blockNum = blockNum;
137         bufHdr->flags &= ~BM_DIRTY;
138
139         /*
140          * lazy memory allocation. (see MAKE_PTR for why we need to do
141          * MAKE_OFFSET.)
142          */
143         if (bufHdr->data == (SHMEM_OFFSET) 0)
144         {
145                 char       *data = (char *) malloc(BLCKSZ);
146
147                 bufHdr->data = MAKE_OFFSET(data);
148         }
149
150         *foundPtr = FALSE;
151         return bufHdr;
152 }
153
154 /*
155  * WriteLocalBuffer -
156  *        writes out a local buffer
157  */
158 int
159 WriteLocalBuffer(Buffer buffer, bool release)
160 {
161         int                     bufid;
162
163         Assert(BufferIsLocal(buffer));
164
165 #ifdef LBDEBUG
166         fprintf(stderr, "LB WRITE %d\n", buffer);
167 #endif
168
169         bufid = -(buffer + 1);
170         LocalBufferDescriptors[bufid].flags |= BM_DIRTY;
171
172         if (release)
173         {
174                 Assert(LocalRefCount[bufid] > 0);
175                 LocalRefCount[bufid]--;
176         }
177
178         return true;
179 }
180
181 /*
182  * FlushLocalBuffer -
183  *        flushes a local buffer
184  */
185 int
186 FlushLocalBuffer(Buffer buffer, bool release)
187 {
188         int                     bufid;
189         Relation        bufrel;
190         BufferDesc *bufHdr;
191
192         Assert(BufferIsLocal(buffer));
193
194 #ifdef LBDEBUG
195         fprintf(stderr, "LB FLUSH %d\n", buffer);
196 #endif
197
198         bufid = -(buffer + 1);
199         bufHdr = &LocalBufferDescriptors[bufid];
200         bufHdr->flags &= ~BM_DIRTY;
201         bufrel = RelationIdCacheGetRelation(bufHdr->tag.relId.relId);
202
203         Assert(bufrel != NULL);
204         smgrflush(DEFAULT_SMGR, bufrel, bufHdr->tag.blockNum,
205                           (char *) MAKE_PTR(bufHdr->data));
206         LocalBufferFlushCount++;
207         RelationDecrementReferenceCount(bufrel);
208
209         Assert(LocalRefCount[bufid] > 0);
210         if (release)
211                 LocalRefCount[bufid]--;
212
213         return true;
214 }
215
216 /*
217  * InitLocalBuffer -
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.
221  */
222 void
223 InitLocalBuffer(void)
224 {
225         int                     i;
226
227         /*
228          * these aren't going away. I'm not gonna use palloc.
229          */
230         LocalBufferDescriptors =
231                 (BufferDesc *) malloc(sizeof(BufferDesc) * NLocBuffer);
232         MemSet(LocalBufferDescriptors, 0, sizeof(BufferDesc) * NLocBuffer);
233         nextFreeLocalBuf = 0;
234
235         for (i = 0; i < NLocBuffer; i++)
236         {
237                 BufferDesc *buf = &LocalBufferDescriptors[i];
238
239                 /*
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
243                  * buffer id is -1.)
244                  */
245                 buf->buf_id = -i - 2;
246         }
247
248         LocalRefCount = (long *) malloc(sizeof(long) * NLocBuffer);
249         MemSet(LocalRefCount, 0, sizeof(long) * NLocBuffer);
250 }
251
252 /*
253  * LocalBufferSync -
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.
257  */
258 void
259 LocalBufferSync(void)
260 {
261         int                     i;
262
263         for (i = 0; i < NLocBuffer; i++)
264         {
265                 BufferDesc *buf = &LocalBufferDescriptors[i];
266                 Relation        bufrel;
267
268                 if (buf->flags & BM_DIRTY)
269                 {
270 #ifdef LBDEBUG
271                         fprintf(stderr, "LB SYNC %d\n", -i - 1);
272 #endif
273                         bufrel = RelationIdCacheGetRelation(buf->tag.relId.relId);
274
275                         Assert(bufrel != NULL);
276
277                         smgrwrite(DEFAULT_SMGR, bufrel, buf->tag.blockNum,
278                                           (char *) MAKE_PTR(buf->data));
279                         LocalBufferFlushCount++;
280                         RelationDecrementReferenceCount(bufrel);
281
282                         buf->tag.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.relId.relId = 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 }