Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * slru.c
4 : : * Simple LRU buffering for wrap-around-able permanent metadata
5 : : *
6 : : * This module is used to maintain various pieces of transaction status
7 : : * indexed by TransactionId (such as commit status, parent transaction ID,
8 : : * commit timestamp), as well as storage for multixacts, serializable
9 : : * isolation locks and NOTIFY traffic. Extensions can define their own
10 : : * SLRUs, too.
11 : : *
12 : : * Under ordinary circumstances we expect that write traffic will occur
13 : : * mostly to the latest page (and to the just-prior page, soon after a
14 : : * page transition). Read traffic will probably touch a larger span of
15 : : * pages, but a relatively small number of buffers should be sufficient.
16 : : *
17 : : * We use a simple least-recently-used scheme to manage a pool of shared
18 : : * page buffers, split in banks by the lowest bits of the page number, and
19 : : * the management algorithm only processes the bank to which the desired
20 : : * page belongs, so a linear search is sufficient; there's no need for a
21 : : * hashtable or anything fancy. The algorithm is straight LRU except that
22 : : * we will never swap out the latest page (since we know it's going to be
23 : : * hit again eventually).
24 : : *
25 : : * We use per-bank control LWLocks to protect the shared data structures,
26 : : * plus per-buffer LWLocks that synchronize I/O for each buffer. The
27 : : * bank's control lock must be held to examine or modify any of the bank's
28 : : * shared state. A process that is reading in or writing out a page
29 : : * buffer does not hold the control lock, only the per-buffer lock for the
30 : : * buffer it is working on. One exception is latest_page_number, which is
31 : : * read and written using atomic ops.
32 : : *
33 : : * "Holding the bank control lock" means exclusive lock in all cases
34 : : * except for SimpleLruReadPage_ReadOnly(); see comments for
35 : : * SlruRecentlyUsed() for the implications of that.
36 : : *
37 : : * When initiating I/O on a buffer, we acquire the per-buffer lock exclusively
38 : : * before releasing the control lock. The per-buffer lock is released after
39 : : * completing the I/O, re-acquiring the control lock, and updating the shared
40 : : * state. (Deadlock is not possible here, because we never try to initiate
41 : : * I/O when someone else is already doing I/O on the same buffer.)
42 : : * To wait for I/O to complete, release the control lock, acquire the
43 : : * per-buffer lock in shared mode, immediately release the per-buffer lock,
44 : : * reacquire the control lock, and then recheck state (since arbitrary things
45 : : * could have happened while we didn't have the lock).
46 : : *
47 : : * As with the regular buffer manager, it is possible for another process
48 : : * to re-dirty a page that is currently being written out. This is handled
49 : : * by re-setting the page's page_dirty flag.
50 : : *
51 : : *
52 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
53 : : * Portions Copyright (c) 1994, Regents of the University of California
54 : : *
55 : : * src/backend/access/transam/slru.c
56 : : *
57 : : *-------------------------------------------------------------------------
58 : : */
59 : : #include "postgres.h"
60 : :
61 : : #include <fcntl.h>
62 : : #include <sys/stat.h>
63 : : #include <unistd.h>
64 : :
65 : : #include "access/slru.h"
66 : : #include "access/transam.h"
67 : : #include "access/xlog.h"
68 : : #include "access/xlogutils.h"
69 : : #include "miscadmin.h"
70 : : #include "pgstat.h"
71 : : #include "storage/fd.h"
72 : : #include "storage/shmem.h"
73 : : #include "utils/guc.h"
74 : : #include "utils/wait_event.h"
75 : :
76 : : /*
77 : : * Converts segment number to the filename of the segment.
78 : : *
79 : : * "path" should point to a buffer at least MAXPGPATH characters long.
80 : : *
81 : : * If ctl->long_segment_names is true, segno can be in the range [0, 2^60-1].
82 : : * The resulting file name is made of 15 characters, e.g. dir/123456789ABCDEF.
83 : : *
84 : : * If ctl->long_segment_names is false, segno can be in the range [0, 2^24-1].
85 : : * The resulting file name is made of 4 to 6 characters, as of:
86 : : *
87 : : * dir/1234 for [0, 2^16-1]
88 : : * dir/12345 for [2^16, 2^20-1]
89 : : * dir/123456 for [2^20, 2^24-1]
90 : : */
91 : : static inline int
837 akorotkov@postgresql 92 :CBC 20435 : SlruFileName(SlruCtl ctl, char *path, int64 segno)
93 : : {
94 [ + + ]: 20435 : if (ctl->long_segment_names)
95 : : {
96 : : /*
97 : : * We could use 16 characters here but the disadvantage would be that
98 : : * the SLRU segments will be hard to distinguish from WAL segments.
99 : : *
100 : : * For this reason we use 15 characters. It is enough but also means
101 : : * that in the future we can't decrease SLRU_PAGES_PER_SEGMENT easily.
102 : : */
103 [ + - - + ]: 16615 : Assert(segno >= 0 && segno <= INT64CONST(0xFFFFFFFFFFFFFFF));
351 peter@eisentraut.org 104 : 16615 : return snprintf(path, MAXPGPATH, "%s/%015" PRIX64, ctl->Dir, segno);
105 : : }
106 : : else
107 : : {
108 : : /*
109 : : * Despite the fact that %04X format string is used up to 24 bit
110 : : * integers are allowed. See SlruCorrectSegmentFilenameLength()
111 : : */
837 akorotkov@postgresql 112 [ + - - + ]: 3820 : Assert(segno >= 0 && segno <= INT64CONST(0xFFFFFF));
113 : 3820 : return snprintf(path, MAXPGPATH, "%s/%04X", (ctl)->Dir,
114 : : (unsigned int) segno);
115 : : }
116 : : }
117 : :
118 : : /*
119 : : * During SimpleLruWriteAll(), we will usually not need to write more than one
120 : : * or two physical files, but we may need to write several pages per file. We
121 : : * can consolidate the I/O requests by leaving files open until control returns
122 : : * to SimpleLruWriteAll(). This data structure remembers which files are open.
123 : : */
124 : : #define MAX_WRITEALL_BUFFERS 16
125 : :
126 : : typedef struct SlruWriteAllData
127 : : {
128 : : int num_files; /* # files actually open */
129 : : int fd[MAX_WRITEALL_BUFFERS]; /* their FD's */
130 : : int64 segno[MAX_WRITEALL_BUFFERS]; /* their log seg#s */
131 : : } SlruWriteAllData;
132 : :
133 : : typedef struct SlruWriteAllData *SlruWriteAll;
134 : :
135 : :
136 : : /*
137 : : * Bank size for the slot array. Pages are assigned a bank according to their
138 : : * page number, with each bank being this size. We want a power of 2 so that
139 : : * we can determine the bank number for a page with just bit shifting; we also
140 : : * want to keep the bank size small so that LRU victim search is fast. 16
141 : : * buffers per bank seems a good number.
142 : : */
143 : : #define SLRU_BANK_BITSHIFT 4
144 : : #define SLRU_BANK_SIZE (1 << SLRU_BANK_BITSHIFT)
145 : :
146 : : /*
147 : : * Macro to get the bank number to which the slot belongs.
148 : : */
149 : : #define SlotGetBankNumber(slotno) ((slotno) >> SLRU_BANK_BITSHIFT)
150 : :
151 : :
152 : : /*
153 : : * Populate a file tag describing a segment file. We only use the segment
154 : : * number, since we can derive everything else we need by having separate
155 : : * sync handler functions for clog, multixact etc.
156 : : */
157 : : #define INIT_SLRUFILETAG(a,xx_handler,xx_segno) \
158 : : ( \
159 : : memset(&(a), 0, sizeof(FileTag)), \
160 : : (a).handler = (xx_handler), \
161 : : (a).segno = (xx_segno) \
162 : : )
163 : :
164 : : /* Saved info for SlruReportIOError */
165 : : typedef enum
166 : : {
167 : : SLRU_OPEN_FAILED,
168 : : SLRU_SEEK_FAILED,
169 : : SLRU_READ_FAILED,
170 : : SLRU_WRITE_FAILED,
171 : : SLRU_FSYNC_FAILED,
172 : : SLRU_CLOSE_FAILED,
173 : : } SlruErrorCause;
174 : :
175 : : static SlruErrorCause slru_errcause;
176 : : static int slru_errno;
177 : :
178 : :
179 : : static void SimpleLruZeroLSNs(SlruCtl ctl, int slotno);
180 : : static void SimpleLruWaitIO(SlruCtl ctl, int slotno);
181 : : static void SlruInternalWritePage(SlruCtl ctl, int slotno, SlruWriteAll fdata);
182 : : static bool SlruPhysicalReadPage(SlruCtl ctl, int64 pageno, int slotno);
183 : : static bool SlruPhysicalWritePage(SlruCtl ctl, int64 pageno, int slotno,
184 : : SlruWriteAll fdata);
185 : : static void SlruReportIOError(SlruCtl ctl, int64 pageno,
186 : : const void *opaque_data);
187 : : static int SlruSelectLRUPage(SlruCtl ctl, int64 pageno);
188 : :
189 : : static bool SlruScanDirCbDeleteCutoff(SlruCtl ctl, char *filename,
190 : : int64 segpage, void *data);
191 : : static void SlruInternalDeleteSegment(SlruCtl ctl, int64 segno);
192 : : static inline void SlruRecentlyUsed(SlruShared shared, int slotno);
193 : :
194 : :
195 : : /*
196 : : * Initialization of shared memory
197 : : */
198 : :
199 : : Size
6801 tgl@sss.pgh.pa.us 200 : 31141 : SimpleLruShmemSize(int nslots, int nlsns)
201 : : {
746 alvherre@alvh.no-ip. 202 : 31141 : int nbanks = nslots / SLRU_BANK_SIZE;
203 : : Size sz;
204 : :
205 [ - + ]: 31141 : Assert(nslots <= SLRU_MAX_ALLOWED_BUFFERS);
206 [ - + ]: 31141 : Assert(nslots % SLRU_BANK_SIZE == 0);
207 : :
208 : : /* we assume nslots isn't so large as to risk overflow */
7404 tgl@sss.pgh.pa.us 209 : 31141 : sz = MAXALIGN(sizeof(SlruSharedData));
7102 bruce@momjian.us 210 : 31141 : sz += MAXALIGN(nslots * sizeof(char *)); /* page_buffer[] */
7404 tgl@sss.pgh.pa.us 211 : 31141 : sz += MAXALIGN(nslots * sizeof(SlruPageStatus)); /* page_status[] */
3189 212 : 31141 : sz += MAXALIGN(nslots * sizeof(bool)); /* page_dirty[] */
837 akorotkov@postgresql 213 : 31141 : sz += MAXALIGN(nslots * sizeof(int64)); /* page_number[] */
3189 tgl@sss.pgh.pa.us 214 : 31141 : sz += MAXALIGN(nslots * sizeof(int)); /* page_lru_count[] */
215 : 31141 : sz += MAXALIGN(nslots * sizeof(LWLockPadded)); /* buffer_locks[] */
746 alvherre@alvh.no-ip. 216 : 31141 : sz += MAXALIGN(nbanks * sizeof(LWLockPadded)); /* bank_locks[] */
217 : 31141 : sz += MAXALIGN(nbanks * sizeof(int)); /* bank_cur_lru_count[] */
218 : :
6801 tgl@sss.pgh.pa.us 219 [ + + ]: 31141 : if (nlsns > 0)
6695 bruce@momjian.us 220 : 4447 : sz += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr)); /* group_lsn[] */
221 : :
7404 tgl@sss.pgh.pa.us 222 : 31141 : return BUFFERALIGN(sz) + BLCKSZ * nslots;
223 : : }
224 : :
225 : : /*
226 : : * Determine a number of SLRU buffers to use.
227 : : *
228 : : * We simply divide shared_buffers by the divisor given and cap
229 : : * that at the maximum given; but always at least SLRU_BANK_SIZE.
230 : : * Round down to the nearest multiple of SLRU_BANK_SIZE.
231 : : */
232 : : int
746 alvherre@alvh.no-ip. 233 : 9840 : SimpleLruAutotuneBuffers(int divisor, int max)
234 : : {
235 : 9840 : return Min(max - (max % SLRU_BANK_SIZE),
236 : : Max(SLRU_BANK_SIZE,
237 : : NBuffers / divisor - (NBuffers / divisor) % SLRU_BANK_SIZE));
238 : : }
239 : :
240 : : /*
241 : : * Initialize, or attach to, a simple LRU cache in shared memory.
242 : : *
243 : : * ctl: address of local (unshared) control structure.
244 : : * name: name of SLRU. (This is user-visible, pick with care!)
245 : : * nslots: number of page slots to use.
246 : : * nlsns: number of LSN groups per page (set to zero if not relevant).
247 : : * subdir: PGDATA-relative subdirectory that will contain the files.
248 : : * buffer_tranche_id: tranche ID to use for the SLRU's per-buffer LWLocks.
249 : : * bank_tranche_id: tranche ID to use for the bank LWLocks.
250 : : * sync_handler: which set of functions to use to handle sync requests
251 : : * long_segment_names: use short or long segment names
252 : : */
253 : : void
6801 tgl@sss.pgh.pa.us 254 : 8054 : SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns,
255 : : const char *subdir, int buffer_tranche_id, int bank_tranche_id,
256 : : SyncRequestHandler sync_handler, bool long_segment_names)
257 : : {
258 : : SlruShared shared;
259 : : bool found;
746 alvherre@alvh.no-ip. 260 : 8054 : int nbanks = nslots / SLRU_BANK_SIZE;
261 : :
262 [ - + ]: 8054 : Assert(nslots <= SLRU_MAX_ALLOWED_BUFFERS);
263 : :
2 heikki.linnakangas@i 264 [ - + ]:GNC 8054 : Assert(ctl->PagePrecedes != NULL);
265 [ - + ]: 8054 : Assert(ctl->errdetail_for_io_error != NULL);
266 : :
7404 tgl@sss.pgh.pa.us 267 :CBC 8054 : shared = (SlruShared) ShmemInitStruct(name,
268 : : SimpleLruShmemSize(nslots, nlsns),
269 : : &found);
270 : :
8313 bruce@momjian.us 271 [ + - ]: 8054 : if (!IsUnderPostmaster)
272 : : {
273 : : /* Initialize locks and shared memory area */
274 : : char *ptr;
275 : : Size offset;
276 : :
277 [ - + ]: 8054 : Assert(!found);
278 : :
279 : 8054 : memset(shared, 0, sizeof(SlruSharedData));
280 : :
7404 tgl@sss.pgh.pa.us 281 : 8054 : shared->num_slots = nslots;
6801 282 : 8054 : shared->lsn_groups_per_page = nlsns;
283 : :
768 alvherre@alvh.no-ip. 284 : 8054 : pg_atomic_init_u64(&shared->latest_page_number, 0);
285 : :
1439 andres@anarazel.de 286 : 8054 : shared->slru_stats_idx = pgstat_get_slru_index(name);
287 : :
7404 tgl@sss.pgh.pa.us 288 : 8054 : ptr = (char *) shared;
289 : 8054 : offset = MAXALIGN(sizeof(SlruSharedData));
290 : 8054 : shared->page_buffer = (char **) (ptr + offset);
291 : 8054 : offset += MAXALIGN(nslots * sizeof(char *));
292 : 8054 : shared->page_status = (SlruPageStatus *) (ptr + offset);
293 : 8054 : offset += MAXALIGN(nslots * sizeof(SlruPageStatus));
294 : 8054 : shared->page_dirty = (bool *) (ptr + offset);
295 : 8054 : offset += MAXALIGN(nslots * sizeof(bool));
837 akorotkov@postgresql 296 : 8054 : shared->page_number = (int64 *) (ptr + offset);
297 : 8054 : offset += MAXALIGN(nslots * sizeof(int64));
7404 tgl@sss.pgh.pa.us 298 : 8054 : shared->page_lru_count = (int *) (ptr + offset);
299 : 8054 : offset += MAXALIGN(nslots * sizeof(int));
300 : :
301 : : /* Initialize LWLocks */
3159 teodor@sigaev.ru 302 : 8054 : shared->buffer_locks = (LWLockPadded *) (ptr + offset);
303 : 8054 : offset += MAXALIGN(nslots * sizeof(LWLockPadded));
746 alvherre@alvh.no-ip. 304 : 8054 : shared->bank_locks = (LWLockPadded *) (ptr + offset);
305 : 8054 : offset += MAXALIGN(nbanks * sizeof(LWLockPadded));
306 : 8054 : shared->bank_cur_lru_count = (int *) (ptr + offset);
307 : 8054 : offset += MAXALIGN(nbanks * sizeof(int));
308 : :
6801 tgl@sss.pgh.pa.us 309 [ + + ]: 8054 : if (nlsns > 0)
310 : : {
311 : 1150 : shared->group_lsn = (XLogRecPtr *) (ptr + offset);
312 : 1150 : offset += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr));
313 : : }
314 : :
315 : 8054 : ptr += BUFFERALIGN(offset);
748 alvherre@alvh.no-ip. 316 [ + + ]: 204934 : for (int slotno = 0; slotno < nslots; slotno++)
317 : : {
3776 rhaas@postgresql.org 318 : 196880 : LWLockInitialize(&shared->buffer_locks[slotno].lock,
319 : : buffer_tranche_id);
320 : :
7404 tgl@sss.pgh.pa.us 321 : 196880 : shared->page_buffer[slotno] = ptr;
8313 bruce@momjian.us 322 : 196880 : shared->page_status[slotno] = SLRU_PAGE_EMPTY;
7435 tgl@sss.pgh.pa.us 323 : 196880 : shared->page_dirty[slotno] = false;
7404 324 : 196880 : shared->page_lru_count[slotno] = 0;
325 : 196880 : ptr += BLCKSZ;
326 : : }
327 : :
328 : : /* Initialize the slot banks. */
746 alvherre@alvh.no-ip. 329 [ + + ]: 20359 : for (int bankno = 0; bankno < nbanks; bankno++)
330 : : {
331 : 12305 : LWLockInitialize(&shared->bank_locks[bankno].lock, bank_tranche_id);
332 : 12305 : shared->bank_cur_lru_count[bankno] = 0;
333 : : }
334 : :
335 : : /* Should fit to estimated shmem size */
3135 tgl@sss.pgh.pa.us 336 [ - + ]: 8054 : Assert(ptr - (char *) shared <= SimpleLruShmemSize(nslots, nlsns));
337 : : }
338 : : else
339 : : {
8313 bruce@momjian.us 340 [ # # ]:UBC 0 : Assert(found);
746 alvherre@alvh.no-ip. 341 [ # # ]: 0 : Assert(shared->num_slots == nslots);
342 : : }
343 : :
344 : : /*
345 : : * Initialize the unshared control struct, including directory path. We
346 : : * assume caller set PagePrecedes.
347 : : */
8313 bruce@momjian.us 348 :CBC 8054 : ctl->shared = shared;
1997 tmunro@postgresql.or 349 : 8054 : ctl->sync_handler = sync_handler;
837 akorotkov@postgresql 350 : 8054 : ctl->long_segment_names = long_segment_names;
430 alvherre@alvh.no-ip. 351 : 8054 : ctl->nbanks = nbanks;
2043 peter@eisentraut.org 352 : 8054 : strlcpy(ctl->Dir, subdir, sizeof(ctl->Dir));
8313 bruce@momjian.us 353 : 8054 : }
354 : :
355 : : /*
356 : : * Helper function for GUC check_hook to check whether slru buffers are in
357 : : * multiples of SLRU_BANK_SIZE.
358 : : */
359 : : bool
746 alvherre@alvh.no-ip. 360 : 11740 : check_slru_buffers(const char *name, int *newval)
361 : : {
362 : : /* Valid values are multiples of SLRU_BANK_SIZE */
363 [ + - ]: 11740 : if (*newval % SLRU_BANK_SIZE == 0)
364 : 11740 : return true;
365 : :
473 alvherre@alvh.no-ip. 366 :UBC 0 : GUC_check_errdetail("\"%s\" must be a multiple of %d.", name,
367 : : SLRU_BANK_SIZE);
746 368 : 0 : return false;
369 : : }
370 : :
371 : : /*
372 : : * Initialize (or reinitialize) a page to zeroes.
373 : : *
374 : : * The page is not actually written, just set up in shared memory.
375 : : * The slot number of the new page is returned.
376 : : *
377 : : * Bank lock must be held at entry, and will be held at exit.
378 : : */
379 : : int
837 akorotkov@postgresql 380 :CBC 2492 : SimpleLruZeroPage(SlruCtl ctl, int64 pageno)
381 : : {
7958 tgl@sss.pgh.pa.us 382 : 2492 : SlruShared shared = ctl->shared;
383 : : int slotno;
384 : :
746 alvherre@alvh.no-ip. 385 [ - + ]: 2492 : Assert(LWLockHeldByMeInMode(SimpleLruGetBankLock(ctl, pageno), LW_EXCLUSIVE));
386 : :
387 : : /* Find a suitable buffer slot for the page */
8313 bruce@momjian.us 388 : 2492 : slotno = SlruSelectLRUPage(ctl, pageno);
389 [ + + + - : 2492 : Assert(shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
+ + - + ]
390 : : (shared->page_status[slotno] == SLRU_PAGE_VALID &&
391 : : !shared->page_dirty[slotno]) ||
392 : : shared->page_number[slotno] == pageno);
393 : :
394 : : /* Mark the slot as containing this page */
395 : 2492 : shared->page_number[slotno] = pageno;
7435 tgl@sss.pgh.pa.us 396 : 2492 : shared->page_status[slotno] = SLRU_PAGE_VALID;
397 : 2492 : shared->page_dirty[slotno] = true;
8313 bruce@momjian.us 398 : 2492 : SlruRecentlyUsed(shared, slotno);
399 : :
400 : : /* Set the buffer to zeroes */
401 [ + - + - : 2492 : MemSet(shared->page_buffer[slotno], 0, BLCKSZ);
+ - - + -
- ]
402 : :
403 : : /* Set the LSNs for this new page to zero */
6801 tgl@sss.pgh.pa.us 404 : 2492 : SimpleLruZeroLSNs(ctl, slotno);
405 : :
406 : : /*
407 : : * Assume this page is now the latest active page.
408 : : *
409 : : * Note that because both this routine and SlruSelectLRUPage run with a
410 : : * SLRU bank lock held, it is not possible for this to be zeroing a page
411 : : * that SlruSelectLRUPage is going to evict simultaneously. Therefore,
412 : : * there's no memory barrier here.
413 : : */
768 alvherre@alvh.no-ip. 414 : 2492 : pg_atomic_write_u64(&shared->latest_page_number, pageno);
415 : :
416 : : /* update the stats counter of zeroed pages */
194 michael@paquier.xyz 417 :GNC 2492 : pgstat_count_slru_blocks_zeroed(shared->slru_stats_idx);
418 : :
8313 bruce@momjian.us 419 :CBC 2492 : return slotno;
420 : : }
421 : :
422 : : /*
423 : : * Zero all the LSNs we store for this slru page.
424 : : *
425 : : * This should be called each time we create a new page, and each time we read
426 : : * in a page from disk into an existing buffer. (Such an old page cannot
427 : : * have any interesting LSNs, since we'd have flushed them before writing
428 : : * the page in the first place.)
429 : : *
430 : : * This assumes that InvalidXLogRecPtr is bitwise-all-0.
431 : : */
432 : : static void
6801 tgl@sss.pgh.pa.us 433 : 19786 : SimpleLruZeroLSNs(SlruCtl ctl, int slotno)
434 : : {
435 : 19786 : SlruShared shared = ctl->shared;
436 : :
437 [ + + ]: 19786 : if (shared->lsn_groups_per_page > 0)
438 [ + - + - : 1028 : MemSet(&shared->group_lsn[slotno * shared->lsn_groups_per_page], 0,
+ - - + -
- ]
439 : : shared->lsn_groups_per_page * sizeof(XLogRecPtr));
440 : 19786 : }
441 : :
442 : : /*
443 : : * This is a convenience wrapper for the common case of zeroing a page and
444 : : * immediately flushing it to disk.
445 : : *
446 : : * SLRU bank lock is acquired and released here.
447 : : */
448 : : void
251 alvherre@kurilemu.de 449 :GNC 217 : SimpleLruZeroAndWritePage(SlruCtl ctl, int64 pageno)
450 : : {
451 : : int slotno;
452 : : LWLock *lock;
453 : :
454 : 217 : lock = SimpleLruGetBankLock(ctl, pageno);
455 : 217 : LWLockAcquire(lock, LW_EXCLUSIVE);
456 : :
457 : : /* Create and zero the page */
458 : 217 : slotno = SimpleLruZeroPage(ctl, pageno);
459 : :
460 : : /* Make sure it's written out */
461 : 217 : SimpleLruWritePage(ctl, slotno);
462 [ - + ]: 217 : Assert(!ctl->shared->page_dirty[slotno]);
463 : :
464 : 217 : LWLockRelease(lock);
465 : 217 : }
466 : :
467 : : /*
468 : : * Wait for any active I/O on a page slot to finish. (This does not
469 : : * guarantee that new I/O hasn't been started before we return, though.
470 : : * In fact the slot might not even contain the same page anymore.)
471 : : *
472 : : * Bank lock must be held at entry, and will be held at exit.
473 : : */
474 : : static void
7435 tgl@sss.pgh.pa.us 475 :GBC 1 : SimpleLruWaitIO(SlruCtl ctl, int slotno)
476 : : {
477 : 1 : SlruShared shared = ctl->shared;
746 alvherre@alvh.no-ip. 478 : 1 : int bankno = SlotGetBankNumber(slotno);
479 : :
740 480 [ - + ]: 1 : Assert(shared->page_status[slotno] != SLRU_PAGE_EMPTY);
481 : :
482 : : /* See notes at top of file */
746 483 : 1 : LWLockRelease(&shared->bank_locks[bankno].lock);
3776 rhaas@postgresql.org 484 : 1 : LWLockAcquire(&shared->buffer_locks[slotno].lock, LW_SHARED);
485 : 1 : LWLockRelease(&shared->buffer_locks[slotno].lock);
746 alvherre@alvh.no-ip. 486 : 1 : LWLockAcquire(&shared->bank_locks[bankno].lock, LW_EXCLUSIVE);
487 : :
488 : : /*
489 : : * If the slot is still in an io-in-progress state, then either someone
490 : : * already started a new I/O on the slot, or a previous I/O failed and
491 : : * neglected to reset the page state. That shouldn't happen, really, but
492 : : * it seems worth a few extra cycles to check and recover from it. We can
493 : : * cheaply test for failure by seeing if the buffer lock is still held (we
494 : : * assume that transaction abort would release the lock).
495 : : */
7435 tgl@sss.pgh.pa.us 496 [ + - ]: 1 : if (shared->page_status[slotno] == SLRU_PAGE_READ_IN_PROGRESS ||
497 [ - + ]: 1 : shared->page_status[slotno] == SLRU_PAGE_WRITE_IN_PROGRESS)
498 : : {
3776 rhaas@postgresql.org 499 [ # # ]:UBC 0 : if (LWLockConditionalAcquire(&shared->buffer_locks[slotno].lock, LW_SHARED))
500 : : {
501 : : /* indeed, the I/O must have failed */
7435 tgl@sss.pgh.pa.us 502 [ # # ]: 0 : if (shared->page_status[slotno] == SLRU_PAGE_READ_IN_PROGRESS)
503 : 0 : shared->page_status[slotno] = SLRU_PAGE_EMPTY;
504 : : else /* write_in_progress */
505 : : {
506 : 0 : shared->page_status[slotno] = SLRU_PAGE_VALID;
507 : 0 : shared->page_dirty[slotno] = true;
508 : : }
3776 rhaas@postgresql.org 509 : 0 : LWLockRelease(&shared->buffer_locks[slotno].lock);
510 : : }
511 : : }
7435 tgl@sss.pgh.pa.us 512 :GBC 1 : }
513 : :
514 : : /*
515 : : * Find a page in a shared buffer, reading it in if necessary.
516 : : * The page number must correspond to an already-initialized page.
517 : : *
518 : : * If write_ok is true then it is OK to return a page that is in
519 : : * WRITE_IN_PROGRESS state; it is the caller's responsibility to be sure
520 : : * that modification of the page is safe. If write_ok is false then we
521 : : * will not return the page until it is not undergoing active I/O.
522 : : *
523 : : * On error, the passed-in 'opaque_data' is passed to the
524 : : * 'errdetail_for_io_error' callback, to provide details on the operation that
525 : : * failed. It is only used for error reporting.
526 : : *
527 : : * Return value is the shared-buffer slot number now holding the page.
528 : : * The buffer's LRU access info is updated.
529 : : *
530 : : * The correct bank lock must be held at entry, and will be held at exit.
531 : : */
532 : : int
837 akorotkov@postgresql 533 :CBC 362776 : SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok,
534 : : const void *opaque_data)
535 : : {
7958 tgl@sss.pgh.pa.us 536 : 362776 : SlruShared shared = ctl->shared;
741 alvherre@alvh.no-ip. 537 : 362776 : LWLock *banklock = SimpleLruGetBankLock(ctl, pageno);
538 : :
539 [ + - ]: 362776 : Assert(LWLockHeldByMeInMode(banklock, LW_EXCLUSIVE));
540 : :
541 : : /* Outer loop handles restart if we must wait for someone else's I/O */
542 : : for (;;)
8313 bruce@momjian.us 543 :GBC 1 : {
544 : : int slotno;
545 : : bool ok;
546 : :
547 : : /* See if page already is in memory; if not, pick victim slot */
8313 bruce@momjian.us 548 :CBC 362777 : slotno = SlruSelectLRUPage(ctl, pageno);
549 : :
550 : : /* Did we find the page in memory? */
746 alvherre@alvh.no-ip. 551 [ + + ]: 362777 : if (shared->page_status[slotno] != SLRU_PAGE_EMPTY &&
552 [ + + ]: 361718 : shared->page_number[slotno] == pageno)
553 : : {
554 : : /*
555 : : * If page is still being read in, we must wait for I/O. Likewise
556 : : * if the page is being written and the caller said that's not OK.
557 : : */
6801 tgl@sss.pgh.pa.us 558 [ + - ]: 345483 : if (shared->page_status[slotno] == SLRU_PAGE_READ_IN_PROGRESS ||
559 [ + + ]: 345483 : (shared->page_status[slotno] == SLRU_PAGE_WRITE_IN_PROGRESS &&
560 [ + - ]: 1 : !write_ok))
561 : : {
7435 tgl@sss.pgh.pa.us 562 :GBC 1 : SimpleLruWaitIO(ctl, slotno);
563 : : /* Now we must recheck state from the top */
564 : 1 : continue;
565 : : }
566 : : /* Otherwise, it's ready to use */
7435 tgl@sss.pgh.pa.us 567 :CBC 345482 : SlruRecentlyUsed(shared, slotno);
568 : :
569 : : /* update the stats counter of pages found in the SLRU */
194 michael@paquier.xyz 570 :GNC 345482 : pgstat_count_slru_blocks_hit(shared->slru_stats_idx);
571 : :
7435 tgl@sss.pgh.pa.us 572 :CBC 345482 : return slotno;
573 : : }
574 : :
575 : : /* We found no match; assert we selected a freeable slot */
576 [ + + + - : 17294 : Assert(shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
- + ]
577 : : (shared->page_status[slotno] == SLRU_PAGE_VALID &&
578 : : !shared->page_dirty[slotno]));
579 : :
580 : : /* Mark the slot read-busy */
8313 bruce@momjian.us 581 : 17294 : shared->page_number[slotno] = pageno;
582 : 17294 : shared->page_status[slotno] = SLRU_PAGE_READ_IN_PROGRESS;
7435 tgl@sss.pgh.pa.us 583 : 17294 : shared->page_dirty[slotno] = false;
584 : :
585 : : /* Acquire per-buffer lock (cannot deadlock, see notes at top) */
3776 rhaas@postgresql.org 586 : 17294 : LWLockAcquire(&shared->buffer_locks[slotno].lock, LW_EXCLUSIVE);
587 : :
588 : : /* Release bank lock while doing I/O */
741 alvherre@alvh.no-ip. 589 : 17294 : LWLockRelease(banklock);
590 : :
591 : : /* Do the read */
8313 bruce@momjian.us 592 : 17294 : ok = SlruPhysicalReadPage(ctl, pageno, slotno);
593 : :
594 : : /* Set the LSNs for this newly read-in page to zero */
6801 tgl@sss.pgh.pa.us 595 : 17294 : SimpleLruZeroLSNs(ctl, slotno);
596 : :
597 : : /* Re-acquire bank control lock and update page state */
741 alvherre@alvh.no-ip. 598 : 17294 : LWLockAcquire(banklock, LW_EXCLUSIVE);
599 : :
8313 bruce@momjian.us 600 [ + - + - : 17294 : Assert(shared->page_number[slotno] == pageno &&
- + ]
601 : : shared->page_status[slotno] == SLRU_PAGE_READ_IN_PROGRESS &&
602 : : !shared->page_dirty[slotno]);
603 : :
7435 tgl@sss.pgh.pa.us 604 [ + + ]: 17294 : shared->page_status[slotno] = ok ? SLRU_PAGE_VALID : SLRU_PAGE_EMPTY;
605 : :
3776 rhaas@postgresql.org 606 : 17294 : LWLockRelease(&shared->buffer_locks[slotno].lock);
607 : :
608 : : /* Now it's okay to ereport if we failed */
8313 bruce@momjian.us 609 [ + + ]: 17294 : if (!ok)
2 heikki.linnakangas@i 610 :GNC 1 : SlruReportIOError(ctl, pageno, opaque_data);
611 : :
8313 bruce@momjian.us 612 :CBC 17293 : SlruRecentlyUsed(shared, slotno);
613 : :
614 : : /* update the stats counter of pages not found in SLRU */
194 michael@paquier.xyz 615 :GNC 17293 : pgstat_count_slru_blocks_read(shared->slru_stats_idx);
616 : :
7874 tgl@sss.pgh.pa.us 617 :CBC 17293 : return slotno;
618 : : }
619 : : }
620 : :
621 : : /*
622 : : * Find a page in a shared buffer, reading it in if necessary.
623 : : * The page number must correspond to an already-initialized page.
624 : : * The caller must intend only read-only access to the page.
625 : : *
626 : : * On error, the passed-in 'opaque_data' is passed to the
627 : : * 'errdetail_for_io_error' callback, to provide details on the operation that
628 : : * failed. It is only used for error reporting.
629 : : *
630 : : * Return value is the shared-buffer slot number now holding the page.
631 : : * The buffer's LRU access info is updated.
632 : : *
633 : : * Bank control lock must NOT be held at entry, but will be held at exit.
634 : : * It is unspecified whether the lock will be shared or exclusive.
635 : : */
636 : : int
2 heikki.linnakangas@i 637 :GNC 791058 : SimpleLruReadPage_ReadOnly(SlruCtl ctl, int64 pageno, const void *opaque_data)
638 : : {
7404 tgl@sss.pgh.pa.us 639 :CBC 791058 : SlruShared shared = ctl->shared;
741 alvherre@alvh.no-ip. 640 : 791058 : LWLock *banklock = SimpleLruGetBankLock(ctl, pageno);
430 641 : 791058 : int bankno = pageno % ctl->nbanks;
746 642 : 791058 : int bankstart = bankno * SLRU_BANK_SIZE;
643 : 791058 : int bankend = bankstart + SLRU_BANK_SIZE;
644 : :
645 : : /* Try to find the page while holding only shared lock */
741 646 : 791058 : LWLockAcquire(banklock, LW_SHARED);
647 : :
648 : : /* See if page is already in a buffer */
746 649 [ + + ]: 792200 : for (int slotno = bankstart; slotno < bankend; slotno++)
650 : : {
651 [ + + ]: 792138 : if (shared->page_status[slotno] != SLRU_PAGE_EMPTY &&
652 [ + + ]: 791179 : shared->page_number[slotno] == pageno &&
7404 tgl@sss.pgh.pa.us 653 [ + - ]: 790996 : shared->page_status[slotno] != SLRU_PAGE_READ_IN_PROGRESS)
654 : : {
655 : : /* See comments for SlruRecentlyUsed() */
656 : 790996 : SlruRecentlyUsed(shared, slotno);
657 : :
658 : : /* update the stats counter of pages found in the SLRU */
194 michael@paquier.xyz 659 :GNC 790996 : pgstat_count_slru_blocks_hit(shared->slru_stats_idx);
660 : :
7404 tgl@sss.pgh.pa.us 661 :CBC 790996 : return slotno;
662 : : }
663 : : }
664 : :
665 : : /* No luck, so switch to normal exclusive lock and do regular read */
741 alvherre@alvh.no-ip. 666 : 62 : LWLockRelease(banklock);
667 : 62 : LWLockAcquire(banklock, LW_EXCLUSIVE);
668 : :
2 heikki.linnakangas@i 669 :GNC 62 : return SimpleLruReadPage(ctl, pageno, true, opaque_data);
670 : : }
671 : :
672 : : /*
673 : : * Write a page from a shared buffer, if necessary.
674 : : * Does nothing if the specified slot is not dirty.
675 : : *
676 : : * NOTE: only one write attempt is made here. Hence, it is possible that
677 : : * the page is still dirty at exit (if someone else re-dirtied it during
678 : : * the write). However, we *do* attempt a fresh write even if the page
679 : : * is already being written; this is for checkpoints.
680 : : *
681 : : * Bank lock must be held at entry, and will be held at exit.
682 : : */
683 : : static void
1997 tmunro@postgresql.or 684 :CBC 6413 : SlruInternalWritePage(SlruCtl ctl, int slotno, SlruWriteAll fdata)
685 : : {
7874 tgl@sss.pgh.pa.us 686 : 6413 : SlruShared shared = ctl->shared;
837 akorotkov@postgresql 687 : 6413 : int64 pageno = shared->page_number[slotno];
746 alvherre@alvh.no-ip. 688 : 6413 : int bankno = SlotGetBankNumber(slotno);
689 : : bool ok;
690 : :
691 [ - + ]: 6413 : Assert(shared->page_status[slotno] != SLRU_PAGE_EMPTY);
692 [ - + ]: 6413 : Assert(LWLockHeldByMeInMode(SimpleLruGetBankLock(ctl, pageno), LW_EXCLUSIVE));
693 : :
694 : : /* If a write is in progress, wait for it to finish */
7435 tgl@sss.pgh.pa.us 695 [ - + ]: 6413 : while (shared->page_status[slotno] == SLRU_PAGE_WRITE_IN_PROGRESS &&
7435 tgl@sss.pgh.pa.us 696 [ # # ]:UBC 0 : shared->page_number[slotno] == pageno)
697 : : {
698 : 0 : SimpleLruWaitIO(ctl, slotno);
699 : : }
700 : :
701 : : /*
702 : : * Do nothing if page is not dirty, or if buffer no longer contains the
703 : : * same page we were called for.
704 : : */
7435 tgl@sss.pgh.pa.us 705 [ + + ]:CBC 6413 : if (!shared->page_dirty[slotno] ||
706 [ + - ]: 3097 : shared->page_status[slotno] != SLRU_PAGE_VALID ||
707 [ - + ]: 3097 : shared->page_number[slotno] != pageno)
8313 bruce@momjian.us 708 : 3316 : return;
709 : :
710 : : /*
711 : : * Mark the slot write-busy, and clear the dirtybit. After this point, a
712 : : * transaction status update on this page will mark it dirty again.
713 : : */
714 : 3097 : shared->page_status[slotno] = SLRU_PAGE_WRITE_IN_PROGRESS;
7435 tgl@sss.pgh.pa.us 715 : 3097 : shared->page_dirty[slotno] = false;
716 : :
717 : : /* Acquire per-buffer lock (cannot deadlock, see notes at top) */
3776 rhaas@postgresql.org 718 : 3097 : LWLockAcquire(&shared->buffer_locks[slotno].lock, LW_EXCLUSIVE);
719 : :
720 : : /* Release bank lock while doing I/O */
746 alvherre@alvh.no-ip. 721 : 3097 : LWLockRelease(&shared->bank_locks[bankno].lock);
722 : :
723 : : /* Do the write */
7958 tgl@sss.pgh.pa.us 724 : 3097 : ok = SlruPhysicalWritePage(ctl, pageno, slotno, fdata);
725 : :
726 : : /* If we failed, and we're in a flush, better close the files */
727 [ - + - - ]: 3097 : if (!ok && fdata)
728 : : {
748 alvherre@alvh.no-ip. 729 [ # # ]:UBC 0 : for (int i = 0; i < fdata->num_files; i++)
4856 heikki.linnakangas@i 730 : 0 : CloseTransientFile(fdata->fd[i]);
731 : : }
732 : :
733 : : /* Re-acquire bank lock and update page state */
746 alvherre@alvh.no-ip. 734 :CBC 3097 : LWLockAcquire(&shared->bank_locks[bankno].lock, LW_EXCLUSIVE);
735 : :
8313 bruce@momjian.us 736 [ + - - + ]: 3097 : Assert(shared->page_number[slotno] == pageno &&
737 : : shared->page_status[slotno] == SLRU_PAGE_WRITE_IN_PROGRESS);
738 : :
739 : : /* If we failed to write, mark the page dirty again */
7435 tgl@sss.pgh.pa.us 740 [ - + ]: 3097 : if (!ok)
7435 tgl@sss.pgh.pa.us 741 :UBC 0 : shared->page_dirty[slotno] = true;
742 : :
7435 tgl@sss.pgh.pa.us 743 :CBC 3097 : shared->page_status[slotno] = SLRU_PAGE_VALID;
744 : :
3776 rhaas@postgresql.org 745 : 3097 : LWLockRelease(&shared->buffer_locks[slotno].lock);
746 : :
747 : : /* Now it's okay to ereport if we failed */
8313 bruce@momjian.us 748 [ - + ]: 3097 : if (!ok)
2 heikki.linnakangas@i 749 :UNC 0 : SlruReportIOError(ctl, pageno, NULL);
750 : :
751 : : /* If part of a checkpoint, count this as a SLRU buffer written. */
1997 tmunro@postgresql.or 752 [ + + ]:CBC 3097 : if (fdata)
753 : : {
529 fujii@postgresql.org 754 : 2701 : CheckpointStats.ckpt_slru_written++;
755 : 2701 : PendingCheckpointerStats.slru_written++;
756 : : }
757 : : }
758 : :
759 : : /*
760 : : * Wrapper of SlruInternalWritePage, for external callers.
761 : : * fdata is always passed a NULL here.
762 : : */
763 : : void
5554 alvherre@alvh.no-ip. 764 : 315 : SimpleLruWritePage(SlruCtl ctl, int slotno)
765 : : {
740 766 [ - + ]: 315 : Assert(ctl->shared->page_status[slotno] != SLRU_PAGE_EMPTY);
767 : :
5554 768 : 315 : SlruInternalWritePage(ctl, slotno, NULL);
769 : 315 : }
770 : :
771 : : /*
772 : : * Return whether the given page exists on disk.
773 : : *
774 : : * A false return means that either the file does not exist, or that it's not
775 : : * large enough to contain the given page.
776 : : */
777 : : bool
837 akorotkov@postgresql 778 : 65 : SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int64 pageno)
779 : : {
780 : 65 : int64 segno = pageno / SLRU_PAGES_PER_SEGMENT;
4591 alvherre@alvh.no-ip. 781 : 65 : int rpageno = pageno % SLRU_PAGES_PER_SEGMENT;
782 : 65 : int offset = rpageno * BLCKSZ;
783 : : char path[MAXPGPATH];
784 : : int fd;
785 : : bool result;
786 : : off_t endpos;
787 : :
788 : : /* update the stats counter of checked pages */
194 michael@paquier.xyz 789 :GNC 65 : pgstat_count_slru_blocks_exists(ctl->shared->slru_stats_idx);
790 : :
4591 alvherre@alvh.no-ip. 791 :CBC 65 : SlruFileName(ctl, path, segno);
792 : :
2563 michael@paquier.xyz 793 : 65 : fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
4591 alvherre@alvh.no-ip. 794 [ + + ]: 65 : if (fd < 0)
795 : : {
796 : : /* expected: file doesn't exist */
797 [ + - ]: 25 : if (errno == ENOENT)
798 : 25 : return false;
799 : :
800 : : /* report error normally */
4591 alvherre@alvh.no-ip. 801 :UBC 0 : slru_errcause = SLRU_OPEN_FAILED;
802 : 0 : slru_errno = errno;
2 heikki.linnakangas@i 803 :UNC 0 : SlruReportIOError(ctl, pageno, NULL);
804 : : }
805 : :
4591 alvherre@alvh.no-ip. 806 [ - + ]:CBC 40 : if ((endpos = lseek(fd, 0, SEEK_END)) < 0)
807 : : {
2839 alvherre@alvh.no-ip. 808 :UBC 0 : slru_errcause = SLRU_SEEK_FAILED;
4591 809 : 0 : slru_errno = errno;
2 heikki.linnakangas@i 810 :UNC 0 : SlruReportIOError(ctl, pageno, NULL);
811 : : }
812 : :
4591 alvherre@alvh.no-ip. 813 :CBC 40 : result = endpos >= (off_t) (offset + BLCKSZ);
814 : :
2444 peter@eisentraut.org 815 [ - + ]: 40 : if (CloseTransientFile(fd) != 0)
816 : : {
2563 michael@paquier.xyz 817 :UBC 0 : slru_errcause = SLRU_CLOSE_FAILED;
818 : 0 : slru_errno = errno;
819 : 0 : return false;
820 : : }
821 : :
4591 alvherre@alvh.no-ip. 822 :CBC 40 : return result;
823 : : }
824 : :
825 : : /*
826 : : * Physical read of a (previously existing) page into a buffer slot
827 : : *
828 : : * On failure, we cannot just ereport(ERROR) since caller has put state in
829 : : * shared memory that must be undone. So, we return false and save enough
830 : : * info in static variables to let SlruReportIOError make the report.
831 : : *
832 : : * For now, assume it's not worth keeping a file pointer open across
833 : : * read/write operations. We could cache one virtual file pointer ...
834 : : */
835 : : static bool
837 akorotkov@postgresql 836 : 17294 : SlruPhysicalReadPage(SlruCtl ctl, int64 pageno, int slotno)
837 : : {
7958 tgl@sss.pgh.pa.us 838 : 17294 : SlruShared shared = ctl->shared;
837 akorotkov@postgresql 839 : 17294 : int64 segno = pageno / SLRU_PAGES_PER_SEGMENT;
8313 bruce@momjian.us 840 : 17294 : int rpageno = pageno % SLRU_PAGES_PER_SEGMENT;
2052 tmunro@postgresql.or 841 : 17294 : off_t offset = rpageno * BLCKSZ;
842 : : char path[MAXPGPATH];
843 : : int fd;
844 : :
8313 bruce@momjian.us 845 : 17294 : SlruFileName(ctl, path, segno);
846 : :
847 : : /*
848 : : * In a crash-and-restart situation, it's possible for us to receive
849 : : * commands to set the commit status of transactions whose bits are in
850 : : * already-truncated segments of the commit log (see notes in
851 : : * SlruPhysicalWritePage). Hence, if we are InRecovery, allow the case
852 : : * where the file doesn't exist, and return zeroes instead.
853 : : */
2563 michael@paquier.xyz 854 : 17294 : fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
8313 bruce@momjian.us 855 [ + + ]: 17294 : if (fd < 0)
856 : : {
8313 bruce@momjian.us 857 [ + - + - ]:GBC 1 : if (errno != ENOENT || !InRecovery)
858 : : {
859 : 1 : slru_errcause = SLRU_OPEN_FAILED;
860 : 1 : slru_errno = errno;
861 : 1 : return false;
862 : : }
863 : :
8275 tgl@sss.pgh.pa.us 864 [ # # ]:UBC 0 : ereport(LOG,
865 : : (errmsg("file \"%s\" doesn't exist, reading as zeroes",
866 : : path)));
8313 bruce@momjian.us 867 [ # # # # : 0 : MemSet(shared->page_buffer[slotno], 0, BLCKSZ);
# # # # #
# ]
868 : 0 : return true;
869 : : }
870 : :
8313 bruce@momjian.us 871 :CBC 17293 : errno = 0;
3284 rhaas@postgresql.org 872 : 17293 : pgstat_report_wait_start(WAIT_EVENT_SLRU_READ);
1263 tmunro@postgresql.or 873 [ - + ]: 17293 : if (pg_pread(fd, shared->page_buffer[slotno], BLCKSZ, offset) != BLCKSZ)
874 : : {
3284 rhaas@postgresql.org 875 :UBC 0 : pgstat_report_wait_end();
8313 bruce@momjian.us 876 : 0 : slru_errcause = SLRU_READ_FAILED;
877 : 0 : slru_errno = errno;
4856 heikki.linnakangas@i 878 : 0 : CloseTransientFile(fd);
8313 bruce@momjian.us 879 : 0 : return false;
880 : : }
3284 rhaas@postgresql.org 881 :CBC 17293 : pgstat_report_wait_end();
882 : :
2444 peter@eisentraut.org 883 [ - + ]: 17293 : if (CloseTransientFile(fd) != 0)
884 : : {
8084 tgl@sss.pgh.pa.us 885 :UBC 0 : slru_errcause = SLRU_CLOSE_FAILED;
886 : 0 : slru_errno = errno;
887 : 0 : return false;
888 : : }
889 : :
8313 bruce@momjian.us 890 :CBC 17293 : return true;
891 : : }
892 : :
893 : : /*
894 : : * Physical write of a page from a buffer slot
895 : : *
896 : : * On failure, we cannot just ereport(ERROR) since caller has put state in
897 : : * shared memory that must be undone. So, we return false and save enough
898 : : * info in static variables to let SlruReportIOError make the report.
899 : : *
900 : : * For now, assume it's not worth keeping a file pointer open across
901 : : * independent read/write operations. We do batch operations during
902 : : * SimpleLruWriteAll, though.
903 : : *
904 : : * fdata is NULL for a standalone write, pointer to open-file info during
905 : : * SimpleLruWriteAll.
906 : : */
907 : : static bool
837 akorotkov@postgresql 908 : 3097 : SlruPhysicalWritePage(SlruCtl ctl, int64 pageno, int slotno, SlruWriteAll fdata)
909 : : {
7958 tgl@sss.pgh.pa.us 910 : 3097 : SlruShared shared = ctl->shared;
837 akorotkov@postgresql 911 : 3097 : int64 segno = pageno / SLRU_PAGES_PER_SEGMENT;
8313 bruce@momjian.us 912 : 3097 : int rpageno = pageno % SLRU_PAGES_PER_SEGMENT;
2052 tmunro@postgresql.or 913 : 3097 : off_t offset = rpageno * BLCKSZ;
914 : : char path[MAXPGPATH];
7958 tgl@sss.pgh.pa.us 915 : 3097 : int fd = -1;
916 : :
917 : : /* update the stats counter of written pages */
194 michael@paquier.xyz 918 :GNC 3097 : pgstat_count_slru_blocks_written(shared->slru_stats_idx);
919 : :
920 : : /*
921 : : * Honor the write-WAL-before-data rule, if appropriate, so that we do not
922 : : * write out data before associated WAL records. This is the same action
923 : : * performed during FlushBuffer() in the main buffer manager.
924 : : */
6801 tgl@sss.pgh.pa.us 925 [ + + ]:CBC 3097 : if (shared->group_lsn != NULL)
926 : : {
927 : : /*
928 : : * We must determine the largest async-commit LSN for the page. This
929 : : * is a bit tedious, but since this entire function is a slow path
930 : : * anyway, it seems better to do this here than to maintain a per-page
931 : : * LSN variable (which'd need an extra comparison in the
932 : : * transaction-commit path).
933 : : */
934 : : XLogRecPtr max_lsn;
935 : : int lsnindex;
936 : :
937 : 1270 : lsnindex = slotno * shared->lsn_groups_per_page;
938 : 1270 : max_lsn = shared->group_lsn[lsnindex++];
748 alvherre@alvh.no-ip. 939 [ + + ]: 1300480 : for (int lsnoff = 1; lsnoff < shared->lsn_groups_per_page; lsnoff++)
940 : : {
6801 tgl@sss.pgh.pa.us 941 : 1299210 : XLogRecPtr this_lsn = shared->group_lsn[lsnindex++];
942 : :
4825 alvherre@alvh.no-ip. 943 [ + + ]: 1299210 : if (max_lsn < this_lsn)
6801 tgl@sss.pgh.pa.us 944 : 47375 : max_lsn = this_lsn;
945 : : }
946 : :
129 alvherre@kurilemu.de 947 [ + + ]:GNC 1270 : if (XLogRecPtrIsValid(max_lsn))
948 : : {
949 : : /*
950 : : * As noted above, elog(ERROR) is not acceptable here, so if
951 : : * XLogFlush were to fail, we must PANIC. This isn't much of a
952 : : * restriction because XLogFlush is just about all critical
953 : : * section anyway, but let's make sure.
954 : : */
6801 tgl@sss.pgh.pa.us 955 :CBC 536 : START_CRIT_SECTION();
956 : 536 : XLogFlush(max_lsn);
957 [ - + ]: 536 : END_CRIT_SECTION();
958 : : }
959 : : }
960 : :
961 : : /*
962 : : * During a SimpleLruWriteAll, we may already have the desired file open.
963 : : */
7958 964 [ + + ]: 3097 : if (fdata)
965 : : {
748 alvherre@alvh.no-ip. 966 [ + + ]: 2701 : for (int i = 0; i < fdata->num_files; i++)
967 : : {
7958 tgl@sss.pgh.pa.us 968 [ + - ]: 37 : if (fdata->segno[i] == segno)
969 : : {
970 : 37 : fd = fdata->fd[i];
971 : 37 : break;
972 : : }
973 : : }
974 : : }
975 : :
976 [ + + ]: 3097 : if (fd < 0)
977 : : {
978 : : /*
979 : : * If the file doesn't already exist, we should create it. It is
980 : : * possible for this to need to happen when writing a page that's not
981 : : * first in its segment; we assume the OS can cope with that. (Note:
982 : : * it might seem that it'd be okay to create files only when
983 : : * SimpleLruZeroPage is called for the first page of a segment.
984 : : * However, if after a crash and restart the REDO logic elects to
985 : : * replay the log from a checkpoint before the latest one, then it's
986 : : * possible that we will get commands to set transaction status of
987 : : * transactions that have already been truncated from the commit log.
988 : : * Easiest way to deal with that is to accept references to
989 : : * nonexistent files here and in SlruPhysicalReadPage.)
990 : : *
991 : : * Note: it is possible for more than one backend to be executing this
992 : : * code simultaneously for different pages of the same file. Hence,
993 : : * don't use O_EXCL or O_TRUNC or anything like that.
994 : : */
995 : 3060 : SlruFileName(ctl, path, segno);
3095 peter_e@gmx.net 996 : 3060 : fd = OpenTransientFile(path, O_RDWR | O_CREAT | PG_BINARY);
8313 bruce@momjian.us 997 [ - + ]: 3060 : if (fd < 0)
998 : : {
7358 tgl@sss.pgh.pa.us 999 :UBC 0 : slru_errcause = SLRU_OPEN_FAILED;
1000 : 0 : slru_errno = errno;
1001 : 0 : return false;
1002 : : }
1003 : :
7958 tgl@sss.pgh.pa.us 1004 [ + + ]:CBC 3060 : if (fdata)
1005 : : {
1997 tmunro@postgresql.or 1006 [ + - ]: 2664 : if (fdata->num_files < MAX_WRITEALL_BUFFERS)
1007 : : {
7404 tgl@sss.pgh.pa.us 1008 : 2664 : fdata->fd[fdata->num_files] = fd;
1009 : 2664 : fdata->segno[fdata->num_files] = segno;
1010 : 2664 : fdata->num_files++;
1011 : : }
1012 : : else
1013 : : {
1014 : : /*
1015 : : * In the unlikely event that we exceed MAX_WRITEALL_BUFFERS,
1016 : : * fall back to treating it as a standalone write.
1017 : : */
7404 tgl@sss.pgh.pa.us 1018 :UBC 0 : fdata = NULL;
1019 : : }
1020 : : }
1021 : : }
1022 : :
8313 bruce@momjian.us 1023 :CBC 3097 : errno = 0;
3284 rhaas@postgresql.org 1024 : 3097 : pgstat_report_wait_start(WAIT_EVENT_SLRU_WRITE);
1263 tmunro@postgresql.or 1025 [ - + ]: 3097 : if (pg_pwrite(fd, shared->page_buffer[slotno], BLCKSZ, offset) != BLCKSZ)
1026 : : {
3284 rhaas@postgresql.org 1027 :UBC 0 : pgstat_report_wait_end();
1028 : : /* if write didn't set errno, assume problem is no disk space */
8313 bruce@momjian.us 1029 [ # # ]: 0 : if (errno == 0)
1030 : 0 : errno = ENOSPC;
1031 : 0 : slru_errcause = SLRU_WRITE_FAILED;
1032 : 0 : slru_errno = errno;
7958 tgl@sss.pgh.pa.us 1033 [ # # ]: 0 : if (!fdata)
4856 heikki.linnakangas@i 1034 : 0 : CloseTransientFile(fd);
8313 bruce@momjian.us 1035 : 0 : return false;
1036 : : }
3284 rhaas@postgresql.org 1037 :CBC 3097 : pgstat_report_wait_end();
1038 : :
1039 : : /* Queue up a sync request for the checkpointer. */
1997 tmunro@postgresql.or 1040 [ + + ]: 3097 : if (ctl->sync_handler != SYNC_HANDLER_NONE)
1041 : : {
1042 : : FileTag tag;
1043 : :
1044 : 2142 : INIT_SLRUFILETAG(tag, ctl->sync_handler, segno);
1045 [ + + ]: 2142 : if (!RegisterSyncRequest(&tag, SYNC_REQUEST, false))
1046 : : {
1047 : : /* No space to enqueue sync request. Do it synchronously. */
1048 : 1 : pgstat_report_wait_start(WAIT_EVENT_SLRU_SYNC);
1049 [ - + ]: 1 : if (pg_fsync(fd) != 0)
1050 : : {
1997 tmunro@postgresql.or 1051 :UBC 0 : pgstat_report_wait_end();
1052 : 0 : slru_errcause = SLRU_FSYNC_FAILED;
1053 : 0 : slru_errno = errno;
1054 : 0 : CloseTransientFile(fd);
1055 : 0 : return false;
1056 : : }
3284 rhaas@postgresql.org 1057 :CBC 1 : pgstat_report_wait_end();
1058 : : }
1059 : : }
1060 : :
1061 : : /* Close file, unless part of flush request. */
1997 tmunro@postgresql.or 1062 [ + + ]: 3097 : if (!fdata)
1063 : : {
2444 peter@eisentraut.org 1064 [ - + ]: 396 : if (CloseTransientFile(fd) != 0)
1065 : : {
7958 tgl@sss.pgh.pa.us 1066 :UBC 0 : slru_errcause = SLRU_CLOSE_FAILED;
1067 : 0 : slru_errno = errno;
1068 : 0 : return false;
1069 : : }
1070 : : }
1071 : :
8313 bruce@momjian.us 1072 :CBC 3097 : return true;
1073 : : }
1074 : :
1075 : : /*
1076 : : * Issue the error message after failure of SlruPhysicalReadPage or
1077 : : * SlruPhysicalWritePage. Call this after cleaning up shared-memory state.
1078 : : */
1079 : : static void
2 heikki.linnakangas@i 1080 :GNC 1 : SlruReportIOError(SlruCtl ctl, int64 pageno, const void *opaque_data)
1081 : : {
837 akorotkov@postgresql 1082 :GBC 1 : int64 segno = pageno / SLRU_PAGES_PER_SEGMENT;
8313 bruce@momjian.us 1083 : 1 : int rpageno = pageno % SLRU_PAGES_PER_SEGMENT;
1084 : 1 : int offset = rpageno * BLCKSZ;
1085 : : char path[MAXPGPATH];
1086 : :
1087 : 1 : SlruFileName(ctl, path, segno);
1088 : 1 : errno = slru_errno;
1089 [ + - - - : 1 : switch (slru_errcause)
- - - ]
1090 : : {
1091 : 1 : case SLRU_OPEN_FAILED:
8275 tgl@sss.pgh.pa.us 1092 [ + - + - ]: 1 : ereport(ERROR,
1093 : : (errcode_for_file_access(),
1094 : : errmsg("could not open file \"%s\": %m", path),
1095 : : opaque_data ? ctl->errdetail_for_io_error(opaque_data) : 0));
1096 : : break;
8313 bruce@momjian.us 1097 :UBC 0 : case SLRU_SEEK_FAILED:
8275 tgl@sss.pgh.pa.us 1098 [ # # # # ]: 0 : ereport(ERROR,
1099 : : (errcode_for_file_access(),
1100 : : errmsg("could not seek in file \"%s\" to offset %d: %m",
1101 : : path, offset),
1102 : : opaque_data ? ctl->errdetail_for_io_error(opaque_data) : 0));
1103 : : break;
8313 bruce@momjian.us 1104 : 0 : case SLRU_READ_FAILED:
2385 peter@eisentraut.org 1105 [ # # ]: 0 : if (errno)
1106 [ # # # # ]: 0 : ereport(ERROR,
1107 : : (errcode_for_file_access(),
1108 : : errmsg("could not read from file \"%s\" at offset %d: %m",
1109 : : path, offset),
1110 : : opaque_data ? ctl->errdetail_for_io_error(opaque_data) : 0));
1111 : : else
1112 [ # # # # ]: 0 : ereport(ERROR,
1113 : : (errmsg("could not read from file \"%s\" at offset %d: read too few bytes",
1114 : : path, offset),
1115 : : opaque_data ? ctl->errdetail_for_io_error(opaque_data) : 0));
1116 : : break;
8313 bruce@momjian.us 1117 : 0 : case SLRU_WRITE_FAILED:
2385 peter@eisentraut.org 1118 [ # # ]: 0 : if (errno)
1119 [ # # # # ]: 0 : ereport(ERROR,
1120 : : (errcode_for_file_access(),
1121 : : errmsg("Could not write to file \"%s\" at offset %d: %m",
1122 : : path, offset),
1123 : : opaque_data ? ctl->errdetail_for_io_error(opaque_data) : 0));
1124 : : else
1125 [ # # # # ]: 0 : ereport(ERROR,
1126 : : (errmsg("Could not write to file \"%s\" at offset %d: wrote too few bytes.",
1127 : : path, offset),
1128 : : opaque_data ? ctl->errdetail_for_io_error(opaque_data) : 0));
1129 : : break;
7958 tgl@sss.pgh.pa.us 1130 : 0 : case SLRU_FSYNC_FAILED:
2673 tmunro@postgresql.or 1131 [ # # # # ]: 0 : ereport(data_sync_elevel(ERROR),
1132 : : (errcode_for_file_access(),
1133 : : errmsg("could not fsync file \"%s\": %m",
1134 : : path),
1135 : : opaque_data ? ctl->errdetail_for_io_error(opaque_data) : 0));
7958 tgl@sss.pgh.pa.us 1136 : 0 : break;
8084 1137 : 0 : case SLRU_CLOSE_FAILED:
1138 [ # # # # ]: 0 : ereport(ERROR,
1139 : : (errcode_for_file_access(),
1140 : : errmsg("could not close file \"%s\": %m",
1141 : : path),
1142 : : opaque_data ? ctl->errdetail_for_io_error(opaque_data) : 0));
1143 : : break;
8313 bruce@momjian.us 1144 : 0 : default:
1145 : : /* can't get here, we trust */
8275 tgl@sss.pgh.pa.us 1146 [ # # ]: 0 : elog(ERROR, "unrecognized SimpleLru error cause: %d",
1147 : : (int) slru_errcause);
1148 : : break;
1149 : : }
8313 bruce@momjian.us 1150 : 0 : }
1151 : :
1152 : : /*
1153 : : * Mark a buffer slot "most recently used".
1154 : : */
1155 : : static inline void
746 alvherre@alvh.no-ip. 1156 :CBC 1156263 : SlruRecentlyUsed(SlruShared shared, int slotno)
1157 : : {
1158 : 1156263 : int bankno = SlotGetBankNumber(slotno);
1159 : 1156263 : int new_lru_count = shared->bank_cur_lru_count[bankno];
1160 : :
1161 [ - + ]: 1156263 : Assert(shared->page_status[slotno] != SLRU_PAGE_EMPTY);
1162 : :
1163 : : /*
1164 : : * The reason for the if-test is that there are often many consecutive
1165 : : * accesses to the same page (particularly the latest page). By
1166 : : * suppressing useless increments of bank_cur_lru_count, we reduce the
1167 : : * probability that old pages' counts will "wrap around" and make them
1168 : : * appear recently used.
1169 : : *
1170 : : * We allow this code to be executed concurrently by multiple processes
1171 : : * within SimpleLruReadPage_ReadOnly(). As long as int reads and writes
1172 : : * are atomic, this should not cause any completely-bogus values to enter
1173 : : * the computation. However, it is possible for either bank_cur_lru_count
1174 : : * or individual page_lru_count entries to be "reset" to lower values than
1175 : : * they should have, in case a process is delayed while it executes this
1176 : : * function. With care in SlruSelectLRUPage(), this does little harm, and
1177 : : * in any case the absolute worst possible consequence is a nonoptimal
1178 : : * choice of page to evict. The gain from allowing concurrent reads of
1179 : : * SLRU pages seems worth it.
1180 : : */
1181 [ + + ]: 1156263 : if (new_lru_count != shared->page_lru_count[slotno])
1182 : : {
1183 : 140101 : shared->bank_cur_lru_count[bankno] = ++new_lru_count;
1184 : 140101 : shared->page_lru_count[slotno] = new_lru_count;
1185 : : }
1186 : 1156263 : }
1187 : :
1188 : : /*
1189 : : * Select the slot to re-use when we need a free slot for the given page.
1190 : : *
1191 : : * The target page number is passed not only because we need to know the
1192 : : * correct bank to use, but also because we need to consider the possibility
1193 : : * that some other process reads in the target page while we are doing I/O to
1194 : : * free a slot. Hence, check or recheck to see if any slot already holds the
1195 : : * target page, and return that slot if so. Thus, the returned slot is
1196 : : * *either* a slot already holding the pageno (could be any state except
1197 : : * EMPTY), *or* a freeable slot (state EMPTY or CLEAN).
1198 : : *
1199 : : * The correct bank lock must be held at entry, and will be held at exit.
1200 : : */
1201 : : static int
837 akorotkov@postgresql 1202 : 365269 : SlruSelectLRUPage(SlruCtl ctl, int64 pageno)
1203 : : {
7958 tgl@sss.pgh.pa.us 1204 : 365269 : SlruShared shared = ctl->shared;
1205 : :
1206 : : /* Outer loop handles restart after I/O */
1207 : : for (;;)
8313 bruce@momjian.us 1208 : 61 : {
1209 : : int cur_count;
5026 1210 : 365330 : int bestvalidslot = 0; /* keep compiler quiet */
5089 rhaas@postgresql.org 1211 : 365330 : int best_valid_delta = -1;
837 akorotkov@postgresql 1212 : 365330 : int64 best_valid_page_number = 0; /* keep compiler quiet */
3189 tgl@sss.pgh.pa.us 1213 : 365330 : int bestinvalidslot = 0; /* keep compiler quiet */
5089 rhaas@postgresql.org 1214 : 365330 : int best_invalid_delta = -1;
837 akorotkov@postgresql 1215 : 365330 : int64 best_invalid_page_number = 0; /* keep compiler quiet */
430 alvherre@alvh.no-ip. 1216 : 365330 : int bankno = pageno % ctl->nbanks;
746 1217 : 365330 : int bankstart = bankno * SLRU_BANK_SIZE;
1218 : 365330 : int bankend = bankstart + SLRU_BANK_SIZE;
1219 : :
741 1220 [ - + ]: 365330 : Assert(LWLockHeldByMe(SimpleLruGetBankLock(ctl, pageno)));
1221 : :
1222 : : /* See if page already has a buffer assigned */
574 1223 [ + + ]: 1317278 : for (int slotno = bankstart; slotno < bankend; slotno++)
1224 : : {
746 1225 [ + + ]: 1297632 : if (shared->page_status[slotno] != SLRU_PAGE_EMPTY &&
1226 [ + + ]: 1246935 : shared->page_number[slotno] == pageno)
8313 bruce@momjian.us 1227 : 345684 : return slotno;
1228 : : }
1229 : :
1230 : : /*
1231 : : * If we find any EMPTY slot, just select that one. Else choose a
1232 : : * victim page to replace. We normally take the least recently used
1233 : : * valid page, but we will never take the slot containing
1234 : : * latest_page_number, even if it appears least recently used. We
1235 : : * will select a slot that is already I/O busy only if there is no
1236 : : * other choice: a read-busy slot will not be least recently used once
1237 : : * the read finishes, and waiting for an I/O on a write-busy slot is
1238 : : * inferior to just picking some other slot. Testing shows the slot
1239 : : * we pick instead will often be clean, allowing us to begin a read at
1240 : : * once.
1241 : : *
1242 : : * Normally the page_lru_count values will all be different and so
1243 : : * there will be a well-defined LRU page. But since we allow
1244 : : * concurrent execution of SlruRecentlyUsed() within
1245 : : * SimpleLruReadPage_ReadOnly(), it is possible that multiple pages
1246 : : * acquire the same lru_count values. In that case we break ties by
1247 : : * choosing the furthest-back page.
1248 : : *
1249 : : * Notice that this next line forcibly advances cur_lru_count to a
1250 : : * value that is certainly beyond any value that will be in the
1251 : : * page_lru_count array after the loop finishes. This ensures that
1252 : : * the next execution of SlruRecentlyUsed will mark the page newly
1253 : : * used, even if it's for a page that has the current counter value.
1254 : : * That gets us back on the path to having good data when there are
1255 : : * multiple pages with the same lru_count.
1256 : : */
746 alvherre@alvh.no-ip. 1257 : 19646 : cur_count = (shared->bank_cur_lru_count[bankno])++;
1258 [ + + ]: 283345 : for (int slotno = bankstart; slotno < bankend; slotno++)
1259 : : {
1260 : : int this_delta;
1261 : : int64 this_page_number;
1262 : :
8313 bruce@momjian.us 1263 [ + + ]: 266935 : if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
1264 : 3236 : return slotno;
1265 : :
7404 tgl@sss.pgh.pa.us 1266 : 263699 : this_delta = cur_count - shared->page_lru_count[slotno];
1267 [ - + ]: 263699 : if (this_delta < 0)
1268 : : {
1269 : : /*
1270 : : * Clean up in case shared updates have caused cur_count
1271 : : * increments to get "lost". We back off the page counts,
1272 : : * rather than trying to increase cur_count, to avoid any
1273 : : * question of infinite loops or failure in the presence of
1274 : : * wrapped-around counts.
1275 : : */
7404 tgl@sss.pgh.pa.us 1276 :UBC 0 : shared->page_lru_count[slotno] = cur_count;
1277 : 0 : this_delta = 0;
1278 : : }
1279 : :
1280 : : /*
1281 : : * If this page is the one most recently zeroed, don't consider it
1282 : : * an eviction candidate. See comments in SimpleLruZeroPage for an
1283 : : * explanation about the lack of a memory barrier here.
1284 : : */
7404 tgl@sss.pgh.pa.us 1285 :CBC 263699 : this_page_number = shared->page_number[slotno];
768 alvherre@alvh.no-ip. 1286 [ + + ]: 263699 : if (this_page_number ==
1287 : 263699 : pg_atomic_read_u64(&shared->latest_page_number))
5089 rhaas@postgresql.org 1288 : 8592 : continue;
1289 : :
1290 [ + - ]: 255107 : if (shared->page_status[slotno] == SLRU_PAGE_VALID)
1291 : : {
1292 [ + + - + ]: 255107 : if (this_delta > best_valid_delta ||
5089 rhaas@postgresql.org 1293 [ # # ]:UBC 0 : (this_delta == best_valid_delta &&
1294 : 0 : ctl->PagePrecedes(this_page_number,
1295 : : best_valid_page_number)))
1296 : : {
5089 rhaas@postgresql.org 1297 :CBC 52006 : bestvalidslot = slotno;
1298 : 52006 : best_valid_delta = this_delta;
1299 : 52006 : best_valid_page_number = this_page_number;
1300 : : }
1301 : : }
1302 : : else
1303 : : {
5089 rhaas@postgresql.org 1304 [ # # # # ]:UBC 0 : if (this_delta > best_invalid_delta ||
1305 [ # # ]: 0 : (this_delta == best_invalid_delta &&
1306 : 0 : ctl->PagePrecedes(this_page_number,
1307 : : best_invalid_page_number)))
1308 : : {
1309 : 0 : bestinvalidslot = slotno;
1310 : 0 : best_invalid_delta = this_delta;
1311 : 0 : best_invalid_page_number = this_page_number;
1312 : : }
1313 : : }
1314 : : }
1315 : :
1316 : : /*
1317 : : * If all pages (except possibly the latest one) are I/O busy, we'll
1318 : : * have to wait for an I/O to complete and then retry. In that
1319 : : * unhappy case, we choose to wait for the I/O on the least recently
1320 : : * used slot, on the assumption that it was likely initiated first of
1321 : : * all the I/Os in progress and may therefore finish first.
1322 : : */
5089 rhaas@postgresql.org 1323 [ - + ]:CBC 16410 : if (best_valid_delta < 0)
1324 : : {
5089 rhaas@postgresql.org 1325 :UBC 0 : SimpleLruWaitIO(ctl, bestinvalidslot);
1326 : 0 : continue;
1327 : : }
1328 : :
1329 : : /*
1330 : : * If the selected page is clean, we're set.
1331 : : */
5089 rhaas@postgresql.org 1332 [ + + ]:CBC 16410 : if (!shared->page_dirty[bestvalidslot])
1333 : 16349 : return bestvalidslot;
1334 : :
1335 : : /*
1336 : : * Write the page.
1337 : : */
1338 : 61 : SlruInternalWritePage(ctl, bestvalidslot, NULL);
1339 : :
1340 : : /*
1341 : : * Now loop back and try again. This is the easiest way of dealing
1342 : : * with corner cases such as the victim page being re-dirtied while we
1343 : : * wrote it.
1344 : : */
1345 : : }
1346 : : }
1347 : :
1348 : : /*
1349 : : * Write dirty pages to disk during checkpoint or database shutdown. Flushing
1350 : : * is deferred until the next call to ProcessSyncRequests(), though we do fsync
1351 : : * the containing directory here to make sure that newly created directory
1352 : : * entries are on disk.
1353 : : */
1354 : : void
1997 tmunro@postgresql.or 1355 : 9052 : SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
1356 : : {
7958 tgl@sss.pgh.pa.us 1357 : 9052 : SlruShared shared = ctl->shared;
1358 : : SlruWriteAllData fdata;
837 akorotkov@postgresql 1359 : 9052 : int64 pageno = 0;
746 alvherre@alvh.no-ip. 1360 : 9052 : int prevbank = SlotGetBankNumber(0);
1361 : : bool ok;
1362 : :
1363 : : /* update the stats counter of flushes */
2132 tgl@sss.pgh.pa.us 1364 : 9052 : pgstat_count_slru_flush(shared->slru_stats_idx);
1365 : :
1366 : : /*
1367 : : * Find and write dirty pages
1368 : : */
7958 1369 : 9052 : fdata.num_files = 0;
1370 : :
746 alvherre@alvh.no-ip. 1371 : 9052 : LWLockAcquire(&shared->bank_locks[prevbank].lock, LW_EXCLUSIVE);
1372 : :
748 1373 [ + + ]: 219228 : for (int slotno = 0; slotno < shared->num_slots; slotno++)
1374 : : {
746 1375 : 210176 : int curbank = SlotGetBankNumber(slotno);
1376 : :
1377 : : /*
1378 : : * If the current bank lock is not same as the previous bank lock then
1379 : : * release the previous lock and acquire the new lock.
1380 : : */
1381 [ + + ]: 210176 : if (curbank != prevbank)
1382 : : {
1383 : 4084 : LWLockRelease(&shared->bank_locks[prevbank].lock);
1384 : 4084 : LWLockAcquire(&shared->bank_locks[curbank].lock, LW_EXCLUSIVE);
1385 : 4084 : prevbank = curbank;
1386 : : }
1387 : :
1388 : : /* Do nothing if slot is unused */
1389 [ + + ]: 210176 : if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
1390 : 204159 : continue;
1391 : :
5554 1392 : 6017 : SlruInternalWritePage(ctl, slotno, &fdata);
1393 : :
1394 : : /*
1395 : : * In some places (e.g. checkpoints), we cannot assert that the slot
1396 : : * is clean now, since another process might have re-dirtied it
1397 : : * already. That's okay.
1398 : : */
3823 andres@anarazel.de 1399 [ - + - - : 6017 : Assert(allow_redirtied ||
- - - - ]
1400 : : shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
1401 : : (shared->page_status[slotno] == SLRU_PAGE_VALID &&
1402 : : !shared->page_dirty[slotno]));
1403 : : }
1404 : :
746 alvherre@alvh.no-ip. 1405 : 9052 : LWLockRelease(&shared->bank_locks[prevbank].lock);
1406 : :
1407 : : /*
1408 : : * Now close any files that were open
1409 : : */
7958 tgl@sss.pgh.pa.us 1410 : 9052 : ok = true;
748 alvherre@alvh.no-ip. 1411 [ + + ]: 11716 : for (int i = 0; i < fdata.num_files; i++)
1412 : : {
2444 peter@eisentraut.org 1413 [ - + ]: 2664 : if (CloseTransientFile(fdata.fd[i]) != 0)
1414 : : {
7958 tgl@sss.pgh.pa.us 1415 :UBC 0 : slru_errcause = SLRU_CLOSE_FAILED;
1416 : 0 : slru_errno = errno;
1417 : 0 : pageno = fdata.segno[i] * SLRU_PAGES_PER_SEGMENT;
1418 : 0 : ok = false;
1419 : : }
1420 : : }
7958 tgl@sss.pgh.pa.us 1421 [ - + ]:CBC 9052 : if (!ok)
2 heikki.linnakangas@i 1422 :UNC 0 : SlruReportIOError(ctl, pageno, NULL);
1423 : :
1424 : : /* Ensure that directory entries for new files are on disk. */
1997 tmunro@postgresql.or 1425 [ + + ]:CBC 9052 : if (ctl->sync_handler != SYNC_HANDLER_NONE)
1998 1426 : 7248 : fsync_fname(ctl->Dir, true);
8313 bruce@momjian.us 1427 : 9052 : }
1428 : :
1429 : : /*
1430 : : * Remove all segments before the one holding the passed page number
1431 : : *
1432 : : * All SLRUs prevent concurrent calls to this function, either with an LWLock
1433 : : * or by calling it only as part of a checkpoint. Mutual exclusion must begin
1434 : : * before computing cutoffPage. Mutual exclusion must end after any limit
1435 : : * update that would permit other backends to write fresh data into the
1436 : : * segment immediately preceding the one containing cutoffPage. Otherwise,
1437 : : * when the SLRU is quite full, SimpleLruTruncate() might delete that segment
1438 : : * after it has accrued freshly-written data.
1439 : : */
1440 : : void
837 akorotkov@postgresql 1441 : 1775 : SimpleLruTruncate(SlruCtl ctl, int64 cutoffPage)
1442 : : {
7958 tgl@sss.pgh.pa.us 1443 : 1775 : SlruShared shared = ctl->shared;
1444 : : int prevbank;
1445 : :
1446 : : /* update the stats counter of truncates */
2132 1447 : 1775 : pgstat_count_slru_truncate(shared->slru_stats_idx);
1448 : :
1449 : : /*
1450 : : * Scan shared memory and remove any pages preceding the cutoff page, to
1451 : : * ensure we won't rewrite them later. (Since this is normally called in
1452 : : * or just after a checkpoint, any dirty pages should have been flushed
1453 : : * already ... we're just being extra careful here.)
1454 : : */
1252 john.naylor@postgres 1455 : 1795 : restart:
1456 : :
1457 : : /*
1458 : : * An important safety check: the current endpoint page must not be
1459 : : * eligible for removal. This check is just a backstop against wraparound
1460 : : * bugs elsewhere in SLRU handling, so we don't care if we read a slightly
1461 : : * outdated value; therefore we don't add a memory barrier.
1462 : : */
768 alvherre@alvh.no-ip. 1463 [ - + ]: 1795 : if (ctl->PagePrecedes(pg_atomic_read_u64(&shared->latest_page_number),
1464 : : cutoffPage))
1465 : : {
8275 tgl@sss.pgh.pa.us 1466 [ # # ]:UBC 0 : ereport(LOG,
1467 : : (errmsg("could not truncate directory \"%s\": apparent wraparound",
1468 : : ctl->Dir)));
8313 bruce@momjian.us 1469 : 0 : return;
1470 : : }
1471 : :
746 alvherre@alvh.no-ip. 1472 :CBC 1795 : prevbank = SlotGetBankNumber(0);
1473 : 1795 : LWLockAcquire(&shared->bank_locks[prevbank].lock, LW_EXCLUSIVE);
768 1474 [ + + ]: 42380 : for (int slotno = 0; slotno < shared->num_slots; slotno++)
1475 : : {
746 1476 : 40605 : int curbank = SlotGetBankNumber(slotno);
1477 : :
1478 : : /*
1479 : : * If the current bank lock is not same as the previous bank lock then
1480 : : * release the previous lock and acquire the new lock.
1481 : : */
1482 [ + + ]: 40605 : if (curbank != prevbank)
1483 : : {
1484 : 754 : LWLockRelease(&shared->bank_locks[prevbank].lock);
1485 : 754 : LWLockAcquire(&shared->bank_locks[curbank].lock, LW_EXCLUSIVE);
1486 : 754 : prevbank = curbank;
1487 : : }
1488 : :
8313 bruce@momjian.us 1489 [ + + ]: 40605 : if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
1490 : 38650 : continue;
1491 [ + + ]: 1955 : if (!ctl->PagePrecedes(shared->page_number[slotno], cutoffPage))
1492 : 1868 : continue;
1493 : :
1494 : : /*
1495 : : * If page is clean, just change state to EMPTY (expected case).
1496 : : */
7435 tgl@sss.pgh.pa.us 1497 [ + - ]: 87 : if (shared->page_status[slotno] == SLRU_PAGE_VALID &&
1498 [ + + ]: 87 : !shared->page_dirty[slotno])
1499 : : {
8313 bruce@momjian.us 1500 : 67 : shared->page_status[slotno] = SLRU_PAGE_EMPTY;
1501 : 67 : continue;
1502 : : }
1503 : :
1504 : : /*
1505 : : * Hmm, we have (or may have) I/O operations acting on the page, so
1506 : : * we've got to wait for them to finish and then start again. This is
1507 : : * the same logic as in SlruSelectLRUPage. (XXX if page is dirty,
1508 : : * wouldn't it be OK to just discard it without writing it?
1509 : : * SlruMayDeleteSegment() uses a stricter qualification, so we might
1510 : : * not delete this page in the end; even if we don't delete it, we
1511 : : * won't have cause to read its data again. For now, keep the logic
1512 : : * the same as it was.)
1513 : : */
7435 tgl@sss.pgh.pa.us 1514 [ + - ]: 20 : if (shared->page_status[slotno] == SLRU_PAGE_VALID)
5554 alvherre@alvh.no-ip. 1515 : 20 : SlruInternalWritePage(ctl, slotno, NULL);
1516 : : else
7435 tgl@sss.pgh.pa.us 1517 :UBC 0 : SimpleLruWaitIO(ctl, slotno);
1518 : :
746 alvherre@alvh.no-ip. 1519 :CBC 20 : LWLockRelease(&shared->bank_locks[prevbank].lock);
8313 bruce@momjian.us 1520 : 20 : goto restart;
1521 : : }
1522 : :
746 alvherre@alvh.no-ip. 1523 : 1775 : LWLockRelease(&shared->bank_locks[prevbank].lock);
1524 : :
1525 : : /* Now we can remove the old segment(s) */
5282 1526 : 1775 : (void) SlruScanDirectory(ctl, SlruScanDirCbDeleteCutoff, &cutoffPage);
1527 : : }
1528 : :
1529 : : /*
1530 : : * Delete an individual SLRU segment.
1531 : : *
1532 : : * NB: This does not touch the SLRU buffers themselves, callers have to ensure
1533 : : * they either can't yet contain anything, or have already been cleaned out.
1534 : : */
1535 : : static void
837 akorotkov@postgresql 1536 : 13 : SlruInternalDeleteSegment(SlruCtl ctl, int64 segno)
1537 : : {
1538 : : char path[MAXPGPATH];
1539 : :
1540 : : /* Forget any fsync requests queued for this segment. */
1956 tmunro@postgresql.or 1541 [ + + ]: 13 : if (ctl->sync_handler != SYNC_HANDLER_NONE)
1542 : : {
1543 : : FileTag tag;
1544 : :
1545 : 7 : INIT_SLRUFILETAG(tag, ctl->sync_handler, segno);
1546 : 7 : RegisterSyncRequest(&tag, SYNC_FORGET_REQUEST, true);
1547 : : }
1548 : :
1549 : : /* Unlink the file. */
1550 : 13 : SlruFileName(ctl, path, segno);
1852 peter@eisentraut.org 1551 [ - + ]: 13 : ereport(DEBUG2, (errmsg_internal("removing file \"%s\"", path)));
4455 alvherre@alvh.no-ip. 1552 : 13 : unlink(path);
1553 : 13 : }
1554 : :
1555 : : /*
1556 : : * Delete an individual SLRU segment, identified by the segment number.
1557 : : */
1558 : : void
837 akorotkov@postgresql 1559 : 2 : SlruDeleteSegment(SlruCtl ctl, int64 segno)
1560 : : {
3823 andres@anarazel.de 1561 : 2 : SlruShared shared = ctl->shared;
746 alvherre@alvh.no-ip. 1562 : 2 : int prevbank = SlotGetBankNumber(0);
1563 : : bool did_write;
1564 : :
1565 : : /* Clean out any possibly existing references to the segment. */
1566 : 2 : LWLockAcquire(&shared->bank_locks[prevbank].lock, LW_EXCLUSIVE);
3823 andres@anarazel.de 1567 : 2 : restart:
1568 : 2 : did_write = false;
748 alvherre@alvh.no-ip. 1569 [ + + ]: 34 : for (int slotno = 0; slotno < shared->num_slots; slotno++)
1570 : : {
1571 : : int64 pagesegno;
746 1572 : 32 : int curbank = SlotGetBankNumber(slotno);
1573 : :
1574 : : /*
1575 : : * If the current bank lock is not same as the previous bank lock then
1576 : : * release the previous lock and acquire the new lock.
1577 : : */
1578 [ - + ]: 32 : if (curbank != prevbank)
1579 : : {
746 alvherre@alvh.no-ip. 1580 :UBC 0 : LWLockRelease(&shared->bank_locks[prevbank].lock);
1581 : 0 : LWLockAcquire(&shared->bank_locks[curbank].lock, LW_EXCLUSIVE);
1582 : 0 : prevbank = curbank;
1583 : : }
1584 : :
3823 andres@anarazel.de 1585 [ - + ]:CBC 32 : if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
3823 andres@anarazel.de 1586 :UBC 0 : continue;
1587 : :
746 alvherre@alvh.no-ip. 1588 :CBC 32 : pagesegno = shared->page_number[slotno] / SLRU_PAGES_PER_SEGMENT;
1589 : : /* not the segment we're looking for */
3823 andres@anarazel.de 1590 [ + + ]: 32 : if (pagesegno != segno)
1591 : 7 : continue;
1592 : :
1593 : : /* If page is clean, just change state to EMPTY (expected case). */
1594 [ + - ]: 25 : if (shared->page_status[slotno] == SLRU_PAGE_VALID &&
1595 [ + - ]: 25 : !shared->page_dirty[slotno])
1596 : : {
1597 : 25 : shared->page_status[slotno] = SLRU_PAGE_EMPTY;
1598 : 25 : continue;
1599 : : }
1600 : :
1601 : : /* Same logic as SimpleLruTruncate() */
3823 andres@anarazel.de 1602 [ # # ]:UBC 0 : if (shared->page_status[slotno] == SLRU_PAGE_VALID)
1603 : 0 : SlruInternalWritePage(ctl, slotno, NULL);
1604 : : else
1605 : 0 : SimpleLruWaitIO(ctl, slotno);
1606 : :
1607 : 0 : did_write = true;
1608 : : }
1609 : :
1610 : : /*
1611 : : * Be extra careful and re-check. The IO functions release the control
1612 : : * lock, so new pages could have been read in.
1613 : : */
3823 andres@anarazel.de 1614 [ - + ]:CBC 2 : if (did_write)
3823 andres@anarazel.de 1615 :UBC 0 : goto restart;
1616 : :
1956 tmunro@postgresql.or 1617 :CBC 2 : SlruInternalDeleteSegment(ctl, segno);
1618 : :
746 alvherre@alvh.no-ip. 1619 : 2 : LWLockRelease(&shared->bank_locks[prevbank].lock);
3823 andres@anarazel.de 1620 : 2 : }
1621 : :
1622 : : /*
1623 : : * Determine whether a segment is okay to delete.
1624 : : *
1625 : : * segpage is the first page of the segment, and cutoffPage is the oldest (in
1626 : : * PagePrecedes order) page in the SLRU containing still-useful data. Since
1627 : : * every core PagePrecedes callback implements "wrap around", check the
1628 : : * segment's first and last pages:
1629 : : *
1630 : : * first<cutoff && last<cutoff: yes
1631 : : * first<cutoff && last>=cutoff: no; cutoff falls inside this segment
1632 : : * first>=cutoff && last<cutoff: no; wrap point falls inside this segment
1633 : : * first>=cutoff && last>=cutoff: no; every page of this segment is too young
1634 : : */
1635 : : static bool
837 akorotkov@postgresql 1636 : 36380 : SlruMayDeleteSegment(SlruCtl ctl, int64 segpage, int64 cutoffPage)
1637 : : {
1638 : 36380 : int64 seg_last_page = segpage + SLRU_PAGES_PER_SEGMENT - 1;
1639 : :
1884 noah@leadboat.com 1640 [ - + ]: 36380 : Assert(segpage % SLRU_PAGES_PER_SEGMENT == 0);
1641 : :
1642 [ + + + + ]: 36767 : return (ctl->PagePrecedes(segpage, cutoffPage) &&
1643 : 387 : ctl->PagePrecedes(seg_last_page, cutoffPage));
1644 : : }
1645 : :
1646 : : #ifdef USE_ASSERT_CHECKING
1647 : : static void
1648 : 17250 : SlruPagePrecedesTestOffset(SlruCtl ctl, int per_page, uint32 offset)
1649 : : {
1650 : : TransactionId lhs,
1651 : : rhs;
1652 : : int64 newestPage,
1653 : : oldestPage;
1654 : : TransactionId newestXact,
1655 : : oldestXact;
1656 : :
1657 : : /*
1658 : : * Compare an XID pair having undefined order (see RFC 1982), a pair at
1659 : : * "opposite ends" of the XID space. TransactionIdPrecedes() treats each
1660 : : * as preceding the other. If RHS is oldestXact, LHS is the first XID we
1661 : : * must not assign.
1662 : : */
1663 : 17250 : lhs = per_page + offset; /* skip first page to avoid non-normal XIDs */
1664 : 17250 : rhs = lhs + (1U << 31);
1665 [ - + ]: 17250 : Assert(TransactionIdPrecedes(lhs, rhs));
1666 [ - + ]: 17250 : Assert(TransactionIdPrecedes(rhs, lhs));
1667 [ - + ]: 17250 : Assert(!TransactionIdPrecedes(lhs - 1, rhs));
1668 [ - + ]: 17250 : Assert(TransactionIdPrecedes(rhs, lhs - 1));
1669 [ - + ]: 17250 : Assert(TransactionIdPrecedes(lhs + 1, rhs));
1670 [ - + ]: 17250 : Assert(!TransactionIdPrecedes(rhs, lhs + 1));
1671 [ - + ]: 17250 : Assert(!TransactionIdFollowsOrEquals(lhs, rhs));
1672 [ - + ]: 17250 : Assert(!TransactionIdFollowsOrEquals(rhs, lhs));
1673 [ - + ]: 17250 : Assert(!ctl->PagePrecedes(lhs / per_page, lhs / per_page));
1674 [ - + ]: 17250 : Assert(!ctl->PagePrecedes(lhs / per_page, rhs / per_page));
1675 [ - + ]: 17250 : Assert(!ctl->PagePrecedes(rhs / per_page, lhs / per_page));
1676 [ - + ]: 17250 : Assert(!ctl->PagePrecedes((lhs - per_page) / per_page, rhs / per_page));
1677 [ - + ]: 17250 : Assert(ctl->PagePrecedes(rhs / per_page, (lhs - 3 * per_page) / per_page));
1678 [ - + ]: 17250 : Assert(ctl->PagePrecedes(rhs / per_page, (lhs - 2 * per_page) / per_page));
1679 [ + + - + ]: 17250 : Assert(ctl->PagePrecedes(rhs / per_page, (lhs - 1 * per_page) / per_page)
1680 : : || (1U << 31) % per_page != 0); /* See CommitTsPagePrecedes() */
1681 [ + + - + ]: 17250 : Assert(ctl->PagePrecedes((lhs + 1 * per_page) / per_page, rhs / per_page)
1682 : : || (1U << 31) % per_page != 0);
1683 [ - + ]: 17250 : Assert(ctl->PagePrecedes((lhs + 2 * per_page) / per_page, rhs / per_page));
1684 [ - + ]: 17250 : Assert(ctl->PagePrecedes((lhs + 3 * per_page) / per_page, rhs / per_page));
1685 [ - + ]: 17250 : Assert(!ctl->PagePrecedes(rhs / per_page, (lhs + per_page) / per_page));
1686 : :
1687 : : /*
1688 : : * GetNewTransactionId() has assigned the last XID it can safely use, and
1689 : : * that XID is in the *LAST* page of the second segment. We must not
1690 : : * delete that segment.
1691 : : */
1692 : 17250 : newestPage = 2 * SLRU_PAGES_PER_SEGMENT - 1;
1693 : 17250 : newestXact = newestPage * per_page + offset;
1694 [ - + ]: 17250 : Assert(newestXact / per_page == newestPage);
1695 : 17250 : oldestXact = newestXact + 1;
1696 : 17250 : oldestXact -= 1U << 31;
1697 : 17250 : oldestPage = oldestXact / per_page;
1698 [ - + ]: 17250 : Assert(!SlruMayDeleteSegment(ctl,
1699 : : (newestPage -
1700 : : newestPage % SLRU_PAGES_PER_SEGMENT),
1701 : : oldestPage));
1702 : :
1703 : : /*
1704 : : * GetNewTransactionId() has assigned the last XID it can safely use, and
1705 : : * that XID is in the *FIRST* page of the second segment. We must not
1706 : : * delete that segment.
1707 : : */
1708 : 17250 : newestPage = SLRU_PAGES_PER_SEGMENT;
1709 : 17250 : newestXact = newestPage * per_page + offset;
1710 [ - + ]: 17250 : Assert(newestXact / per_page == newestPage);
1711 : 17250 : oldestXact = newestXact + 1;
1712 : 17250 : oldestXact -= 1U << 31;
1713 : 17250 : oldestPage = oldestXact / per_page;
1714 [ - + ]: 17250 : Assert(!SlruMayDeleteSegment(ctl,
1715 : : (newestPage -
1716 : : newestPage % SLRU_PAGES_PER_SEGMENT),
1717 : : oldestPage));
1718 : 17250 : }
1719 : :
1720 : : /*
1721 : : * Unit-test a PagePrecedes function.
1722 : : *
1723 : : * This assumes every uint32 >= FirstNormalTransactionId is a valid key. It
1724 : : * assumes each value occupies a contiguous, fixed-size region of SLRU bytes.
1725 : : * (MultiXactMemberCtl separates flags from XIDs. NotifyCtl has
1726 : : * variable-length entries, no keys, and no random access. These unit tests
1727 : : * do not apply to them.)
1728 : : */
1729 : : void
1730 : 5750 : SlruPagePrecedesUnitTests(SlruCtl ctl, int per_page)
1731 : : {
1732 : : /* Test first, middle and last entries of a page. */
1733 : 5750 : SlruPagePrecedesTestOffset(ctl, per_page, 0);
1734 : 5750 : SlruPagePrecedesTestOffset(ctl, per_page, per_page / 2);
1735 : 5750 : SlruPagePrecedesTestOffset(ctl, per_page, per_page - 1);
1736 : 5750 : }
1737 : : #endif
1738 : :
1739 : : /*
1740 : : * SlruScanDirectory callback
1741 : : * This callback reports true if there's any segment wholly prior to the
1742 : : * one containing the page passed as "data".
1743 : : */
1744 : : bool
837 akorotkov@postgresql 1745 : 104 : SlruScanDirCbReportPresence(SlruCtl ctl, char *filename, int64 segpage,
1746 : : void *data)
1747 : : {
1748 : 104 : int64 cutoffPage = *(int64 *) data;
1749 : :
1884 noah@leadboat.com 1750 [ - + ]: 104 : if (SlruMayDeleteSegment(ctl, segpage, cutoffPage))
5026 bruce@momjian.us 1751 :UBC 0 : return true; /* found one; don't iterate any more */
1752 : :
5026 bruce@momjian.us 1753 :CBC 104 : return false; /* keep going */
1754 : : }
1755 : :
1756 : : /*
1757 : : * SlruScanDirectory callback.
1758 : : * This callback deletes segments prior to the one passed in as "data".
1759 : : */
1760 : : static bool
837 akorotkov@postgresql 1761 : 1776 : SlruScanDirCbDeleteCutoff(SlruCtl ctl, char *filename, int64 segpage,
1762 : : void *data)
1763 : : {
1764 : 1776 : int64 cutoffPage = *(int64 *) data;
1765 : :
1884 noah@leadboat.com 1766 [ + + ]: 1776 : if (SlruMayDeleteSegment(ctl, segpage, cutoffPage))
1956 tmunro@postgresql.or 1767 : 3 : SlruInternalDeleteSegment(ctl, segpage / SLRU_PAGES_PER_SEGMENT);
1768 : :
5026 bruce@momjian.us 1769 : 1776 : return false; /* keep going */
1770 : : }
1771 : :
1772 : : /*
1773 : : * SlruScanDirectory callback.
1774 : : * This callback deletes all segments.
1775 : : */
1776 : : bool
837 akorotkov@postgresql 1777 : 8 : SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int64 segpage, void *data)
1778 : : {
1956 tmunro@postgresql.or 1779 : 8 : SlruInternalDeleteSegment(ctl, segpage / SLRU_PAGES_PER_SEGMENT);
1780 : :
5026 bruce@momjian.us 1781 : 8 : return false; /* keep going */
1782 : : }
1783 : :
1784 : : /*
1785 : : * An internal function used by SlruScanDirectory().
1786 : : *
1787 : : * Returns true if a file with a name of a given length may be a correct
1788 : : * SLRU segment.
1789 : : */
1790 : : static inline bool
837 akorotkov@postgresql 1791 : 9992 : SlruCorrectSegmentFilenameLength(SlruCtl ctl, size_t len)
1792 : : {
1793 [ + + ]: 9992 : if (ctl->long_segment_names)
1794 : 2315 : return (len == 15); /* see SlruFileName() */
1795 : : else
1796 : :
1797 : : /*
1798 : : * Commit 638cf09e76d allowed 5-character lengths. Later commit
1799 : : * 73c986adde5 allowed 6-character length.
1800 : : *
1801 : : * Note: There is an ongoing plan to migrate all SLRUs to 64-bit page
1802 : : * numbers, and the corresponding 15-character file names, which may
1803 : : * eventually deprecate the support for 4, 5, and 6-character names.
1804 : : */
1805 [ + + + - : 7677 : return (len == 4 || len == 5 || len == 6);
- + ]
1806 : : }
1807 : :
1808 : : /*
1809 : : * Scan the SimpleLru directory and apply a callback to each file found in it.
1810 : : *
1811 : : * If the callback returns true, the scan is stopped. The last return value
1812 : : * from the callback is returned.
1813 : : *
1814 : : * The callback receives the following arguments: 1. the SlruCtl struct for the
1815 : : * slru being truncated; 2. the filename being considered; 3. the page number
1816 : : * for the first page of that file; 4. a pointer to the opaque data given to us
1817 : : * by the caller.
1818 : : *
1819 : : * Note that the ordering in which the directory is scanned is not guaranteed.
1820 : : *
1821 : : * Note that no locking is applied.
1822 : : */
1823 : : bool
5282 alvherre@alvh.no-ip. 1824 : 4052 : SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
1825 : : {
5276 tgl@sss.pgh.pa.us 1826 : 4052 : bool retval = false;
1827 : : DIR *cldir;
1828 : : struct dirent *clde;
1829 : : int64 segno;
1830 : : int64 segpage;
1831 : :
8056 1832 : 4052 : cldir = AllocateDir(ctl->Dir);
7574 1833 [ + + ]: 14044 : while ((clde = ReadDir(cldir, ctl->Dir)) != NULL)
1834 : : {
1835 : : size_t len;
1836 : :
4455 alvherre@alvh.no-ip. 1837 : 9992 : len = strlen(clde->d_name);
1838 : :
837 akorotkov@postgresql 1839 [ + + ]: 9992 : if (SlruCorrectSegmentFilenameLength(ctl, len) &&
4455 alvherre@alvh.no-ip. 1840 [ + - ]: 1888 : strspn(clde->d_name, "0123456789ABCDEF") == len)
1841 : : {
837 akorotkov@postgresql 1842 : 1888 : segno = strtoi64(clde->d_name, NULL, 16);
8313 bruce@momjian.us 1843 : 1888 : segpage = segno * SLRU_PAGES_PER_SEGMENT;
1844 : :
5282 alvherre@alvh.no-ip. 1845 [ + + ]: 1888 : elog(DEBUG2, "SlruScanDirectory invoking callback on %s/%s",
1846 : : ctl->Dir, clde->d_name);
1847 : 1888 : retval = callback(ctl, clde->d_name, segpage, data);
1848 [ - + ]: 1888 : if (retval)
5282 alvherre@alvh.no-ip. 1849 :UBC 0 : break;
1850 : : }
1851 : : }
8056 tgl@sss.pgh.pa.us 1852 :CBC 4052 : FreeDir(cldir);
1853 : :
5282 alvherre@alvh.no-ip. 1854 : 4052 : return retval;
1855 : : }
1856 : :
1857 : : /*
1858 : : * Individual SLRUs (clog, ...) have to provide a sync.c handler function so
1859 : : * that they can provide the correct "SlruCtl" (otherwise we don't know how to
1860 : : * build the path), but they just forward to this common implementation that
1861 : : * performs the fsync.
1862 : : */
1863 : : int
1997 tmunro@postgresql.or 1864 : 2 : SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path)
1865 : : {
1866 : : int fd;
1867 : : int save_errno;
1868 : : int result;
1869 : :
1870 : 2 : SlruFileName(ctl, path, ftag->segno);
1871 : :
1872 : 2 : fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
1873 [ - + ]: 2 : if (fd < 0)
1997 tmunro@postgresql.or 1874 :UBC 0 : return -1;
1875 : :
1054 michael@paquier.xyz 1876 :CBC 2 : pgstat_report_wait_start(WAIT_EVENT_SLRU_FLUSH_SYNC);
1997 tmunro@postgresql.or 1877 : 2 : result = pg_fsync(fd);
1054 michael@paquier.xyz 1878 : 2 : pgstat_report_wait_end();
1997 tmunro@postgresql.or 1879 : 2 : save_errno = errno;
1880 : :
1881 : 2 : CloseTransientFile(fd);
1882 : :
1883 : 2 : errno = save_errno;
1884 : 2 : return result;
1885 : : }
|