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-2023, 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 "miscadmin.h"
32 #include "pg_trace.h"
33 #include "pgstat.h"
34 #include "postmaster/bgwriter.h"
35 #include "storage/bufmgr.h"
36 #include "storage/fd.h"
37 #include "storage/md.h"
38 #include "storage/relfilelocator.h"
39 #include "storage/smgr.h"
40 #include "storage/sync.h"
41 #include "utils/hsearch.h"
42 #include "utils/memutils.h"
43 
44 /*
45  * The magnetic disk storage manager keeps track of open file
46  * descriptors in its own descriptor pool. This is done to make it
47  * easier to support relations that are larger than the operating
48  * system's file size limit (often 2GBytes). In order to do that,
49  * we break relations up into "segment" files that are each shorter than
50  * the OS file size limit. The segment size is set by the RELSEG_SIZE
51  * configuration constant in pg_config.h.
52  *
53  * On disk, a relation must consist of consecutively numbered segment
54  * files in the pattern
55  * -- Zero or more full segments of exactly RELSEG_SIZE blocks each
56  * -- Exactly one partial segment of size 0 <= size < RELSEG_SIZE blocks
57  * -- Optionally, any number of inactive segments of size 0 blocks.
58  * The full and partial segments are collectively the "active" segments.
59  * Inactive segments are those that once contained data but are currently
60  * not needed because of an mdtruncate() operation. The reason for leaving
61  * them present at size zero, rather than unlinking them, is that other
62  * backends and/or the checkpointer might be holding open file references to
63  * such segments. If the relation expands again after mdtruncate(), such
64  * that a deactivated segment becomes active again, it is important that
65  * such file references still be valid --- else data might get written
66  * out to an unlinked old copy of a segment file that will eventually
67  * disappear.
68  *
69  * File descriptors are stored in the per-fork md_seg_fds arrays inside
70  * SMgrRelation. The length of these arrays is stored in md_num_open_segs.
71  * Note that a fork's md_num_open_segs having a specific value does not
72  * necessarily mean the relation doesn't have additional segments; we may
73  * just not have opened the next segment yet. (We could not have "all
74  * segments are in the array" as an invariant anyway, since another backend
75  * could extend the relation while we aren't looking.) We do not have
76  * entries for inactive segments, however; as soon as we find a partial
77  * segment, we assume that any subsequent segments are inactive.
78  *
79  * The entire MdfdVec array is palloc'd in the MdCxt memory context.
80  */
81 
82 typedef struct _MdfdVec
83 {
84  File mdfd_vfd; /* fd number in fd.c's pool */
85  BlockNumber mdfd_segno; /* segment number, from 0 */
87 
88 static MemoryContext MdCxt; /* context for all MdfdVec objects */
89 
90 
91 /* Populate a file tag describing an md.c segment file. */
92 #define INIT_MD_FILETAG(a,xx_rlocator,xx_forknum,xx_segno) \
93 ( \
94  memset(&(a), 0, sizeof(FileTag)), \
95  (a).handler = SYNC_HANDLER_MD, \
96  (a).rlocator = (xx_rlocator), \
97  (a).forknum = (xx_forknum), \
98  (a).segno = (xx_segno) \
99 )
100 
101 
102 /*** behavior for mdopen & _mdfd_getseg ***/
103 /* ereport if segment not present */
104 #define EXTENSION_FAIL (1 << 0)
105 /* return NULL if segment not present */
106 #define EXTENSION_RETURN_NULL (1 << 1)
107 /* create new segments as needed */
108 #define EXTENSION_CREATE (1 << 2)
109 /* create new segments if needed during recovery */
110 #define EXTENSION_CREATE_RECOVERY (1 << 3)
111 /*
112  * Allow opening segments which are preceded by segments smaller than
113  * RELSEG_SIZE, e.g. inactive segments (see above). Note that this breaks
114  * mdnblocks() and related functionality henceforth - which currently is ok,
115  * because this is only required in the checkpointer which never uses
116  * mdnblocks().
117  */
118 #define EXTENSION_DONT_CHECK_SIZE (1 << 4)
119 /* don't try to open a segment, if not already open */
120 #define EXTENSION_DONT_OPEN (1 << 5)
121 
122 
123 /* local routines */
124 static void mdunlinkfork(RelFileLocatorBackend rlocator, ForkNumber forknum,
125  bool isRedo);
126 static MdfdVec *mdopenfork(SMgrRelation reln, ForkNumber forknum, int behavior);
127 static void register_dirty_segment(SMgrRelation reln, ForkNumber forknum,
128  MdfdVec *seg);
129 static void register_unlink_segment(RelFileLocatorBackend rlocator, ForkNumber forknum,
130  BlockNumber segno);
131 static void register_forget_request(RelFileLocatorBackend rlocator, ForkNumber forknum,
132  BlockNumber segno);
133 static void _fdvec_resize(SMgrRelation reln,
134  ForkNumber forknum,
135  int nseg);
136 static char *_mdfd_segpath(SMgrRelation reln, ForkNumber forknum,
137  BlockNumber segno);
138 static MdfdVec *_mdfd_openseg(SMgrRelation reln, ForkNumber forknum,
139  BlockNumber segno, int oflags);
140 static MdfdVec *_mdfd_getseg(SMgrRelation reln, ForkNumber forknum,
141  BlockNumber blkno, bool skipFsync, int behavior);
142 static BlockNumber _mdnblocks(SMgrRelation reln, ForkNumber forknum,
143  MdfdVec *seg);
144 
145 
146 /*
147  * mdinit() -- Initialize private state for magnetic disk storage manager.
148  */
149 void
150 mdinit(void)
151 {
153  "MdSmgr",
155 }
156 
157 /*
158  * mdexists() -- Does the physical file exist?
159  *
160  * Note: this will return true for lingering files, with pending deletions
161  */
162 bool
164 {
165  /*
166  * Close it first, to ensure that we notice if the fork has been unlinked
167  * since we opened it. As an optimization, we can skip that in recovery,
168  * which already closes relations when dropping them.
169  */
170  if (!InRecovery)
171  mdclose(reln, forknum);
172 
173  return (mdopenfork(reln, forknum, EXTENSION_RETURN_NULL) != NULL);
174 }
175 
176 /*
177  * mdcreate() -- Create a new relation on magnetic disk.
178  *
179  * If isRedo is true, it's okay for the relation to exist already.
180  */
181 void
182 mdcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
183 {
184  MdfdVec *mdfd;
185  char *path;
186  File fd;
187 
188  if (isRedo && reln->md_num_open_segs[forknum] > 0)
189  return; /* created and opened already... */
190 
191  Assert(reln->md_num_open_segs[forknum] == 0);
192 
193  /*
194  * We may be using the target table space for the first time in this
195  * database, so create a per-database subdirectory if needed.
196  *
197  * XXX this is a fairly ugly violation of module layering, but this seems
198  * to be the best place to put the check. Maybe TablespaceCreateDbspace
199  * should be here and not in commands/tablespace.c? But that would imply
200  * importing a lot of stuff that smgr.c oughtn't know, either.
201  */
204  isRedo);
205 
206  path = relpath(reln->smgr_rlocator, forknum);
207 
208  fd = PathNameOpenFile(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY);
209 
210  if (fd < 0)
211  {
212  int save_errno = errno;
213 
214  if (isRedo)
215  fd = PathNameOpenFile(path, O_RDWR | PG_BINARY);
216  if (fd < 0)
217  {
218  /* be sure to report the error reported by create, not open */
219  errno = save_errno;
220  ereport(ERROR,
222  errmsg("could not create file \"%s\": %m", path)));
223  }
224  }
225 
226  pfree(path);
227 
228  _fdvec_resize(reln, forknum, 1);
229  mdfd = &reln->md_seg_fds[forknum][0];
230  mdfd->mdfd_vfd = fd;
231  mdfd->mdfd_segno = 0;
232 }
233 
234 /*
235  * mdunlink() -- Unlink a relation.
236  *
237  * Note that we're passed a RelFileLocatorBackend --- by the time this is called,
238  * there won't be an SMgrRelation hashtable entry anymore.
239  *
240  * forknum can be a fork number to delete a specific fork, or InvalidForkNumber
241  * to delete all forks.
242  *
243  * For regular relations, we don't unlink the first segment file of the rel,
244  * but just truncate it to zero length, and record a request to unlink it after
245  * the next checkpoint. Additional segments can be unlinked immediately,
246  * however. Leaving the empty file in place prevents that relfilenumber
247  * from being reused. The scenario this protects us from is:
248  * 1. We delete a relation (and commit, and actually remove its file).
249  * 2. We create a new relation, which by chance gets the same relfilenumber as
250  * the just-deleted one (OIDs must've wrapped around for that to happen).
251  * 3. We crash before another checkpoint occurs.
252  * During replay, we would delete the file and then recreate it, which is fine
253  * if the contents of the file were repopulated by subsequent WAL entries.
254  * But if we didn't WAL-log insertions, but instead relied on fsyncing the
255  * file after populating it (as we do at wal_level=minimal), the contents of
256  * the file would be lost forever. By leaving the empty file until after the
257  * next checkpoint, we prevent reassignment of the relfilenumber until it's
258  * safe, because relfilenumber assignment skips over any existing file.
259  *
260  * Additional segments, if any, are truncated and then unlinked. The reason
261  * for truncating is that other backends may still hold open FDs for these at
262  * the smgr level, so that the kernel can't remove the file yet. We want to
263  * reclaim the disk space right away despite that.
264  *
265  * We do not need to go through this dance for temp relations, though, because
266  * we never make WAL entries for temp rels, and so a temp rel poses no threat
267  * to the health of a regular rel that has taken over its relfilenumber.
268  * The fact that temp rels and regular rels have different file naming
269  * patterns provides additional safety. Other backends shouldn't have open
270  * FDs for them, either.
271  *
272  * We also don't do it while performing a binary upgrade. There is no reuse
273  * hazard in that case, since after a crash or even a simple ERROR, the
274  * upgrade fails and the whole cluster must be recreated from scratch.
275  * Furthermore, it is important to remove the files from disk immediately,
276  * because we may be about to reuse the same relfilenumber.
277  *
278  * All the above applies only to the relation's main fork; other forks can
279  * just be removed immediately, since they are not needed to prevent the
280  * relfilenumber from being recycled. Also, we do not carefully
281  * track whether other forks have been created or not, but just attempt to
282  * unlink them unconditionally; so we should never complain about ENOENT.
283  *
284  * If isRedo is true, it's unsurprising for the relation to be already gone.
285  * Also, we should remove the file immediately instead of queuing a request
286  * for later, since during redo there's no possibility of creating a
287  * conflicting relation.
288  *
289  * Note: we currently just never warn about ENOENT at all. We could warn in
290  * the main-fork, non-isRedo case, but it doesn't seem worth the trouble.
291  *
292  * Note: any failure should be reported as WARNING not ERROR, because
293  * we are usually not in a transaction anymore when this is called.
294  */
295 void
296 mdunlink(RelFileLocatorBackend rlocator, ForkNumber forknum, bool isRedo)
297 {
298  /* Now do the per-fork work */
299  if (forknum == InvalidForkNumber)
300  {
301  for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
302  mdunlinkfork(rlocator, forknum, isRedo);
303  }
304  else
305  mdunlinkfork(rlocator, forknum, isRedo);
306 }
307 
308 /*
309  * Truncate a file to release disk space.
310  */
311 static int
312 do_truncate(const char *path)
313 {
314  int save_errno;
315  int ret;
316 
317  ret = pg_truncate(path, 0);
318 
319  /* Log a warning here to avoid repetition in callers. */
320  if (ret < 0 && errno != ENOENT)
321  {
322  save_errno = errno;
325  errmsg("could not truncate file \"%s\": %m", path)));
326  errno = save_errno;
327  }
328 
329  return ret;
330 }
331 
332 static void
333 mdunlinkfork(RelFileLocatorBackend rlocator, ForkNumber forknum, bool isRedo)
334 {
335  char *path;
336  int ret;
337  int save_errno;
338 
339  path = relpath(rlocator, forknum);
340 
341  /*
342  * Truncate and then unlink the first segment, or just register a request
343  * to unlink it later, as described in the comments for mdunlink().
344  */
345  if (isRedo || IsBinaryUpgrade || forknum != MAIN_FORKNUM ||
346  RelFileLocatorBackendIsTemp(rlocator))
347  {
348  if (!RelFileLocatorBackendIsTemp(rlocator))
349  {
350  /* Prevent other backends' fds from holding on to the disk space */
351  ret = do_truncate(path);
352 
353  /* Forget any pending sync requests for the first segment */
354  save_errno = errno;
355  register_forget_request(rlocator, forknum, 0 /* first seg */ );
356  errno = save_errno;
357  }
358  else
359  ret = 0;
360 
361  /* Next unlink the file, unless it was already found to be missing */
362  if (ret >= 0 || errno != ENOENT)
363  {
364  ret = unlink(path);
365  if (ret < 0 && errno != ENOENT)
366  {
367  save_errno = errno;
370  errmsg("could not remove file \"%s\": %m", path)));
371  errno = save_errno;
372  }
373  }
374  }
375  else
376  {
377  /* Prevent other backends' fds from holding on to the disk space */
378  ret = do_truncate(path);
379 
380  /* Register request to unlink first segment later */
381  save_errno = errno;
382  register_unlink_segment(rlocator, forknum, 0 /* first seg */ );
383  errno = save_errno;
384  }
385 
386  /*
387  * Delete any additional segments.
388  *
389  * Note that because we loop until getting ENOENT, we will correctly
390  * remove all inactive segments as well as active ones. Ideally we'd
391  * continue the loop until getting exactly that errno, but that risks an
392  * infinite loop if the problem is directory-wide (for instance, if we
393  * suddenly can't read the data directory itself). We compromise by
394  * continuing after a non-ENOENT truncate error, but stopping after any
395  * unlink error. If there is indeed a directory-wide problem, additional
396  * unlink attempts wouldn't work anyway.
397  */
398  if (ret >= 0 || errno != ENOENT)
399  {
400  char *segpath = (char *) palloc(strlen(path) + 12);
401  BlockNumber segno;
402 
403  for (segno = 1;; segno++)
404  {
405  sprintf(segpath, "%s.%u", path, segno);
406 
407  if (!RelFileLocatorBackendIsTemp(rlocator))
408  {
409  /*
410  * Prevent other backends' fds from holding on to the disk
411  * space. We're done if we see ENOENT, though.
412  */
413  if (do_truncate(segpath) < 0 && errno == ENOENT)
414  break;
415 
416  /*
417  * Forget any pending sync requests for this segment before we
418  * try to unlink.
419  */
420  register_forget_request(rlocator, forknum, segno);
421  }
422 
423  if (unlink(segpath) < 0)
424  {
425  /* ENOENT is expected after the last segment... */
426  if (errno != ENOENT)
429  errmsg("could not remove file \"%s\": %m", segpath)));
430  break;
431  }
432  }
433  pfree(segpath);
434  }
435 
436  pfree(path);
437 }
438 
439 /*
440  * mdextend() -- Add a block to the specified relation.
441  *
442  * The semantics are nearly the same as mdwrite(): write at the
443  * specified position. However, this is to be used for the case of
444  * extending a relation (i.e., blocknum is at or beyond the current
445  * EOF). Note that we assume writing a block beyond current EOF
446  * causes intervening file space to become filled with zeroes.
447  */
448 void
449 mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
450  const void *buffer, bool skipFsync)
451 {
452  off_t seekpos;
453  int nbytes;
454  MdfdVec *v;
455 
456  /* This assert is too expensive to have on normally ... */
457 #ifdef CHECK_WRITE_VS_EXTEND
458  Assert(blocknum >= mdnblocks(reln, forknum));
459 #endif
460 
461  /*
462  * If a relation manages to grow to 2^32-1 blocks, refuse to extend it any
463  * more --- we mustn't create a block whose number actually is
464  * InvalidBlockNumber. (Note that this failure should be unreachable
465  * because of upstream checks in bufmgr.c.)
466  */
467  if (blocknum == InvalidBlockNumber)
468  ereport(ERROR,
469  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
470  errmsg("cannot extend file \"%s\" beyond %u blocks",
471  relpath(reln->smgr_rlocator, forknum),
473 
474  v = _mdfd_getseg(reln, forknum, blocknum, skipFsync, EXTENSION_CREATE);
475 
476  seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
477 
478  Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
479 
480  if ((nbytes = FileWrite(v->mdfd_vfd, buffer, BLCKSZ, seekpos, WAIT_EVENT_DATA_FILE_EXTEND)) != BLCKSZ)
481  {
482  if (nbytes < 0)
483  ereport(ERROR,
485  errmsg("could not extend file \"%s\": %m",
486  FilePathName(v->mdfd_vfd)),
487  errhint("Check free disk space.")));
488  /* short write: complain appropriately */
489  ereport(ERROR,
490  (errcode(ERRCODE_DISK_FULL),
491  errmsg("could not extend file \"%s\": wrote only %d of %d bytes at block %u",
493  nbytes, BLCKSZ, blocknum),
494  errhint("Check free disk space.")));
495  }
496 
497  if (!skipFsync && !SmgrIsTemp(reln))
498  register_dirty_segment(reln, forknum, v);
499 
500  Assert(_mdnblocks(reln, forknum, v) <= ((BlockNumber) RELSEG_SIZE));
501 }
502 
503 /*
504  * mdopenfork() -- Open one fork of the specified relation.
505  *
506  * Note we only open the first segment, when there are multiple segments.
507  *
508  * If first segment is not present, either ereport or return NULL according
509  * to "behavior". We treat EXTENSION_CREATE the same as EXTENSION_FAIL;
510  * EXTENSION_CREATE means it's OK to extend an existing relation, not to
511  * invent one out of whole cloth.
512  */
513 static MdfdVec *
514 mdopenfork(SMgrRelation reln, ForkNumber forknum, int behavior)
515 {
516  MdfdVec *mdfd;
517  char *path;
518  File fd;
519 
520  /* No work if already open */
521  if (reln->md_num_open_segs[forknum] > 0)
522  return &reln->md_seg_fds[forknum][0];
523 
524  path = relpath(reln->smgr_rlocator, forknum);
525 
526  fd = PathNameOpenFile(path, O_RDWR | PG_BINARY);
527 
528  if (fd < 0)
529  {
530  if ((behavior & EXTENSION_RETURN_NULL) &&
531  FILE_POSSIBLY_DELETED(errno))
532  {
533  pfree(path);
534  return NULL;
535  }
536  ereport(ERROR,
538  errmsg("could not open file \"%s\": %m", path)));
539  }
540 
541  pfree(path);
542 
543  _fdvec_resize(reln, forknum, 1);
544  mdfd = &reln->md_seg_fds[forknum][0];
545  mdfd->mdfd_vfd = fd;
546  mdfd->mdfd_segno = 0;
547 
548  Assert(_mdnblocks(reln, forknum, mdfd) <= ((BlockNumber) RELSEG_SIZE));
549 
550  return mdfd;
551 }
552 
553 /*
554  * mdopen() -- Initialize newly-opened relation.
555  */
556 void
558 {
559  /* mark it not open */
560  for (int forknum = 0; forknum <= MAX_FORKNUM; forknum++)
561  reln->md_num_open_segs[forknum] = 0;
562 }
563 
564 /*
565  * mdclose() -- Close the specified relation, if it isn't closed already.
566  */
567 void
569 {
570  int nopensegs = reln->md_num_open_segs[forknum];
571 
572  /* No work if already closed */
573  if (nopensegs == 0)
574  return;
575 
576  /* close segments starting from the end */
577  while (nopensegs > 0)
578  {
579  MdfdVec *v = &reln->md_seg_fds[forknum][nopensegs - 1];
580 
581  FileClose(v->mdfd_vfd);
582  _fdvec_resize(reln, forknum, nopensegs - 1);
583  nopensegs--;
584  }
585 }
586 
587 /*
588  * mdprefetch() -- Initiate asynchronous read of the specified block of a relation
589  */
590 bool
592 {
593 #ifdef USE_PREFETCH
594  off_t seekpos;
595  MdfdVec *v;
596 
597  v = _mdfd_getseg(reln, forknum, blocknum, false,
599  if (v == NULL)
600  return false;
601 
602  seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
603 
604  Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
605 
606  (void) FilePrefetch(v->mdfd_vfd, seekpos, BLCKSZ, WAIT_EVENT_DATA_FILE_PREFETCH);
607 #endif /* USE_PREFETCH */
608 
609  return true;
610 }
611 
612 /*
613  * mdwriteback() -- Tell the kernel to write pages back to storage.
614  *
615  * This accepts a range of blocks because flushing several pages at once is
616  * considerably more efficient than doing so individually.
617  */
618 void
620  BlockNumber blocknum, BlockNumber nblocks)
621 {
622  /*
623  * Issue flush requests in as few requests as possible; have to split at
624  * segment boundaries though, since those are actually separate files.
625  */
626  while (nblocks > 0)
627  {
628  BlockNumber nflush = nblocks;
629  off_t seekpos;
630  MdfdVec *v;
631  int segnum_start,
632  segnum_end;
633 
634  v = _mdfd_getseg(reln, forknum, blocknum, true /* not used */ ,
636 
637  /*
638  * We might be flushing buffers of already removed relations, that's
639  * ok, just ignore that case. If the segment file wasn't open already
640  * (ie from a recent mdwrite()), then we don't want to re-open it, to
641  * avoid a race with PROCSIGNAL_BARRIER_SMGRRELEASE that might leave
642  * us with a descriptor to a file that is about to be unlinked.
643  */
644  if (!v)
645  return;
646 
647  /* compute offset inside the current segment */
648  segnum_start = blocknum / RELSEG_SIZE;
649 
650  /* compute number of desired writes within the current segment */
651  segnum_end = (blocknum + nblocks - 1) / RELSEG_SIZE;
652  if (segnum_start != segnum_end)
653  nflush = RELSEG_SIZE - (blocknum % ((BlockNumber) RELSEG_SIZE));
654 
655  Assert(nflush >= 1);
656  Assert(nflush <= nblocks);
657 
658  seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
659 
660  FileWriteback(v->mdfd_vfd, seekpos, (off_t) BLCKSZ * nflush, WAIT_EVENT_DATA_FILE_FLUSH);
661 
662  nblocks -= nflush;
663  blocknum += nflush;
664  }
665 }
666 
667 /*
668  * mdread() -- Read the specified block from a relation.
669  */
670 void
671 mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
672  void *buffer)
673 {
674  off_t seekpos;
675  int nbytes;
676  MdfdVec *v;
677 
678  TRACE_POSTGRESQL_SMGR_MD_READ_START(forknum, blocknum,
682  reln->smgr_rlocator.backend);
683 
684  v = _mdfd_getseg(reln, forknum, blocknum, false,
686 
687  seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
688 
689  Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
690 
691  nbytes = FileRead(v->mdfd_vfd, buffer, BLCKSZ, seekpos, WAIT_EVENT_DATA_FILE_READ);
692 
693  TRACE_POSTGRESQL_SMGR_MD_READ_DONE(forknum, blocknum,
697  reln->smgr_rlocator.backend,
698  nbytes,
699  BLCKSZ);
700 
701  if (nbytes != BLCKSZ)
702  {
703  if (nbytes < 0)
704  ereport(ERROR,
706  errmsg("could not read block %u in file \"%s\": %m",
707  blocknum, FilePathName(v->mdfd_vfd))));
708 
709  /*
710  * Short read: we are at or past EOF, or we read a partial block at
711  * EOF. Normally this is an error; upper levels should never try to
712  * read a nonexistent block. However, if zero_damaged_pages is ON or
713  * we are InRecovery, we should instead return zeroes without
714  * complaining. This allows, for example, the case of trying to
715  * update a block that was later truncated away.
716  */
718  MemSet(buffer, 0, BLCKSZ);
719  else
720  ereport(ERROR,
722  errmsg("could not read block %u in file \"%s\": read only %d of %d bytes",
723  blocknum, FilePathName(v->mdfd_vfd),
724  nbytes, BLCKSZ)));
725  }
726 }
727 
728 /*
729  * mdwrite() -- Write the supplied block at the appropriate location.
730  *
731  * This is to be used only for updating already-existing blocks of a
732  * relation (ie, those before the current EOF). To extend a relation,
733  * use mdextend().
734  */
735 void
736 mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
737  const void *buffer, bool skipFsync)
738 {
739  off_t seekpos;
740  int nbytes;
741  MdfdVec *v;
742 
743  /* This assert is too expensive to have on normally ... */
744 #ifdef CHECK_WRITE_VS_EXTEND
745  Assert(blocknum < mdnblocks(reln, forknum));
746 #endif
747 
748  TRACE_POSTGRESQL_SMGR_MD_WRITE_START(forknum, blocknum,
752  reln->smgr_rlocator.backend);
753 
754  v = _mdfd_getseg(reln, forknum, blocknum, skipFsync,
756 
757  seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
758 
759  Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
760 
761  nbytes = FileWrite(v->mdfd_vfd, buffer, BLCKSZ, seekpos, WAIT_EVENT_DATA_FILE_WRITE);
762 
763  TRACE_POSTGRESQL_SMGR_MD_WRITE_DONE(forknum, blocknum,
767  reln->smgr_rlocator.backend,
768  nbytes,
769  BLCKSZ);
770 
771  if (nbytes != BLCKSZ)
772  {
773  if (nbytes < 0)
774  ereport(ERROR,
776  errmsg("could not write block %u in file \"%s\": %m",
777  blocknum, FilePathName(v->mdfd_vfd))));
778  /* short write: complain appropriately */
779  ereport(ERROR,
780  (errcode(ERRCODE_DISK_FULL),
781  errmsg("could not write block %u in file \"%s\": wrote only %d of %d bytes",
782  blocknum,
784  nbytes, BLCKSZ),
785  errhint("Check free disk space.")));
786  }
787 
788  if (!skipFsync && !SmgrIsTemp(reln))
789  register_dirty_segment(reln, forknum, v);
790 }
791 
792 /*
793  * mdnblocks() -- Get the number of blocks stored in a relation.
794  *
795  * Important side effect: all active segments of the relation are opened
796  * and added to the md_seg_fds array. If this routine has not been
797  * called, then only segments up to the last one actually touched
798  * are present in the array.
799  */
802 {
803  MdfdVec *v;
804  BlockNumber nblocks;
805  BlockNumber segno;
806 
807  mdopenfork(reln, forknum, EXTENSION_FAIL);
808 
809  /* mdopen has opened the first segment */
810  Assert(reln->md_num_open_segs[forknum] > 0);
811 
812  /*
813  * Start from the last open segments, to avoid redundant seeks. We have
814  * previously verified that these segments are exactly RELSEG_SIZE long,
815  * and it's useless to recheck that each time.
816  *
817  * NOTE: this assumption could only be wrong if another backend has
818  * truncated the relation. We rely on higher code levels to handle that
819  * scenario by closing and re-opening the md fd, which is handled via
820  * relcache flush. (Since the checkpointer doesn't participate in
821  * relcache flush, it could have segment entries for inactive segments;
822  * that's OK because the checkpointer never needs to compute relation
823  * size.)
824  */
825  segno = reln->md_num_open_segs[forknum] - 1;
826  v = &reln->md_seg_fds[forknum][segno];
827 
828  for (;;)
829  {
830  nblocks = _mdnblocks(reln, forknum, v);
831  if (nblocks > ((BlockNumber) RELSEG_SIZE))
832  elog(FATAL, "segment too big");
833  if (nblocks < ((BlockNumber) RELSEG_SIZE))
834  return (segno * ((BlockNumber) RELSEG_SIZE)) + nblocks;
835 
836  /*
837  * If segment is exactly RELSEG_SIZE, advance to next one.
838  */
839  segno++;
840 
841  /*
842  * We used to pass O_CREAT here, but that has the disadvantage that it
843  * might create a segment which has vanished through some operating
844  * system misadventure. In such a case, creating the segment here
845  * undermines _mdfd_getseg's attempts to notice and report an error
846  * upon access to a missing segment.
847  */
848  v = _mdfd_openseg(reln, forknum, segno, 0);
849  if (v == NULL)
850  return segno * ((BlockNumber) RELSEG_SIZE);
851  }
852 }
853 
854 /*
855  * mdtruncate() -- Truncate relation to specified number of blocks.
856  */
857 void
859 {
860  BlockNumber curnblk;
861  BlockNumber priorblocks;
862  int curopensegs;
863 
864  /*
865  * NOTE: mdnblocks makes sure we have opened all active segments, so that
866  * truncation loop will get them all!
867  */
868  curnblk = mdnblocks(reln, forknum);
869  if (nblocks > curnblk)
870  {
871  /* Bogus request ... but no complaint if InRecovery */
872  if (InRecovery)
873  return;
874  ereport(ERROR,
875  (errmsg("could not truncate file \"%s\" to %u blocks: it's only %u blocks now",
876  relpath(reln->smgr_rlocator, forknum),
877  nblocks, curnblk)));
878  }
879  if (nblocks == curnblk)
880  return; /* no work */
881 
882  /*
883  * Truncate segments, starting at the last one. Starting at the end makes
884  * managing the memory for the fd array easier, should there be errors.
885  */
886  curopensegs = reln->md_num_open_segs[forknum];
887  while (curopensegs > 0)
888  {
889  MdfdVec *v;
890 
891  priorblocks = (curopensegs - 1) * RELSEG_SIZE;
892 
893  v = &reln->md_seg_fds[forknum][curopensegs - 1];
894 
895  if (priorblocks > nblocks)
896  {
897  /*
898  * This segment is no longer active. We truncate the file, but do
899  * not delete it, for reasons explained in the header comments.
900  */
902  ereport(ERROR,
904  errmsg("could not truncate file \"%s\": %m",
905  FilePathName(v->mdfd_vfd))));
906 
907  if (!SmgrIsTemp(reln))
908  register_dirty_segment(reln, forknum, v);
909 
910  /* we never drop the 1st segment */
911  Assert(v != &reln->md_seg_fds[forknum][0]);
912 
913  FileClose(v->mdfd_vfd);
914  _fdvec_resize(reln, forknum, curopensegs - 1);
915  }
916  else if (priorblocks + ((BlockNumber) RELSEG_SIZE) > nblocks)
917  {
918  /*
919  * This is the last segment we want to keep. Truncate the file to
920  * the right length. NOTE: if nblocks is exactly a multiple K of
921  * RELSEG_SIZE, we will truncate the K+1st segment to 0 length but
922  * keep it. This adheres to the invariant given in the header
923  * comments.
924  */
925  BlockNumber lastsegblocks = nblocks - priorblocks;
926 
927  if (FileTruncate(v->mdfd_vfd, (off_t) lastsegblocks * BLCKSZ, WAIT_EVENT_DATA_FILE_TRUNCATE) < 0)
928  ereport(ERROR,
930  errmsg("could not truncate file \"%s\" to %u blocks: %m",
932  nblocks)));
933  if (!SmgrIsTemp(reln))
934  register_dirty_segment(reln, forknum, v);
935  }
936  else
937  {
938  /*
939  * We still need this segment, so nothing to do for this and any
940  * earlier segment.
941  */
942  break;
943  }
944  curopensegs--;
945  }
946 }
947 
948 /*
949  * mdimmedsync() -- Immediately sync a relation to stable storage.
950  *
951  * Note that only writes already issued are synced; this routine knows
952  * nothing of dirty buffers that may exist inside the buffer manager. We
953  * sync active and inactive segments; smgrDoPendingSyncs() relies on this.
954  * Consider a relation skipping WAL. Suppose a checkpoint syncs blocks of
955  * some segment, then mdtruncate() renders that segment inactive. If we
956  * crash before the next checkpoint syncs the newly-inactive segment, that
957  * segment may survive recovery, reintroducing unwanted data into the table.
958  */
959 void
961 {
962  int segno;
963  int min_inactive_seg;
964 
965  /*
966  * NOTE: mdnblocks makes sure we have opened all active segments, so that
967  * fsync loop will get them all!
968  */
969  mdnblocks(reln, forknum);
970 
971  min_inactive_seg = segno = reln->md_num_open_segs[forknum];
972 
973  /*
974  * Temporarily open inactive segments, then close them after sync. There
975  * may be some inactive segments left opened after fsync() error, but that
976  * is harmless. We don't bother to clean them up and take a risk of
977  * further trouble. The next mdclose() will soon close them.
978  */
979  while (_mdfd_openseg(reln, forknum, segno, 0) != NULL)
980  segno++;
981 
982  while (segno > 0)
983  {
984  MdfdVec *v = &reln->md_seg_fds[forknum][segno - 1];
985 
986  /*
987  * fsyncs done through mdimmedsync() should be tracked in a separate
988  * IOContext than those done through mdsyncfiletag() to differentiate
989  * between unavoidable client backend fsyncs (e.g. those done during
990  * index build) and those which ideally would have been done by the
991  * checkpointer. Since other IO operations bypassing the buffer
992  * manager could also be tracked in such an IOContext, wait until
993  * these are also tracked to track immediate fsyncs.
994  */
998  errmsg("could not fsync file \"%s\": %m",
999  FilePathName(v->mdfd_vfd))));
1000 
1001  /* Close inactive segments immediately */
1002  if (segno > min_inactive_seg)
1003  {
1004  FileClose(v->mdfd_vfd);
1005  _fdvec_resize(reln, forknum, segno - 1);
1006  }
1007 
1008  segno--;
1009  }
1010 }
1011 
1012 /*
1013  * register_dirty_segment() -- Mark a relation segment as needing fsync
1014  *
1015  * If there is a local pending-ops table, just make an entry in it for
1016  * ProcessSyncRequests to process later. Otherwise, try to pass off the
1017  * fsync request to the checkpointer process. If that fails, just do the
1018  * fsync locally before returning (we hope this will not happen often
1019  * enough to be a performance problem).
1020  */
1021 static void
1023 {
1024  FileTag tag;
1025 
1026  INIT_MD_FILETAG(tag, reln->smgr_rlocator.locator, forknum, seg->mdfd_segno);
1027 
1028  /* Temp relations should never be fsync'd */
1029  Assert(!SmgrIsTemp(reln));
1030 
1031  if (!RegisterSyncRequest(&tag, SYNC_REQUEST, false /* retryOnError */ ))
1032  {
1033  /*
1034  * We have no way of knowing if the current IOContext is
1035  * IOCONTEXT_NORMAL or IOCONTEXT_[BULKREAD, BULKWRITE, VACUUM] at this
1036  * point, so count the fsync as being in the IOCONTEXT_NORMAL
1037  * IOContext. This is probably okay, because the number of backend
1038  * fsyncs doesn't say anything about the efficacy of the
1039  * BufferAccessStrategy. And counting both fsyncs done in
1040  * IOCONTEXT_NORMAL and IOCONTEXT_[BULKREAD, BULKWRITE, VACUUM] under
1041  * IOCONTEXT_NORMAL is likely clearer when investigating the number of
1042  * backend fsyncs.
1043  */
1045 
1046  ereport(DEBUG1,
1047  (errmsg_internal("could not forward fsync request because request queue is full")));
1048 
1052  errmsg("could not fsync file \"%s\": %m",
1053  FilePathName(seg->mdfd_vfd))));
1054  }
1055 }
1056 
1057 /*
1058  * register_unlink_segment() -- Schedule a file to be deleted after next checkpoint
1059  */
1060 static void
1062  BlockNumber segno)
1063 {
1064  FileTag tag;
1065 
1066  INIT_MD_FILETAG(tag, rlocator.locator, forknum, segno);
1067 
1068  /* Should never be used with temp relations */
1069  Assert(!RelFileLocatorBackendIsTemp(rlocator));
1070 
1071  RegisterSyncRequest(&tag, SYNC_UNLINK_REQUEST, true /* retryOnError */ );
1072 }
1073 
1074 /*
1075  * register_forget_request() -- forget any fsyncs for a relation fork's segment
1076  */
1077 static void
1079  BlockNumber segno)
1080 {
1081  FileTag tag;
1082 
1083  INIT_MD_FILETAG(tag, rlocator.locator, forknum, segno);
1084 
1085  RegisterSyncRequest(&tag, SYNC_FORGET_REQUEST, true /* retryOnError */ );
1086 }
1087 
1088 /*
1089  * ForgetDatabaseSyncRequests -- forget any fsyncs and unlinks for a DB
1090  */
1091 void
1093 {
1094  FileTag tag;
1095  RelFileLocator rlocator;
1096 
1097  rlocator.dbOid = dbid;
1098  rlocator.spcOid = 0;
1099  rlocator.relNumber = 0;
1100 
1102 
1103  RegisterSyncRequest(&tag, SYNC_FILTER_REQUEST, true /* retryOnError */ );
1104 }
1105 
1106 /*
1107  * DropRelationFiles -- drop files of all given relations
1108  */
1109 void
1110 DropRelationFiles(RelFileLocator *delrels, int ndelrels, bool isRedo)
1111 {
1112  SMgrRelation *srels;
1113  int i;
1114 
1115  srels = palloc(sizeof(SMgrRelation) * ndelrels);
1116  for (i = 0; i < ndelrels; i++)
1117  {
1118  SMgrRelation srel = smgropen(delrels[i], InvalidBackendId);
1119 
1120  if (isRedo)
1121  {
1122  ForkNumber fork;
1123 
1124  for (fork = 0; fork <= MAX_FORKNUM; fork++)
1125  XLogDropRelation(delrels[i], fork);
1126  }
1127  srels[i] = srel;
1128  }
1129 
1130  smgrdounlinkall(srels, ndelrels, isRedo);
1131 
1132  for (i = 0; i < ndelrels; i++)
1133  smgrclose(srels[i]);
1134  pfree(srels);
1135 }
1136 
1137 
1138 /*
1139  * _fdvec_resize() -- Resize the fork's open segments array
1140  */
1141 static void
1143  ForkNumber forknum,
1144  int nseg)
1145 {
1146  if (nseg == 0)
1147  {
1148  if (reln->md_num_open_segs[forknum] > 0)
1149  {
1150  pfree(reln->md_seg_fds[forknum]);
1151  reln->md_seg_fds[forknum] = NULL;
1152  }
1153  }
1154  else if (reln->md_num_open_segs[forknum] == 0)
1155  {
1156  reln->md_seg_fds[forknum] =
1157  MemoryContextAlloc(MdCxt, sizeof(MdfdVec) * nseg);
1158  }
1159  else
1160  {
1161  /*
1162  * It doesn't seem worthwhile complicating the code to amortize
1163  * repalloc() calls. Those are far faster than PathNameOpenFile() or
1164  * FileClose(), and the memory context internally will sometimes avoid
1165  * doing an actual reallocation.
1166  */
1167  reln->md_seg_fds[forknum] =
1168  repalloc(reln->md_seg_fds[forknum],
1169  sizeof(MdfdVec) * nseg);
1170  }
1171 
1172  reln->md_num_open_segs[forknum] = nseg;
1173 }
1174 
1175 /*
1176  * Return the filename for the specified segment of the relation. The
1177  * returned string is palloc'd.
1178  */
1179 static char *
1181 {
1182  char *path,
1183  *fullpath;
1184 
1185  path = relpath(reln->smgr_rlocator, forknum);
1186 
1187  if (segno > 0)
1188  {
1189  fullpath = psprintf("%s.%u", path, segno);
1190  pfree(path);
1191  }
1192  else
1193  fullpath = path;
1194 
1195  return fullpath;
1196 }
1197 
1198 /*
1199  * Open the specified segment of the relation,
1200  * and make a MdfdVec object for it. Returns NULL on failure.
1201  */
1202 static MdfdVec *
1204  int oflags)
1205 {
1206  MdfdVec *v;
1207  File fd;
1208  char *fullpath;
1209 
1210  fullpath = _mdfd_segpath(reln, forknum, segno);
1211 
1212  /* open the file */
1213  fd = PathNameOpenFile(fullpath, O_RDWR | PG_BINARY | oflags);
1214 
1215  pfree(fullpath);
1216 
1217  if (fd < 0)
1218  return NULL;
1219 
1220  /*
1221  * Segments are always opened in order from lowest to highest, so we must
1222  * be adding a new one at the end.
1223  */
1224  Assert(segno == reln->md_num_open_segs[forknum]);
1225 
1226  _fdvec_resize(reln, forknum, segno + 1);
1227 
1228  /* fill the entry */
1229  v = &reln->md_seg_fds[forknum][segno];
1230  v->mdfd_vfd = fd;
1231  v->mdfd_segno = segno;
1232 
1233  Assert(_mdnblocks(reln, forknum, v) <= ((BlockNumber) RELSEG_SIZE));
1234 
1235  /* all done */
1236  return v;
1237 }
1238 
1239 /*
1240  * _mdfd_getseg() -- Find the segment of the relation holding the
1241  * specified block.
1242  *
1243  * If the segment doesn't exist, we ereport, return NULL, or create the
1244  * segment, according to "behavior". Note: skipFsync is only used in the
1245  * EXTENSION_CREATE case.
1246  */
1247 static MdfdVec *
1249  bool skipFsync, int behavior)
1250 {
1251  MdfdVec *v;
1252  BlockNumber targetseg;
1253  BlockNumber nextsegno;
1254 
1255  /* some way to handle non-existent segments needs to be specified */
1256  Assert(behavior &
1259 
1260  targetseg = blkno / ((BlockNumber) RELSEG_SIZE);
1261 
1262  /* if an existing and opened segment, we're done */
1263  if (targetseg < reln->md_num_open_segs[forknum])
1264  {
1265  v = &reln->md_seg_fds[forknum][targetseg];
1266  return v;
1267  }
1268 
1269  /* The caller only wants the segment if we already had it open. */
1270  if (behavior & EXTENSION_DONT_OPEN)
1271  return NULL;
1272 
1273  /*
1274  * The target segment is not yet open. Iterate over all the segments
1275  * between the last opened and the target segment. This way missing
1276  * segments either raise an error, or get created (according to
1277  * 'behavior'). Start with either the last opened, or the first segment if
1278  * none was opened before.
1279  */
1280  if (reln->md_num_open_segs[forknum] > 0)
1281  v = &reln->md_seg_fds[forknum][reln->md_num_open_segs[forknum] - 1];
1282  else
1283  {
1284  v = mdopenfork(reln, forknum, behavior);
1285  if (!v)
1286  return NULL; /* if behavior & EXTENSION_RETURN_NULL */
1287  }
1288 
1289  for (nextsegno = reln->md_num_open_segs[forknum];
1290  nextsegno <= targetseg; nextsegno++)
1291  {
1292  BlockNumber nblocks = _mdnblocks(reln, forknum, v);
1293  int flags = 0;
1294 
1295  Assert(nextsegno == v->mdfd_segno + 1);
1296 
1297  if (nblocks > ((BlockNumber) RELSEG_SIZE))
1298  elog(FATAL, "segment too big");
1299 
1300  if ((behavior & EXTENSION_CREATE) ||
1301  (InRecovery && (behavior & EXTENSION_CREATE_RECOVERY)))
1302  {
1303  /*
1304  * Normally we will create new segments only if authorized by the
1305  * caller (i.e., we are doing mdextend()). But when doing WAL
1306  * recovery, create segments anyway; this allows cases such as
1307  * replaying WAL data that has a write into a high-numbered
1308  * segment of a relation that was later deleted. We want to go
1309  * ahead and create the segments so we can finish out the replay.
1310  *
1311  * We have to maintain the invariant that segments before the last
1312  * active segment are of size RELSEG_SIZE; therefore, if
1313  * extending, pad them out with zeroes if needed. (This only
1314  * matters if in recovery, or if the caller is extending the
1315  * relation discontiguously, but that can happen in hash indexes.)
1316  */
1317  if (nblocks < ((BlockNumber) RELSEG_SIZE))
1318  {
1319  char *zerobuf = palloc0(BLCKSZ);
1320 
1321  mdextend(reln, forknum,
1322  nextsegno * ((BlockNumber) RELSEG_SIZE) - 1,
1323  zerobuf, skipFsync);
1324  pfree(zerobuf);
1325  }
1326  flags = O_CREAT;
1327  }
1328  else if (!(behavior & EXTENSION_DONT_CHECK_SIZE) &&
1329  nblocks < ((BlockNumber) RELSEG_SIZE))
1330  {
1331  /*
1332  * When not extending (or explicitly including truncated
1333  * segments), only open the next segment if the current one is
1334  * exactly RELSEG_SIZE. If not (this branch), either return NULL
1335  * or fail.
1336  */
1337  if (behavior & EXTENSION_RETURN_NULL)
1338  {
1339  /*
1340  * Some callers discern between reasons for _mdfd_getseg()
1341  * returning NULL based on errno. As there's no failing
1342  * syscall involved in this case, explicitly set errno to
1343  * ENOENT, as that seems the closest interpretation.
1344  */
1345  errno = ENOENT;
1346  return NULL;
1347  }
1348 
1349  ereport(ERROR,
1351  errmsg("could not open file \"%s\" (target block %u): previous segment is only %u blocks",
1352  _mdfd_segpath(reln, forknum, nextsegno),
1353  blkno, nblocks)));
1354  }
1355 
1356  v = _mdfd_openseg(reln, forknum, nextsegno, flags);
1357 
1358  if (v == NULL)
1359  {
1360  if ((behavior & EXTENSION_RETURN_NULL) &&
1361  FILE_POSSIBLY_DELETED(errno))
1362  return NULL;
1363  ereport(ERROR,
1365  errmsg("could not open file \"%s\" (target block %u): %m",
1366  _mdfd_segpath(reln, forknum, nextsegno),
1367  blkno)));
1368  }
1369  }
1370 
1371  return v;
1372 }
1373 
1374 /*
1375  * Get number of blocks present in a single disk file
1376  */
1377 static BlockNumber
1379 {
1380  off_t len;
1381 
1382  len = FileSize(seg->mdfd_vfd);
1383  if (len < 0)
1384  ereport(ERROR,
1386  errmsg("could not seek to end of file \"%s\": %m",
1387  FilePathName(seg->mdfd_vfd))));
1388  /* note that this calculation will ignore any partial block at EOF */
1389  return (BlockNumber) (len / BLCKSZ);
1390 }
1391 
1392 /*
1393  * Sync a file to disk, given a file tag. Write the path into an output
1394  * buffer so the caller can use it in error messages.
1395  *
1396  * Return 0 on success, -1 on failure, with errno set.
1397  */
1398 int
1399 mdsyncfiletag(const FileTag *ftag, char *path)
1400 {
1402  File file;
1403  bool need_to_close;
1404  int result,
1405  save_errno;
1406 
1407  /* See if we already have the file open, or need to open it. */
1408  if (ftag->segno < reln->md_num_open_segs[ftag->forknum])
1409  {
1410  file = reln->md_seg_fds[ftag->forknum][ftag->segno].mdfd_vfd;
1411  strlcpy(path, FilePathName(file), MAXPGPATH);
1412  need_to_close = false;
1413  }
1414  else
1415  {
1416  char *p;
1417 
1418  p = _mdfd_segpath(reln, ftag->forknum, ftag->segno);
1419  strlcpy(path, p, MAXPGPATH);
1420  pfree(p);
1421 
1422  file = PathNameOpenFile(path, O_RDWR | PG_BINARY);
1423  if (file < 0)
1424  return -1;
1425  need_to_close = true;
1426  }
1427 
1428  /* Sync the file. */
1429  result = FileSync(file, WAIT_EVENT_DATA_FILE_SYNC);
1430  save_errno = errno;
1431 
1432  if (need_to_close)
1433  FileClose(file);
1434 
1436 
1437  errno = save_errno;
1438  return result;
1439 }
1440 
1441 /*
1442  * Unlink a file, given a file tag. Write the path into an output
1443  * buffer so the caller can use it in error messages.
1444  *
1445  * Return 0 on success, -1 on failure, with errno set.
1446  */
1447 int
1448 mdunlinkfiletag(const FileTag *ftag, char *path)
1449 {
1450  char *p;
1451 
1452  /* Compute the path. */
1453  p = relpathperm(ftag->rlocator, MAIN_FORKNUM);
1454  strlcpy(path, p, MAXPGPATH);
1455  pfree(p);
1456 
1457  /* Try to unlink the file. */
1458  return unlink(path);
1459 }
1460 
1461 /*
1462  * Check if a given candidate request matches a given tag, when processing
1463  * a SYNC_FILTER_REQUEST request. This will be called for all pending
1464  * requests to find out whether to forget them.
1465  */
1466 bool
1467 mdfiletagmatches(const FileTag *ftag, const FileTag *candidate)
1468 {
1469  /*
1470  * For now we only use filter requests as a way to drop all scheduled
1471  * callbacks relating to a given database, when dropping the database.
1472  * We'll return true for all candidates that have the same database OID as
1473  * the ftag from the SYNC_FILTER_REQUEST request, so they're forgotten.
1474  */
1475  return ftag->rlocator.dbOid == candidate->rlocator.dbOid;
1476 }
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
bool zero_damaged_pages
Definition: bufmgr.c:134
#define PG_BINARY
Definition: c.h:1260
#define MemSet(start, val, len)
Definition: c.h:1004
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1156
int errcode_for_file_access(void)
Definition: elog.c:881
int errhint(const char *fmt,...)
Definition: elog.c:1316
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#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:2012
int FileSync(File file, uint32 wait_event_info)
Definition: fd.c:2189
void FileClose(File file)
Definition: fd.c:1884
int FilePrefetch(File file, off_t offset, off_t amount, uint32 wait_event_info)
Definition: fd.c:1984
int data_sync_elevel(int elevel)
Definition: fd.c:3737
File PathNameOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:1481
int FileWrite(File file, const void *buffer, size_t amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:2091
int FileRead(File file, void *buffer, size_t amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:2035
char * FilePathName(File file)
Definition: fd.c:2262
off_t FileSize(File file)
Definition: fd.c:2210
int FileTruncate(File file, off_t offset, uint32 wait_event_info)
Definition: fd.c:2227
int pg_truncate(const char *path, off_t length)
Definition: fd.c:631
#define FILE_POSSIBLY_DELETED(err)
Definition: fd.h:75
int File
Definition: fd.h:54
bool IsBinaryUpgrade
Definition: globals.c:114
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
void pfree(void *pointer)
Definition: mcxt.c:1436
MemoryContext TopMemoryContext
Definition: mcxt.c:141
void * palloc0(Size size)
Definition: mcxt.c:1241
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1456
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1005
void * palloc(Size size)
Definition: mcxt.c:1210
void mdunlink(RelFileLocatorBackend rlocator, ForkNumber forknum, bool isRedo)
Definition: md.c:296
static void register_forget_request(RelFileLocatorBackend rlocator, ForkNumber forknum, BlockNumber segno)
Definition: md.c:1078
#define EXTENSION_CREATE_RECOVERY
Definition: md.c:110
static BlockNumber _mdnblocks(SMgrRelation reln, ForkNumber forknum, MdfdVec *seg)
Definition: md.c:1378
static void mdunlinkfork(RelFileLocatorBackend rlocator, ForkNumber forknum, bool isRedo)
Definition: md.c:333
void mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
Definition: md.c:736
bool mdfiletagmatches(const FileTag *ftag, const FileTag *candidate)
Definition: md.c:1467
bool mdexists(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:163
static void register_unlink_segment(RelFileLocatorBackend rlocator, ForkNumber forknum, BlockNumber segno)
Definition: md.c:1061
#define EXTENSION_DONT_OPEN
Definition: md.c:120
BlockNumber mdnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:801
int mdunlinkfiletag(const FileTag *ftag, char *path)
Definition: md.c:1448
static MemoryContext MdCxt
Definition: md.c:88
void mdcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: md.c:182
void mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync)
Definition: md.c:449
static int do_truncate(const char *path)
Definition: md.c:312
void mdinit(void)
Definition: md.c:150
void mdclose(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:568
static MdfdVec * _mdfd_openseg(SMgrRelation reln, ForkNumber forknum, BlockNumber segno, int oflags)
Definition: md.c:1203
static void register_dirty_segment(SMgrRelation reln, ForkNumber forknum, MdfdVec *seg)
Definition: md.c:1022
int mdsyncfiletag(const FileTag *ftag, char *path)
Definition: md.c:1399
void mdwriteback(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
Definition: md.c:619
void mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, void *buffer)
Definition: md.c:671
static MdfdVec * _mdfd_getseg(SMgrRelation reln, ForkNumber forknum, BlockNumber blkno, bool skipFsync, int behavior)
Definition: md.c:1248
#define EXTENSION_RETURN_NULL
Definition: md.c:106
static char * _mdfd_segpath(SMgrRelation reln, ForkNumber forknum, BlockNumber segno)
Definition: md.c:1180
void mdopen(SMgrRelation reln)
Definition: md.c:557
#define EXTENSION_CREATE
Definition: md.c:108
bool mdprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
Definition: md.c:591
#define INIT_MD_FILETAG(a, xx_rlocator, xx_forknum, xx_segno)
Definition: md.c:92
#define EXTENSION_DONT_CHECK_SIZE
Definition: md.c:118
#define EXTENSION_FAIL
Definition: md.c:104
static MdfdVec * mdopenfork(SMgrRelation reln, ForkNumber forknum, int behavior)
Definition: md.c:514
void DropRelationFiles(RelFileLocator *delrels, int ndelrels, bool isRedo)
Definition: md.c:1110
static void _fdvec_resize(SMgrRelation reln, ForkNumber forknum, int nseg)
Definition: md.c:1142
void ForgetDatabaseSyncRequests(Oid dbid)
Definition: md.c:1092
void mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
Definition: md.c:858
void mdimmedsync(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:960
struct _MdfdVec MdfdVec
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:41
#define MAXPGPATH
const void size_t len
@ IOOBJECT_RELATION
Definition: pgstat.h:277
@ IOCONTEXT_NORMAL
Definition: pgstat.h:287
@ IOOP_FSYNC
Definition: pgstat.h:297
void pgstat_count_io_op(IOObject io_object, IOContext io_context, IOOp io_op)
Definition: pgstat_io.c:66
#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:256
SMgrRelation smgropen(RelFileLocator rlocator, BackendId backend)
Definition: smgr.c:146
void smgrdounlinkall(SMgrRelation *rels, int nrels, bool isRedo)
Definition: smgr.c:420
#define SmgrIsTemp(smgr)
Definition: smgr.h:77
Definition: sync.h:51
RelFileLocator rlocator
Definition: sync.h:54
int16 forknum
Definition: sync.h:53
uint32 segno
Definition: sync.h:55
RelFileLocator locator
RelFileNumber relNumber
int md_num_open_segs[MAX_FORKNUM+1]
Definition: smgr.h:68
struct _MdfdVec * md_seg_fds[MAX_FORKNUM+1]
Definition: smgr.h:69
RelFileLocatorBackend smgr_rlocator
Definition: smgr.h:42
Definition: md.c:83
File mdfd_vfd
Definition: md.c:84
BlockNumber mdfd_segno
Definition: md.c:85
bool RegisterSyncRequest(const FileTag *ftag, SyncRequestType type, bool retryOnError)
Definition: sync.c:587
@ 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
@ WAIT_EVENT_DATA_FILE_IMMEDIATE_SYNC
Definition: wait_event.h:179
@ WAIT_EVENT_DATA_FILE_READ
Definition: wait_event.h:181
@ WAIT_EVENT_DATA_FILE_WRITE
Definition: wait_event.h:184
@ WAIT_EVENT_DATA_FILE_FLUSH
Definition: wait_event.h:178
@ WAIT_EVENT_DATA_FILE_EXTEND
Definition: wait_event.h:177
@ WAIT_EVENT_DATA_FILE_TRUNCATE
Definition: wait_event.h:183
@ WAIT_EVENT_DATA_FILE_PREFETCH
Definition: wait_event.h:180
@ WAIT_EVENT_DATA_FILE_SYNC
Definition: wait_event.h:182
bool InRecovery
Definition: xlogutils.c:53
void XLogDropRelation(RelFileLocator rlocator, ForkNumber forknum)
Definition: xlogutils.c:658