PostgreSQL Source Code  git master
md.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * md.c
4  * This code manages relations that reside on magnetic disk.
5  *
6  * Or at least, that was what the Berkeley folk had in mind when they named
7  * this file. In reality, what this code provides is an interface from
8  * the smgr API to Unix-like filesystem APIs, so it will work with any type
9  * of device for which the operating system provides filesystem support.
10  * It doesn't matter whether the bits are on spinning rust or some other
11  * storage technology.
12  *
13  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
14  * Portions Copyright (c) 1994, Regents of the University of California
15  *
16  *
17  * IDENTIFICATION
18  * src/backend/storage/smgr/md.c
19  *
20  *-------------------------------------------------------------------------
21  */
22 #include "postgres.h"
23 
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <sys/file.h>
27 
28 #include "access/xlog.h"
29 #include "access/xlogutils.h"
30 #include "commands/tablespace.h"
31 #include "common/file_utils.h"
32 #include "miscadmin.h"
33 #include "pg_trace.h"
34 #include "pgstat.h"
35 #include "postmaster/bgwriter.h"
36 #include "storage/bufmgr.h"
37 #include "storage/fd.h"
38 #include "storage/md.h"
39 #include "storage/relfilelocator.h"
40 #include "storage/smgr.h"
41 #include "storage/sync.h"
42 #include "utils/hsearch.h"
43 #include "utils/memutils.h"
44 
45 /*
46  * The magnetic disk storage manager keeps track of open file
47  * descriptors in its own descriptor pool. This is done to make it
48  * easier to support relations that are larger than the operating
49  * system's file size limit (often 2GBytes). In order to do that,
50  * we break relations up into "segment" files that are each shorter than
51  * the OS file size limit. The segment size is set by the RELSEG_SIZE
52  * configuration constant in pg_config.h.
53  *
54  * On disk, a relation must consist of consecutively numbered segment
55  * files in the pattern
56  * -- Zero or more full segments of exactly RELSEG_SIZE blocks each
57  * -- Exactly one partial segment of size 0 <= size < RELSEG_SIZE blocks
58  * -- Optionally, any number of inactive segments of size 0 blocks.
59  * The full and partial segments are collectively the "active" segments.
60  * Inactive segments are those that once contained data but are currently
61  * not needed because of an mdtruncate() operation. The reason for leaving
62  * them present at size zero, rather than unlinking them, is that other
63  * backends and/or the checkpointer might be holding open file references to
64  * such segments. If the relation expands again after mdtruncate(), such
65  * that a deactivated segment becomes active again, it is important that
66  * such file references still be valid --- else data might get written
67  * out to an unlinked old copy of a segment file that will eventually
68  * disappear.
69  *
70  * File descriptors are stored in the per-fork md_seg_fds arrays inside
71  * SMgrRelation. The length of these arrays is stored in md_num_open_segs.
72  * Note that a fork's md_num_open_segs having a specific value does not
73  * necessarily mean the relation doesn't have additional segments; we may
74  * just not have opened the next segment yet. (We could not have "all
75  * segments are in the array" as an invariant anyway, since another backend
76  * could extend the relation while we aren't looking.) We do not have
77  * entries for inactive segments, however; as soon as we find a partial
78  * segment, we assume that any subsequent segments are inactive.
79  *
80  * The entire MdfdVec array is palloc'd in the MdCxt memory context.
81  */
82 
83 typedef struct _MdfdVec
84 {
85  File mdfd_vfd; /* fd number in fd.c's pool */
86  BlockNumber mdfd_segno; /* segment number, from 0 */
88 
89 static MemoryContext MdCxt; /* context for all MdfdVec objects */
90 
91 
92 /* Populate a file tag describing an md.c segment file. */
93 #define INIT_MD_FILETAG(a,xx_rlocator,xx_forknum,xx_segno) \
94 ( \
95  memset(&(a), 0, sizeof(FileTag)), \
96  (a).handler = SYNC_HANDLER_MD, \
97  (a).rlocator = (xx_rlocator), \
98  (a).forknum = (xx_forknum), \
99  (a).segno = (xx_segno) \
100 )
101 
102 
103 /*** behavior for mdopen & _mdfd_getseg ***/
104 /* ereport if segment not present */
105 #define EXTENSION_FAIL (1 << 0)
106 /* return NULL if segment not present */
107 #define EXTENSION_RETURN_NULL (1 << 1)
108 /* create new segments as needed */
109 #define EXTENSION_CREATE (1 << 2)
110 /* create new segments if needed during recovery */
111 #define EXTENSION_CREATE_RECOVERY (1 << 3)
112 /*
113  * Allow opening segments which are preceded by segments smaller than
114  * RELSEG_SIZE, e.g. inactive segments (see above). Note that this breaks
115  * mdnblocks() and related functionality henceforth - which currently is ok,
116  * because this is only required in the checkpointer which never uses
117  * mdnblocks().
118  */
119 #define EXTENSION_DONT_CHECK_SIZE (1 << 4)
120 /* don't try to open a segment, if not already open */
121 #define EXTENSION_DONT_OPEN (1 << 5)
122 
123 
124 /* local routines */
125 static void mdunlinkfork(RelFileLocatorBackend rlocator, ForkNumber forknum,
126  bool isRedo);
127 static MdfdVec *mdopenfork(SMgrRelation reln, ForkNumber forknum, int behavior);
128 static void register_dirty_segment(SMgrRelation reln, ForkNumber forknum,
129  MdfdVec *seg);
130 static void register_unlink_segment(RelFileLocatorBackend rlocator, ForkNumber forknum,
131  BlockNumber segno);
132 static void register_forget_request(RelFileLocatorBackend rlocator, ForkNumber forknum,
133  BlockNumber segno);
134 static void _fdvec_resize(SMgrRelation reln,
135  ForkNumber forknum,
136  int nseg);
137 static char *_mdfd_segpath(SMgrRelation reln, ForkNumber forknum,
138  BlockNumber segno);
139 static MdfdVec *_mdfd_openseg(SMgrRelation reln, ForkNumber forknum,
140  BlockNumber segno, int oflags);
141 static MdfdVec *_mdfd_getseg(SMgrRelation reln, ForkNumber forknum,
142  BlockNumber blkno, bool skipFsync, int behavior);
143 static BlockNumber _mdnblocks(SMgrRelation reln, ForkNumber forknum,
144  MdfdVec *seg);
145 
146 static inline int
148 {
149  int flags = O_RDWR | PG_BINARY;
150 
152  flags |= PG_O_DIRECT;
153 
154  return flags;
155 }
156 
157 /*
158  * mdinit() -- Initialize private state for magnetic disk storage manager.
159  */
160 void
161 mdinit(void)
162 {
164  "MdSmgr",
166 }
167 
168 /*
169  * mdexists() -- Does the physical file exist?
170  *
171  * Note: this will return true for lingering files, with pending deletions
172  */
173 bool
175 {
176  /*
177  * Close it first, to ensure that we notice if the fork has been unlinked
178  * since we opened it. As an optimization, we can skip that in recovery,
179  * which already closes relations when dropping them.
180  */
181  if (!InRecovery)
182  mdclose(reln, forknum);
183 
184  return (mdopenfork(reln, forknum, EXTENSION_RETURN_NULL) != NULL);
185 }
186 
187 /*
188  * mdcreate() -- Create a new relation on magnetic disk.
189  *
190  * If isRedo is true, it's okay for the relation to exist already.
191  */
192 void
193 mdcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
194 {
195  MdfdVec *mdfd;
196  char *path;
197  File fd;
198 
199  if (isRedo && reln->md_num_open_segs[forknum] > 0)
200  return; /* created and opened already... */
201 
202  Assert(reln->md_num_open_segs[forknum] == 0);
203 
204  /*
205  * We may be using the target table space for the first time in this
206  * database, so create a per-database subdirectory if needed.
207  *
208  * XXX this is a fairly ugly violation of module layering, but this seems
209  * to be the best place to put the check. Maybe TablespaceCreateDbspace
210  * should be here and not in commands/tablespace.c? But that would imply
211  * importing a lot of stuff that smgr.c oughtn't know, either.
212  */
215  isRedo);
216 
217  path = relpath(reln->smgr_rlocator, forknum);
218 
219  fd = PathNameOpenFile(path, _mdfd_open_flags() | O_CREAT | O_EXCL);
220 
221  if (fd < 0)
222  {
223  int save_errno = errno;
224 
225  if (isRedo)
227  if (fd < 0)
228  {
229  /* be sure to report the error reported by create, not open */
230  errno = save_errno;
231  ereport(ERROR,
233  errmsg("could not create file \"%s\": %m", path)));
234  }
235  }
236 
237  pfree(path);
238 
239  _fdvec_resize(reln, forknum, 1);
240  mdfd = &reln->md_seg_fds[forknum][0];
241  mdfd->mdfd_vfd = fd;
242  mdfd->mdfd_segno = 0;
243 
244  if (!SmgrIsTemp(reln))
245  register_dirty_segment(reln, forknum, mdfd);
246 }
247 
248 /*
249  * mdunlink() -- Unlink a relation.
250  *
251  * Note that we're passed a RelFileLocatorBackend --- by the time this is called,
252  * there won't be an SMgrRelation hashtable entry anymore.
253  *
254  * forknum can be a fork number to delete a specific fork, or InvalidForkNumber
255  * to delete all forks.
256  *
257  * For regular relations, we don't unlink the first segment file of the rel,
258  * but just truncate it to zero length, and record a request to unlink it after
259  * the next checkpoint. Additional segments can be unlinked immediately,
260  * however. Leaving the empty file in place prevents that relfilenumber
261  * from being reused. The scenario this protects us from is:
262  * 1. We delete a relation (and commit, and actually remove its file).
263  * 2. We create a new relation, which by chance gets the same relfilenumber as
264  * the just-deleted one (OIDs must've wrapped around for that to happen).
265  * 3. We crash before another checkpoint occurs.
266  * During replay, we would delete the file and then recreate it, which is fine
267  * if the contents of the file were repopulated by subsequent WAL entries.
268  * But if we didn't WAL-log insertions, but instead relied on fsyncing the
269  * file after populating it (as we do at wal_level=minimal), the contents of
270  * the file would be lost forever. By leaving the empty file until after the
271  * next checkpoint, we prevent reassignment of the relfilenumber until it's
272  * safe, because relfilenumber assignment skips over any existing file.
273  *
274  * Additional segments, if any, are truncated and then unlinked. The reason
275  * for truncating is that other backends may still hold open FDs for these at
276  * the smgr level, so that the kernel can't remove the file yet. We want to
277  * reclaim the disk space right away despite that.
278  *
279  * We do not need to go through this dance for temp relations, though, because
280  * we never make WAL entries for temp rels, and so a temp rel poses no threat
281  * to the health of a regular rel that has taken over its relfilenumber.
282  * The fact that temp rels and regular rels have different file naming
283  * patterns provides additional safety. Other backends shouldn't have open
284  * FDs for them, either.
285  *
286  * We also don't do it while performing a binary upgrade. There is no reuse
287  * hazard in that case, since after a crash or even a simple ERROR, the
288  * upgrade fails and the whole cluster must be recreated from scratch.
289  * Furthermore, it is important to remove the files from disk immediately,
290  * because we may be about to reuse the same relfilenumber.
291  *
292  * All the above applies only to the relation's main fork; other forks can
293  * just be removed immediately, since they are not needed to prevent the
294  * relfilenumber from being recycled. Also, we do not carefully
295  * track whether other forks have been created or not, but just attempt to
296  * unlink them unconditionally; so we should never complain about ENOENT.
297  *
298  * If isRedo is true, it's unsurprising for the relation to be already gone.
299  * Also, we should remove the file immediately instead of queuing a request
300  * for later, since during redo there's no possibility of creating a
301  * conflicting relation.
302  *
303  * Note: we currently just never warn about ENOENT at all. We could warn in
304  * the main-fork, non-isRedo case, but it doesn't seem worth the trouble.
305  *
306  * Note: any failure should be reported as WARNING not ERROR, because
307  * we are usually not in a transaction anymore when this is called.
308  */
309 void
310 mdunlink(RelFileLocatorBackend rlocator, ForkNumber forknum, bool isRedo)
311 {
312  /* Now do the per-fork work */
313  if (forknum == InvalidForkNumber)
314  {
315  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
316  mdunlinkfork(rlocator, forknum, isRedo);
317  }
318  else
319  mdunlinkfork(rlocator, forknum, isRedo);
320 }
321 
322 /*
323  * Truncate a file to release disk space.
324  */
325 static int
326 do_truncate(const char *path)
327 {
328  int save_errno;
329  int ret;
330 
331  ret = pg_truncate(path, 0);
332 
333  /* Log a warning here to avoid repetition in callers. */
334  if (ret < 0 && errno != ENOENT)
335  {
336  save_errno = errno;
339  errmsg("could not truncate file \"%s\": %m", path)));
340  errno = save_errno;
341  }
342 
343  return ret;
344 }
345 
346 static void
347 mdunlinkfork(RelFileLocatorBackend rlocator, ForkNumber forknum, bool isRedo)
348 {
349  char *path;
350  int ret;
351  int save_errno;
352 
353  path = relpath(rlocator, forknum);
354 
355  /*
356  * Truncate and then unlink the first segment, or just register a request
357  * to unlink it later, as described in the comments for mdunlink().
358  */
359  if (isRedo || IsBinaryUpgrade || forknum != MAIN_FORKNUM ||
360  RelFileLocatorBackendIsTemp(rlocator))
361  {
362  if (!RelFileLocatorBackendIsTemp(rlocator))
363  {
364  /* Prevent other backends' fds from holding on to the disk space */
365  ret = do_truncate(path);
366 
367  /* Forget any pending sync requests for the first segment */
368  save_errno = errno;
369  register_forget_request(rlocator, forknum, 0 /* first seg */ );
370  errno = save_errno;
371  }
372  else
373  ret = 0;
374 
375  /* Next unlink the file, unless it was already found to be missing */
376  if (ret >= 0 || errno != ENOENT)
377  {
378  ret = unlink(path);
379  if (ret < 0 && errno != ENOENT)
380  {
381  save_errno = errno;
384  errmsg("could not remove file \"%s\": %m", path)));
385  errno = save_errno;
386  }
387  }
388  }
389  else
390  {
391  /* Prevent other backends' fds from holding on to the disk space */
392  ret = do_truncate(path);
393 
394  /* Register request to unlink first segment later */
395  save_errno = errno;
396  register_unlink_segment(rlocator, forknum, 0 /* first seg */ );
397  errno = save_errno;
398  }
399 
400  /*
401  * Delete any additional segments.
402  *
403  * Note that because we loop until getting ENOENT, we will correctly
404  * remove all inactive segments as well as active ones. Ideally we'd
405  * continue the loop until getting exactly that errno, but that risks an
406  * infinite loop if the problem is directory-wide (for instance, if we
407  * suddenly can't read the data directory itself). We compromise by
408  * continuing after a non-ENOENT truncate error, but stopping after any
409  * unlink error. If there is indeed a directory-wide problem, additional
410  * unlink attempts wouldn't work anyway.
411  */
412  if (ret >= 0 || errno != ENOENT)
413  {
414  char *segpath = (char *) palloc(strlen(path) + 12);
415  BlockNumber segno;
416 
417  for (segno = 1;; segno++)
418  {
419  sprintf(segpath, "%s.%u", path, segno);
420 
421  if (!RelFileLocatorBackendIsTemp(rlocator))
422  {
423  /*
424  * Prevent other backends' fds from holding on to the disk
425  * space. We're done if we see ENOENT, though.
426  */
427  if (do_truncate(segpath) < 0 && errno == ENOENT)
428  break;
429 
430  /*
431  * Forget any pending sync requests for this segment before we
432  * try to unlink.
433  */
434  register_forget_request(rlocator, forknum, segno);
435  }
436 
437  if (unlink(segpath) < 0)
438  {
439  /* ENOENT is expected after the last segment... */
440  if (errno != ENOENT)
443  errmsg("could not remove file \"%s\": %m", segpath)));
444  break;
445  }
446  }
447  pfree(segpath);
448  }
449 
450  pfree(path);
451 }
452 
453 /*
454  * mdextend() -- Add a block to the specified relation.
455  *
456  * The semantics are nearly the same as mdwrite(): write at the
457  * specified position. However, this is to be used for the case of
458  * extending a relation (i.e., blocknum is at or beyond the current
459  * EOF). Note that we assume writing a block beyond current EOF
460  * causes intervening file space to become filled with zeroes.
461  */
462 void
463 mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
464  const void *buffer, bool skipFsync)
465 {
466  off_t seekpos;
467  int nbytes;
468  MdfdVec *v;
469 
470  /* If this build supports direct I/O, the buffer must be I/O aligned. */
471  if (PG_O_DIRECT != 0 && PG_IO_ALIGN_SIZE <= BLCKSZ)
472  Assert((uintptr_t) buffer == TYPEALIGN(PG_IO_ALIGN_SIZE, buffer));
473 
474  /* This assert is too expensive to have on normally ... */
475 #ifdef CHECK_WRITE_VS_EXTEND
476  Assert(blocknum >= mdnblocks(reln, forknum));
477 #endif
478 
479  /*
480  * If a relation manages to grow to 2^32-1 blocks, refuse to extend it any
481  * more --- we mustn't create a block whose number actually is
482  * InvalidBlockNumber. (Note that this failure should be unreachable
483  * because of upstream checks in bufmgr.c.)
484  */
485  if (blocknum == InvalidBlockNumber)
486  ereport(ERROR,
487  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
488  errmsg("cannot extend file \"%s\" beyond %u blocks",
489  relpath(reln->smgr_rlocator, forknum),
491 
492  v = _mdfd_getseg(reln, forknum, blocknum, skipFsync, EXTENSION_CREATE);
493 
494  seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
495 
496  Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
497 
498  if ((nbytes = FileWrite(v->mdfd_vfd, buffer, BLCKSZ, seekpos, WAIT_EVENT_DATA_FILE_EXTEND)) != BLCKSZ)
499  {
500  if (nbytes < 0)
501  ereport(ERROR,
503  errmsg("could not extend file \"%s\": %m",
504  FilePathName(v->mdfd_vfd)),
505  errhint("Check free disk space.")));
506  /* short write: complain appropriately */
507  ereport(ERROR,
508  (errcode(ERRCODE_DISK_FULL),
509  errmsg("could not extend file \"%s\": wrote only %d of %d bytes at block %u",
511  nbytes, BLCKSZ, blocknum),
512  errhint("Check free disk space.")));
513  }
514 
515  if (!skipFsync && !SmgrIsTemp(reln))
516  register_dirty_segment(reln, forknum, v);
517 
518  Assert(_mdnblocks(reln, forknum, v) <= ((BlockNumber) RELSEG_SIZE));
519 }
520 
521 /*
522  * mdzeroextend() -- Add new zeroed out blocks to the specified relation.
523  *
524  * Similar to mdextend(), except the relation can be extended by multiple
525  * blocks at once and the added blocks will be filled with zeroes.
526  */
527 void
529  BlockNumber blocknum, int nblocks, bool skipFsync)
530 {
531  MdfdVec *v;
532  BlockNumber curblocknum = blocknum;
533  int remblocks = nblocks;
534 
535  Assert(nblocks > 0);
536 
537  /* This assert is too expensive to have on normally ... */
538 #ifdef CHECK_WRITE_VS_EXTEND
539  Assert(blocknum >= mdnblocks(reln, forknum));
540 #endif
541 
542  /*
543  * If a relation manages to grow to 2^32-1 blocks, refuse to extend it any
544  * more --- we mustn't create a block whose number actually is
545  * InvalidBlockNumber or larger.
546  */
547  if ((uint64) blocknum + nblocks >= (uint64) InvalidBlockNumber)
548  ereport(ERROR,
549  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
550  errmsg("cannot extend file \"%s\" beyond %u blocks",
551  relpath(reln->smgr_rlocator, forknum),
553 
554  while (remblocks > 0)
555  {
556  BlockNumber segstartblock = curblocknum % ((BlockNumber) RELSEG_SIZE);
557  off_t seekpos = (off_t) BLCKSZ * segstartblock;
558  int numblocks;
559 
560  if (segstartblock + remblocks > RELSEG_SIZE)
561  numblocks = RELSEG_SIZE - segstartblock;
562  else
563  numblocks = remblocks;
564 
565  v = _mdfd_getseg(reln, forknum, curblocknum, skipFsync, EXTENSION_CREATE);
566 
567  Assert(segstartblock < RELSEG_SIZE);
568  Assert(segstartblock + numblocks <= RELSEG_SIZE);
569 
570  /*
571  * If available and useful, use posix_fallocate() (via
572  * FileFallocate()) to extend the relation. That's often more
573  * efficient than using write(), as it commonly won't cause the kernel
574  * to allocate page cache space for the extended pages.
575  *
576  * However, we don't use FileFallocate() for small extensions, as it
577  * defeats delayed allocation on some filesystems. Not clear where
578  * that decision should be made though? For now just use a cutoff of
579  * 8, anything between 4 and 8 worked OK in some local testing.
580  */
581  if (numblocks > 8)
582  {
583  int ret;
584 
585  ret = FileFallocate(v->mdfd_vfd,
586  seekpos, (off_t) BLCKSZ * numblocks,
587  WAIT_EVENT_DATA_FILE_EXTEND);
588  if (ret != 0)
589  {
590  ereport(ERROR,
592  errmsg("could not extend file \"%s\" with FileFallocate(): %m",
593  FilePathName(v->mdfd_vfd)),
594  errhint("Check free disk space."));
595  }
596  }
597  else
598  {
599  int ret;
600 
601  /*
602  * Even if we don't want to use fallocate, we can still extend a
603  * bit more efficiently than writing each 8kB block individually.
604  * pg_pwrite_zeros() (via FileZero()) uses pg_pwritev_with_retry()
605  * to avoid multiple writes or needing a zeroed buffer for the
606  * whole length of the extension.
607  */
608  ret = FileZero(v->mdfd_vfd,
609  seekpos, (off_t) BLCKSZ * numblocks,
610  WAIT_EVENT_DATA_FILE_EXTEND);
611  if (ret < 0)
612  ereport(ERROR,
614  errmsg("could not extend file \"%s\": %m",
615  FilePathName(v->mdfd_vfd)),
616  errhint("Check free disk space."));
617  }
618 
619  if (!skipFsync && !SmgrIsTemp(reln))
620  register_dirty_segment(reln, forknum, v);
621 
622  Assert(_mdnblocks(reln, forknum, v) <= ((BlockNumber) RELSEG_SIZE));
623 
624  remblocks -= numblocks;
625  curblocknum += numblocks;
626  }
627 }
628 
629 /*
630  * mdopenfork() -- Open one fork of the specified relation.
631  *
632  * Note we only open the first segment, when there are multiple segments.
633  *
634  * If first segment is not present, either ereport or return NULL according
635  * to "behavior". We treat EXTENSION_CREATE the same as EXTENSION_FAIL;
636  * EXTENSION_CREATE means it's OK to extend an existing relation, not to
637  * invent one out of whole cloth.
638  */
639 static MdfdVec *
640 mdopenfork(SMgrRelation reln, ForkNumber forknum, int behavior)
641 {
642  MdfdVec *mdfd;
643  char *path;
644  File fd;
645 
646  /* No work if already open */
647  if (reln->md_num_open_segs[forknum] > 0)
648  return &reln->md_seg_fds[forknum][0];
649 
650  path = relpath(reln->smgr_rlocator, forknum);
651 
653 
654  if (fd < 0)
655  {
656  if ((behavior & EXTENSION_RETURN_NULL) &&
657  FILE_POSSIBLY_DELETED(errno))
658  {
659  pfree(path);
660  return NULL;
661  }
662  ereport(ERROR,
664  errmsg("could not open file \"%s\": %m", path)));
665  }
666 
667  pfree(path);
668 
669  _fdvec_resize(reln, forknum, 1);
670  mdfd = &reln->md_seg_fds[forknum][0];
671  mdfd->mdfd_vfd = fd;
672  mdfd->mdfd_segno = 0;
673 
674  Assert(_mdnblocks(reln, forknum, mdfd) <= ((BlockNumber) RELSEG_SIZE));
675 
676  return mdfd;
677 }
678 
679 /*
680  * mdopen() -- Initialize newly-opened relation.
681  */
682 void
684 {
685  /* mark it not open */
686  for (int forknum = 0; forknum <= MAX_FORKNUM; forknum++)
687  reln->md_num_open_segs[forknum] = 0;
688 }
689 
690 /*
691  * mdclose() -- Close the specified relation, if it isn't closed already.
692  */
693 void
695 {
696  int nopensegs = reln->md_num_open_segs[forknum];
697 
698  /* No work if already closed */
699  if (nopensegs == 0)
700  return;
701 
702  /* close segments starting from the end */
703  while (nopensegs > 0)
704  {
705  MdfdVec *v = &reln->md_seg_fds[forknum][nopensegs - 1];
706 
707  FileClose(v->mdfd_vfd);
708  _fdvec_resize(reln, forknum, nopensegs - 1);
709  nopensegs--;
710  }
711 }
712 
713 /*
714  * mdprefetch() -- Initiate asynchronous read of the specified blocks of a relation
715  */
716 bool
718  int nblocks)
719 {
720 #ifdef USE_PREFETCH
721 
723 
724  if ((uint64) blocknum + nblocks > (uint64) MaxBlockNumber + 1)
725  return false;
726 
727  while (nblocks > 0)
728  {
729  off_t seekpos;
730  MdfdVec *v;
731  int nblocks_this_segment;
732 
733  v = _mdfd_getseg(reln, forknum, blocknum, false,
735  if (v == NULL)
736  return false;
737 
738  seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
739 
740  Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
741 
742  nblocks_this_segment =
743  Min(nblocks,
744  RELSEG_SIZE - (blocknum % ((BlockNumber) RELSEG_SIZE)));
745 
746  (void) FilePrefetch(v->mdfd_vfd, seekpos, BLCKSZ * nblocks_this_segment,
747  WAIT_EVENT_DATA_FILE_PREFETCH);
748 
749  blocknum += nblocks_this_segment;
750  nblocks -= nblocks_this_segment;
751  }
752 #endif /* USE_PREFETCH */
753 
754  return true;
755 }
756 
757 /*
758  * Convert an array of buffer address into an array of iovec objects, and
759  * return the number that were required. 'iov' must have enough space for up
760  * to 'nblocks' elements, but the number used may be less depending on
761  * merging. In the case of a run of fully contiguous buffers, a single iovec
762  * will be populated that can be handled as a plain non-vectored I/O.
763  */
764 static int
765 buffers_to_iovec(struct iovec *iov, void **buffers, int nblocks)
766 {
767  struct iovec *iovp;
768  int iovcnt;
769 
770  Assert(nblocks >= 1);
771 
772  /* If this build supports direct I/O, buffers must be I/O aligned. */
773  for (int i = 0; i < nblocks; ++i)
774  {
775  if (PG_O_DIRECT != 0 && PG_IO_ALIGN_SIZE <= BLCKSZ)
776  Assert((uintptr_t) buffers[i] ==
777  TYPEALIGN(PG_IO_ALIGN_SIZE, buffers[i]));
778  }
779 
780  /* Start the first iovec off with the first buffer. */
781  iovp = &iov[0];
782  iovp->iov_base = buffers[0];
783  iovp->iov_len = BLCKSZ;
784  iovcnt = 1;
785 
786  /* Try to merge the rest. */
787  for (int i = 1; i < nblocks; ++i)
788  {
789  void *buffer = buffers[i];
790 
791  if (((char *) iovp->iov_base + iovp->iov_len) == buffer)
792  {
793  /* Contiguous with the last iovec. */
794  iovp->iov_len += BLCKSZ;
795  }
796  else
797  {
798  /* Need a new iovec. */
799  iovp++;
800  iovp->iov_base = buffer;
801  iovp->iov_len = BLCKSZ;
802  iovcnt++;
803  }
804  }
805 
806  return iovcnt;
807 }
808 
809 /*
810  * mdreadv() -- Read the specified blocks from a relation.
811  */
812 void
813 mdreadv(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
814  void **buffers, BlockNumber nblocks)
815 {
816  while (nblocks > 0)
817  {
818  struct iovec iov[PG_IOV_MAX];
819  int iovcnt;
820  off_t seekpos;
821  int nbytes;
822  MdfdVec *v;
823  BlockNumber nblocks_this_segment;
824  size_t transferred_this_segment;
825  size_t size_this_segment;
826 
827  v = _mdfd_getseg(reln, forknum, blocknum, false,
829 
830  seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
831 
832  Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
833 
834  nblocks_this_segment =
835  Min(nblocks,
836  RELSEG_SIZE - (blocknum % ((BlockNumber) RELSEG_SIZE)));
837  nblocks_this_segment = Min(nblocks_this_segment, lengthof(iov));
838 
839  iovcnt = buffers_to_iovec(iov, buffers, nblocks_this_segment);
840  size_this_segment = nblocks_this_segment * BLCKSZ;
841  transferred_this_segment = 0;
842 
843  /*
844  * Inner loop to continue after a short read. We'll keep going until
845  * we hit EOF rather than assuming that a short read means we hit the
846  * end.
847  */
848  for (;;)
849  {
850  TRACE_POSTGRESQL_SMGR_MD_READ_START(forknum, blocknum,
854  reln->smgr_rlocator.backend);
855  nbytes = FileReadV(v->mdfd_vfd, iov, iovcnt, seekpos,
856  WAIT_EVENT_DATA_FILE_READ);
857  TRACE_POSTGRESQL_SMGR_MD_READ_DONE(forknum, blocknum,
861  reln->smgr_rlocator.backend,
862  nbytes,
863  size_this_segment - transferred_this_segment);
864 
865 #ifdef SIMULATE_SHORT_READ
866  nbytes = Min(nbytes, 4096);
867 #endif
868 
869  if (nbytes < 0)
870  ereport(ERROR,
872  errmsg("could not read blocks %u..%u in file \"%s\": %m",
873  blocknum,
874  blocknum + nblocks_this_segment - 1,
875  FilePathName(v->mdfd_vfd))));
876 
877  if (nbytes == 0)
878  {
879  /*
880  * We are at or past EOF, or we read a partial block at EOF.
881  * Normally this is an error; upper levels should never try to
882  * read a nonexistent block. However, if zero_damaged_pages
883  * is ON or we are InRecovery, we should instead return zeroes
884  * without complaining. This allows, for example, the case of
885  * trying to update a block that was later truncated away.
886  */
888  {
889  for (BlockNumber i = transferred_this_segment / BLCKSZ;
890  i < nblocks_this_segment;
891  ++i)
892  memset(buffers[i], 0, BLCKSZ);
893  break;
894  }
895  else
896  ereport(ERROR,
898  errmsg("could not read blocks %u..%u in file \"%s\": read only %zu of %zu bytes",
899  blocknum,
900  blocknum + nblocks_this_segment - 1,
902  transferred_this_segment,
903  size_this_segment)));
904  }
905 
906  /* One loop should usually be enough. */
907  transferred_this_segment += nbytes;
908  Assert(transferred_this_segment <= size_this_segment);
909  if (transferred_this_segment == size_this_segment)
910  break;
911 
912  /* Adjust position and vectors after a short read. */
913  seekpos += nbytes;
914  iovcnt = compute_remaining_iovec(iov, iov, iovcnt, nbytes);
915  }
916 
917  nblocks -= nblocks_this_segment;
918  buffers += nblocks_this_segment;
919  blocknum += nblocks_this_segment;
920  }
921 }
922 
923 /*
924  * mdwritev() -- Write the supplied blocks at the appropriate location.
925  *
926  * This is to be used only for updating already-existing blocks of a
927  * relation (ie, those before the current EOF). To extend a relation,
928  * use mdextend().
929  */
930 void
931 mdwritev(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
932  const void **buffers, BlockNumber nblocks, bool skipFsync)
933 {
934  /* This assert is too expensive to have on normally ... */
935 #ifdef CHECK_WRITE_VS_EXTEND
936  Assert(blocknum < mdnblocks(reln, forknum));
937 #endif
938 
939  while (nblocks > 0)
940  {
941  struct iovec iov[PG_IOV_MAX];
942  int iovcnt;
943  off_t seekpos;
944  int nbytes;
945  MdfdVec *v;
946  BlockNumber nblocks_this_segment;
947  size_t transferred_this_segment;
948  size_t size_this_segment;
949 
950  v = _mdfd_getseg(reln, forknum, blocknum, skipFsync,
952 
953  seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
954 
955  Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
956 
957  nblocks_this_segment =
958  Min(nblocks,
959  RELSEG_SIZE - (blocknum % ((BlockNumber) RELSEG_SIZE)));
960  nblocks_this_segment = Min(nblocks_this_segment, lengthof(iov));
961 
962  iovcnt = buffers_to_iovec(iov, (void **) buffers, nblocks_this_segment);
963  size_this_segment = nblocks_this_segment * BLCKSZ;
964  transferred_this_segment = 0;
965 
966  /*
967  * Inner loop to continue after a short write. If the reason is that
968  * we're out of disk space, a future attempt should get an ENOSPC
969  * error from the kernel.
970  */
971  for (;;)
972  {
973  TRACE_POSTGRESQL_SMGR_MD_WRITE_START(forknum, blocknum,
977  reln->smgr_rlocator.backend);
978  nbytes = FileWriteV(v->mdfd_vfd, iov, iovcnt, seekpos,
979  WAIT_EVENT_DATA_FILE_WRITE);
980  TRACE_POSTGRESQL_SMGR_MD_WRITE_DONE(forknum, blocknum,
984  reln->smgr_rlocator.backend,
985  nbytes,
986  size_this_segment - transferred_this_segment);
987 
988 #ifdef SIMULATE_SHORT_WRITE
989  nbytes = Min(nbytes, 4096);
990 #endif
991 
992  if (nbytes < 0)
993  {
994  bool enospc = errno == ENOSPC;
995 
996  ereport(ERROR,
998  errmsg("could not write blocks %u..%u in file \"%s\": %m",
999  blocknum,
1000  blocknum + nblocks_this_segment - 1,
1001  FilePathName(v->mdfd_vfd)),
1002  enospc ? errhint("Check free disk space.") : 0));
1003  }
1004 
1005  /* One loop should usually be enough. */
1006  transferred_this_segment += nbytes;
1007  Assert(transferred_this_segment <= size_this_segment);
1008  if (transferred_this_segment == size_this_segment)
1009  break;
1010 
1011  /* Adjust position and iovecs after a short write. */
1012  seekpos += nbytes;
1013  iovcnt = compute_remaining_iovec(iov, iov, iovcnt, nbytes);
1014  }
1015 
1016  if (!skipFsync && !SmgrIsTemp(reln))
1017  register_dirty_segment(reln, forknum, v);
1018 
1019  nblocks -= nblocks_this_segment;
1020  buffers += nblocks_this_segment;
1021  blocknum += nblocks_this_segment;
1022  }
1023 }
1024 
1025 
1026 /*
1027  * mdwriteback() -- Tell the kernel to write pages back to storage.
1028  *
1029  * This accepts a range of blocks because flushing several pages at once is
1030  * considerably more efficient than doing so individually.
1031  */
1032 void
1034  BlockNumber blocknum, BlockNumber nblocks)
1035 {
1037 
1038  /*
1039  * Issue flush requests in as few requests as possible; have to split at
1040  * segment boundaries though, since those are actually separate files.
1041  */
1042  while (nblocks > 0)
1043  {
1044  BlockNumber nflush = nblocks;
1045  off_t seekpos;
1046  MdfdVec *v;
1047  int segnum_start,
1048  segnum_end;
1049 
1050  v = _mdfd_getseg(reln, forknum, blocknum, true /* not used */ ,
1052 
1053  /*
1054  * We might be flushing buffers of already removed relations, that's
1055  * ok, just ignore that case. If the segment file wasn't open already
1056  * (ie from a recent mdwrite()), then we don't want to re-open it, to
1057  * avoid a race with PROCSIGNAL_BARRIER_SMGRRELEASE that might leave
1058  * us with a descriptor to a file that is about to be unlinked.
1059  */
1060  if (!v)
1061  return;
1062 
1063  /* compute offset inside the current segment */
1064  segnum_start = blocknum / RELSEG_SIZE;
1065 
1066  /* compute number of desired writes within the current segment */
1067  segnum_end = (blocknum + nblocks - 1) / RELSEG_SIZE;
1068  if (segnum_start != segnum_end)
1069  nflush = RELSEG_SIZE - (blocknum % ((BlockNumber) RELSEG_SIZE));
1070 
1071  Assert(nflush >= 1);
1072  Assert(nflush <= nblocks);
1073 
1074  seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
1075 
1076  FileWriteback(v->mdfd_vfd, seekpos, (off_t) BLCKSZ * nflush, WAIT_EVENT_DATA_FILE_FLUSH);
1077 
1078  nblocks -= nflush;
1079  blocknum += nflush;
1080  }
1081 }
1082 
1083 /*
1084  * mdnblocks() -- Get the number of blocks stored in a relation.
1085  *
1086  * Important side effect: all active segments of the relation are opened
1087  * and added to the md_seg_fds array. If this routine has not been
1088  * called, then only segments up to the last one actually touched
1089  * are present in the array.
1090  */
1093 {
1094  MdfdVec *v;
1095  BlockNumber nblocks;
1096  BlockNumber segno;
1097 
1098  mdopenfork(reln, forknum, EXTENSION_FAIL);
1099 
1100  /* mdopen has opened the first segment */
1101  Assert(reln->md_num_open_segs[forknum] > 0);
1102 
1103  /*
1104  * Start from the last open segments, to avoid redundant seeks. We have
1105  * previously verified that these segments are exactly RELSEG_SIZE long,
1106  * and it's useless to recheck that each time.
1107  *
1108  * NOTE: this assumption could only be wrong if another backend has
1109  * truncated the relation. We rely on higher code levels to handle that
1110  * scenario by closing and re-opening the md fd, which is handled via
1111  * relcache flush. (Since the checkpointer doesn't participate in
1112  * relcache flush, it could have segment entries for inactive segments;
1113  * that's OK because the checkpointer never needs to compute relation
1114  * size.)
1115  */
1116  segno = reln->md_num_open_segs[forknum] - 1;
1117  v = &reln->md_seg_fds[forknum][segno];
1118 
1119  for (;;)
1120  {
1121  nblocks = _mdnblocks(reln, forknum, v);
1122  if (nblocks > ((BlockNumber) RELSEG_SIZE))
1123  elog(FATAL, "segment too big");
1124  if (nblocks < ((BlockNumber) RELSEG_SIZE))
1125  return (segno * ((BlockNumber) RELSEG_SIZE)) + nblocks;
1126 
1127  /*
1128  * If segment is exactly RELSEG_SIZE, advance to next one.
1129  */
1130  segno++;
1131 
1132  /*
1133  * We used to pass O_CREAT here, but that has the disadvantage that it
1134  * might create a segment which has vanished through some operating
1135  * system misadventure. In such a case, creating the segment here
1136  * undermines _mdfd_getseg's attempts to notice and report an error
1137  * upon access to a missing segment.
1138  */
1139  v = _mdfd_openseg(reln, forknum, segno, 0);
1140  if (v == NULL)
1141  return segno * ((BlockNumber) RELSEG_SIZE);
1142  }
1143 }
1144 
1145 /*
1146  * mdtruncate() -- Truncate relation to specified number of blocks.
1147  */
1148 void
1150 {
1151  BlockNumber curnblk;
1152  BlockNumber priorblocks;
1153  int curopensegs;
1154 
1155  /*
1156  * NOTE: mdnblocks makes sure we have opened all active segments, so that
1157  * truncation loop will get them all!
1158  */
1159  curnblk = mdnblocks(reln, forknum);
1160  if (nblocks > curnblk)
1161  {
1162  /* Bogus request ... but no complaint if InRecovery */
1163  if (InRecovery)
1164  return;
1165  ereport(ERROR,
1166  (errmsg("could not truncate file \"%s\" to %u blocks: it's only %u blocks now",
1167  relpath(reln->smgr_rlocator, forknum),
1168  nblocks, curnblk)));
1169  }
1170  if (nblocks == curnblk)
1171  return; /* no work */
1172 
1173  /*
1174  * Truncate segments, starting at the last one. Starting at the end makes
1175  * managing the memory for the fd array easier, should there be errors.
1176  */
1177  curopensegs = reln->md_num_open_segs[forknum];
1178  while (curopensegs > 0)
1179  {
1180  MdfdVec *v;
1181 
1182  priorblocks = (curopensegs - 1) * RELSEG_SIZE;
1183 
1184  v = &reln->md_seg_fds[forknum][curopensegs - 1];
1185 
1186  if (priorblocks > nblocks)
1187  {
1188  /*
1189  * This segment is no longer active. We truncate the file, but do
1190  * not delete it, for reasons explained in the header comments.
1191  */
1192  if (FileTruncate(v->mdfd_vfd, 0, WAIT_EVENT_DATA_FILE_TRUNCATE) < 0)
1193  ereport(ERROR,
1195  errmsg("could not truncate file \"%s\": %m",
1196  FilePathName(v->mdfd_vfd))));
1197 
1198  if (!SmgrIsTemp(reln))
1199  register_dirty_segment(reln, forknum, v);
1200 
1201  /* we never drop the 1st segment */
1202  Assert(v != &reln->md_seg_fds[forknum][0]);
1203 
1204  FileClose(v->mdfd_vfd);
1205  _fdvec_resize(reln, forknum, curopensegs - 1);
1206  }
1207  else if (priorblocks + ((BlockNumber) RELSEG_SIZE) > nblocks)
1208  {
1209  /*
1210  * This is the last segment we want to keep. Truncate the file to
1211  * the right length. NOTE: if nblocks is exactly a multiple K of
1212  * RELSEG_SIZE, we will truncate the K+1st segment to 0 length but
1213  * keep it. This adheres to the invariant given in the header
1214  * comments.
1215  */
1216  BlockNumber lastsegblocks = nblocks - priorblocks;
1217 
1218  if (FileTruncate(v->mdfd_vfd, (off_t) lastsegblocks * BLCKSZ, WAIT_EVENT_DATA_FILE_TRUNCATE) < 0)
1219  ereport(ERROR,
1221  errmsg("could not truncate file \"%s\" to %u blocks: %m",
1222  FilePathName(v->mdfd_vfd),
1223  nblocks)));
1224  if (!SmgrIsTemp(reln))
1225  register_dirty_segment(reln, forknum, v);
1226  }
1227  else
1228  {
1229  /*
1230  * We still need this segment, so nothing to do for this and any
1231  * earlier segment.
1232  */
1233  break;
1234  }
1235  curopensegs--;
1236  }
1237 }
1238 
1239 /*
1240  * mdregistersync() -- Mark whole relation as needing fsync
1241  */
1242 void
1244 {
1245  int segno;
1246  int min_inactive_seg;
1247 
1248  /*
1249  * NOTE: mdnblocks makes sure we have opened all active segments, so that
1250  * the loop below will get them all!
1251  */
1252  mdnblocks(reln, forknum);
1253 
1254  min_inactive_seg = segno = reln->md_num_open_segs[forknum];
1255 
1256  /*
1257  * Temporarily open inactive segments, then close them after sync. There
1258  * may be some inactive segments left opened after error, but that is
1259  * harmless. We don't bother to clean them up and take a risk of further
1260  * trouble. The next mdclose() will soon close them.
1261  */
1262  while (_mdfd_openseg(reln, forknum, segno, 0) != NULL)
1263  segno++;
1264 
1265  while (segno > 0)
1266  {
1267  MdfdVec *v = &reln->md_seg_fds[forknum][segno - 1];
1268 
1269  register_dirty_segment(reln, forknum, v);
1270 
1271  /* Close inactive segments immediately */
1272  if (segno > min_inactive_seg)
1273  {
1274  FileClose(v->mdfd_vfd);
1275  _fdvec_resize(reln, forknum, segno - 1);
1276  }
1277 
1278  segno--;
1279  }
1280 }
1281 
1282 /*
1283  * mdimmedsync() -- Immediately sync a relation to stable storage.
1284  *
1285  * Note that only writes already issued are synced; this routine knows
1286  * nothing of dirty buffers that may exist inside the buffer manager. We
1287  * sync active and inactive segments; smgrDoPendingSyncs() relies on this.
1288  * Consider a relation skipping WAL. Suppose a checkpoint syncs blocks of
1289  * some segment, then mdtruncate() renders that segment inactive. If we
1290  * crash before the next checkpoint syncs the newly-inactive segment, that
1291  * segment may survive recovery, reintroducing unwanted data into the table.
1292  */
1293 void
1295 {
1296  int segno;
1297  int min_inactive_seg;
1298 
1299  /*
1300  * NOTE: mdnblocks makes sure we have opened all active segments, so that
1301  * the loop below will get them all!
1302  */
1303  mdnblocks(reln, forknum);
1304 
1305  min_inactive_seg = segno = reln->md_num_open_segs[forknum];
1306 
1307  /*
1308  * Temporarily open inactive segments, then close them after sync. There
1309  * may be some inactive segments left opened after fsync() error, but that
1310  * is harmless. We don't bother to clean them up and take a risk of
1311  * further trouble. The next mdclose() will soon close them.
1312  */
1313  while (_mdfd_openseg(reln, forknum, segno, 0) != NULL)
1314  segno++;
1315 
1316  while (segno > 0)
1317  {
1318  MdfdVec *v = &reln->md_seg_fds[forknum][segno - 1];
1319 
1320  /*
1321  * fsyncs done through mdimmedsync() should be tracked in a separate
1322  * IOContext than those done through mdsyncfiletag() to differentiate
1323  * between unavoidable client backend fsyncs (e.g. those done during
1324  * index build) and those which ideally would have been done by the
1325  * checkpointer. Since other IO operations bypassing the buffer
1326  * manager could also be tracked in such an IOContext, wait until
1327  * these are also tracked to track immediate fsyncs.
1328  */
1329  if (FileSync(v->mdfd_vfd, WAIT_EVENT_DATA_FILE_IMMEDIATE_SYNC) < 0)
1332  errmsg("could not fsync file \"%s\": %m",
1333  FilePathName(v->mdfd_vfd))));
1334 
1335  /* Close inactive segments immediately */
1336  if (segno > min_inactive_seg)
1337  {
1338  FileClose(v->mdfd_vfd);
1339  _fdvec_resize(reln, forknum, segno - 1);
1340  }
1341 
1342  segno--;
1343  }
1344 }
1345 
1346 /*
1347  * register_dirty_segment() -- Mark a relation segment as needing fsync
1348  *
1349  * If there is a local pending-ops table, just make an entry in it for
1350  * ProcessSyncRequests to process later. Otherwise, try to pass off the
1351  * fsync request to the checkpointer process. If that fails, just do the
1352  * fsync locally before returning (we hope this will not happen often
1353  * enough to be a performance problem).
1354  */
1355 static void
1357 {
1358  FileTag tag;
1359 
1360  INIT_MD_FILETAG(tag, reln->smgr_rlocator.locator, forknum, seg->mdfd_segno);
1361 
1362  /* Temp relations should never be fsync'd */
1363  Assert(!SmgrIsTemp(reln));
1364 
1365  if (!RegisterSyncRequest(&tag, SYNC_REQUEST, false /* retryOnError */ ))
1366  {
1367  instr_time io_start;
1368 
1369  ereport(DEBUG1,
1370  (errmsg_internal("could not forward fsync request because request queue is full")));
1371 
1373 
1374  if (FileSync(seg->mdfd_vfd, WAIT_EVENT_DATA_FILE_SYNC) < 0)
1377  errmsg("could not fsync file \"%s\": %m",
1378  FilePathName(seg->mdfd_vfd))));
1379 
1380  /*
1381  * We have no way of knowing if the current IOContext is
1382  * IOCONTEXT_NORMAL or IOCONTEXT_[BULKREAD, BULKWRITE, VACUUM] at this
1383  * point, so count the fsync as being in the IOCONTEXT_NORMAL
1384  * IOContext. This is probably okay, because the number of backend
1385  * fsyncs doesn't say anything about the efficacy of the
1386  * BufferAccessStrategy. And counting both fsyncs done in
1387  * IOCONTEXT_NORMAL and IOCONTEXT_[BULKREAD, BULKWRITE, VACUUM] under
1388  * IOCONTEXT_NORMAL is likely clearer when investigating the number of
1389  * backend fsyncs.
1390  */
1392  IOOP_FSYNC, io_start, 1);
1393  }
1394 }
1395 
1396 /*
1397  * register_unlink_segment() -- Schedule a file to be deleted after next checkpoint
1398  */
1399 static void
1401  BlockNumber segno)
1402 {
1403  FileTag tag;
1404 
1405  INIT_MD_FILETAG(tag, rlocator.locator, forknum, segno);
1406 
1407  /* Should never be used with temp relations */
1408  Assert(!RelFileLocatorBackendIsTemp(rlocator));
1409 
1410  RegisterSyncRequest(&tag, SYNC_UNLINK_REQUEST, true /* retryOnError */ );
1411 }
1412 
1413 /*
1414  * register_forget_request() -- forget any fsyncs for a relation fork's segment
1415  */
1416 static void
1418  BlockNumber segno)
1419 {
1420  FileTag tag;
1421 
1422  INIT_MD_FILETAG(tag, rlocator.locator, forknum, segno);
1423 
1424  RegisterSyncRequest(&tag, SYNC_FORGET_REQUEST, true /* retryOnError */ );
1425 }
1426 
1427 /*
1428  * ForgetDatabaseSyncRequests -- forget any fsyncs and unlinks for a DB
1429  */
1430 void
1432 {
1433  FileTag tag;
1434  RelFileLocator rlocator;
1435 
1436  rlocator.dbOid = dbid;
1437  rlocator.spcOid = 0;
1438  rlocator.relNumber = 0;
1439 
1441 
1442  RegisterSyncRequest(&tag, SYNC_FILTER_REQUEST, true /* retryOnError */ );
1443 }
1444 
1445 /*
1446  * DropRelationFiles -- drop files of all given relations
1447  */
1448 void
1449 DropRelationFiles(RelFileLocator *delrels, int ndelrels, bool isRedo)
1450 {
1451  SMgrRelation *srels;
1452  int i;
1453 
1454  srels = palloc(sizeof(SMgrRelation) * ndelrels);
1455  for (i = 0; i < ndelrels; i++)
1456  {
1457  SMgrRelation srel = smgropen(delrels[i], InvalidBackendId);
1458 
1459  if (isRedo)
1460  {
1461  ForkNumber fork;
1462 
1463  for (fork = 0; fork <= MAX_FORKNUM; fork++)
1464  XLogDropRelation(delrels[i], fork);
1465  }
1466  srels[i] = srel;
1467  }
1468 
1469  smgrdounlinkall(srels, ndelrels, isRedo);
1470 
1471  for (i = 0; i < ndelrels; i++)
1472  smgrclose(srels[i]);
1473  pfree(srels);
1474 }
1475 
1476 
1477 /*
1478  * _fdvec_resize() -- Resize the fork's open segments array
1479  */
1480 static void
1482  ForkNumber forknum,
1483  int nseg)
1484 {
1485  if (nseg == 0)
1486  {
1487  if (reln->md_num_open_segs[forknum] > 0)
1488  {
1489  pfree(reln->md_seg_fds[forknum]);
1490  reln->md_seg_fds[forknum] = NULL;
1491  }
1492  }
1493  else if (reln->md_num_open_segs[forknum] == 0)
1494  {
1495  reln->md_seg_fds[forknum] =
1496  MemoryContextAlloc(MdCxt, sizeof(MdfdVec) * nseg);
1497  }
1498  else
1499  {
1500  /*
1501  * It doesn't seem worthwhile complicating the code to amortize
1502  * repalloc() calls. Those are far faster than PathNameOpenFile() or
1503  * FileClose(), and the memory context internally will sometimes avoid
1504  * doing an actual reallocation.
1505  */
1506  reln->md_seg_fds[forknum] =
1507  repalloc(reln->md_seg_fds[forknum],
1508  sizeof(MdfdVec) * nseg);
1509  }
1510 
1511  reln->md_num_open_segs[forknum] = nseg;
1512 }
1513 
1514 /*
1515  * Return the filename for the specified segment of the relation. The
1516  * returned string is palloc'd.
1517  */
1518 static char *
1520 {
1521  char *path,
1522  *fullpath;
1523 
1524  path = relpath(reln->smgr_rlocator, forknum);
1525 
1526  if (segno > 0)
1527  {
1528  fullpath = psprintf("%s.%u", path, segno);
1529  pfree(path);
1530  }
1531  else
1532  fullpath = path;
1533 
1534  return fullpath;
1535 }
1536 
1537 /*
1538  * Open the specified segment of the relation,
1539  * and make a MdfdVec object for it. Returns NULL on failure.
1540  */
1541 static MdfdVec *
1543  int oflags)
1544 {
1545  MdfdVec *v;
1546  File fd;
1547  char *fullpath;
1548 
1549  fullpath = _mdfd_segpath(reln, forknum, segno);
1550 
1551  /* open the file */
1552  fd = PathNameOpenFile(fullpath, _mdfd_open_flags() | oflags);
1553 
1554  pfree(fullpath);
1555 
1556  if (fd < 0)
1557  return NULL;
1558 
1559  /*
1560  * Segments are always opened in order from lowest to highest, so we must
1561  * be adding a new one at the end.
1562  */
1563  Assert(segno == reln->md_num_open_segs[forknum]);
1564 
1565  _fdvec_resize(reln, forknum, segno + 1);
1566 
1567  /* fill the entry */
1568  v = &reln->md_seg_fds[forknum][segno];
1569  v->mdfd_vfd = fd;
1570  v->mdfd_segno = segno;
1571 
1572  Assert(_mdnblocks(reln, forknum, v) <= ((BlockNumber) RELSEG_SIZE));
1573 
1574  /* all done */
1575  return v;
1576 }
1577 
1578 /*
1579  * _mdfd_getseg() -- Find the segment of the relation holding the
1580  * specified block.
1581  *
1582  * If the segment doesn't exist, we ereport, return NULL, or create the
1583  * segment, according to "behavior". Note: skipFsync is only used in the
1584  * EXTENSION_CREATE case.
1585  */
1586 static MdfdVec *
1588  bool skipFsync, int behavior)
1589 {
1590  MdfdVec *v;
1591  BlockNumber targetseg;
1592  BlockNumber nextsegno;
1593 
1594  /* some way to handle non-existent segments needs to be specified */
1595  Assert(behavior &
1598 
1599  targetseg = blkno / ((BlockNumber) RELSEG_SIZE);
1600 
1601  /* if an existing and opened segment, we're done */
1602  if (targetseg < reln->md_num_open_segs[forknum])
1603  {
1604  v = &reln->md_seg_fds[forknum][targetseg];
1605  return v;
1606  }
1607 
1608  /* The caller only wants the segment if we already had it open. */
1609  if (behavior & EXTENSION_DONT_OPEN)
1610  return NULL;
1611 
1612  /*
1613  * The target segment is not yet open. Iterate over all the segments
1614  * between the last opened and the target segment. This way missing
1615  * segments either raise an error, or get created (according to
1616  * 'behavior'). Start with either the last opened, or the first segment if
1617  * none was opened before.
1618  */
1619  if (reln->md_num_open_segs[forknum] > 0)
1620  v = &reln->md_seg_fds[forknum][reln->md_num_open_segs[forknum] - 1];
1621  else
1622  {
1623  v = mdopenfork(reln, forknum, behavior);
1624  if (!v)
1625  return NULL; /* if behavior & EXTENSION_RETURN_NULL */
1626  }
1627 
1628  for (nextsegno = reln->md_num_open_segs[forknum];
1629  nextsegno <= targetseg; nextsegno++)
1630  {
1631  BlockNumber nblocks = _mdnblocks(reln, forknum, v);
1632  int flags = 0;
1633 
1634  Assert(nextsegno == v->mdfd_segno + 1);
1635 
1636  if (nblocks > ((BlockNumber) RELSEG_SIZE))
1637  elog(FATAL, "segment too big");
1638 
1639  if ((behavior & EXTENSION_CREATE) ||
1640  (InRecovery && (behavior & EXTENSION_CREATE_RECOVERY)))
1641  {
1642  /*
1643  * Normally we will create new segments only if authorized by the
1644  * caller (i.e., we are doing mdextend()). But when doing WAL
1645  * recovery, create segments anyway; this allows cases such as
1646  * replaying WAL data that has a write into a high-numbered
1647  * segment of a relation that was later deleted. We want to go
1648  * ahead and create the segments so we can finish out the replay.
1649  *
1650  * We have to maintain the invariant that segments before the last
1651  * active segment are of size RELSEG_SIZE; therefore, if
1652  * extending, pad them out with zeroes if needed. (This only
1653  * matters if in recovery, or if the caller is extending the
1654  * relation discontiguously, but that can happen in hash indexes.)
1655  */
1656  if (nblocks < ((BlockNumber) RELSEG_SIZE))
1657  {
1658  char *zerobuf = palloc_aligned(BLCKSZ, PG_IO_ALIGN_SIZE,
1659  MCXT_ALLOC_ZERO);
1660 
1661  mdextend(reln, forknum,
1662  nextsegno * ((BlockNumber) RELSEG_SIZE) - 1,
1663  zerobuf, skipFsync);
1664  pfree(zerobuf);
1665  }
1666  flags = O_CREAT;
1667  }
1668  else if (!(behavior & EXTENSION_DONT_CHECK_SIZE) &&
1669  nblocks < ((BlockNumber) RELSEG_SIZE))
1670  {
1671  /*
1672  * When not extending (or explicitly including truncated
1673  * segments), only open the next segment if the current one is
1674  * exactly RELSEG_SIZE. If not (this branch), either return NULL
1675  * or fail.
1676  */
1677  if (behavior & EXTENSION_RETURN_NULL)
1678  {
1679  /*
1680  * Some callers discern between reasons for _mdfd_getseg()
1681  * returning NULL based on errno. As there's no failing
1682  * syscall involved in this case, explicitly set errno to
1683  * ENOENT, as that seems the closest interpretation.
1684  */
1685  errno = ENOENT;
1686  return NULL;
1687  }
1688 
1689  ereport(ERROR,
1691  errmsg("could not open file \"%s\" (target block %u): previous segment is only %u blocks",
1692  _mdfd_segpath(reln, forknum, nextsegno),
1693  blkno, nblocks)));
1694  }
1695 
1696  v = _mdfd_openseg(reln, forknum, nextsegno, flags);
1697 
1698  if (v == NULL)
1699  {
1700  if ((behavior & EXTENSION_RETURN_NULL) &&
1701  FILE_POSSIBLY_DELETED(errno))
1702  return NULL;
1703  ereport(ERROR,
1705  errmsg("could not open file \"%s\" (target block %u): %m",
1706  _mdfd_segpath(reln, forknum, nextsegno),
1707  blkno)));
1708  }
1709  }
1710 
1711  return v;
1712 }
1713 
1714 /*
1715  * Get number of blocks present in a single disk file
1716  */
1717 static BlockNumber
1719 {
1720  off_t len;
1721 
1722  len = FileSize(seg->mdfd_vfd);
1723  if (len < 0)
1724  ereport(ERROR,
1726  errmsg("could not seek to end of file \"%s\": %m",
1727  FilePathName(seg->mdfd_vfd))));
1728  /* note that this calculation will ignore any partial block at EOF */
1729  return (BlockNumber) (len / BLCKSZ);
1730 }
1731 
1732 /*
1733  * Sync a file to disk, given a file tag. Write the path into an output
1734  * buffer so the caller can use it in error messages.
1735  *
1736  * Return 0 on success, -1 on failure, with errno set.
1737  */
1738 int
1739 mdsyncfiletag(const FileTag *ftag, char *path)
1740 {
1742  File file;
1743  instr_time io_start;
1744  bool need_to_close;
1745  int result,
1746  save_errno;
1747 
1748  /* See if we already have the file open, or need to open it. */
1749  if (ftag->segno < reln->md_num_open_segs[ftag->forknum])
1750  {
1751  file = reln->md_seg_fds[ftag->forknum][ftag->segno].mdfd_vfd;
1752  strlcpy(path, FilePathName(file), MAXPGPATH);
1753  need_to_close = false;
1754  }
1755  else
1756  {
1757  char *p;
1758 
1759  p = _mdfd_segpath(reln, ftag->forknum, ftag->segno);
1760  strlcpy(path, p, MAXPGPATH);
1761  pfree(p);
1762 
1763  file = PathNameOpenFile(path, _mdfd_open_flags());
1764  if (file < 0)
1765  return -1;
1766  need_to_close = true;
1767  }
1768 
1770 
1771  /* Sync the file. */
1772  result = FileSync(file, WAIT_EVENT_DATA_FILE_SYNC);
1773  save_errno = errno;
1774 
1775  if (need_to_close)
1776  FileClose(file);
1777 
1779  IOOP_FSYNC, io_start, 1);
1780 
1781  errno = save_errno;
1782  return result;
1783 }
1784 
1785 /*
1786  * Unlink a file, given a file tag. Write the path into an output
1787  * buffer so the caller can use it in error messages.
1788  *
1789  * Return 0 on success, -1 on failure, with errno set.
1790  */
1791 int
1792 mdunlinkfiletag(const FileTag *ftag, char *path)
1793 {
1794  char *p;
1795 
1796  /* Compute the path. */
1797  p = relpathperm(ftag->rlocator, MAIN_FORKNUM);
1798  strlcpy(path, p, MAXPGPATH);
1799  pfree(p);
1800 
1801  /* Try to unlink the file. */
1802  return unlink(path);
1803 }
1804 
1805 /*
1806  * Check if a given candidate request matches a given tag, when processing
1807  * a SYNC_FILTER_REQUEST request. This will be called for all pending
1808  * requests to find out whether to forget them.
1809  */
1810 bool
1811 mdfiletagmatches(const FileTag *ftag, const FileTag *candidate)
1812 {
1813  /*
1814  * For now we only use filter requests as a way to drop all scheduled
1815  * callbacks relating to a given database, when dropping the database.
1816  * We'll return true for all candidates that have the same database OID as
1817  * the ftag from the SYNC_FILTER_REQUEST request, so they're forgotten.
1818  */
1819  return ftag->rlocator.dbOid == candidate->rlocator.dbOid;
1820 }
void TablespaceCreateDbspace(Oid spcOid, Oid dbOid, bool isRedo)
Definition: tablespace.c:118
#define InvalidBackendId
Definition: backendid.h:23
uint32 BlockNumber
Definition: block.h:31
#define InvalidBlockNumber
Definition: block.h:33
#define MaxBlockNumber
Definition: block.h:35
bool track_io_timing
Definition: bufmgr.c:139
bool zero_damaged_pages
Definition: bufmgr.c:136
#define Min(x, y)
Definition: c.h:993
#define TYPEALIGN(ALIGNVAL, LEN)
Definition: c.h:793
#define PG_BINARY
Definition: c.h:1262
#define lengthof(array)
Definition: c.h:777
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1162
int errcode_for_file_access(void)
Definition: elog.c:883
int errhint(const char *fmt,...)
Definition: elog.c:1322
int errcode(int sqlerrcode)
Definition: elog.c:860
int errmsg(const char *fmt,...)
Definition: elog.c:1075
#define FATAL
Definition: elog.h:41
#define WARNING
Definition: elog.h:36
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
void FileWriteback(File file, off_t offset, off_t nbytes, uint32 wait_event_info)
Definition: fd.c:2110
int io_direct_flags
Definition: fd.c:168
int FileSync(File file, uint32 wait_event_info)
Definition: fd.c:2297
int FileWriteV(File file, const struct iovec *iov, int iovcnt, off_t offset, uint32 wait_event_info)
Definition: fd.c:2192
void FileClose(File file)
Definition: fd.c:1978
int FileReadV(File file, const struct iovec *iov, int iovcnt, off_t offset, uint32 wait_event_info)
Definition: fd.c:2136
int FileFallocate(File file, off_t offset, off_t amount, uint32 wait_event_info)
Definition: fd.c:2369
int FilePrefetch(File file, off_t offset, off_t amount, uint32 wait_event_info)
Definition: fd.c:2078
int data_sync_elevel(int elevel)
Definition: fd.c:3936
File PathNameOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:1575
char * FilePathName(File file)
Definition: fd.c:2461
int FileZero(File file, off_t offset, off_t amount, uint32 wait_event_info)
Definition: fd.c:2324
off_t FileSize(File file)
Definition: fd.c:2409
int FileTruncate(File file, off_t offset, uint32 wait_event_info)
Definition: fd.c:2426
int pg_truncate(const char *path, off_t length)
Definition: fd.c:720
#define IO_DIRECT_DATA
Definition: fd.h:54
static int FileWrite(File file, const void *buffer, size_t amount, off_t offset, uint32 wait_event_info)
Definition: fd.h:208
#define FILE_POSSIBLY_DELETED(err)
Definition: fd.h:78
int File
Definition: fd.h:51
#define PG_O_DIRECT
Definition: fd.h:97
#define MCXT_ALLOC_ZERO
Definition: fe_memutils.h:18
int compute_remaining_iovec(struct iovec *destination, const struct iovec *source, int iovcnt, size_t transferred)
Definition: file_utils.c:592
bool IsBinaryUpgrade
Definition: globals.c:117
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
void pfree(void *pointer)
Definition: mcxt.c:1431
MemoryContext TopMemoryContext
Definition: mcxt.c:141
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1451
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1034
void * palloc_aligned(Size size, Size alignto, int flags)
Definition: mcxt.c:1421
void * palloc(Size size)
Definition: mcxt.c:1201
void mdunlink(RelFileLocatorBackend rlocator, ForkNumber forknum, bool isRedo)
Definition: md.c:310
static void register_forget_request(RelFileLocatorBackend rlocator, ForkNumber forknum, BlockNumber segno)
Definition: md.c:1417
#define EXTENSION_CREATE_RECOVERY
Definition: md.c:111
static BlockNumber _mdnblocks(SMgrRelation reln, ForkNumber forknum, MdfdVec *seg)
Definition: md.c:1718
static void mdunlinkfork(RelFileLocatorBackend rlocator, ForkNumber forknum, bool isRedo)
Definition: md.c:347
void mdwritev(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void **buffers, BlockNumber nblocks, bool skipFsync)
Definition: md.c:931
bool mdfiletagmatches(const FileTag *ftag, const FileTag *candidate)
Definition: md.c:1811
bool mdexists(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:174
void mdreadv(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void **buffers, BlockNumber nblocks)
Definition: md.c:813
static void register_unlink_segment(RelFileLocatorBackend rlocator, ForkNumber forknum, BlockNumber segno)
Definition: md.c:1400
#define EXTENSION_DONT_OPEN
Definition: md.c:121
BlockNumber mdnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:1092
int mdunlinkfiletag(const FileTag *ftag, char *path)
Definition: md.c:1792
static MemoryContext MdCxt
Definition: md.c:89
void mdcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: md.c:193
void mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
Definition: md.c:463
static int do_truncate(const char *path)
Definition: md.c:326
void mdinit(void)
Definition: md.c:161
void mdclose(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:694
void mdzeroextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks, bool skipFsync)
Definition: md.c:528
static MdfdVec * _mdfd_openseg(SMgrRelation reln, ForkNumber forknum, BlockNumber segno, int oflags)
Definition: md.c:1542
static void register_dirty_segment(SMgrRelation reln, ForkNumber forknum, MdfdVec *seg)
Definition: md.c:1356
int mdsyncfiletag(const FileTag *ftag, char *path)
Definition: md.c:1739
void mdwriteback(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
Definition: md.c:1033
static MdfdVec * _mdfd_getseg(SMgrRelation reln, ForkNumber forknum, BlockNumber blkno, bool skipFsync, int behavior)
Definition: md.c:1587
#define EXTENSION_RETURN_NULL
Definition: md.c:107
static char * _mdfd_segpath(SMgrRelation reln, ForkNumber forknum, BlockNumber segno)
Definition: md.c:1519
bool mdprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, int nblocks)
Definition: md.c:717
void mdregistersync(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:1243
void mdopen(SMgrRelation reln)
Definition: md.c:683
#define EXTENSION_CREATE
Definition: md.c:109
static int _mdfd_open_flags(void)
Definition: md.c:147
#define INIT_MD_FILETAG(a, xx_rlocator, xx_forknum, xx_segno)
Definition: md.c:93
#define EXTENSION_DONT_CHECK_SIZE
Definition: md.c:119
#define EXTENSION_FAIL
Definition: md.c:105
static MdfdVec * mdopenfork(SMgrRelation reln, ForkNumber forknum, int behavior)
Definition: md.c:640
void DropRelationFiles(RelFileLocator *delrels, int ndelrels, bool isRedo)
Definition: md.c:1449
static int buffers_to_iovec(struct iovec *iov, void **buffers, int nblocks)
Definition: md.c:765
static void _fdvec_resize(SMgrRelation reln, ForkNumber forknum, int nseg)
Definition: md.c:1481
void ForgetDatabaseSyncRequests(Oid dbid)
Definition: md.c:1431
void mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
Definition: md.c:1149
void mdimmedsync(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:1294
struct _MdfdVec MdfdVec
#define AllocSetContextCreate
Definition: memutils.h:128
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:152
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:41
#define MAXPGPATH
#define PG_IO_ALIGN_SIZE
const void size_t len
#define PG_IOV_MAX
Definition: pg_iovec.h:37
@ IOOBJECT_RELATION
Definition: pgstat.h:280
@ IOCONTEXT_NORMAL
Definition: pgstat.h:290
@ IOOP_FSYNC
Definition: pgstat.h:300
instr_time pgstat_prepare_io_time(bool track_io_guc)
Definition: pgstat_io.c:100
void pgstat_count_io_op_time(IOObject io_object, IOContext io_context, IOOp io_op, instr_time start_time, uint32 cnt)
Definition: pgstat_io.c:122
#define sprintf
Definition: port.h:240
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
unsigned int Oid
Definition: postgres_ext.h:31
static int fd(const char *x, int i)
Definition: preproc-init.c:105
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define RelFileLocatorBackendIsTemp(rlocator)
ForkNumber
Definition: relpath.h:48
@ MAIN_FORKNUM
Definition: relpath.h:50
@ InvalidForkNumber
Definition: relpath.h:49
#define MAX_FORKNUM
Definition: relpath.h:62
#define relpath(rlocator, forknum)
Definition: relpath.h:94
#define relpathperm(rlocator, forknum)
Definition: relpath.h:90
void smgrclose(SMgrRelation reln)
Definition: smgr.c:321
SMgrRelation smgropen(RelFileLocator rlocator, BackendId backend)
Definition: smgr.c:199
void smgrdounlinkall(SMgrRelation *rels, int nrels, bool isRedo)
Definition: smgr.c:465
#define SmgrIsTemp(smgr)
Definition: smgr.h:73
Definition: sync.h:51
RelFileLocator rlocator
Definition: sync.h:54
int16 forknum
Definition: sync.h:53
uint64 segno
Definition: sync.h:55
RelFileLocator locator
RelFileNumber relNumber
int md_num_open_segs[MAX_FORKNUM+1]
Definition: smgr.h:60
struct _MdfdVec * md_seg_fds[MAX_FORKNUM+1]
Definition: smgr.h:61
RelFileLocatorBackend smgr_rlocator
Definition: smgr.h:37
Definition: md.c:84
File mdfd_vfd
Definition: md.c:85
BlockNumber mdfd_segno
Definition: md.c:86
bool RegisterSyncRequest(const FileTag *ftag, SyncRequestType type, bool retryOnError)
Definition: sync.c:585
@ SYNC_FILTER_REQUEST
Definition: sync.h:28
@ SYNC_FORGET_REQUEST
Definition: sync.h:27
@ SYNC_UNLINK_REQUEST
Definition: sync.h:26
@ SYNC_REQUEST
Definition: sync.h:25
bool InRecovery
Definition: xlogutils.c:53
void XLogDropRelation(RelFileLocator rlocator, ForkNumber forknum)
Definition: xlogutils.c:644