OSDN Git Service

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