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-2020, 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/relfilenode.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 */
86 } MdfdVec;
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_rnode,xx_forknum,xx_segno) \
93 ( \
94  memset(&(a), 0, sizeof(FileTag)), \
95  (a).handler = SYNC_HANDLER_MD, \
96  (a).rnode = (xx_rnode), \
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 
120 
121 /* local routines */
122 static void mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum,
123  bool isRedo);
124 static MdfdVec *mdopenfork(SMgrRelation reln, ForkNumber forknum, int behavior);
125 static void register_dirty_segment(SMgrRelation reln, ForkNumber forknum,
126  MdfdVec *seg);
127 static void register_unlink_segment(RelFileNodeBackend rnode, ForkNumber forknum,
128  BlockNumber segno);
129 static void register_forget_request(RelFileNodeBackend rnode, ForkNumber forknum,
130  BlockNumber segno);
131 static void _fdvec_resize(SMgrRelation reln,
132  ForkNumber forknum,
133  int nseg);
134 static char *_mdfd_segpath(SMgrRelation reln, ForkNumber forknum,
135  BlockNumber segno);
136 static MdfdVec *_mdfd_openseg(SMgrRelation reln, ForkNumber forkno,
137  BlockNumber segno, int oflags);
138 static MdfdVec *_mdfd_getseg(SMgrRelation reln, ForkNumber forkno,
139  BlockNumber blkno, bool skipFsync, int behavior);
140 static BlockNumber _mdnblocks(SMgrRelation reln, ForkNumber forknum,
141  MdfdVec *seg);
142 
143 
144 /*
145  * mdinit() -- Initialize private state for magnetic disk storage manager.
146  */
147 void
148 mdinit(void)
149 {
151  "MdSmgr",
153 }
154 
155 /*
156  * mdexists() -- Does the physical file exist?
157  *
158  * Note: this will return true for lingering files, with pending deletions
159  */
160 bool
162 {
163  /*
164  * Close it first, to ensure that we notice if the fork has been unlinked
165  * since we opened it.
166  */
167  mdclose(reln, forkNum);
168 
169  return (mdopenfork(reln, forkNum, EXTENSION_RETURN_NULL) != NULL);
170 }
171 
172 /*
173  * mdcreate() -- Create a new relation on magnetic disk.
174  *
175  * If isRedo is true, it's okay for the relation to exist already.
176  */
177 void
178 mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
179 {
180  MdfdVec *mdfd;
181  char *path;
182  File fd;
183 
184  if (isRedo && reln->md_num_open_segs[forkNum] > 0)
185  return; /* created and opened already... */
186 
187  Assert(reln->md_num_open_segs[forkNum] == 0);
188 
189  /*
190  * We may be using the target table space for the first time in this
191  * database, so create a per-database subdirectory if needed.
192  *
193  * XXX this is a fairly ugly violation of module layering, but this seems
194  * to be the best place to put the check. Maybe TablespaceCreateDbspace
195  * should be here and not in commands/tablespace.c? But that would imply
196  * importing a lot of stuff that smgr.c oughtn't know, either.
197  */
199  reln->smgr_rnode.node.dbNode,
200  isRedo);
201 
202  path = relpath(reln->smgr_rnode, forkNum);
203 
204  fd = PathNameOpenFile(path, O_RDWR | O_CREAT | O_EXCL | PG_BINARY);
205 
206  if (fd < 0)
207  {
208  int save_errno = errno;
209 
210  if (isRedo)
211  fd = PathNameOpenFile(path, O_RDWR | PG_BINARY);
212  if (fd < 0)
213  {
214  /* be sure to report the error reported by create, not open */
215  errno = save_errno;
216  ereport(ERROR,
218  errmsg("could not create file \"%s\": %m", path)));
219  }
220  }
221 
222  pfree(path);
223 
224  _fdvec_resize(reln, forkNum, 1);
225  mdfd = &reln->md_seg_fds[forkNum][0];
226  mdfd->mdfd_vfd = fd;
227  mdfd->mdfd_segno = 0;
228 }
229 
230 /*
231  * mdunlink() -- Unlink a relation.
232  *
233  * Note that we're passed a RelFileNodeBackend --- by the time this is called,
234  * there won't be an SMgrRelation hashtable entry anymore.
235  *
236  * forkNum can be a fork number to delete a specific fork, or InvalidForkNumber
237  * to delete all forks.
238  *
239  * For regular relations, we don't unlink the first segment file of the rel,
240  * but just truncate it to zero length, and record a request to unlink it after
241  * the next checkpoint. Additional segments can be unlinked immediately,
242  * however. Leaving the empty file in place prevents that relfilenode
243  * number from being reused. The scenario this protects us from is:
244  * 1. We delete a relation (and commit, and actually remove its file).
245  * 2. We create a new relation, which by chance gets the same relfilenode as
246  * the just-deleted one (OIDs must've wrapped around for that to happen).
247  * 3. We crash before another checkpoint occurs.
248  * During replay, we would delete the file and then recreate it, which is fine
249  * if the contents of the file were repopulated by subsequent WAL entries.
250  * But if we didn't WAL-log insertions, but instead relied on fsyncing the
251  * file after populating it (as for instance CLUSTER and CREATE INDEX do),
252  * the contents of the file would be lost forever. By leaving the empty file
253  * until after the next checkpoint, we prevent reassignment of the relfilenode
254  * number until it's safe, because relfilenode assignment skips over any
255  * existing file.
256  *
257  * We do not need to go through this dance for temp relations, though, because
258  * we never make WAL entries for temp rels, and so a temp rel poses no threat
259  * to the health of a regular rel that has taken over its relfilenode number.
260  * The fact that temp rels and regular rels have different file naming
261  * patterns provides additional safety.
262  *
263  * All the above applies only to the relation's main fork; other forks can
264  * just be removed immediately, since they are not needed to prevent the
265  * relfilenode number from being recycled. Also, we do not carefully
266  * track whether other forks have been created or not, but just attempt to
267  * unlink them unconditionally; so we should never complain about ENOENT.
268  *
269  * If isRedo is true, it's unsurprising for the relation to be already gone.
270  * Also, we should remove the file immediately instead of queuing a request
271  * for later, since during redo there's no possibility of creating a
272  * conflicting relation.
273  *
274  * Note: any failure should be reported as WARNING not ERROR, because
275  * we are usually not in a transaction anymore when this is called.
276  */
277 void
278 mdunlink(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
279 {
280  /* Now do the per-fork work */
281  if (forkNum == InvalidForkNumber)
282  {
283  for (forkNum = 0; forkNum <= MAX_FORKNUM; forkNum++)
284  mdunlinkfork(rnode, forkNum, isRedo);
285  }
286  else
287  mdunlinkfork(rnode, forkNum, isRedo);
288 }
289 
290 static void
291 mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
292 {
293  char *path;
294  int ret;
295 
296  path = relpath(rnode, forkNum);
297 
298  /*
299  * Delete or truncate the first segment.
300  */
301  if (isRedo || forkNum != MAIN_FORKNUM || RelFileNodeBackendIsTemp(rnode))
302  {
303  /* First, forget any pending sync requests for the first segment */
304  if (!RelFileNodeBackendIsTemp(rnode))
305  register_forget_request(rnode, forkNum, 0 /* first seg */ );
306 
307  /* Next unlink the file */
308  ret = unlink(path);
309  if (ret < 0 && errno != ENOENT)
312  errmsg("could not remove file \"%s\": %m", path)));
313  }
314  else
315  {
316  /* truncate(2) would be easier here, but Windows hasn't got it */
317  int fd;
318 
319  fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
320  if (fd >= 0)
321  {
322  int save_errno;
323 
324  ret = ftruncate(fd, 0);
325  save_errno = errno;
326  CloseTransientFile(fd);
327  errno = save_errno;
328  }
329  else
330  ret = -1;
331  if (ret < 0 && errno != ENOENT)
334  errmsg("could not truncate file \"%s\": %m", path)));
335 
336  /* Register request to unlink first segment later */
337  register_unlink_segment(rnode, forkNum, 0 /* first seg */ );
338  }
339 
340  /*
341  * Delete any additional segments.
342  */
343  if (ret >= 0)
344  {
345  char *segpath = (char *) palloc(strlen(path) + 12);
346  BlockNumber segno;
347 
348  /*
349  * Note that because we loop until getting ENOENT, we will correctly
350  * remove all inactive segments as well as active ones.
351  */
352  for (segno = 1;; segno++)
353  {
354  /*
355  * Forget any pending sync requests for this segment before we try
356  * to unlink.
357  */
358  if (!RelFileNodeBackendIsTemp(rnode))
359  register_forget_request(rnode, forkNum, segno);
360 
361  sprintf(segpath, "%s.%u", path, segno);
362  if (unlink(segpath) < 0)
363  {
364  /* ENOENT is expected after the last segment... */
365  if (errno != ENOENT)
368  errmsg("could not remove file \"%s\": %m", segpath)));
369  break;
370  }
371  }
372  pfree(segpath);
373  }
374 
375  pfree(path);
376 }
377 
378 /*
379  * mdextend() -- Add a block to the specified relation.
380  *
381  * The semantics are nearly the same as mdwrite(): write at the
382  * specified position. However, this is to be used for the case of
383  * extending a relation (i.e., blocknum is at or beyond the current
384  * EOF). Note that we assume writing a block beyond current EOF
385  * causes intervening file space to become filled with zeroes.
386  */
387 void
388 mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
389  char *buffer, bool skipFsync)
390 {
391  off_t seekpos;
392  int nbytes;
393  MdfdVec *v;
394 
395  /* This assert is too expensive to have on normally ... */
396 #ifdef CHECK_WRITE_VS_EXTEND
397  Assert(blocknum >= mdnblocks(reln, forknum));
398 #endif
399 
400  /*
401  * If a relation manages to grow to 2^32-1 blocks, refuse to extend it any
402  * more --- we mustn't create a block whose number actually is
403  * InvalidBlockNumber.
404  */
405  if (blocknum == InvalidBlockNumber)
406  ereport(ERROR,
407  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
408  errmsg("cannot extend file \"%s\" beyond %u blocks",
409  relpath(reln->smgr_rnode, forknum),
411 
412  v = _mdfd_getseg(reln, forknum, blocknum, skipFsync, EXTENSION_CREATE);
413 
414  seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
415 
416  Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
417 
418  if ((nbytes = FileWrite(v->mdfd_vfd, buffer, BLCKSZ, seekpos, WAIT_EVENT_DATA_FILE_EXTEND)) != BLCKSZ)
419  {
420  if (nbytes < 0)
421  ereport(ERROR,
423  errmsg("could not extend file \"%s\": %m",
424  FilePathName(v->mdfd_vfd)),
425  errhint("Check free disk space.")));
426  /* short write: complain appropriately */
427  ereport(ERROR,
428  (errcode(ERRCODE_DISK_FULL),
429  errmsg("could not extend file \"%s\": wrote only %d of %d bytes at block %u",
431  nbytes, BLCKSZ, blocknum),
432  errhint("Check free disk space.")));
433  }
434 
435  if (!skipFsync && !SmgrIsTemp(reln))
436  register_dirty_segment(reln, forknum, v);
437 
438  Assert(_mdnblocks(reln, forknum, v) <= ((BlockNumber) RELSEG_SIZE));
439 }
440 
441 /*
442  * mdopenfork() -- Open one fork of the specified relation.
443  *
444  * Note we only open the first segment, when there are multiple segments.
445  *
446  * If first segment is not present, either ereport or return NULL according
447  * to "behavior". We treat EXTENSION_CREATE the same as EXTENSION_FAIL;
448  * EXTENSION_CREATE means it's OK to extend an existing relation, not to
449  * invent one out of whole cloth.
450  */
451 static MdfdVec *
452 mdopenfork(SMgrRelation reln, ForkNumber forknum, int behavior)
453 {
454  MdfdVec *mdfd;
455  char *path;
456  File fd;
457 
458  /* No work if already open */
459  if (reln->md_num_open_segs[forknum] > 0)
460  return &reln->md_seg_fds[forknum][0];
461 
462  path = relpath(reln->smgr_rnode, forknum);
463 
464  fd = PathNameOpenFile(path, O_RDWR | PG_BINARY);
465 
466  if (fd < 0)
467  {
468  if ((behavior & EXTENSION_RETURN_NULL) &&
469  FILE_POSSIBLY_DELETED(errno))
470  {
471  pfree(path);
472  return NULL;
473  }
474  ereport(ERROR,
476  errmsg("could not open file \"%s\": %m", path)));
477  }
478 
479  pfree(path);
480 
481  _fdvec_resize(reln, forknum, 1);
482  mdfd = &reln->md_seg_fds[forknum][0];
483  mdfd->mdfd_vfd = fd;
484  mdfd->mdfd_segno = 0;
485 
486  Assert(_mdnblocks(reln, forknum, mdfd) <= ((BlockNumber) RELSEG_SIZE));
487 
488  return mdfd;
489 }
490 
491 /*
492  * mdopen() -- Initialize newly-opened relation.
493  */
494 void
496 {
497  /* mark it not open */
498  for (int forknum = 0; forknum <= MAX_FORKNUM; forknum++)
499  reln->md_num_open_segs[forknum] = 0;
500 }
501 
502 /*
503  * mdclose() -- Close the specified relation, if it isn't closed already.
504  */
505 void
507 {
508  int nopensegs = reln->md_num_open_segs[forknum];
509 
510  /* No work if already closed */
511  if (nopensegs == 0)
512  return;
513 
514  /* close segments starting from the end */
515  while (nopensegs > 0)
516  {
517  MdfdVec *v = &reln->md_seg_fds[forknum][nopensegs - 1];
518 
519  FileClose(v->mdfd_vfd);
520  _fdvec_resize(reln, forknum, nopensegs - 1);
521  nopensegs--;
522  }
523 }
524 
525 /*
526  * mdprefetch() -- Initiate asynchronous read of the specified block of a relation
527  */
528 void
530 {
531 #ifdef USE_PREFETCH
532  off_t seekpos;
533  MdfdVec *v;
534 
535  v = _mdfd_getseg(reln, forknum, blocknum, false, EXTENSION_FAIL);
536 
537  seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
538 
539  Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
540 
541  (void) FilePrefetch(v->mdfd_vfd, seekpos, BLCKSZ, WAIT_EVENT_DATA_FILE_PREFETCH);
542 #endif /* USE_PREFETCH */
543 }
544 
545 /*
546  * mdwriteback() -- Tell the kernel to write pages back to storage.
547  *
548  * This accepts a range of blocks because flushing several pages at once is
549  * considerably more efficient than doing so individually.
550  */
551 void
553  BlockNumber blocknum, BlockNumber nblocks)
554 {
555  /*
556  * Issue flush requests in as few requests as possible; have to split at
557  * segment boundaries though, since those are actually separate files.
558  */
559  while (nblocks > 0)
560  {
561  BlockNumber nflush = nblocks;
562  off_t seekpos;
563  MdfdVec *v;
564  int segnum_start,
565  segnum_end;
566 
567  v = _mdfd_getseg(reln, forknum, blocknum, true /* not used */ ,
569 
570  /*
571  * We might be flushing buffers of already removed relations, that's
572  * ok, just ignore that case.
573  */
574  if (!v)
575  return;
576 
577  /* compute offset inside the current segment */
578  segnum_start = blocknum / RELSEG_SIZE;
579 
580  /* compute number of desired writes within the current segment */
581  segnum_end = (blocknum + nblocks - 1) / RELSEG_SIZE;
582  if (segnum_start != segnum_end)
583  nflush = RELSEG_SIZE - (blocknum % ((BlockNumber) RELSEG_SIZE));
584 
585  Assert(nflush >= 1);
586  Assert(nflush <= nblocks);
587 
588  seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
589 
590  FileWriteback(v->mdfd_vfd, seekpos, (off_t) BLCKSZ * nflush, WAIT_EVENT_DATA_FILE_FLUSH);
591 
592  nblocks -= nflush;
593  blocknum += nflush;
594  }
595 }
596 
597 /*
598  * mdread() -- Read the specified block from a relation.
599  */
600 void
601 mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
602  char *buffer)
603 {
604  off_t seekpos;
605  int nbytes;
606  MdfdVec *v;
607 
608  TRACE_POSTGRESQL_SMGR_MD_READ_START(forknum, blocknum,
609  reln->smgr_rnode.node.spcNode,
610  reln->smgr_rnode.node.dbNode,
611  reln->smgr_rnode.node.relNode,
612  reln->smgr_rnode.backend);
613 
614  v = _mdfd_getseg(reln, forknum, blocknum, false,
616 
617  seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
618 
619  Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
620 
621  nbytes = FileRead(v->mdfd_vfd, buffer, BLCKSZ, seekpos, WAIT_EVENT_DATA_FILE_READ);
622 
623  TRACE_POSTGRESQL_SMGR_MD_READ_DONE(forknum, blocknum,
624  reln->smgr_rnode.node.spcNode,
625  reln->smgr_rnode.node.dbNode,
626  reln->smgr_rnode.node.relNode,
627  reln->smgr_rnode.backend,
628  nbytes,
629  BLCKSZ);
630 
631  if (nbytes != BLCKSZ)
632  {
633  if (nbytes < 0)
634  ereport(ERROR,
636  errmsg("could not read block %u in file \"%s\": %m",
637  blocknum, FilePathName(v->mdfd_vfd))));
638 
639  /*
640  * Short read: we are at or past EOF, or we read a partial block at
641  * EOF. Normally this is an error; upper levels should never try to
642  * read a nonexistent block. However, if zero_damaged_pages is ON or
643  * we are InRecovery, we should instead return zeroes without
644  * complaining. This allows, for example, the case of trying to
645  * update a block that was later truncated away.
646  */
648  MemSet(buffer, 0, BLCKSZ);
649  else
650  ereport(ERROR,
652  errmsg("could not read block %u in file \"%s\": read only %d of %d bytes",
653  blocknum, FilePathName(v->mdfd_vfd),
654  nbytes, BLCKSZ)));
655  }
656 }
657 
658 /*
659  * mdwrite() -- Write the supplied block at the appropriate location.
660  *
661  * This is to be used only for updating already-existing blocks of a
662  * relation (ie, those before the current EOF). To extend a relation,
663  * use mdextend().
664  */
665 void
666 mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
667  char *buffer, bool skipFsync)
668 {
669  off_t seekpos;
670  int nbytes;
671  MdfdVec *v;
672 
673  /* This assert is too expensive to have on normally ... */
674 #ifdef CHECK_WRITE_VS_EXTEND
675  Assert(blocknum < mdnblocks(reln, forknum));
676 #endif
677 
678  TRACE_POSTGRESQL_SMGR_MD_WRITE_START(forknum, blocknum,
679  reln->smgr_rnode.node.spcNode,
680  reln->smgr_rnode.node.dbNode,
681  reln->smgr_rnode.node.relNode,
682  reln->smgr_rnode.backend);
683 
684  v = _mdfd_getseg(reln, forknum, blocknum, skipFsync,
686 
687  seekpos = (off_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
688 
689  Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE);
690 
691  nbytes = FileWrite(v->mdfd_vfd, buffer, BLCKSZ, seekpos, WAIT_EVENT_DATA_FILE_WRITE);
692 
693  TRACE_POSTGRESQL_SMGR_MD_WRITE_DONE(forknum, blocknum,
694  reln->smgr_rnode.node.spcNode,
695  reln->smgr_rnode.node.dbNode,
696  reln->smgr_rnode.node.relNode,
697  reln->smgr_rnode.backend,
698  nbytes,
699  BLCKSZ);
700 
701  if (nbytes != BLCKSZ)
702  {
703  if (nbytes < 0)
704  ereport(ERROR,
706  errmsg("could not write block %u in file \"%s\": %m",
707  blocknum, FilePathName(v->mdfd_vfd))));
708  /* short write: complain appropriately */
709  ereport(ERROR,
710  (errcode(ERRCODE_DISK_FULL),
711  errmsg("could not write block %u in file \"%s\": wrote only %d of %d bytes",
712  blocknum,
714  nbytes, BLCKSZ),
715  errhint("Check free disk space.")));
716  }
717 
718  if (!skipFsync && !SmgrIsTemp(reln))
719  register_dirty_segment(reln, forknum, v);
720 }
721 
722 /*
723  * mdnblocks() -- Get the number of blocks stored in a relation.
724  *
725  * Important side effect: all active segments of the relation are opened
726  * and added to the md_seg_fds array. If this routine has not been
727  * called, then only segments up to the last one actually touched
728  * are present in the array.
729  */
732 {
733  MdfdVec *v = mdopenfork(reln, forknum, EXTENSION_FAIL);
734  BlockNumber nblocks;
735  BlockNumber segno = 0;
736 
737  /* mdopen has opened the first segment */
738  Assert(reln->md_num_open_segs[forknum] > 0);
739 
740  /*
741  * Start from the last open segments, to avoid redundant seeks. We have
742  * previously verified that these segments are exactly RELSEG_SIZE long,
743  * and it's useless to recheck that each time.
744  *
745  * NOTE: this assumption could only be wrong if another backend has
746  * truncated the relation. We rely on higher code levels to handle that
747  * scenario by closing and re-opening the md fd, which is handled via
748  * relcache flush. (Since the checkpointer doesn't participate in
749  * relcache flush, it could have segment entries for inactive segments;
750  * that's OK because the checkpointer never needs to compute relation
751  * size.)
752  */
753  segno = reln->md_num_open_segs[forknum] - 1;
754  v = &reln->md_seg_fds[forknum][segno];
755 
756  for (;;)
757  {
758  nblocks = _mdnblocks(reln, forknum, v);
759  if (nblocks > ((BlockNumber) RELSEG_SIZE))
760  elog(FATAL, "segment too big");
761  if (nblocks < ((BlockNumber) RELSEG_SIZE))
762  return (segno * ((BlockNumber) RELSEG_SIZE)) + nblocks;
763 
764  /*
765  * If segment is exactly RELSEG_SIZE, advance to next one.
766  */
767  segno++;
768 
769  /*
770  * We used to pass O_CREAT here, but that has the disadvantage that it
771  * might create a segment which has vanished through some operating
772  * system misadventure. In such a case, creating the segment here
773  * undermines _mdfd_getseg's attempts to notice and report an error
774  * upon access to a missing segment.
775  */
776  v = _mdfd_openseg(reln, forknum, segno, 0);
777  if (v == NULL)
778  return segno * ((BlockNumber) RELSEG_SIZE);
779  }
780 }
781 
782 /*
783  * mdtruncate() -- Truncate relation to specified number of blocks.
784  */
785 void
787 {
788  BlockNumber curnblk;
789  BlockNumber priorblocks;
790  int curopensegs;
791 
792  /*
793  * NOTE: mdnblocks makes sure we have opened all active segments, so that
794  * truncation loop will get them all!
795  */
796  curnblk = mdnblocks(reln, forknum);
797  if (nblocks > curnblk)
798  {
799  /* Bogus request ... but no complaint if InRecovery */
800  if (InRecovery)
801  return;
802  ereport(ERROR,
803  (errmsg("could not truncate file \"%s\" to %u blocks: it's only %u blocks now",
804  relpath(reln->smgr_rnode, forknum),
805  nblocks, curnblk)));
806  }
807  if (nblocks == curnblk)
808  return; /* no work */
809 
810  /*
811  * Truncate segments, starting at the last one. Starting at the end makes
812  * managing the memory for the fd array easier, should there be errors.
813  */
814  curopensegs = reln->md_num_open_segs[forknum];
815  while (curopensegs > 0)
816  {
817  MdfdVec *v;
818 
819  priorblocks = (curopensegs - 1) * RELSEG_SIZE;
820 
821  v = &reln->md_seg_fds[forknum][curopensegs - 1];
822 
823  if (priorblocks > nblocks)
824  {
825  /*
826  * This segment is no longer active. We truncate the file, but do
827  * not delete it, for reasons explained in the header comments.
828  */
830  ereport(ERROR,
832  errmsg("could not truncate file \"%s\": %m",
833  FilePathName(v->mdfd_vfd))));
834 
835  if (!SmgrIsTemp(reln))
836  register_dirty_segment(reln, forknum, v);
837 
838  /* we never drop the 1st segment */
839  Assert(v != &reln->md_seg_fds[forknum][0]);
840 
841  FileClose(v->mdfd_vfd);
842  _fdvec_resize(reln, forknum, curopensegs - 1);
843  }
844  else if (priorblocks + ((BlockNumber) RELSEG_SIZE) > nblocks)
845  {
846  /*
847  * This is the last segment we want to keep. Truncate the file to
848  * the right length. NOTE: if nblocks is exactly a multiple K of
849  * RELSEG_SIZE, we will truncate the K+1st segment to 0 length but
850  * keep it. This adheres to the invariant given in the header
851  * comments.
852  */
853  BlockNumber lastsegblocks = nblocks - priorblocks;
854 
855  if (FileTruncate(v->mdfd_vfd, (off_t) lastsegblocks * BLCKSZ, WAIT_EVENT_DATA_FILE_TRUNCATE) < 0)
856  ereport(ERROR,
858  errmsg("could not truncate file \"%s\" to %u blocks: %m",
860  nblocks)));
861  if (!SmgrIsTemp(reln))
862  register_dirty_segment(reln, forknum, v);
863  }
864  else
865  {
866  /*
867  * We still need this segment, so nothing to do for this and any
868  * earlier segment.
869  */
870  break;
871  }
872  curopensegs--;
873  }
874 }
875 
876 /*
877  * mdimmedsync() -- Immediately sync a relation to stable storage.
878  *
879  * Note that only writes already issued are synced; this routine knows
880  * nothing of dirty buffers that may exist inside the buffer manager.
881  */
882 void
884 {
885  int segno;
886 
887  /*
888  * NOTE: mdnblocks makes sure we have opened all active segments, so that
889  * fsync loop will get them all!
890  */
891  mdnblocks(reln, forknum);
892 
893  segno = reln->md_num_open_segs[forknum];
894 
895  while (segno > 0)
896  {
897  MdfdVec *v = &reln->md_seg_fds[forknum][segno - 1];
898 
902  errmsg("could not fsync file \"%s\": %m",
903  FilePathName(v->mdfd_vfd))));
904  segno--;
905  }
906 }
907 
908 /*
909  * register_dirty_segment() -- Mark a relation segment as needing fsync
910  *
911  * If there is a local pending-ops table, just make an entry in it for
912  * ProcessSyncRequests to process later. Otherwise, try to pass off the
913  * fsync request to the checkpointer process. If that fails, just do the
914  * fsync locally before returning (we hope this will not happen often
915  * enough to be a performance problem).
916  */
917 static void
919 {
920  FileTag tag;
921 
922  INIT_MD_FILETAG(tag, reln->smgr_rnode.node, forknum, seg->mdfd_segno);
923 
924  /* Temp relations should never be fsync'd */
925  Assert(!SmgrIsTemp(reln));
926 
927  if (!RegisterSyncRequest(&tag, SYNC_REQUEST, false /* retryOnError */ ))
928  {
929  ereport(DEBUG1,
930  (errmsg("could not forward fsync request because request queue is full")));
931 
935  errmsg("could not fsync file \"%s\": %m",
936  FilePathName(seg->mdfd_vfd))));
937  }
938 }
939 
940 /*
941  * register_unlink_segment() -- Schedule a file to be deleted after next checkpoint
942  */
943 static void
945  BlockNumber segno)
946 {
947  FileTag tag;
948 
949  INIT_MD_FILETAG(tag, rnode.node, forknum, segno);
950 
951  /* Should never be used with temp relations */
953 
954  RegisterSyncRequest(&tag, SYNC_UNLINK_REQUEST, true /* retryOnError */ );
955 }
956 
957 /*
958  * register_forget_request() -- forget any fsyncs for a relation fork's segment
959  */
960 static void
962  BlockNumber segno)
963 {
964  FileTag tag;
965 
966  INIT_MD_FILETAG(tag, rnode.node, forknum, segno);
967 
968  RegisterSyncRequest(&tag, SYNC_FORGET_REQUEST, true /* retryOnError */ );
969 }
970 
971 /*
972  * ForgetDatabaseSyncRequests -- forget any fsyncs and unlinks for a DB
973  */
974 void
976 {
977  FileTag tag;
978  RelFileNode rnode;
979 
980  rnode.dbNode = dbid;
981  rnode.spcNode = 0;
982  rnode.relNode = 0;
983 
985 
986  RegisterSyncRequest(&tag, SYNC_FILTER_REQUEST, true /* retryOnError */ );
987 }
988 
989 /*
990  * DropRelationFiles -- drop files of all given relations
991  */
992 void
993 DropRelationFiles(RelFileNode *delrels, int ndelrels, bool isRedo)
994 {
995  SMgrRelation *srels;
996  int i;
997 
998  srels = palloc(sizeof(SMgrRelation) * ndelrels);
999  for (i = 0; i < ndelrels; i++)
1000  {
1001  SMgrRelation srel = smgropen(delrels[i], InvalidBackendId);
1002 
1003  if (isRedo)
1004  {
1005  ForkNumber fork;
1006 
1007  for (fork = 0; fork <= MAX_FORKNUM; fork++)
1008  XLogDropRelation(delrels[i], fork);
1009  }
1010  srels[i] = srel;
1011  }
1012 
1013  smgrdounlinkall(srels, ndelrels, isRedo);
1014 
1015  for (i = 0; i < ndelrels; i++)
1016  smgrclose(srels[i]);
1017  pfree(srels);
1018 }
1019 
1020 
1021 /*
1022  * _fdvec_resize() -- Resize the fork's open segments array
1023  */
1024 static void
1026  ForkNumber forknum,
1027  int nseg)
1028 {
1029  if (nseg == 0)
1030  {
1031  if (reln->md_num_open_segs[forknum] > 0)
1032  {
1033  pfree(reln->md_seg_fds[forknum]);
1034  reln->md_seg_fds[forknum] = NULL;
1035  }
1036  }
1037  else if (reln->md_num_open_segs[forknum] == 0)
1038  {
1039  reln->md_seg_fds[forknum] =
1040  MemoryContextAlloc(MdCxt, sizeof(MdfdVec) * nseg);
1041  }
1042  else
1043  {
1044  /*
1045  * It doesn't seem worthwhile complicating the code to amortize
1046  * repalloc() calls. Those are far faster than PathNameOpenFile() or
1047  * FileClose(), and the memory context internally will sometimes avoid
1048  * doing an actual reallocation.
1049  */
1050  reln->md_seg_fds[forknum] =
1051  repalloc(reln->md_seg_fds[forknum],
1052  sizeof(MdfdVec) * nseg);
1053  }
1054 
1055  reln->md_num_open_segs[forknum] = nseg;
1056 }
1057 
1058 /*
1059  * Return the filename for the specified segment of the relation. The
1060  * returned string is palloc'd.
1061  */
1062 static char *
1064 {
1065  char *path,
1066  *fullpath;
1067 
1068  path = relpath(reln->smgr_rnode, forknum);
1069 
1070  if (segno > 0)
1071  {
1072  fullpath = psprintf("%s.%u", path, segno);
1073  pfree(path);
1074  }
1075  else
1076  fullpath = path;
1077 
1078  return fullpath;
1079 }
1080 
1081 /*
1082  * Open the specified segment of the relation,
1083  * and make a MdfdVec object for it. Returns NULL on failure.
1084  */
1085 static MdfdVec *
1087  int oflags)
1088 {
1089  MdfdVec *v;
1090  int fd;
1091  char *fullpath;
1092 
1093  fullpath = _mdfd_segpath(reln, forknum, segno);
1094 
1095  /* open the file */
1096  fd = PathNameOpenFile(fullpath, O_RDWR | PG_BINARY | oflags);
1097 
1098  pfree(fullpath);
1099 
1100  if (fd < 0)
1101  return NULL;
1102 
1103  /*
1104  * Segments are always opened in order from lowest to highest, so we must
1105  * be adding a new one at the end.
1106  */
1107  Assert(segno == reln->md_num_open_segs[forknum]);
1108 
1109  _fdvec_resize(reln, forknum, segno + 1);
1110 
1111  /* fill the entry */
1112  v = &reln->md_seg_fds[forknum][segno];
1113  v->mdfd_vfd = fd;
1114  v->mdfd_segno = segno;
1115 
1116  Assert(_mdnblocks(reln, forknum, v) <= ((BlockNumber) RELSEG_SIZE));
1117 
1118  /* all done */
1119  return v;
1120 }
1121 
1122 /*
1123  * _mdfd_getseg() -- Find the segment of the relation holding the
1124  * specified block.
1125  *
1126  * If the segment doesn't exist, we ereport, return NULL, or create the
1127  * segment, according to "behavior". Note: skipFsync is only used in the
1128  * EXTENSION_CREATE case.
1129  */
1130 static MdfdVec *
1132  bool skipFsync, int behavior)
1133 {
1134  MdfdVec *v;
1135  BlockNumber targetseg;
1136  BlockNumber nextsegno;
1137 
1138  /* some way to handle non-existent segments needs to be specified */
1139  Assert(behavior &
1141 
1142  targetseg = blkno / ((BlockNumber) RELSEG_SIZE);
1143 
1144  /* if an existing and opened segment, we're done */
1145  if (targetseg < reln->md_num_open_segs[forknum])
1146  {
1147  v = &reln->md_seg_fds[forknum][targetseg];
1148  return v;
1149  }
1150 
1151  /*
1152  * The target segment is not yet open. Iterate over all the segments
1153  * between the last opened and the target segment. This way missing
1154  * segments either raise an error, or get created (according to
1155  * 'behavior'). Start with either the last opened, or the first segment if
1156  * none was opened before.
1157  */
1158  if (reln->md_num_open_segs[forknum] > 0)
1159  v = &reln->md_seg_fds[forknum][reln->md_num_open_segs[forknum] - 1];
1160  else
1161  {
1162  v = mdopenfork(reln, forknum, behavior);
1163  if (!v)
1164  return NULL; /* if behavior & EXTENSION_RETURN_NULL */
1165  }
1166 
1167  for (nextsegno = reln->md_num_open_segs[forknum];
1168  nextsegno <= targetseg; nextsegno++)
1169  {
1170  BlockNumber nblocks = _mdnblocks(reln, forknum, v);
1171  int flags = 0;
1172 
1173  Assert(nextsegno == v->mdfd_segno + 1);
1174 
1175  if (nblocks > ((BlockNumber) RELSEG_SIZE))
1176  elog(FATAL, "segment too big");
1177 
1178  if ((behavior & EXTENSION_CREATE) ||
1179  (InRecovery && (behavior & EXTENSION_CREATE_RECOVERY)))
1180  {
1181  /*
1182  * Normally we will create new segments only if authorized by the
1183  * caller (i.e., we are doing mdextend()). But when doing WAL
1184  * recovery, create segments anyway; this allows cases such as
1185  * replaying WAL data that has a write into a high-numbered
1186  * segment of a relation that was later deleted. We want to go
1187  * ahead and create the segments so we can finish out the replay.
1188  *
1189  * We have to maintain the invariant that segments before the last
1190  * active segment are of size RELSEG_SIZE; therefore, if
1191  * extending, pad them out with zeroes if needed. (This only
1192  * matters if in recovery, or if the caller is extending the
1193  * relation discontiguously, but that can happen in hash indexes.)
1194  */
1195  if (nblocks < ((BlockNumber) RELSEG_SIZE))
1196  {
1197  char *zerobuf = palloc0(BLCKSZ);
1198 
1199  mdextend(reln, forknum,
1200  nextsegno * ((BlockNumber) RELSEG_SIZE) - 1,
1201  zerobuf, skipFsync);
1202  pfree(zerobuf);
1203  }
1204  flags = O_CREAT;
1205  }
1206  else if (!(behavior & EXTENSION_DONT_CHECK_SIZE) &&
1207  nblocks < ((BlockNumber) RELSEG_SIZE))
1208  {
1209  /*
1210  * When not extending (or explicitly including truncated
1211  * segments), only open the next segment if the current one is
1212  * exactly RELSEG_SIZE. If not (this branch), either return NULL
1213  * or fail.
1214  */
1215  if (behavior & EXTENSION_RETURN_NULL)
1216  {
1217  /*
1218  * Some callers discern between reasons for _mdfd_getseg()
1219  * returning NULL based on errno. As there's no failing
1220  * syscall involved in this case, explicitly set errno to
1221  * ENOENT, as that seems the closest interpretation.
1222  */
1223  errno = ENOENT;
1224  return NULL;
1225  }
1226 
1227  ereport(ERROR,
1229  errmsg("could not open file \"%s\" (target block %u): previous segment is only %u blocks",
1230  _mdfd_segpath(reln, forknum, nextsegno),
1231  blkno, nblocks)));
1232  }
1233 
1234  v = _mdfd_openseg(reln, forknum, nextsegno, flags);
1235 
1236  if (v == NULL)
1237  {
1238  if ((behavior & EXTENSION_RETURN_NULL) &&
1239  FILE_POSSIBLY_DELETED(errno))
1240  return NULL;
1241  ereport(ERROR,
1243  errmsg("could not open file \"%s\" (target block %u): %m",
1244  _mdfd_segpath(reln, forknum, nextsegno),
1245  blkno)));
1246  }
1247  }
1248 
1249  return v;
1250 }
1251 
1252 /*
1253  * Get number of blocks present in a single disk file
1254  */
1255 static BlockNumber
1257 {
1258  off_t len;
1259 
1260  len = FileSize(seg->mdfd_vfd);
1261  if (len < 0)
1262  ereport(ERROR,
1264  errmsg("could not seek to end of file \"%s\": %m",
1265  FilePathName(seg->mdfd_vfd))));
1266  /* note that this calculation will ignore any partial block at EOF */
1267  return (BlockNumber) (len / BLCKSZ);
1268 }
1269 
1270 /*
1271  * Sync a file to disk, given a file tag. Write the path into an output
1272  * buffer so the caller can use it in error messages.
1273  *
1274  * Return 0 on success, -1 on failure, with errno set.
1275  */
1276 int
1277 mdsyncfiletag(const FileTag *ftag, char *path)
1278 {
1280  File file;
1281  bool need_to_close;
1282  int result,
1283  save_errno;
1284 
1285  /* See if we already have the file open, or need to open it. */
1286  if (ftag->segno < reln->md_num_open_segs[ftag->forknum])
1287  {
1288  file = reln->md_seg_fds[ftag->forknum][ftag->segno].mdfd_vfd;
1289  strlcpy(path, FilePathName(file), MAXPGPATH);
1290  need_to_close = false;
1291  }
1292  else
1293  {
1294  char *p;
1295 
1296  p = _mdfd_segpath(reln, ftag->forknum, ftag->segno);
1297  strlcpy(path, p, MAXPGPATH);
1298  pfree(p);
1299 
1300  file = PathNameOpenFile(path, O_RDWR | PG_BINARY);
1301  if (file < 0)
1302  return -1;
1303  need_to_close = true;
1304  }
1305 
1306  /* Sync the file. */
1307  result = FileSync(file, WAIT_EVENT_DATA_FILE_SYNC);
1308  save_errno = errno;
1309 
1310  if (need_to_close)
1311  FileClose(file);
1312 
1313  errno = save_errno;
1314  return result;
1315 }
1316 
1317 /*
1318  * Unlink a file, given a file tag. Write the path into an output
1319  * buffer so the caller can use it in error messages.
1320  *
1321  * Return 0 on success, -1 on failure, with errno set.
1322  */
1323 int
1324 mdunlinkfiletag(const FileTag *ftag, char *path)
1325 {
1326  char *p;
1327 
1328  /* Compute the path. */
1329  p = relpathperm(ftag->rnode, MAIN_FORKNUM);
1330  strlcpy(path, p, MAXPGPATH);
1331  pfree(p);
1332 
1333  /* Try to unlink the file. */
1334  return unlink(path);
1335 }
1336 
1337 /*
1338  * Check if a given candidate request matches a given tag, when processing
1339  * a SYNC_FILTER_REQUEST request. This will be called for all pending
1340  * requests to find out whether to forget them.
1341  */
1342 bool
1343 mdfiletagmatches(const FileTag *ftag, const FileTag *candidate)
1344 {
1345  /*
1346  * For now we only use filter requests as a way to drop all scheduled
1347  * callbacks relating to a given database, when dropping the database.
1348  * We'll return true for all candidates that have the same database OID as
1349  * the ftag from the SYNC_FILTER_REQUEST request, so they're forgotten.
1350  */
1351  return ftag->rnode.dbNode == candidate->rnode.dbNode;
1352 }
uint32 segno
Definition: sync.h:50
#define relpathperm(rnode, forknum)
Definition: relpath.h:83
void mdimmedsync(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:883
#define AllocSetContextCreate
Definition: memutils.h:170
void smgrclose(SMgrRelation reln)
Definition: smgr.c:256
#define DEBUG1
Definition: elog.h:25
static MdfdVec * _mdfd_getseg(SMgrRelation reln, ForkNumber forkno, BlockNumber blkno, bool skipFsync, int behavior)
Definition: md.c:1131
int errhint(const char *fmt,...)
Definition: elog.c:1071
#define EXTENSION_DONT_CHECK_SIZE
Definition: md.c:118
File PathNameOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:1434
#define RelFileNodeBackendIsTemp(rnode)
Definition: relfilenode.h:78
BlockNumber mdnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:731
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
BlockNumber mdfd_segno
Definition: md.c:85
bool InRecovery
Definition: xlog.c:201
struct _MdfdVec MdfdVec
off_t FileSize(File file)
Definition: fd.c:2146
int16 forknum
Definition: sync.h:48
int errcode(int sqlerrcode)
Definition: elog.c:610
void smgrdounlinkall(SMgrRelation *rels, int nrels, bool isRedo)
Definition: smgr.c:405
#define MemSet(start, val, len)
Definition: c.h:971
RelFileNode rnode
Definition: sync.h:49
uint32 BlockNumber
Definition: block.h:31
#define INIT_MD_FILETAG(a, xx_rnode, xx_forknum, xx_segno)
Definition: md.c:92
unsigned int Oid
Definition: postgres_ext.h:31
void mdinit(void)
Definition: md.c:148
char * FilePathName(File file)
Definition: fd.c:2198
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1234
#define EXTENSION_FAIL
Definition: md.c:104
static void register_forget_request(RelFileNodeBackend rnode, ForkNumber forknum, BlockNumber segno)
Definition: md.c:961
static MemoryContext MdCxt
Definition: md.c:88
void mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: md.c:388
#define SmgrIsTemp(smgr)
Definition: smgr.h:79
#define sprintf
Definition: port.h:194
void pfree(void *pointer)
Definition: mcxt.c:1056
static MdfdVec * mdopenfork(SMgrRelation reln, ForkNumber forknum, int behavior)
Definition: md.c:452
#define ERROR
Definition: elog.h:43
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2370
#define EXTENSION_RETURN_NULL
Definition: md.c:106
static void register_unlink_segment(RelFileNodeBackend rnode, ForkNumber forknum, BlockNumber segno)
Definition: md.c:944
#define FATAL
Definition: elog.h:52
#define MAXPGPATH
void DropRelationFiles(RelFileNode *delrels, int ndelrels, bool isRedo)
Definition: md.c:993
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
RelFileNodeBackend smgr_rnode
Definition: smgr.h:42
int FileSync(File file, uint32 wait_event_info)
Definition: fd.c:2125
static void mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
Definition: md.c:291
void mdunlink(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
Definition: md.c:278
int errcode_for_file_access(void)
Definition: elog.c:633
#define EXTENSION_CREATE_RECOVERY
Definition: md.c:110
void mdwriteback(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, BlockNumber nblocks)
Definition: md.c:552
MemoryContext TopMemoryContext
Definition: mcxt.c:44
SMgrRelation smgropen(RelFileNode rnode, BackendId backend)
Definition: smgr.c:145
ForkNumber
Definition: relpath.h:40
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:45
int CloseTransientFile(int fd)
Definition: fd.c:2547
#define WARNING
Definition: elog.h:40
int mdunlinkfiletag(const FileTag *ftag, char *path)
Definition: md.c:1324
void mdopen(SMgrRelation reln)
Definition: md.c:495
int FileWrite(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:2027
#define InvalidBackendId
Definition: backendid.h:23
void * palloc0(Size size)
Definition: mcxt.c:980
int data_sync_elevel(int elevel)
Definition: fd.c:3597
static void _fdvec_resize(SMgrRelation reln, ForkNumber forknum, int nseg)
Definition: md.c:1025
bool RegisterSyncRequest(const FileTag *ftag, SyncRequestType type, bool retryOnError)
Definition: sync.c:530
Definition: md.c:82
bool mdfiletagmatches(const FileTag *ftag, const FileTag *candidate)
Definition: md.c:1343
static MdfdVec * _mdfd_openseg(SMgrRelation reln, ForkNumber forkno, BlockNumber segno, int oflags)
Definition: md.c:1086
void mdtruncate(SMgrRelation reln, ForkNumber forknum, BlockNumber nblocks)
Definition: md.c:786
#define ereport(elevel,...)
Definition: elog.h:144
#define EXTENSION_CREATE
Definition: md.c:108
bool mdexists(SMgrRelation reln, ForkNumber forkNum)
Definition: md.c:161
RelFileNode node
Definition: relfilenode.h:74
void ForgetDatabaseSyncRequests(Oid dbid)
Definition: md.c:975
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
void FileClose(File file)
Definition: fd.c:1824
int FilePrefetch(File file, off_t offset, int amount, uint32 wait_event_info)
Definition: fd.c:1920
void mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: md.c:666
#define Assert(condition)
Definition: c.h:738
void FileWriteback(File file, off_t offset, off_t nbytes, uint32 wait_event_info)
Definition: fd.c:1948
void TablespaceCreateDbspace(Oid spcNode, Oid dbNode, bool isRedo)
Definition: tablespace.c:115
static BlockNumber _mdnblocks(SMgrRelation reln, ForkNumber forknum, MdfdVec *seg)
Definition: md.c:1256
void mdprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum)
Definition: md.c:529
BackendId backend
Definition: relfilenode.h:75
#define InvalidBlockNumber
Definition: block.h:33
#define MAX_FORKNUM
Definition: relpath.h:55
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
int mdsyncfiletag(const FileTag *ftag, char *path)
Definition: md.c:1277
void mdclose(SMgrRelation reln, ForkNumber forknum)
Definition: md.c:506
void XLogDropRelation(RelFileNode rnode, ForkNumber forknum)
Definition: xlogutils.c:611
int md_num_open_segs[MAX_FORKNUM+1]
Definition: smgr.h:70
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
#define elog(elevel,...)
Definition: elog.h:214
int i
void mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
Definition: md.c:178
#define relpath(rnode, forknum)
Definition: relpath.h:87
struct _MdfdVec * md_seg_fds[MAX_FORKNUM+1]
Definition: smgr.h:71
int FileRead(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:1971
int FileTruncate(File file, off_t offset, uint32 wait_event_info)
Definition: fd.c:2163
File mdfd_vfd
Definition: md.c:84
int File
Definition: fd.h:49
Definition: sync.h:45
#define FILE_POSSIBLY_DELETED(err)
Definition: fd.h:69
static void register_dirty_segment(SMgrRelation reln, ForkNumber forknum, MdfdVec *seg)
Definition: md.c:918
static char * _mdfd_segpath(SMgrRelation reln, ForkNumber forknum, BlockNumber segno)
Definition: md.c:1063
bool zero_damaged_pages
Definition: bufmgr.c:109
void mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer)
Definition: md.c:601
#define ftruncate(a, b)
Definition: win32_port.h:59