Age Owner Branch data TLA Line data Source code
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-2026, 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 <limits.h>
25 : : #include <unistd.h>
26 : : #include <fcntl.h>
27 : : #include <sys/file.h>
28 : :
29 : : #include "access/xlogutils.h"
30 : : #include "commands/tablespace.h"
31 : : #include "common/file_utils.h"
32 : : #include "miscadmin.h"
33 : : #include "pg_trace.h"
34 : : #include "pgstat.h"
35 : : #include "storage/aio.h"
36 : : #include "storage/bufmgr.h"
37 : : #include "storage/fd.h"
38 : : #include "storage/md.h"
39 : : #include "storage/relfilelocator.h"
40 : : #include "storage/smgr.h"
41 : : #include "storage/sync.h"
42 : : #include "utils/memutils.h"
43 : : #include "utils/wait_event.h"
44 : :
45 : : /*
46 : : * The magnetic disk storage manager keeps track of open file
47 : : * descriptors in its own descriptor pool. This is done to make it
48 : : * easier to support relations that are larger than the operating
49 : : * system's file size limit (often 2GBytes). In order to do that,
50 : : * we break relations up into "segment" files that are each shorter than
51 : : * the OS file size limit. The segment size is set by the RELSEG_SIZE
52 : : * configuration constant in pg_config.h.
53 : : *
54 : : * On disk, a relation must consist of consecutively numbered segment
55 : : * files in the pattern
56 : : * -- Zero or more full segments of exactly RELSEG_SIZE blocks each
57 : : * -- Exactly one partial segment of size 0 <= size < RELSEG_SIZE blocks
58 : : * -- Optionally, any number of inactive segments of size 0 blocks.
59 : : * The full and partial segments are collectively the "active" segments.
60 : : * Inactive segments are those that once contained data but are currently
61 : : * not needed because of an mdtruncate() operation. The reason for leaving
62 : : * them present at size zero, rather than unlinking them, is that other
63 : : * backends and/or the checkpointer might be holding open file references to
64 : : * such segments. If the relation expands again after mdtruncate(), such
65 : : * that a deactivated segment becomes active again, it is important that
66 : : * such file references still be valid --- else data might get written
67 : : * out to an unlinked old copy of a segment file that will eventually
68 : : * disappear.
69 : : *
70 : : * RELSEG_SIZE must fit into BlockNumber; but since we expose its value
71 : : * as an integer GUC, it actually needs to fit in signed int. It's worth
72 : : * having a cross-check for this since configure's --with-segsize options
73 : : * could let people select insane values.
74 : : */
75 : : StaticAssertDecl(RELSEG_SIZE > 0 && RELSEG_SIZE <= INT_MAX,
76 : : "RELSEG_SIZE must fit in an integer");
77 : :
78 : : /*
79 : : * File descriptors are stored in the per-fork md_seg_fds arrays inside
80 : : * SMgrRelation. The length of these arrays is stored in md_num_open_segs.
81 : : * Note that a fork's md_num_open_segs having a specific value does not
82 : : * necessarily mean the relation doesn't have additional segments; we may
83 : : * just not have opened the next segment yet. (We could not have "all
84 : : * segments are in the array" as an invariant anyway, since another backend
85 : : * could extend the relation while we aren't looking.) We do not have
86 : : * entries for inactive segments, however; as soon as we find a partial
87 : : * segment, we assume that any subsequent segments are inactive.
88 : : *
89 : : * The entire MdfdVec array is palloc'd in the MdCxt memory context.
90 : : */
91 : :
92 : : typedef struct _MdfdVec
93 : : {
94 : : File mdfd_vfd; /* fd number in fd.c's pool */
95 : : BlockNumber mdfd_segno; /* segment number, from 0 */
96 : : } MdfdVec;
97 : :
98 : : static MemoryContext MdCxt; /* context for all MdfdVec objects */
99 : :
100 : :
101 : : /* Populate a file tag describing an md.c segment file. */
102 : : #define INIT_MD_FILETAG(a,xx_rlocator,xx_forknum,xx_segno) \
103 : : ( \
104 : : memset(&(a), 0, sizeof(FileTag)), \
105 : : (a).handler = SYNC_HANDLER_MD, \
106 : : (a).rlocator = (xx_rlocator), \
107 : : (a).forknum = (xx_forknum), \
108 : : (a).segno = (xx_segno) \
109 : : )
110 : :
111 : :
112 : : /*** behavior for mdopen & _mdfd_getseg ***/
113 : : /* ereport if segment not present */
114 : : #define EXTENSION_FAIL (1 << 0)
115 : : /* return NULL if segment not present */
116 : : #define EXTENSION_RETURN_NULL (1 << 1)
117 : : /* create new segments as needed */
118 : : #define EXTENSION_CREATE (1 << 2)
119 : : /* create new segments if needed during recovery */
120 : : #define EXTENSION_CREATE_RECOVERY (1 << 3)
121 : : /* don't try to open a segment, if not already open */
122 : : #define EXTENSION_DONT_OPEN (1 << 5)
123 : :
124 : :
125 : : /*
126 : : * Fixed-length string to represent paths to files that need to be built by
127 : : * md.c.
128 : : *
129 : : * The maximum number of segments is MaxBlockNumber / RELSEG_SIZE, where
130 : : * RELSEG_SIZE can be set to 1 (for testing only).
131 : : */
132 : : #define SEGMENT_CHARS OIDCHARS
133 : : #define MD_PATH_STR_MAXLEN \
134 : : (\
135 : : REL_PATH_STR_MAXLEN \
136 : : + sizeof((char)'.') \
137 : : + SEGMENT_CHARS \
138 : : )
139 : : typedef struct MdPathStr
140 : : {
141 : : char str[MD_PATH_STR_MAXLEN + 1];
142 : : } MdPathStr;
143 : :
144 : :
145 : : /* local routines */
146 : : static void mdunlinkfork(RelFileLocatorBackend rlocator, ForkNumber forknum,
147 : : bool isRedo);
148 : : static MdfdVec *mdopenfork(SMgrRelation reln, ForkNumber forknum, int behavior);
149 : : static void register_dirty_segment(SMgrRelation reln, ForkNumber forknum,
150 : : MdfdVec *seg);
151 : : static void register_unlink_tombstone(RelFileLocatorBackend rlocator);
152 : : static void register_forget_request(RelFileLocatorBackend rlocator, ForkNumber forknum,
153 : : BlockNumber segno);
154 : : static void _fdvec_resize(SMgrRelation reln,
155 : : ForkNumber forknum,
156 : : int nseg);
157 : : static MdPathStr _mdfd_segpath(SMgrRelation reln, ForkNumber forknum,
158 : : BlockNumber segno);
159 : : static MdfdVec *_mdfd_openseg(SMgrRelation reln, ForkNumber forknum,
160 : : BlockNumber segno, int oflags);
161 : : static MdfdVec *_mdfd_getseg(SMgrRelation reln, ForkNumber forknum,
162 : : BlockNumber blkno, bool skipFsync, int behavior);
163 : : static BlockNumber _mdnblocks(SMgrRelation reln, ForkNumber forknum,
164 : : MdfdVec *seg);
165 : :
166 : : static PgAioResult md_readv_complete(PgAioHandle *ioh, PgAioResult prior_result, uint8 cb_data);
167 : : static void md_readv_report(PgAioResult result, const PgAioTargetData *td, int elevel);
168 : :
169 : : const PgAioHandleCallbacks aio_md_readv_cb = {
170 : : .complete_shared = md_readv_complete,
171 : : .report = md_readv_report,
172 : : };
173 : :
174 : :
175 : : static inline int
1204 tmunro@postgresql.or 176 :CBC 1542066 : _mdfd_open_flags(void)
177 : : {
178 : 1542066 : int flags = O_RDWR | PG_BINARY;
179 : :
180 [ + + ]: 1542066 : if (io_direct_flags & IO_DIRECT_DATA)
181 : 336 : flags |= PG_O_DIRECT;
182 : :
183 : 1542066 : return flags;
184 : : }
185 : :
186 : : /*
187 : : * mdinit() -- Initialize private state for magnetic disk storage manager.
188 : : */
189 : : void
9159 tgl@sss.pgh.pa.us 190 : 22797 : mdinit(void)
191 : : {
9523 192 : 22797 : MdCxt = AllocSetContextCreate(TopMemoryContext,
193 : : "MdSmgr",
194 : : ALLOCSET_DEFAULT_SIZES);
6239 heikki.linnakangas@i 195 : 22797 : }
196 : :
197 : : /*
198 : : * mdexists() -- Does the physical file exist?
199 : : *
200 : : * Note: this will return true for lingering files, with pending deletions
201 : : */
202 : : bool
1405 pg@bowt.ie 203 : 588436 : mdexists(SMgrRelation reln, ForkNumber forknum)
204 : : {
205 : : /*
206 : : * Close it first, to ensure that we notice if the fork has been unlinked
207 : : * since we opened it. As an optimization, we can skip that in recovery,
208 : : * which already closes relations when dropping them.
209 : : */
1570 tmunro@postgresql.or 210 [ + + ]: 588436 : if (!InRecovery)
1405 pg@bowt.ie 211 : 565806 : mdclose(reln, forknum);
212 : :
213 : 588436 : return (mdopenfork(reln, forknum, EXTENSION_RETURN_NULL) != NULL);
214 : : }
215 : :
216 : : /*
217 : : * mdcreate() -- Create a new relation on magnetic disk.
218 : : *
219 : : * If isRedo is true, it's okay for the relation to exist already.
220 : : */
221 : : void
222 : 5990818 : mdcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
223 : : {
224 : : MdfdVec *mdfd;
225 : : RelPathStr path;
226 : : File fd;
227 : :
228 [ + + + + ]: 5990818 : if (isRedo && reln->md_num_open_segs[forknum] > 0)
7143 tgl@sss.pgh.pa.us 229 : 5783421 : return; /* created and opened already... */
230 : :
1405 pg@bowt.ie 231 [ - + ]: 207397 : Assert(reln->md_num_open_segs[forknum] == 0);
232 : :
233 : : /*
234 : : * We may be using the target table space for the first time in this
235 : : * database, so create a per-database subdirectory if needed.
236 : : *
237 : : * XXX this is a fairly ugly violation of module layering, but this seems
238 : : * to be the best place to put the check. Maybe TablespaceCreateDbspace
239 : : * should be here and not in commands/tablespace.c? But that would imply
240 : : * importing a lot of stuff that smgr.c oughtn't know, either.
241 : : */
1480 rhaas@postgresql.org 242 : 207397 : TablespaceCreateDbspace(reln->smgr_rlocator.locator.spcOid,
243 : : reln->smgr_rlocator.locator.dbOid,
244 : : isRedo);
245 : :
1405 pg@bowt.ie 246 : 207397 : path = relpath(reln->smgr_rlocator, forknum);
247 : :
515 andres@anarazel.de 248 : 207397 : fd = PathNameOpenFile(path.str, _mdfd_open_flags() | O_CREAT | O_EXCL);
249 : :
10548 bruce@momjian.us 250 [ + + ]: 207397 : if (fd < 0)
251 : : {
9256 252 : 17288 : int save_errno = errno;
253 : :
2735 akapila@postgresql.o 254 [ + - ]: 17288 : if (isRedo)
515 andres@anarazel.de 255 : 17288 : fd = PathNameOpenFile(path.str, _mdfd_open_flags());
10548 bruce@momjian.us 256 [ - + ]: 17288 : if (fd < 0)
257 : : {
258 : : /* be sure to report the error reported by create, not open */
9532 tgl@sss.pgh.pa.us 259 :UBC 0 : errno = save_errno;
7143 260 [ # # ]: 0 : ereport(ERROR,
261 : : (errcode_for_file_access(),
262 : : errmsg("could not create file \"%s\": %m", path.str)));
263 : : }
264 : : }
265 : :
1405 pg@bowt.ie 266 :CBC 207397 : _fdvec_resize(reln, forknum, 1);
267 : 207397 : mdfd = &reln->md_seg_fds[forknum][0];
3607 andres@anarazel.de 268 : 207397 : mdfd->mdfd_vfd = fd;
269 : 207397 : mdfd->mdfd_segno = 0;
270 : :
1117 heikki.linnakangas@i 271 [ + + ]: 207397 : if (!SmgrIsTemp(reln))
272 : 202523 : register_dirty_segment(reln, forknum, mdfd);
273 : : }
274 : :
275 : : /*
276 : : * mdunlink() -- Unlink a relation.
277 : : *
278 : : * Note that we're passed a RelFileLocatorBackend --- by the time this is called,
279 : : * there won't be an SMgrRelation hashtable entry anymore.
280 : : *
281 : : * forknum can be a fork number to delete a specific fork, or InvalidForkNumber
282 : : * to delete all forks.
283 : : *
284 : : * For regular relations, we don't unlink the first segment file of the rel,
285 : : * but just truncate it to zero length, and record a request to unlink it after
286 : : * the next checkpoint. Additional segments can be unlinked immediately,
287 : : * however. Leaving the empty file in place prevents that relfilenumber
288 : : * from being reused. The scenario this protects us from is:
289 : : * 1. We delete a relation (and commit, and actually remove its file).
290 : : * 2. We create a new relation, which by chance gets the same relfilenumber as
291 : : * the just-deleted one (OIDs must've wrapped around for that to happen).
292 : : * 3. We crash before another checkpoint occurs.
293 : : * During replay, we would delete the file and then recreate it, which is fine
294 : : * if the contents of the file were repopulated by subsequent WAL entries.
295 : : * But if we didn't WAL-log insertions, but instead relied on fsyncing the
296 : : * file after populating it (as we do at wal_level=minimal), the contents of
297 : : * the file would be lost forever. By leaving the empty file until after the
298 : : * next checkpoint, we prevent reassignment of the relfilenumber until it's
299 : : * safe, because relfilenumber assignment skips over any existing file.
300 : : *
301 : : * Additional segments, if any, are truncated and then unlinked. The reason
302 : : * for truncating is that other backends may still hold open FDs for these at
303 : : * the smgr level, so that the kernel can't remove the file yet. We want to
304 : : * reclaim the disk space right away despite that.
305 : : *
306 : : * We do not need to go through this dance for temp relations, though, because
307 : : * we never make WAL entries for temp rels, and so a temp rel poses no threat
308 : : * to the health of a regular rel that has taken over its relfilenumber.
309 : : * The fact that temp rels and regular rels have different file naming
310 : : * patterns provides additional safety. Other backends shouldn't have open
311 : : * FDs for them, either.
312 : : *
313 : : * We also don't do it while performing a binary upgrade. There is no reuse
314 : : * hazard in that case, since after a crash or even a simple ERROR, the
315 : : * upgrade fails and the whole cluster must be recreated from scratch.
316 : : * Furthermore, it is important to remove the files from disk immediately,
317 : : * because we may be about to reuse the same relfilenumber.
318 : : *
319 : : * All the above applies only to the relation's main fork; other forks can
320 : : * just be removed immediately, since they are not needed to prevent the
321 : : * relfilenumber from being recycled. Also, we do not carefully
322 : : * track whether other forks have been created or not, but just attempt to
323 : : * unlink them unconditionally; so we should never complain about ENOENT.
324 : : *
325 : : * If isRedo is true, it's unsurprising for the relation to be already gone.
326 : : * Also, we should remove the file immediately instead of queuing a request
327 : : * for later, since during redo there's no possibility of creating a
328 : : * conflicting relation.
329 : : *
330 : : * Note: we currently just never warn about ENOENT at all. We could warn in
331 : : * the main-fork, non-isRedo case, but it doesn't seem worth the trouble.
332 : : *
333 : : * Note: any failure should be reported as WARNING not ERROR, because
334 : : * we are usually not in a transaction anymore when this is called.
335 : : */
336 : : void
1405 pg@bowt.ie 337 : 247804 : mdunlink(RelFileLocatorBackend rlocator, ForkNumber forknum, bool isRedo)
338 : : {
339 : : /* Now do the per-fork work */
340 [ - + ]: 247804 : if (forknum == InvalidForkNumber)
341 : : {
1405 pg@bowt.ie 342 [ # # ]:UBC 0 : for (forknum = 0; forknum <= MAX_FORKNUM; forknum++)
343 : 0 : mdunlinkfork(rlocator, forknum, isRedo);
344 : : }
345 : : else
1405 pg@bowt.ie 346 :CBC 247804 : mdunlinkfork(rlocator, forknum, isRedo);
5119 tgl@sss.pgh.pa.us 347 : 247804 : }
348 : :
349 : : /*
350 : : * Truncate a file to release disk space.
351 : : */
352 : : static int
2062 tmunro@postgresql.or 353 : 290192 : do_truncate(const char *path)
354 : : {
355 : : int save_errno;
356 : : int ret;
357 : :
358 : 290192 : ret = pg_truncate(path, 0);
359 : :
360 : : /* Log a warning here to avoid repetition in callers. */
361 [ + + - + ]: 290192 : if (ret < 0 && errno != ENOENT)
362 : : {
2062 tmunro@postgresql.or 363 :UBC 0 : save_errno = errno;
364 [ # # ]: 0 : ereport(WARNING,
365 : : (errcode_for_file_access(),
366 : : errmsg("could not truncate file \"%s\": %m", path)));
367 : 0 : errno = save_errno;
368 : : }
369 : :
2062 tmunro@postgresql.or 370 :CBC 290192 : return ret;
371 : : }
372 : :
373 : : static void
1405 pg@bowt.ie 374 : 247804 : mdunlinkfork(RelFileLocatorBackend rlocator, ForkNumber forknum, bool isRedo)
375 : : {
376 : : RelPathStr path;
377 : : int ret;
378 : : int save_errno;
379 : :
380 : 247804 : path = relpath(rlocator, forknum);
381 : :
382 : : /*
383 : : * Truncate and then unlink the first segment, or just register a request
384 : : * to unlink it later, as described in the comments for mdunlink().
385 : : */
1354 tgl@sss.pgh.pa.us 386 [ + + + + : 247804 : if (isRedo || IsBinaryUpgrade || forknum != MAIN_FORKNUM ||
+ + ]
387 [ + + ]: 53066 : RelFileLocatorBackendIsTemp(rlocator))
388 : : {
1480 rhaas@postgresql.org 389 [ + + ]: 199225 : if (!RelFileLocatorBackendIsTemp(rlocator))
390 : : {
391 : : /* Prevent other backends' fds from holding on to the disk space */
515 andres@anarazel.de 392 : 181277 : ret = do_truncate(path.str);
393 : :
394 : : /* Forget any pending sync requests for the first segment */
1356 tgl@sss.pgh.pa.us 395 : 181277 : save_errno = errno;
1405 pg@bowt.ie 396 : 181277 : register_forget_request(rlocator, forknum, 0 /* first seg */ );
1356 tgl@sss.pgh.pa.us 397 : 181277 : errno = save_errno;
398 : : }
399 : : else
2062 tmunro@postgresql.or 400 : 17948 : ret = 0;
401 : :
402 : : /* Next unlink the file, unless it was already found to be missing */
1354 tgl@sss.pgh.pa.us 403 [ + + - + ]: 199225 : if (ret >= 0 || errno != ENOENT)
404 : : {
515 andres@anarazel.de 405 : 29710 : ret = unlink(path.str);
2062 tmunro@postgresql.or 406 [ + + - + ]: 29710 : if (ret < 0 && errno != ENOENT)
407 : : {
1354 tgl@sss.pgh.pa.us 408 :UBC 0 : save_errno = errno;
2062 tmunro@postgresql.or 409 [ # # ]: 0 : ereport(WARNING,
410 : : (errcode_for_file_access(),
411 : : errmsg("could not remove file \"%s\": %m", path.str)));
1354 tgl@sss.pgh.pa.us 412 : 0 : errno = save_errno;
413 : : }
414 : : }
415 : : }
416 : : else
417 : : {
418 : : /* Prevent other backends' fds from holding on to the disk space */
515 andres@anarazel.de 419 :CBC 48579 : ret = do_truncate(path.str);
420 : :
421 : : /* Register request to unlink first segment later */
1354 tgl@sss.pgh.pa.us 422 : 48579 : save_errno = errno;
18 heikki.linnakangas@i 423 :GNC 48579 : register_unlink_tombstone(rlocator);
1354 tgl@sss.pgh.pa.us 424 :CBC 48579 : errno = save_errno;
425 : : }
426 : :
427 : : /*
428 : : * Delete any additional segments.
429 : : *
430 : : * Note that because we loop until getting ENOENT, we will correctly
431 : : * remove all inactive segments as well as active ones. Ideally we'd
432 : : * continue the loop until getting exactly that errno, but that risks an
433 : : * infinite loop if the problem is directory-wide (for instance, if we
434 : : * suddenly can't read the data directory itself). We compromise by
435 : : * continuing after a non-ENOENT truncate error, but stopping after any
436 : : * unlink error. If there is indeed a directory-wide problem, additional
437 : : * unlink attempts wouldn't work anyway.
438 : : */
439 [ + + - + ]: 247804 : if (ret >= 0 || errno != ENOENT)
440 : : {
441 : : MdPathStr segpath;
442 : : BlockNumber segno;
443 : :
444 : 65210 : for (segno = 1;; segno++)
445 : : {
515 andres@anarazel.de 446 : 65210 : sprintf(segpath.str, "%s.%u", path.str, segno);
447 : :
1480 rhaas@postgresql.org 448 [ + + ]: 65210 : if (!RelFileLocatorBackendIsTemp(rlocator))
449 : : {
450 : : /*
451 : : * Prevent other backends' fds from holding on to the disk
452 : : * space. We're done if we see ENOENT, though.
453 : : */
515 andres@anarazel.de 454 [ + - + - ]: 60336 : if (do_truncate(segpath.str) < 0 && errno == ENOENT)
2062 tmunro@postgresql.or 455 : 60336 : break;
456 : :
457 : : /*
458 : : * Forget any pending sync requests for this segment before we
459 : : * try to unlink.
460 : : */
1405 pg@bowt.ie 461 :UBC 0 : register_forget_request(rlocator, forknum, segno);
462 : : }
463 : :
515 andres@anarazel.de 464 [ + - ]:CBC 4874 : if (unlink(segpath.str) < 0)
465 : : {
466 : : /* ENOENT is expected after the last segment... */
9390 tgl@sss.pgh.pa.us 467 [ - + ]: 4874 : if (errno != ENOENT)
7143 tgl@sss.pgh.pa.us 468 [ # # ]:UBC 0 : ereport(WARNING,
469 : : (errcode_for_file_access(),
470 : : errmsg("could not remove file \"%s\": %m", segpath.str)));
9390 tgl@sss.pgh.pa.us 471 :CBC 4874 : break;
472 : : }
473 : : }
474 : : }
10973 scrappy@hub.org 475 : 247804 : }
476 : :
477 : : /*
478 : : * mdextend() -- Add a block to the specified relation.
479 : : *
480 : : * The semantics are nearly the same as mdwrite(): write at the
481 : : * specified position. However, this is to be used for the case of
482 : : * extending a relation (i.e., blocknum is at or beyond the current
483 : : * EOF). Note that we assume writing a block beyond current EOF
484 : : * causes intervening file space to become filled with zeroes.
485 : : */
486 : : void
6557 heikki.linnakangas@i 487 : 143023 : mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
488 : : const void *buffer, bool skipFsync)
489 : : {
490 : : pgoff_t seekpos;
491 : : ssize_t nbytes;
492 : : MdfdVec *v;
493 : :
494 : : /* If this build supports direct I/O, the buffer must be I/O aligned. */
495 : : if (PG_O_DIRECT != 0 && PG_IO_ALIGN_SIZE <= BLCKSZ)
1204 tmunro@postgresql.or 496 [ - + ]: 143023 : Assert((uintptr_t) buffer == TYPEALIGN(PG_IO_ALIGN_SIZE, buffer));
497 : :
498 : : /* This assert is too expensive to have on normally ... */
499 : : #ifdef CHECK_WRITE_VS_EXTEND
500 : : Assert(blocknum >= mdnblocks(reln, forknum));
501 : : #endif
502 : :
503 : : /*
504 : : * If a relation manages to grow to 2^32-1 blocks, refuse to extend it any
505 : : * more --- we mustn't create a block whose number actually is
506 : : * InvalidBlockNumber. (Note that this failure should be unreachable
507 : : * because of upstream checks in bufmgr.c.)
508 : : */
7143 tgl@sss.pgh.pa.us 509 [ - + ]: 143023 : if (blocknum == InvalidBlockNumber)
7143 tgl@sss.pgh.pa.us 510 [ # # ]:UBC 0 : ereport(ERROR,
511 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
512 : : errmsg("cannot extend file \"%s\" beyond %u blocks",
513 : : relpath(reln->smgr_rlocator, forknum).str,
514 : : InvalidBlockNumber)));
515 : :
5825 rhaas@postgresql.org 516 :CBC 143023 : v = _mdfd_getseg(reln, forknum, blocknum, skipFsync, EXTENSION_CREATE);
517 : :
254 michael@paquier.xyz 518 : 143023 : seekpos = (pgoff_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
519 : :
520 [ - + ]: 143023 : Assert(seekpos < (pgoff_t) BLCKSZ * RELSEG_SIZE);
521 : :
2817 tmunro@postgresql.or 522 [ - + ]: 143023 : if ((nbytes = FileWrite(v->mdfd_vfd, buffer, BLCKSZ, seekpos, WAIT_EVENT_DATA_FILE_EXTEND)) != BLCKSZ)
523 : : {
7143 tgl@sss.pgh.pa.us 524 [ # # ]:UBC 0 : if (nbytes < 0)
525 [ # # ]: 0 : ereport(ERROR,
526 : : (errcode_for_file_access(),
527 : : errmsg("could not extend file \"%s\": %m",
528 : : FilePathName(v->mdfd_vfd)),
529 : : errhint("Check free disk space.")));
530 : : /* short write: complain appropriately */
531 [ # # ]: 0 : ereport(ERROR,
532 : : (errcode(ERRCODE_DISK_FULL),
533 : : errmsg("could not extend file \"%s\": wrote only %zd of %zu bytes at block %u",
534 : : FilePathName(v->mdfd_vfd),
535 : : nbytes, (size_t) BLCKSZ, blocknum),
536 : : errhint("Check free disk space.")));
537 : : }
538 : :
5825 rhaas@postgresql.org 539 [ + + + - ]:CBC 143023 : if (!skipFsync && !SmgrIsTemp(reln))
6557 heikki.linnakangas@i 540 : 38 : register_dirty_segment(reln, forknum, v);
541 : :
542 [ - + ]: 143023 : Assert(_mdnblocks(reln, forknum, v) <= ((BlockNumber) RELSEG_SIZE));
10973 scrappy@hub.org 543 : 143023 : }
544 : :
545 : : /*
546 : : * mdzeroextend() -- Add new zeroed out blocks to the specified relation.
547 : : *
548 : : * Similar to mdextend(), except the relation can be extended by multiple
549 : : * blocks at once and the added blocks will be filled with zeroes.
550 : : */
551 : : void
1207 andres@anarazel.de 552 : 263797 : mdzeroextend(SMgrRelation reln, ForkNumber forknum,
553 : : BlockNumber blocknum, int nblocks, bool skipFsync)
554 : : {
555 : : MdfdVec *v;
556 : 263797 : BlockNumber curblocknum = blocknum;
557 : 263797 : int remblocks = nblocks;
558 : :
559 [ - + ]: 263797 : Assert(nblocks > 0);
560 : :
561 : : /* This assert is too expensive to have on normally ... */
562 : : #ifdef CHECK_WRITE_VS_EXTEND
563 : : Assert(blocknum >= mdnblocks(reln, forknum));
564 : : #endif
565 : :
566 : : /*
567 : : * If a relation manages to grow to 2^32-1 blocks, refuse to extend it any
568 : : * more --- we mustn't create a block whose number actually is
569 : : * InvalidBlockNumber or larger.
570 : : */
571 [ - + ]: 263797 : if ((uint64) blocknum + nblocks >= (uint64) InvalidBlockNumber)
1207 andres@anarazel.de 572 [ # # ]:UBC 0 : ereport(ERROR,
573 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
574 : : errmsg("cannot extend file \"%s\" beyond %u blocks",
575 : : relpath(reln->smgr_rlocator, forknum).str,
576 : : InvalidBlockNumber)));
577 : :
1207 andres@anarazel.de 578 [ + + ]:CBC 527594 : while (remblocks > 0)
579 : : {
1163 tgl@sss.pgh.pa.us 580 : 263797 : BlockNumber segstartblock = curblocknum % ((BlockNumber) RELSEG_SIZE);
254 michael@paquier.xyz 581 : 263797 : pgoff_t seekpos = (pgoff_t) BLCKSZ * segstartblock;
582 : : int numblocks;
583 : :
1207 andres@anarazel.de 584 [ - + ]: 263797 : if (segstartblock + remblocks > RELSEG_SIZE)
1207 andres@anarazel.de 585 :UBC 0 : numblocks = RELSEG_SIZE - segstartblock;
586 : : else
1207 andres@anarazel.de 587 :CBC 263797 : numblocks = remblocks;
588 : :
589 : 263797 : v = _mdfd_getseg(reln, forknum, curblocknum, skipFsync, EXTENSION_CREATE);
590 : :
591 [ - + ]: 263797 : Assert(segstartblock < RELSEG_SIZE);
592 [ - + ]: 263797 : Assert(segstartblock + numblocks <= RELSEG_SIZE);
593 : :
594 : : /*
595 : : * If available and useful, use posix_fallocate() (via
596 : : * FileFallocate()) to extend the relation. That's often more
597 : : * efficient than using write(), as it commonly won't cause the kernel
598 : : * to allocate page cache space for the extended pages.
599 : : *
600 : : * However, we don't use FileFallocate() for small extensions, as it
601 : : * defeats delayed allocation on some filesystems. Not clear where
602 : : * that decision should be made though? For now just use a cutoff of
603 : : * 8, anything between 4 and 8 worked OK in some local testing.
604 : : */
420 tmunro@postgresql.or 605 [ + + ]: 263797 : if (numblocks > 8 &&
606 [ + - ]: 704 : file_extend_method != FILE_EXTEND_METHOD_WRITE_ZEROS)
1207 andres@anarazel.de 607 : 704 : {
420 tmunro@postgresql.or 608 : 704 : int ret = 0;
609 : :
610 : : #ifdef HAVE_POSIX_FALLOCATE
611 [ + - ]: 704 : if (file_extend_method == FILE_EXTEND_METHOD_POSIX_FALLOCATE)
612 : : {
613 : 704 : ret = FileFallocate(v->mdfd_vfd,
614 : : seekpos, (pgoff_t) BLCKSZ * numblocks,
615 : : WAIT_EVENT_DATA_FILE_EXTEND);
616 : : }
617 : : else
618 : : #endif
619 : : {
420 tmunro@postgresql.or 620 [ # # ]:UBC 0 : elog(ERROR, "unsupported file_extend_method: %d",
621 : : file_extend_method);
622 : : }
1207 andres@anarazel.de 623 [ - + ]:CBC 704 : if (ret != 0)
624 : : {
1207 andres@anarazel.de 625 [ # # ]:UBC 0 : ereport(ERROR,
626 : : errcode_for_file_access(),
627 : : errmsg("could not extend file \"%s\" with FileFallocate(): %m",
628 : : FilePathName(v->mdfd_vfd)),
629 : : errhint("Check free disk space."));
630 : : }
631 : : }
632 : : else
633 : : {
634 : : int ret;
635 : :
636 : : /*
637 : : * Even if we don't want to use fallocate, we can still extend a
638 : : * bit more efficiently than writing each 8kB block individually.
639 : : * pg_pwrite_zeros() (via FileZero()) uses pg_pwritev_with_retry()
640 : : * to avoid multiple writes or needing a zeroed buffer for the
641 : : * whole length of the extension.
642 : : */
1207 andres@anarazel.de 643 :CBC 263093 : ret = FileZero(v->mdfd_vfd,
644 : : seekpos, (pgoff_t) BLCKSZ * numblocks,
645 : : WAIT_EVENT_DATA_FILE_EXTEND);
646 [ - + ]: 263093 : if (ret < 0)
1207 andres@anarazel.de 647 [ # # ]:UBC 0 : ereport(ERROR,
648 : : errcode_for_file_access(),
649 : : errmsg("could not extend file \"%s\": %m",
650 : : FilePathName(v->mdfd_vfd)),
651 : : errhint("Check free disk space."));
652 : : }
653 : :
1207 andres@anarazel.de 654 [ + - + + ]:CBC 263797 : if (!skipFsync && !SmgrIsTemp(reln))
655 : 248818 : register_dirty_segment(reln, forknum, v);
656 : :
657 [ - + ]: 263797 : Assert(_mdnblocks(reln, forknum, v) <= ((BlockNumber) RELSEG_SIZE));
658 : :
659 : 263797 : remblocks -= numblocks;
660 : 263797 : curblocknum += numblocks;
661 : : }
662 : 263797 : }
663 : :
664 : : /*
665 : : * mdopenfork() -- Open one fork of the specified relation.
666 : : *
667 : : * Note we only open the first segment, when there are multiple segments.
668 : : *
669 : : * If first segment is not present, either ereport or return NULL according
670 : : * to "behavior". We treat EXTENSION_CREATE the same as EXTENSION_FAIL;
671 : : * EXTENSION_CREATE means it's OK to extend an existing relation, not to
672 : : * invent one out of whole cloth.
673 : : */
674 : : static MdfdVec *
2565 tmunro@postgresql.or 675 : 4124195 : mdopenfork(SMgrRelation reln, ForkNumber forknum, int behavior)
676 : : {
677 : : MdfdVec *mdfd;
678 : : RelPathStr path;
679 : : File fd;
680 : :
681 : : /* No work if already open */
3607 andres@anarazel.de 682 [ + + ]: 4124195 : if (reln->md_num_open_segs[forknum] > 0)
683 : 2839147 : return &reln->md_seg_fds[forknum][0];
684 : :
1480 rhaas@postgresql.org 685 : 1285048 : path = relpath(reln->smgr_rlocator, forknum);
686 : :
515 andres@anarazel.de 687 : 1285048 : fd = PathNameOpenFile(path.str, _mdfd_open_flags());
688 : :
10548 bruce@momjian.us 689 [ + + ]: 1285048 : if (fd < 0)
690 : : {
2735 akapila@postgresql.o 691 [ + + ]: 406926 : if ((behavior & EXTENSION_RETURN_NULL) &&
692 [ + - ]: 406904 : FILE_POSSIBLY_DELETED(errno))
693 : 406904 : return NULL;
694 [ + - ]: 22 : ereport(ERROR,
695 : : (errcode_for_file_access(),
696 : : errmsg("could not open file \"%s\": %m", path.str)));
697 : : }
698 : :
3607 andres@anarazel.de 699 : 878122 : _fdvec_resize(reln, forknum, 1);
700 : 878122 : mdfd = &reln->md_seg_fds[forknum][0];
8090 tgl@sss.pgh.pa.us 701 : 878122 : mdfd->mdfd_vfd = fd;
702 : 878122 : mdfd->mdfd_segno = 0;
703 : :
6557 heikki.linnakangas@i 704 [ - + ]: 878122 : Assert(_mdnblocks(reln, forknum, mdfd) <= ((BlockNumber) RELSEG_SIZE));
705 : :
8090 tgl@sss.pgh.pa.us 706 : 878122 : return mdfd;
707 : : }
708 : :
709 : : /*
710 : : * mdopen() -- Initialize newly-opened relation.
711 : : */
712 : : void
2565 tmunro@postgresql.or 713 : 1201739 : mdopen(SMgrRelation reln)
714 : : {
715 : : /* mark it not open */
716 [ + + ]: 6008695 : for (int forknum = 0; forknum <= MAX_FORKNUM; forknum++)
717 : 4806956 : reln->md_num_open_segs[forknum] = 0;
718 : 1201739 : }
719 : :
720 : : /*
721 : : * mdclose() -- Close the specified relation, if it isn't closed already.
722 : : */
723 : : void
6557 heikki.linnakangas@i 724 : 4450802 : mdclose(SMgrRelation reln, ForkNumber forknum)
725 : : {
3607 andres@anarazel.de 726 : 4450802 : int nopensegs = reln->md_num_open_segs[forknum];
727 : :
728 : : /* No work if already closed */
729 [ + + ]: 4450802 : if (nopensegs == 0)
7143 tgl@sss.pgh.pa.us 730 : 3803701 : return;
731 : :
732 : : /* close segments starting from the end */
3607 andres@anarazel.de 733 [ + + ]: 1294202 : while (nopensegs > 0)
734 : : {
735 : 647101 : MdfdVec *v = &reln->md_seg_fds[forknum][nopensegs - 1];
736 : :
2388 noah@leadboat.com 737 : 647101 : FileClose(v->mdfd_vfd);
738 : 647101 : _fdvec_resize(reln, forknum, nopensegs - 1);
3607 andres@anarazel.de 739 : 647101 : nopensegs--;
740 : : }
741 : : }
742 : :
743 : : /*
744 : : * mdprefetch() -- Initiate asynchronous read of the specified blocks of a relation
745 : : */
746 : : bool
952 tmunro@postgresql.or 747 : 9493 : mdprefetch(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
748 : : int nblocks)
749 : : {
750 : : #ifdef USE_PREFETCH
751 : :
1204 752 [ - + ]: 9493 : Assert((io_direct_flags & IO_DIRECT_DATA) == 0);
753 : :
952 754 [ - + ]: 9493 : if ((uint64) blocknum + nblocks > (uint64) MaxBlockNumber + 1)
2299 tmunro@postgresql.or 755 :UBC 0 : return false;
756 : :
952 tmunro@postgresql.or 757 [ + + ]:CBC 18986 : while (nblocks > 0)
758 : : {
759 : : pgoff_t seekpos;
760 : : MdfdVec *v;
761 : : int nblocks_this_segment;
762 : :
763 : 9493 : v = _mdfd_getseg(reln, forknum, blocknum, false,
764 [ + + ]: 9493 : InRecovery ? EXTENSION_RETURN_NULL : EXTENSION_FAIL);
765 [ - + ]: 9493 : if (v == NULL)
952 tmunro@postgresql.or 766 :UBC 0 : return false;
767 : :
254 michael@paquier.xyz 768 :CBC 9493 : seekpos = (pgoff_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
769 : :
770 [ - + ]: 9493 : Assert(seekpos < (pgoff_t) BLCKSZ * RELSEG_SIZE);
771 : :
952 tmunro@postgresql.or 772 : 9493 : nblocks_this_segment =
773 : 9493 : Min(nblocks,
774 : : RELSEG_SIZE - (blocknum % ((BlockNumber) RELSEG_SIZE)));
775 : :
776 : 9493 : (void) FilePrefetch(v->mdfd_vfd, seekpos, BLCKSZ * nblocks_this_segment,
777 : : WAIT_EVENT_DATA_FILE_PREFETCH);
778 : :
779 : 9493 : blocknum += nblocks_this_segment;
780 : 9493 : nblocks -= nblocks_this_segment;
781 : : }
782 : : #endif /* USE_PREFETCH */
783 : :
2299 784 : 9493 : return true;
785 : : }
786 : :
787 : : /*
788 : : * Convert an array of buffer address into an array of iovec objects, and
789 : : * return the number that were required. 'iov' must have enough space for up
790 : : * to 'nblocks' elements, but the number used may be less depending on
791 : : * merging. In the case of a run of fully contiguous buffers, a single iovec
792 : : * will be populated that can be handled as a plain non-vectored I/O.
793 : : */
794 : : static int
950 795 : 2187349 : buffers_to_iovec(struct iovec *iov, void **buffers, int nblocks)
796 : : {
797 : : struct iovec *iovp;
798 : : int iovcnt;
799 : :
800 [ - + ]: 2187349 : Assert(nblocks >= 1);
801 : :
802 : : /* If this build supports direct I/O, buffers must be I/O aligned. */
803 [ + + ]: 4552895 : for (int i = 0; i < nblocks; ++i)
804 : : {
805 : : if (PG_O_DIRECT != 0 && PG_IO_ALIGN_SIZE <= BLCKSZ)
806 [ - + ]: 2365546 : Assert((uintptr_t) buffers[i] ==
807 : : TYPEALIGN(PG_IO_ALIGN_SIZE, buffers[i]));
808 : : }
809 : :
810 : : /* Start the first iovec off with the first buffer. */
811 : 2187349 : iovp = &iov[0];
812 : 2187349 : iovp->iov_base = buffers[0];
813 : 2187349 : iovp->iov_len = BLCKSZ;
814 : 2187349 : iovcnt = 1;
815 : :
816 : : /* Try to merge the rest. */
817 [ + + ]: 2365546 : for (int i = 1; i < nblocks; ++i)
818 : : {
819 : 178197 : void *buffer = buffers[i];
820 : :
821 [ + + ]: 178197 : if (((char *) iovp->iov_base + iovp->iov_len) == buffer)
822 : : {
823 : : /* Contiguous with the last iovec. */
824 : 177330 : iovp->iov_len += BLCKSZ;
825 : : }
826 : : else
827 : : {
828 : : /* Need a new iovec. */
829 : 867 : iovp++;
830 : 867 : iovp->iov_base = buffer;
831 : 867 : iovp->iov_len = BLCKSZ;
832 : 867 : iovcnt++;
833 : : }
834 : : }
835 : :
836 : 2187349 : return iovcnt;
837 : : }
838 : :
839 : : /*
840 : : * mdmaxcombine() -- Return the maximum number of total blocks that can be
841 : : * combined with an IO starting at blocknum.
842 : : */
843 : : uint32
655 andres@anarazel.de 844 : 36687 : mdmaxcombine(SMgrRelation reln, ForkNumber forknum,
845 : : BlockNumber blocknum)
846 : : {
847 : : BlockNumber segoff;
848 : :
849 : 36687 : segoff = blocknum % ((BlockNumber) RELSEG_SIZE);
850 : :
851 : 36687 : return RELSEG_SIZE - segoff;
852 : : }
853 : :
854 : : /*
855 : : * mdreadv() -- Read the specified blocks from a relation.
856 : : */
857 : : void
950 tmunro@postgresql.or 858 : 798 : mdreadv(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
859 : : void **buffers, BlockNumber nblocks)
860 : : {
861 [ + + ]: 1596 : while (nblocks > 0)
862 : : {
863 : : struct iovec iov[PG_IOV_MAX];
864 : : int iovcnt;
865 : : pgoff_t seekpos;
866 : : ssize_t nbytes;
867 : : MdfdVec *v;
868 : : BlockNumber nblocks_this_segment;
869 : : size_t transferred_this_segment;
870 : : size_t size_this_segment;
871 : :
872 : 798 : v = _mdfd_getseg(reln, forknum, blocknum, false,
873 : : EXTENSION_FAIL | EXTENSION_CREATE_RECOVERY);
874 : :
254 michael@paquier.xyz 875 : 798 : seekpos = (pgoff_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
876 : :
877 [ - + ]: 798 : Assert(seekpos < (pgoff_t) BLCKSZ * RELSEG_SIZE);
878 : :
950 tmunro@postgresql.or 879 : 798 : nblocks_this_segment =
880 : 798 : Min(nblocks,
881 : : RELSEG_SIZE - (blocknum % ((BlockNumber) RELSEG_SIZE)));
882 : 798 : nblocks_this_segment = Min(nblocks_this_segment, lengthof(iov));
883 : :
655 andres@anarazel.de 884 [ - + ]: 798 : if (nblocks_this_segment != nblocks)
655 andres@anarazel.de 885 [ # # ]:UBC 0 : elog(ERROR, "read crosses segment boundary");
886 : :
950 tmunro@postgresql.or 887 :CBC 798 : iovcnt = buffers_to_iovec(iov, buffers, nblocks_this_segment);
888 : 798 : size_this_segment = nblocks_this_segment * BLCKSZ;
889 : 798 : transferred_this_segment = 0;
890 : :
891 : : /*
892 : : * Inner loop to continue after a short read. We'll keep going until
893 : : * we hit EOF rather than assuming that a short read means we hit the
894 : : * end.
895 : : */
896 : : for (;;)
897 : : {
950 tmunro@postgresql.or 898 :UBC 0 : TRACE_POSTGRESQL_SMGR_MD_READ_START(forknum, blocknum,
899 : : reln->smgr_rlocator.locator.spcOid,
900 : : reln->smgr_rlocator.locator.dbOid,
901 : : reln->smgr_rlocator.locator.relNumber,
902 : : reln->smgr_rlocator.backend);
950 tmunro@postgresql.or 903 :CBC 798 : nbytes = FileReadV(v->mdfd_vfd, iov, iovcnt, seekpos,
904 : : WAIT_EVENT_DATA_FILE_READ);
905 : : TRACE_POSTGRESQL_SMGR_MD_READ_DONE(forknum, blocknum,
906 : : reln->smgr_rlocator.locator.spcOid,
907 : : reln->smgr_rlocator.locator.dbOid,
908 : : reln->smgr_rlocator.locator.relNumber,
909 : : reln->smgr_rlocator.backend,
910 : : nbytes,
911 : : size_this_segment - transferred_this_segment);
912 : :
913 : : #ifdef SIMULATE_SHORT_READ
914 : : nbytes = Min(nbytes, 4096);
915 : : #endif
916 : :
917 [ - + ]: 798 : if (nbytes < 0)
950 tmunro@postgresql.or 918 [ # # ]:UBC 0 : ereport(ERROR,
919 : : (errcode_for_file_access(),
920 : : errmsg("could not read blocks %u..%u in file \"%s\": %m",
921 : : blocknum,
922 : : blocknum + nblocks_this_segment - 1,
923 : : FilePathName(v->mdfd_vfd))));
924 : :
950 tmunro@postgresql.or 925 [ - + ]:CBC 798 : if (nbytes == 0)
926 : : {
927 : : /*
928 : : * We are at or past EOF, or we read a partial block at EOF.
929 : : * Normally this is an error; upper levels should never try to
930 : : * read a nonexistent block. However, if zero_damaged_pages
931 : : * is ON or we are InRecovery, we should instead return zeroes
932 : : * without complaining. This allows, for example, the case of
933 : : * trying to update a block that was later truncated away.
934 : : *
935 : : * NB: We think that this codepath is unreachable in recovery
936 : : * and incomplete with zero_damaged_pages, as missing segments
937 : : * are not created. Putting blocks into the buffer-pool that
938 : : * do not exist on disk is rather problematic, as it will not
939 : : * be found by scans that rely on smgrnblocks(), as they are
940 : : * beyond EOF. It also can cause weird problems with relation
941 : : * extension, as relation extension does not expect blocks
942 : : * beyond EOF to exist.
943 : : *
944 : : * Therefore we do not want to copy the logic into
945 : : * mdstartreadv(), where it would have to be more complicated
946 : : * due to potential differences in the zero_damaged_pages
947 : : * setting between the definer and completor of IO.
948 : : *
949 : : * For PG 18, we are putting an Assert(false) in mdreadv()
950 : : * (triggering failures in assertion-enabled builds, but
951 : : * continuing to work in production builds). Afterwards we
952 : : * plan to remove this code entirely.
953 : : */
950 tmunro@postgresql.or 954 [ # # # # ]:UBC 0 : if (zero_damaged_pages || InRecovery)
955 : : {
480 andres@anarazel.de 956 : 0 : Assert(false); /* see comment above */
957 : :
958 : : for (BlockNumber i = transferred_this_segment / BLCKSZ;
959 : : i < nblocks_this_segment;
960 : : ++i)
961 : : memset(buffers[i], 0, BLCKSZ);
962 : : break;
963 : : }
964 : : else
950 tmunro@postgresql.or 965 [ # # ]: 0 : ereport(ERROR,
966 : : (errcode(ERRCODE_DATA_CORRUPTED),
967 : : errmsg("could not read blocks %u..%u in file \"%s\": read only %zu of %zu bytes",
968 : : blocknum,
969 : : blocknum + nblocks_this_segment - 1,
970 : : FilePathName(v->mdfd_vfd),
971 : : transferred_this_segment,
972 : : size_this_segment)));
973 : : }
974 : :
975 : : /* One loop should usually be enough. */
950 tmunro@postgresql.or 976 :CBC 798 : transferred_this_segment += nbytes;
977 [ - + ]: 798 : Assert(transferred_this_segment <= size_this_segment);
978 [ + - ]: 798 : if (transferred_this_segment == size_this_segment)
979 : 798 : break;
980 : :
981 : : /* Adjust position and vectors after a short read. */
950 tmunro@postgresql.or 982 :UBC 0 : seekpos += nbytes;
983 : 0 : iovcnt = compute_remaining_iovec(iov, iov, iovcnt, nbytes);
984 : : }
985 : :
950 tmunro@postgresql.or 986 :CBC 798 : nblocks -= nblocks_this_segment;
987 : 798 : buffers += nblocks_this_segment;
988 : 798 : blocknum += nblocks_this_segment;
989 : : }
10973 scrappy@hub.org 990 : 798 : }
991 : :
992 : : /*
993 : : * mdstartreadv() -- Asynchronous version of mdreadv().
994 : : */
995 : : void
483 andres@anarazel.de 996 : 1449996 : mdstartreadv(PgAioHandle *ioh,
997 : : SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
998 : : void **buffers, BlockNumber nblocks)
999 : : {
1000 : : pgoff_t seekpos;
1001 : : MdfdVec *v;
1002 : : BlockNumber nblocks_this_segment;
1003 : : struct iovec *iov;
1004 : : int iovcnt;
1005 : : int ret;
1006 : :
1007 : 1449996 : v = _mdfd_getseg(reln, forknum, blocknum, false,
1008 : : EXTENSION_FAIL | EXTENSION_CREATE_RECOVERY);
1009 : :
254 michael@paquier.xyz 1010 : 1449981 : seekpos = (pgoff_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
1011 : :
1012 [ - + ]: 1449981 : Assert(seekpos < (pgoff_t) BLCKSZ * RELSEG_SIZE);
1013 : :
483 andres@anarazel.de 1014 : 1449981 : nblocks_this_segment =
1015 : 1449981 : Min(nblocks,
1016 : : RELSEG_SIZE - (blocknum % ((BlockNumber) RELSEG_SIZE)));
1017 : :
1018 [ - + ]: 1449981 : if (nblocks_this_segment != nblocks)
483 andres@anarazel.de 1019 [ # # ]:UBC 0 : elog(ERROR, "read crossing segment boundary");
1020 : :
483 andres@anarazel.de 1021 :CBC 1449981 : iovcnt = pgaio_io_get_iovec(ioh, &iov);
1022 : :
1023 [ - + ]: 1449981 : Assert(nblocks <= iovcnt);
1024 : :
1025 : 1449981 : iovcnt = buffers_to_iovec(iov, buffers, nblocks_this_segment);
1026 : :
1027 [ - + ]: 1449981 : Assert(iovcnt <= nblocks_this_segment);
1028 : :
1029 [ + + ]: 1449981 : if (!(io_direct_flags & IO_DIRECT_DATA))
1030 : 1448487 : pgaio_io_set_flag(ioh, PGAIO_HF_BUFFERED);
1031 : :
1032 : 1449981 : pgaio_io_set_target_smgr(ioh,
1033 : : reln,
1034 : : forknum,
1035 : : blocknum,
1036 : : nblocks,
1037 : : false);
1038 : 1449981 : pgaio_io_register_callbacks(ioh, PGAIO_HCB_MD_READV, 0);
1039 : :
1040 : 1449981 : ret = FileStartReadV(ioh, v->mdfd_vfd, iovcnt, seekpos, WAIT_EVENT_DATA_FILE_READ);
1041 [ - + ]: 1449981 : if (ret != 0)
483 andres@anarazel.de 1042 [ # # ]:UBC 0 : ereport(ERROR,
1043 : : (errcode_for_file_access(),
1044 : : errmsg("could not start reading blocks %u..%u in file \"%s\": %m",
1045 : : blocknum,
1046 : : blocknum + nblocks_this_segment - 1,
1047 : : FilePathName(v->mdfd_vfd))));
1048 : :
1049 : : /*
1050 : : * The error checks corresponding to the post-read checks in mdreadv() are
1051 : : * in md_readv_complete().
1052 : : *
1053 : : * However we chose, at least for now, to not implement the
1054 : : * zero_damaged_pages logic present in mdreadv(). As outlined in mdreadv()
1055 : : * that logic is rather problematic, and we want to get rid of it. Here
1056 : : * equivalent logic would have to be more complicated due to potential
1057 : : * differences in the zero_damaged_pages setting between the definer and
1058 : : * completor of IO.
1059 : : */
483 andres@anarazel.de 1060 :CBC 1449981 : }
1061 : :
1062 : : /*
1063 : : * mdwritev() -- Write the supplied blocks at the appropriate location.
1064 : : *
1065 : : * This is to be used only for updating already-existing blocks of a
1066 : : * relation (ie, those before the current EOF). To extend a relation,
1067 : : * use mdextend().
1068 : : */
1069 : : void
950 tmunro@postgresql.or 1070 : 736570 : mdwritev(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
1071 : : const void **buffers, BlockNumber nblocks, bool skipFsync)
1072 : : {
1073 : : /* This assert is too expensive to have on normally ... */
1074 : : #ifdef CHECK_WRITE_VS_EXTEND
1075 : : Assert((uint64) blocknum + (uint64) nblocks <= (uint64) mdnblocks(reln, forknum));
1076 : : #endif
1077 : :
1078 [ + + ]: 1473140 : while (nblocks > 0)
1079 : : {
1080 : : struct iovec iov[PG_IOV_MAX];
1081 : : int iovcnt;
1082 : : pgoff_t seekpos;
1083 : : ssize_t nbytes;
1084 : : MdfdVec *v;
1085 : : BlockNumber nblocks_this_segment;
1086 : : size_t transferred_this_segment;
1087 : : size_t size_this_segment;
1088 : :
1089 : 736570 : v = _mdfd_getseg(reln, forknum, blocknum, skipFsync,
1090 : : EXTENSION_FAIL | EXTENSION_CREATE_RECOVERY);
1091 : :
254 michael@paquier.xyz 1092 : 736570 : seekpos = (pgoff_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
1093 : :
1094 [ - + ]: 736570 : Assert(seekpos < (pgoff_t) BLCKSZ * RELSEG_SIZE);
1095 : :
950 tmunro@postgresql.or 1096 : 736570 : nblocks_this_segment =
1097 : 736570 : Min(nblocks,
1098 : : RELSEG_SIZE - (blocknum % ((BlockNumber) RELSEG_SIZE)));
1099 : 736570 : nblocks_this_segment = Min(nblocks_this_segment, lengthof(iov));
1100 : :
655 andres@anarazel.de 1101 [ - + ]: 736570 : if (nblocks_this_segment != nblocks)
655 andres@anarazel.de 1102 [ # # ]:UBC 0 : elog(ERROR, "write crosses segment boundary");
1103 : :
950 tmunro@postgresql.or 1104 :CBC 736570 : iovcnt = buffers_to_iovec(iov, (void **) buffers, nblocks_this_segment);
1105 : 736570 : size_this_segment = nblocks_this_segment * BLCKSZ;
1106 : 736570 : transferred_this_segment = 0;
1107 : :
1108 : : /*
1109 : : * Inner loop to continue after a short write. If the reason is that
1110 : : * we're out of disk space, a future attempt should get an ENOSPC
1111 : : * error from the kernel.
1112 : : */
1113 : : for (;;)
1114 : : {
950 tmunro@postgresql.or 1115 :UBC 0 : TRACE_POSTGRESQL_SMGR_MD_WRITE_START(forknum, blocknum,
1116 : : reln->smgr_rlocator.locator.spcOid,
1117 : : reln->smgr_rlocator.locator.dbOid,
1118 : : reln->smgr_rlocator.locator.relNumber,
1119 : : reln->smgr_rlocator.backend);
950 tmunro@postgresql.or 1120 :CBC 736570 : nbytes = FileWriteV(v->mdfd_vfd, iov, iovcnt, seekpos,
1121 : : WAIT_EVENT_DATA_FILE_WRITE);
1122 : : TRACE_POSTGRESQL_SMGR_MD_WRITE_DONE(forknum, blocknum,
1123 : : reln->smgr_rlocator.locator.spcOid,
1124 : : reln->smgr_rlocator.locator.dbOid,
1125 : : reln->smgr_rlocator.locator.relNumber,
1126 : : reln->smgr_rlocator.backend,
1127 : : nbytes,
1128 : : size_this_segment - transferred_this_segment);
1129 : :
1130 : : #ifdef SIMULATE_SHORT_WRITE
1131 : : nbytes = Min(nbytes, 4096);
1132 : : #endif
1133 : :
1134 [ - + ]: 736570 : if (nbytes < 0)
1135 : : {
950 tmunro@postgresql.or 1136 :UBC 0 : bool enospc = errno == ENOSPC;
1137 : :
1138 [ # # # # ]: 0 : ereport(ERROR,
1139 : : (errcode_for_file_access(),
1140 : : errmsg("could not write blocks %u..%u in file \"%s\": %m",
1141 : : blocknum,
1142 : : blocknum + nblocks_this_segment - 1,
1143 : : FilePathName(v->mdfd_vfd)),
1144 : : enospc ? errhint("Check free disk space.") : 0));
1145 : : }
1146 : :
1147 : : /* One loop should usually be enough. */
950 tmunro@postgresql.or 1148 :CBC 736570 : transferred_this_segment += nbytes;
1149 [ - + ]: 736570 : Assert(transferred_this_segment <= size_this_segment);
1150 [ + - ]: 736570 : if (transferred_this_segment == size_this_segment)
1151 : 736570 : break;
1152 : :
1153 : : /* Adjust position and iovecs after a short write. */
950 tmunro@postgresql.or 1154 :UBC 0 : seekpos += nbytes;
1155 : 0 : iovcnt = compute_remaining_iovec(iov, iov, iovcnt, nbytes);
1156 : : }
1157 : :
950 tmunro@postgresql.or 1158 [ + + + + ]:CBC 736570 : if (!skipFsync && !SmgrIsTemp(reln))
1159 : 731724 : register_dirty_segment(reln, forknum, v);
1160 : :
1161 : 736570 : nblocks -= nblocks_this_segment;
1162 : 736570 : buffers += nblocks_this_segment;
1163 : 736570 : blocknum += nblocks_this_segment;
1164 : : }
9603 tgl@sss.pgh.pa.us 1165 : 736570 : }
1166 : :
1167 : :
1168 : : /*
1169 : : * mdwriteback() -- Tell the kernel to write pages back to storage.
1170 : : *
1171 : : * This accepts a range of blocks because flushing several pages at once is
1172 : : * considerably more efficient than doing so individually.
1173 : : */
1174 : : void
1163 peter@eisentraut.org 1175 :UBC 0 : mdwriteback(SMgrRelation reln, ForkNumber forknum,
1176 : : BlockNumber blocknum, BlockNumber nblocks)
1177 : : {
1178 [ # # ]: 0 : Assert((io_direct_flags & IO_DIRECT_DATA) == 0);
1179 : :
1180 : : /*
1181 : : * Issue flush requests in as few requests as possible; have to split at
1182 : : * segment boundaries though, since those are actually separate files.
1183 : : */
1184 [ # # ]: 0 : while (nblocks > 0)
1185 : : {
1186 : 0 : BlockNumber nflush = nblocks;
1187 : : pgoff_t seekpos;
1188 : : MdfdVec *v;
1189 : : int segnum_start,
1190 : : segnum_end;
1191 : :
1192 : 0 : v = _mdfd_getseg(reln, forknum, blocknum, true /* not used */ ,
1193 : : EXTENSION_DONT_OPEN);
1194 : :
1195 : : /*
1196 : : * We might be flushing buffers of already removed relations, that's
1197 : : * ok, just ignore that case. If the segment file wasn't open already
1198 : : * (ie from a recent mdwrite()), then we don't want to re-open it, to
1199 : : * avoid a race with PROCSIGNAL_BARRIER_SMGRRELEASE that might leave
1200 : : * us with a descriptor to a file that is about to be unlinked.
1201 : : */
1202 [ # # ]: 0 : if (!v)
1203 : 0 : return;
1204 : :
1205 : : /* compute offset inside the current segment */
1206 : 0 : segnum_start = blocknum / RELSEG_SIZE;
1207 : :
1208 : : /* compute number of desired writes within the current segment */
1209 : 0 : segnum_end = (blocknum + nblocks - 1) / RELSEG_SIZE;
1210 [ # # ]: 0 : if (segnum_start != segnum_end)
1211 : 0 : nflush = RELSEG_SIZE - (blocknum % ((BlockNumber) RELSEG_SIZE));
1212 : :
1213 [ # # ]: 0 : Assert(nflush >= 1);
1214 [ # # ]: 0 : Assert(nflush <= nblocks);
1215 : :
254 michael@paquier.xyz 1216 : 0 : seekpos = (pgoff_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
1217 : :
1218 : 0 : FileWriteback(v->mdfd_vfd, seekpos, (pgoff_t) BLCKSZ * nflush, WAIT_EVENT_DATA_FILE_FLUSH);
1219 : :
1163 peter@eisentraut.org 1220 : 0 : nblocks -= nflush;
1221 : 0 : blocknum += nflush;
1222 : : }
1223 : : }
1224 : :
1225 : : /*
1226 : : * mdnblocks() -- Get the number of blocks stored in a relation.
1227 : : *
1228 : : * Important side effect: all active segments of the relation are opened
1229 : : * and added to the md_seg_fds array. If this routine has not been
1230 : : * called, then only segments up to the last one actually touched
1231 : : * are present in the array.
1232 : : */
1233 : : BlockNumber
6557 heikki.linnakangas@i 1234 :CBC 2773048 : mdnblocks(SMgrRelation reln, ForkNumber forknum)
1235 : : {
1236 : : MdfdVec *v;
1237 : : BlockNumber nblocks;
1238 : : BlockNumber segno;
1239 : :
2151 bruce@momjian.us 1240 : 2773048 : mdopenfork(reln, forknum, EXTENSION_FAIL);
1241 : :
1242 : : /* mdopen has opened the first segment */
3607 andres@anarazel.de 1243 [ - + ]: 2773029 : Assert(reln->md_num_open_segs[forknum] > 0);
1244 : :
1245 : : /*
1246 : : * Start from the last open segments, to avoid redundant seeks. We have
1247 : : * previously verified that these segments are exactly RELSEG_SIZE long,
1248 : : * and it's useless to recheck that each time.
1249 : : *
1250 : : * NOTE: this assumption could only be wrong if another backend has
1251 : : * truncated the relation. We rely on higher code levels to handle that
1252 : : * scenario by closing and re-opening the md fd, which is handled via
1253 : : * relcache flush. (Since the checkpointer doesn't participate in
1254 : : * relcache flush, it could have segment entries for inactive segments;
1255 : : * that's OK because the checkpointer never needs to compute relation
1256 : : * size.)
1257 : : */
1258 : 2773029 : segno = reln->md_num_open_segs[forknum] - 1;
1259 : 2773029 : v = &reln->md_seg_fds[forknum][segno];
1260 : :
1261 : : for (;;)
1262 : : {
6557 heikki.linnakangas@i 1263 : 2773029 : nblocks = _mdnblocks(reln, forknum, v);
9159 tgl@sss.pgh.pa.us 1264 [ - + ]: 2773029 : if (nblocks > ((BlockNumber) RELSEG_SIZE))
8402 tgl@sss.pgh.pa.us 1265 [ # # ]:UBC 0 : elog(FATAL, "segment too big");
9159 tgl@sss.pgh.pa.us 1266 [ + - ]:CBC 2773029 : if (nblocks < ((BlockNumber) RELSEG_SIZE))
1267 : 2773029 : return (segno * ((BlockNumber) RELSEG_SIZE)) + nblocks;
1268 : :
1269 : : /*
1270 : : * If segment is exactly RELSEG_SIZE, advance to next one.
1271 : : */
9207 tgl@sss.pgh.pa.us 1272 :UBC 0 : segno++;
1273 : :
1274 : : /*
1275 : : * We used to pass O_CREAT here, but that has the disadvantage that it
1276 : : * might create a segment which has vanished through some operating
1277 : : * system misadventure. In such a case, creating the segment here
1278 : : * undermines _mdfd_getseg's attempts to notice and report an error
1279 : : * upon access to a missing segment.
1280 : : */
3607 andres@anarazel.de 1281 : 0 : v = _mdfd_openseg(reln, forknum, segno, 0);
1282 [ # # ]: 0 : if (v == NULL)
1283 : 0 : return segno * ((BlockNumber) RELSEG_SIZE);
1284 : : }
1285 : : }
1286 : :
1287 : : /*
1288 : : * mdtruncate() -- Truncate relation to specified number of blocks.
1289 : : *
1290 : : * Guaranteed not to allocate memory, so it can be used in a critical section.
1291 : : * Caller must have called smgrnblocks() to obtain curnblk while holding a
1292 : : * sufficient lock to prevent a change in relation size, and not used any smgr
1293 : : * functions for this relation or handled interrupts in between. This makes
1294 : : * sure we have opened all active segments, so that truncate loop will get
1295 : : * them all!
1296 : : *
1297 : : * If nblocks > curnblk, the request is ignored when we are InRecovery,
1298 : : * otherwise, an error is raised.
1299 : : */
1300 : : void
582 tmunro@postgresql.or 1301 :CBC 1214 : mdtruncate(SMgrRelation reln, ForkNumber forknum,
1302 : : BlockNumber curnblk, BlockNumber nblocks)
1303 : : {
1304 : : BlockNumber priorblocks;
1305 : : int curopensegs;
1306 : :
9159 tgl@sss.pgh.pa.us 1307 [ - + ]: 1214 : if (nblocks > curnblk)
1308 : : {
1309 : : /* Bogus request ... but no complaint if InRecovery */
7143 tgl@sss.pgh.pa.us 1310 [ # # ]:UBC 0 : if (InRecovery)
1311 : 0 : return;
1312 [ # # ]: 0 : ereport(ERROR,
1313 : : (errmsg("could not truncate file \"%s\" to %u blocks: it's only %u blocks now",
1314 : : relpath(reln->smgr_rlocator, forknum).str,
1315 : : nblocks, curnblk)));
1316 : : }
9823 tgl@sss.pgh.pa.us 1317 [ + + ]:CBC 1214 : if (nblocks == curnblk)
7143 1318 : 492 : return; /* no work */
1319 : :
1320 : : /*
1321 : : * Truncate segments, starting at the last one. Starting at the end makes
1322 : : * managing the memory for the fd array easier, should there be errors.
1323 : : */
3607 andres@anarazel.de 1324 : 722 : curopensegs = reln->md_num_open_segs[forknum];
1325 [ + + ]: 1444 : while (curopensegs > 0)
1326 : : {
1327 : : MdfdVec *v;
1328 : :
1329 : 722 : priorblocks = (curopensegs - 1) * RELSEG_SIZE;
1330 : :
1331 : 722 : v = &reln->md_seg_fds[forknum][curopensegs - 1];
1332 : :
9823 tgl@sss.pgh.pa.us 1333 [ - + ]: 722 : if (priorblocks > nblocks)
1334 : : {
1335 : : /*
1336 : : * This segment is no longer active. We truncate the file, but do
1337 : : * not delete it, for reasons explained in the header comments.
1338 : : */
3416 rhaas@postgresql.org 1339 [ # # ]:UBC 0 : if (FileTruncate(v->mdfd_vfd, 0, WAIT_EVENT_DATA_FILE_TRUNCATE) < 0)
7143 tgl@sss.pgh.pa.us 1340 [ # # ]: 0 : ereport(ERROR,
1341 : : (errcode_for_file_access(),
1342 : : errmsg("could not truncate file \"%s\": %m",
1343 : : FilePathName(v->mdfd_vfd))));
1344 : :
5825 rhaas@postgresql.org 1345 [ # # ]: 0 : if (!SmgrIsTemp(reln))
6557 heikki.linnakangas@i 1346 : 0 : register_dirty_segment(reln, forknum, v);
1347 : :
1348 : : /* we never drop the 1st segment */
3607 andres@anarazel.de 1349 [ # # ]: 0 : Assert(v != &reln->md_seg_fds[forknum][0]);
1350 : :
1351 : 0 : FileClose(v->mdfd_vfd);
1352 : 0 : _fdvec_resize(reln, forknum, curopensegs - 1);
1353 : : }
9159 tgl@sss.pgh.pa.us 1354 [ + - ]:CBC 722 : else if (priorblocks + ((BlockNumber) RELSEG_SIZE) > nblocks)
1355 : : {
1356 : : /*
1357 : : * This is the last segment we want to keep. Truncate the file to
1358 : : * the right length. NOTE: if nblocks is exactly a multiple K of
1359 : : * RELSEG_SIZE, we will truncate the K+1st segment to 0 length but
1360 : : * keep it. This adheres to the invariant given in the header
1361 : : * comments.
1362 : : */
9039 bruce@momjian.us 1363 : 722 : BlockNumber lastsegblocks = nblocks - priorblocks;
1364 : :
254 michael@paquier.xyz 1365 [ - + ]: 722 : if (FileTruncate(v->mdfd_vfd, (pgoff_t) lastsegblocks * BLCKSZ, WAIT_EVENT_DATA_FILE_TRUNCATE) < 0)
7143 tgl@sss.pgh.pa.us 1366 [ # # ]:UBC 0 : ereport(ERROR,
1367 : : (errcode_for_file_access(),
1368 : : errmsg("could not truncate file \"%s\" to %u blocks: %m",
1369 : : FilePathName(v->mdfd_vfd),
1370 : : nblocks)));
5825 rhaas@postgresql.org 1371 [ + + ]:CBC 722 : if (!SmgrIsTemp(reln))
6557 heikki.linnakangas@i 1372 : 504 : register_dirty_segment(reln, forknum, v);
1373 : : }
1374 : : else
1375 : : {
1376 : : /*
1377 : : * We still need this segment, so nothing to do for this and any
1378 : : * earlier segment.
1379 : : */
3607 andres@anarazel.de 1380 :UBC 0 : break;
1381 : : }
3607 andres@anarazel.de 1382 :CBC 722 : curopensegs--;
1383 : : }
1384 : : }
1385 : :
1386 : : /*
1387 : : * mdregistersync() -- Mark whole relation as needing fsync
1388 : : */
1389 : : void
883 heikki.linnakangas@i 1390 : 32325 : mdregistersync(SMgrRelation reln, ForkNumber forknum)
1391 : : {
1392 : : int segno;
1393 : : int min_inactive_seg;
1394 : :
1395 : : /*
1396 : : * NOTE: mdnblocks makes sure we have opened all active segments, so that
1397 : : * the loop below will get them all!
1398 : : */
1399 : 32325 : mdnblocks(reln, forknum);
1400 : :
1401 : 32325 : min_inactive_seg = segno = reln->md_num_open_segs[forknum];
1402 : :
1403 : : /*
1404 : : * Temporarily open inactive segments, then close them after sync. There
1405 : : * may be some inactive segments left opened after error, but that is
1406 : : * harmless. We don't bother to clean them up and take a risk of further
1407 : : * trouble. The next mdclose() will soon close them.
1408 : : */
1409 [ - + ]: 32325 : while (_mdfd_openseg(reln, forknum, segno, 0) != NULL)
883 heikki.linnakangas@i 1410 :UBC 0 : segno++;
1411 : :
883 heikki.linnakangas@i 1412 [ + + ]:CBC 64650 : while (segno > 0)
1413 : : {
1414 : 32325 : MdfdVec *v = &reln->md_seg_fds[forknum][segno - 1];
1415 : :
1416 : 32325 : register_dirty_segment(reln, forknum, v);
1417 : :
1418 : : /* Close inactive segments immediately */
1419 [ - + ]: 32325 : if (segno > min_inactive_seg)
1420 : : {
883 heikki.linnakangas@i 1421 :UBC 0 : FileClose(v->mdfd_vfd);
1422 : 0 : _fdvec_resize(reln, forknum, segno - 1);
1423 : : }
1424 : :
883 heikki.linnakangas@i 1425 :CBC 32325 : segno--;
1426 : : }
1427 : 32325 : }
1428 : :
1429 : : /*
1430 : : * mdimmedsync() -- Immediately sync a relation to stable storage.
1431 : : *
1432 : : * Note that only writes already issued are synced; this routine knows
1433 : : * nothing of dirty buffers that may exist inside the buffer manager. We
1434 : : * sync active and inactive segments; smgrDoPendingSyncs() relies on this.
1435 : : * Consider a relation skipping WAL. Suppose a checkpoint syncs blocks of
1436 : : * some segment, then mdtruncate() renders that segment inactive. If we
1437 : : * crash before the next checkpoint syncs the newly-inactive segment, that
1438 : : * segment may survive recovery, reintroducing unwanted data into the table.
1439 : : */
1440 : : void
6557 1441 : 8 : mdimmedsync(SMgrRelation reln, ForkNumber forknum)
1442 : : {
1443 : : int segno;
1444 : : int min_inactive_seg;
1445 : :
1446 : : /*
1447 : : * NOTE: mdnblocks makes sure we have opened all active segments, so that
1448 : : * the loop below will get them all!
1449 : : */
5584 peter_e@gmx.net 1450 : 8 : mdnblocks(reln, forknum);
1451 : :
2303 noah@leadboat.com 1452 : 8 : min_inactive_seg = segno = reln->md_num_open_segs[forknum];
1453 : :
1454 : : /*
1455 : : * Temporarily open inactive segments, then close them after sync. There
1456 : : * may be some inactive segments left opened after fsync() error, but that
1457 : : * is harmless. We don't bother to clean them up and take a risk of
1458 : : * further trouble. The next mdclose() will soon close them.
1459 : : */
1460 [ - + ]: 8 : while (_mdfd_openseg(reln, forknum, segno, 0) != NULL)
2303 noah@leadboat.com 1461 :UBC 0 : segno++;
1462 : :
3607 andres@anarazel.de 1463 [ + + ]:CBC 16 : while (segno > 0)
1464 : : {
1465 : 8 : MdfdVec *v = &reln->md_seg_fds[forknum][segno - 1];
1466 : :
1467 : : /*
1468 : : * fsyncs done through mdimmedsync() should be tracked in a separate
1469 : : * IOContext than those done through mdsyncfiletag() to differentiate
1470 : : * between unavoidable client backend fsyncs (e.g. those done during
1471 : : * index build) and those which ideally would have been done by the
1472 : : * checkpointer. Since other IO operations bypassing the buffer
1473 : : * manager could also be tracked in such an IOContext, wait until
1474 : : * these are also tracked to track immediate fsyncs.
1475 : : */
3416 rhaas@postgresql.org 1476 [ - + ]: 8 : if (FileSync(v->mdfd_vfd, WAIT_EVENT_DATA_FILE_IMMEDIATE_SYNC) < 0)
2805 tmunro@postgresql.or 1477 [ # # ]:UBC 0 : ereport(data_sync_elevel(ERROR),
1478 : : (errcode_for_file_access(),
1479 : : errmsg("could not fsync file \"%s\": %m",
1480 : : FilePathName(v->mdfd_vfd))));
1481 : :
1482 : : /* Close inactive segments immediately */
2303 noah@leadboat.com 1483 [ - + ]:CBC 8 : if (segno > min_inactive_seg)
1484 : : {
2303 noah@leadboat.com 1485 :UBC 0 : FileClose(v->mdfd_vfd);
1486 : 0 : _fdvec_resize(reln, forknum, segno - 1);
1487 : : }
1488 : :
3607 andres@anarazel.de 1489 :CBC 8 : segno--;
1490 : : }
8088 tgl@sss.pgh.pa.us 1491 : 8 : }
1492 : :
1493 : : int
483 andres@anarazel.de 1494 : 494116 : mdfd(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, uint32 *off)
1495 : : {
1496 : 494116 : MdfdVec *v = mdopenfork(reln, forknum, EXTENSION_FAIL);
1497 : :
1498 : 494116 : v = _mdfd_getseg(reln, forknum, blocknum, false,
1499 : : EXTENSION_FAIL);
1500 : :
254 michael@paquier.xyz 1501 : 494116 : *off = (pgoff_t) BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE));
1502 : :
1503 [ - + ]: 494116 : Assert(*off < (pgoff_t) BLCKSZ * RELSEG_SIZE);
1504 : :
483 andres@anarazel.de 1505 : 494116 : return FileGetRawDesc(v->mdfd_vfd);
1506 : : }
1507 : :
1508 : : /*
1509 : : * register_dirty_segment() -- Mark a relation segment as needing fsync
1510 : : *
1511 : : * If there is a local pending-ops table, just make an entry in it for
1512 : : * ProcessSyncRequests to process later. Otherwise, try to pass off the
1513 : : * fsync request to the checkpointer process. If that fails, just do the
1514 : : * fsync locally before returning (we hope this will not happen often
1515 : : * enough to be a performance problem).
1516 : : */
1517 : : static void
6557 heikki.linnakangas@i 1518 : 1215932 : register_dirty_segment(SMgrRelation reln, ForkNumber forknum, MdfdVec *seg)
1519 : : {
1520 : : FileTag tag;
1521 : :
1480 rhaas@postgresql.org 1522 : 1215932 : INIT_MD_FILETAG(tag, reln->smgr_rlocator.locator, forknum, seg->mdfd_segno);
1523 : :
1524 : : /* Temp relations should never be fsync'd */
5121 tgl@sss.pgh.pa.us 1525 [ - + ]: 1215932 : Assert(!SmgrIsTemp(reln));
1526 : :
2669 tmunro@postgresql.or 1527 [ + + ]: 1215932 : if (!RegisterSyncRequest(&tag, SYNC_REQUEST, false /* retryOnError */ ))
1528 : : {
1529 : : instr_time io_start;
1530 : :
1205 andres@anarazel.de 1531 [ - + ]: 225 : ereport(DEBUG1,
1532 : : (errmsg_internal("could not forward fsync request because request queue is full")));
1533 : :
514 michael@paquier.xyz 1534 : 225 : io_start = pgstat_prepare_io_time(track_io_timing);
1535 : :
1205 andres@anarazel.de 1536 [ - + ]: 225 : if (FileSync(seg->mdfd_vfd, WAIT_EVENT_DATA_FILE_SYNC) < 0)
1205 andres@anarazel.de 1537 [ # # ]:UBC 0 : ereport(data_sync_elevel(ERROR),
1538 : : (errcode_for_file_access(),
1539 : : errmsg("could not fsync file \"%s\": %m",
1540 : : FilePathName(seg->mdfd_vfd))));
1541 : :
1542 : : /*
1543 : : * We have no way of knowing if the current IOContext is
1544 : : * IOCONTEXT_NORMAL or IOCONTEXT_[BULKREAD, BULKWRITE, VACUUM] at this
1545 : : * point, so count the fsync as being in the IOCONTEXT_NORMAL
1546 : : * IOContext. This is probably okay, because the number of backend
1547 : : * fsyncs doesn't say anything about the efficacy of the
1548 : : * BufferAccessStrategy. And counting both fsyncs done in
1549 : : * IOCONTEXT_NORMAL and IOCONTEXT_[BULKREAD, BULKWRITE, VACUUM] under
1550 : : * IOCONTEXT_NORMAL is likely clearer when investigating the number of
1551 : : * backend fsyncs.
1552 : : */
1205 andres@anarazel.de 1553 :CBC 225 : pgstat_count_io_op_time(IOOBJECT_RELATION, IOCONTEXT_NORMAL,
1554 : : IOOP_FSYNC, io_start, 1, 0);
1555 : : }
10973 scrappy@hub.org 1556 : 1215932 : }
1557 : :
1558 : : /*
1559 : : * register_unlink_tombstone() -- Schedule a tombstone file to be deleted
1560 : : *
1561 : : * A tombstone file is an empty first segment of a relation that has already
1562 : : * been dropped (see mdunlink()). This function schedules it to be deleted
1563 : : * after the next checkpoint.
1564 : : */
1565 : : static void
18 heikki.linnakangas@i 1566 :GNC 48579 : register_unlink_tombstone(RelFileLocatorBackend rlocator)
1567 : : {
1568 : : FileTag tag;
1569 : :
1570 : 48579 : INIT_MD_FILETAG(tag, rlocator.locator, MAIN_FORKNUM, 0);
1571 : :
1572 : : /* Should never be used with temp relations */
1480 rhaas@postgresql.org 1573 [ - + ]:CBC 48579 : Assert(!RelFileLocatorBackendIsTemp(rlocator));
1574 : :
2669 tmunro@postgresql.or 1575 : 48579 : RegisterSyncRequest(&tag, SYNC_UNLINK_REQUEST, true /* retryOnError */ );
6827 tgl@sss.pgh.pa.us 1576 : 48579 : }
1577 : :
1578 : : /*
1579 : : * register_forget_request() -- forget any fsyncs for a relation fork's segment
1580 : : */
1581 : : static void
1480 rhaas@postgresql.org 1582 : 181277 : register_forget_request(RelFileLocatorBackend rlocator, ForkNumber forknum,
1583 : : BlockNumber segno)
1584 : : {
1585 : : FileTag tag;
1586 : :
1587 : 181277 : INIT_MD_FILETAG(tag, rlocator.locator, forknum, segno);
1588 : :
2669 tmunro@postgresql.or 1589 : 181277 : RegisterSyncRequest(&tag, SYNC_FORGET_REQUEST, true /* retryOnError */ );
7129 tgl@sss.pgh.pa.us 1590 : 181277 : }
1591 : :
1592 : : /*
1593 : : * ForgetDatabaseSyncRequests -- forget any fsyncs and unlinks for a DB
1594 : : */
1595 : : void
2669 tmunro@postgresql.or 1596 : 69 : ForgetDatabaseSyncRequests(Oid dbid)
1597 : : {
1598 : : FileTag tag;
1599 : : RelFileLocator rlocator;
1600 : :
1480 rhaas@postgresql.org 1601 : 69 : rlocator.dbOid = dbid;
1602 : 69 : rlocator.spcOid = 0;
1603 : 69 : rlocator.relNumber = 0;
1604 : :
1605 : 69 : INIT_MD_FILETAG(tag, rlocator, InvalidForkNumber, InvalidBlockNumber);
1606 : :
2669 tmunro@postgresql.or 1607 : 69 : RegisterSyncRequest(&tag, SYNC_FILTER_REQUEST, true /* retryOnError */ );
9401 vadim4o@yahoo.com 1608 : 69 : }
1609 : :
1610 : : /*
1611 : : * DropRelationFiles -- drop files of all given relations
1612 : : */
1613 : : void
1480 rhaas@postgresql.org 1614 : 2992 : DropRelationFiles(RelFileLocator *delrels, int ndelrels, bool isRedo)
1615 : : {
1616 : : SMgrRelation *srels;
1617 : : int i;
1618 : :
227 michael@paquier.xyz 1619 : 2992 : srels = palloc_array(SMgrRelation, ndelrels);
2942 fujii@postgresql.org 1620 [ + + ]: 11787 : for (i = 0; i < ndelrels; i++)
1621 : : {
874 heikki.linnakangas@i 1622 : 8795 : SMgrRelation srel = smgropen(delrels[i], INVALID_PROC_NUMBER);
1623 : :
2942 fujii@postgresql.org 1624 [ + + ]: 8795 : if (isRedo)
1625 : : {
1626 : : ForkNumber fork;
1627 : :
1628 [ + + ]: 43785 : for (fork = 0; fork <= MAX_FORKNUM; fork++)
1629 : 35028 : XLogDropRelation(delrels[i], fork);
1630 : : }
1631 : 8795 : srels[i] = srel;
1632 : : }
1633 : :
1634 : 2992 : smgrdounlinkall(srels, ndelrels, isRedo);
1635 : :
2677 tomas.vondra@postgre 1636 [ + + ]: 11787 : for (i = 0; i < ndelrels; i++)
2942 fujii@postgresql.org 1637 : 8795 : smgrclose(srels[i]);
1638 : 2992 : pfree(srels);
1639 : 2992 : }
1640 : :
1641 : :
1642 : : /*
1643 : : * _fdvec_resize() -- Resize the fork's open segments array
1644 : : */
1645 : : static void
3607 andres@anarazel.de 1646 : 1732620 : _fdvec_resize(SMgrRelation reln,
1647 : : ForkNumber forknum,
1648 : : int nseg)
1649 : : {
1650 [ + + ]: 1732620 : if (nseg == 0)
1651 : : {
1652 [ + - ]: 647101 : if (reln->md_num_open_segs[forknum] > 0)
1653 : : {
1654 : 647101 : pfree(reln->md_seg_fds[forknum]);
1655 : 647101 : reln->md_seg_fds[forknum] = NULL;
1656 : : }
1657 : : }
1658 [ + - ]: 1085519 : else if (reln->md_num_open_segs[forknum] == 0)
1659 : : {
1660 : 1085519 : reln->md_seg_fds[forknum] =
1661 : 1085519 : MemoryContextAlloc(MdCxt, sizeof(MdfdVec) * nseg);
1662 : : }
582 tmunro@postgresql.or 1663 [ # # ]:UBC 0 : else if (nseg > reln->md_num_open_segs[forknum])
1664 : : {
1665 : : /*
1666 : : * It doesn't seem worthwhile complicating the code to amortize
1667 : : * repalloc() calls. Those are far faster than PathNameOpenFile() or
1668 : : * FileClose(), and the memory context internally will sometimes avoid
1669 : : * doing an actual reallocation.
1670 : : */
3607 andres@anarazel.de 1671 : 0 : reln->md_seg_fds[forknum] =
1672 : 0 : repalloc(reln->md_seg_fds[forknum],
1673 : : sizeof(MdfdVec) * nseg);
1674 : : }
1675 : : else
1676 : : {
1677 : : /*
1678 : : * We don't reallocate a smaller array, because we want mdtruncate()
1679 : : * to be able to promise that it won't allocate memory, so that it is
1680 : : * allowed in a critical section. This means that a bit of space in
1681 : : * the array is now wasted, until the next time we add a segment and
1682 : : * reallocate.
1683 : : */
1684 : : }
1685 : :
3607 andres@anarazel.de 1686 :CBC 1732620 : reln->md_num_open_segs[forknum] = nseg;
10656 vadim4o@yahoo.com 1687 : 1732620 : }
1688 : :
1689 : : /*
1690 : : * Return the filename for the specified segment of the relation. The
1691 : : * returned string is palloc'd.
1692 : : */
1693 : : static MdPathStr
6198 heikki.linnakangas@i 1694 : 32345 : _mdfd_segpath(SMgrRelation reln, ForkNumber forknum, BlockNumber segno)
1695 : : {
1696 : : RelPathStr path;
1697 : : MdPathStr fullpath;
1698 : :
1480 rhaas@postgresql.org 1699 : 32345 : path = relpath(reln->smgr_rlocator, forknum);
1700 : :
10548 bruce@momjian.us 1701 [ + - ]: 32345 : if (segno > 0)
515 andres@anarazel.de 1702 : 32345 : sprintf(fullpath.str, "%s.%u", path.str, segno);
1703 : : else
515 andres@anarazel.de 1704 :UBC 0 : strcpy(fullpath.str, path.str);
1705 : :
6198 heikki.linnakangas@i 1706 :CBC 32345 : return fullpath;
1707 : : }
1708 : :
1709 : : /*
1710 : : * Open the specified segment of the relation,
1711 : : * and make a MdfdVec object for it. Returns NULL on failure.
1712 : : */
1713 : : static MdfdVec *
1714 : 32333 : _mdfd_openseg(SMgrRelation reln, ForkNumber forknum, BlockNumber segno,
1715 : : int oflags)
1716 : : {
1717 : : MdfdVec *v;
1718 : : File fd;
1719 : : MdPathStr fullpath;
1720 : :
1721 : 32333 : fullpath = _mdfd_segpath(reln, forknum, segno);
1722 : :
1723 : : /* open the file */
515 andres@anarazel.de 1724 : 32333 : fd = PathNameOpenFile(fullpath.str, _mdfd_open_flags() | oflags);
1725 : :
10548 bruce@momjian.us 1726 [ + - ]: 32333 : if (fd < 0)
8235 neilc@samurai.com 1727 : 32333 : return NULL;
1728 : :
1729 : : /*
1730 : : * Segments are always opened in order from lowest to highest, so we must
1731 : : * be adding a new one at the end.
1732 : : */
2371 tmunro@postgresql.or 1733 [ # # ]:UBC 0 : Assert(segno == reln->md_num_open_segs[forknum]);
1734 : :
1735 : 0 : _fdvec_resize(reln, forknum, segno + 1);
1736 : :
1737 : : /* fill the entry */
3607 andres@anarazel.de 1738 : 0 : v = &reln->md_seg_fds[forknum][segno];
10548 bruce@momjian.us 1739 : 0 : v->mdfd_vfd = fd;
8090 tgl@sss.pgh.pa.us 1740 : 0 : v->mdfd_segno = segno;
1741 : :
6557 heikki.linnakangas@i 1742 [ # # ]: 0 : Assert(_mdnblocks(reln, forknum, v) <= ((BlockNumber) RELSEG_SIZE));
1743 : :
1744 : : /* all done */
10189 bruce@momjian.us 1745 : 0 : return v;
1746 : : }
1747 : :
1748 : : /*
1749 : : * _mdfd_getseg() -- Find the segment of the relation holding the
1750 : : * specified block.
1751 : : *
1752 : : * If the segment doesn't exist, we ereport, return NULL, or create the
1753 : : * segment, according to "behavior". Note: skipFsync is only used in the
1754 : : * EXTENSION_CREATE case.
1755 : : */
1756 : : static MdfdVec *
6557 heikki.linnakangas@i 1757 :CBC 3097793 : _mdfd_getseg(SMgrRelation reln, ForkNumber forknum, BlockNumber blkno,
1758 : : bool skipFsync, int behavior)
1759 : : {
1760 : : MdfdVec *v;
1761 : : BlockNumber targetseg;
1762 : : BlockNumber nextsegno;
1763 : :
1764 : : /* some way to handle non-existent segments needs to be specified */
3734 andres@anarazel.de 1765 [ - + ]: 3097793 : Assert(behavior &
1766 : : (EXTENSION_FAIL | EXTENSION_CREATE | EXTENSION_RETURN_NULL |
1767 : : EXTENSION_DONT_OPEN));
1768 : :
7143 tgl@sss.pgh.pa.us 1769 : 3097793 : targetseg = blkno / ((BlockNumber) RELSEG_SIZE);
1770 : :
1771 : : /* if an existing and opened segment, we're done */
3607 andres@anarazel.de 1772 [ + + ]: 3097793 : if (targetseg < reln->md_num_open_segs[forknum])
1773 : : {
1774 : 2829186 : v = &reln->md_seg_fds[forknum][targetseg];
1775 : 2829186 : return v;
1776 : : }
1777 : :
1778 : : /* The caller only wants the segment if we already had it open. */
1540 tmunro@postgresql.or 1779 [ - + ]: 268607 : if (behavior & EXTENSION_DONT_OPEN)
1540 tmunro@postgresql.or 1780 :UBC 0 : return NULL;
1781 : :
1782 : : /*
1783 : : * The target segment is not yet open. Iterate over all the segments
1784 : : * between the last opened and the target segment. This way missing
1785 : : * segments either raise an error, or get created (according to
1786 : : * 'behavior'). Start with either the last opened, or the first segment if
1787 : : * none was opened before.
1788 : : */
3607 andres@anarazel.de 1789 [ + + ]:CBC 268607 : if (reln->md_num_open_segs[forknum] > 0)
1790 : 12 : v = &reln->md_seg_fds[forknum][reln->md_num_open_segs[forknum] - 1];
1791 : : else
1792 : : {
2565 tmunro@postgresql.or 1793 : 268595 : v = mdopenfork(reln, forknum, behavior);
3607 andres@anarazel.de 1794 [ - + ]: 268592 : if (!v)
3607 andres@anarazel.de 1795 :UBC 0 : return NULL; /* if behavior & EXTENSION_RETURN_NULL */
1796 : : }
1797 : :
3607 andres@anarazel.de 1798 :CBC 268604 : for (nextsegno = reln->md_num_open_segs[forknum];
1799 [ + + ]: 268604 : nextsegno <= targetseg; nextsegno++)
1800 : : {
1801 : 12 : BlockNumber nblocks = _mdnblocks(reln, forknum, v);
1802 : 12 : int flags = 0;
1803 : :
1804 [ - + ]: 12 : Assert(nextsegno == v->mdfd_segno + 1);
1805 : :
1806 [ - + ]: 12 : if (nblocks > ((BlockNumber) RELSEG_SIZE))
3607 andres@anarazel.de 1807 [ # # ]:UBC 0 : elog(FATAL, "segment too big");
1808 : :
3607 andres@anarazel.de 1809 [ + - ]:CBC 12 : if ((behavior & EXTENSION_CREATE) ||
1810 [ - + - - ]: 12 : (InRecovery && (behavior & EXTENSION_CREATE_RECOVERY)))
1811 : : {
1812 : : /*
1813 : : * Normally we will create new segments only if authorized by the
1814 : : * caller (i.e., we are doing mdextend()). But when doing WAL
1815 : : * recovery, create segments anyway; this allows cases such as
1816 : : * replaying WAL data that has a write into a high-numbered
1817 : : * segment of a relation that was later deleted. We want to go
1818 : : * ahead and create the segments so we can finish out the replay.
1819 : : *
1820 : : * We have to maintain the invariant that segments before the last
1821 : : * active segment are of size RELSEG_SIZE; therefore, if
1822 : : * extending, pad them out with zeroes if needed. (This only
1823 : : * matters if in recovery, or if the caller is extending the
1824 : : * relation discontiguously, but that can happen in hash indexes.)
1825 : : */
3607 andres@anarazel.de 1826 [ # # ]:UBC 0 : if (nblocks < ((BlockNumber) RELSEG_SIZE))
1827 : : {
1204 tmunro@postgresql.or 1828 : 0 : char *zerobuf = palloc_aligned(BLCKSZ, PG_IO_ALIGN_SIZE,
1829 : : MCXT_ALLOC_ZERO);
1830 : :
3607 andres@anarazel.de 1831 : 0 : mdextend(reln, forknum,
1832 : 0 : nextsegno * ((BlockNumber) RELSEG_SIZE) - 1,
1833 : : zerobuf, skipFsync);
1834 : 0 : pfree(zerobuf);
1835 : : }
1836 : 0 : flags = O_CREAT;
1837 : : }
588 tmunro@postgresql.or 1838 [ + - ]:CBC 12 : else if (nblocks < ((BlockNumber) RELSEG_SIZE))
1839 : : {
1840 : : /*
1841 : : * When not extending, only open the next segment if the current
1842 : : * one is exactly RELSEG_SIZE. If not (this branch), either
1843 : : * return NULL or fail.
1844 : : */
3607 andres@anarazel.de 1845 [ - + ]: 12 : if (behavior & EXTENSION_RETURN_NULL)
1846 : : {
1847 : : /*
1848 : : * Some callers discern between reasons for _mdfd_getseg()
1849 : : * returning NULL based on errno. As there's no failing
1850 : : * syscall involved in this case, explicitly set errno to
1851 : : * ENOENT, as that seems the closest interpretation.
1852 : : */
3607 andres@anarazel.de 1853 :UBC 0 : errno = ENOENT;
1854 : 0 : return NULL;
1855 : : }
1856 : :
3607 andres@anarazel.de 1857 [ + - ]:CBC 12 : ereport(ERROR,
1858 : : (errcode_for_file_access(),
1859 : : errmsg("could not open file \"%s\" (target block %u): previous segment is only %u blocks",
1860 : : _mdfd_segpath(reln, forknum, nextsegno).str,
1861 : : blkno, nblocks)));
1862 : : }
1863 : :
3607 andres@anarazel.de 1864 :UBC 0 : v = _mdfd_openseg(reln, forknum, nextsegno, flags);
1865 : :
1866 [ # # ]: 0 : if (v == NULL)
1867 : : {
1868 [ # # ]: 0 : if ((behavior & EXTENSION_RETURN_NULL) &&
1869 [ # # ]: 0 : FILE_POSSIBLY_DELETED(errno))
1870 : 0 : return NULL;
1871 [ # # ]: 0 : ereport(ERROR,
1872 : : (errcode_for_file_access(),
1873 : : errmsg("could not open file \"%s\" (target block %u): %m",
1874 : : _mdfd_segpath(reln, forknum, nextsegno).str,
1875 : : blkno)));
1876 : : }
1877 : : }
1878 : :
10189 bruce@momjian.us 1879 :CBC 268592 : return v;
1880 : : }
1881 : :
1882 : : /*
1883 : : * Get number of blocks present in a single disk file
1884 : : */
1885 : : static BlockNumber
6557 heikki.linnakangas@i 1886 : 4057983 : _mdnblocks(SMgrRelation reln, ForkNumber forknum, MdfdVec *seg)
1887 : : {
1888 : : pgoff_t len;
1889 : :
2817 tmunro@postgresql.or 1890 : 4057983 : len = FileSize(seg->mdfd_vfd);
9600 bruce@momjian.us 1891 [ - + ]: 4057983 : if (len < 0)
7143 tgl@sss.pgh.pa.us 1892 [ # # ]:UBC 0 : ereport(ERROR,
1893 : : (errcode_for_file_access(),
1894 : : errmsg("could not seek to end of file \"%s\": %m",
1895 : : FilePathName(seg->mdfd_vfd))));
1896 : : /* note that this calculation will ignore any partial block at EOF */
7143 tgl@sss.pgh.pa.us 1897 :CBC 4057983 : return (BlockNumber) (len / BLCKSZ);
1898 : : }
1899 : :
1900 : : /*
1901 : : * Sync a file to disk, given a file tag. Write the path into an output
1902 : : * buffer so the caller can use it in error messages.
1903 : : *
1904 : : * Return 0 on success, -1 on failure, with errno set.
1905 : : */
1906 : : int
2669 tmunro@postgresql.or 1907 :UBC 0 : mdsyncfiletag(const FileTag *ftag, char *path)
1908 : : {
874 heikki.linnakangas@i 1909 : 0 : SMgrRelation reln = smgropen(ftag->rlocator, INVALID_PROC_NUMBER);
1910 : : File file;
1911 : : instr_time io_start;
1912 : : bool need_to_close;
1913 : : int result,
1914 : : save_errno;
1915 : :
1916 : : /* See if we already have the file open, or need to open it. */
2415 tmunro@postgresql.or 1917 [ # # ]: 0 : if (ftag->segno < reln->md_num_open_segs[ftag->forknum])
1918 : : {
1919 : 0 : file = reln->md_seg_fds[ftag->forknum][ftag->segno].mdfd_vfd;
1920 : 0 : strlcpy(path, FilePathName(file), MAXPGPATH);
1921 : 0 : need_to_close = false;
1922 : : }
1923 : : else
1924 : : {
1925 : : MdPathStr p;
1926 : :
1927 : 0 : p = _mdfd_segpath(reln, ftag->forknum, ftag->segno);
515 andres@anarazel.de 1928 : 0 : strlcpy(path, p.str, MD_PATH_STR_MAXLEN);
1929 : :
1204 tmunro@postgresql.or 1930 : 0 : file = PathNameOpenFile(path, _mdfd_open_flags());
2415 1931 [ # # ]: 0 : if (file < 0)
1932 : 0 : return -1;
1933 : 0 : need_to_close = true;
1934 : : }
1935 : :
514 michael@paquier.xyz 1936 : 0 : io_start = pgstat_prepare_io_time(track_io_timing);
1937 : :
1938 : : /* Sync the file. */
2415 tmunro@postgresql.or 1939 : 0 : result = FileSync(file, WAIT_EVENT_DATA_FILE_SYNC);
1940 : 0 : save_errno = errno;
1941 : :
1942 [ # # ]: 0 : if (need_to_close)
1943 : 0 : FileClose(file);
1944 : :
1205 andres@anarazel.de 1945 : 0 : pgstat_count_io_op_time(IOOBJECT_RELATION, IOCONTEXT_NORMAL,
1946 : : IOOP_FSYNC, io_start, 1, 0);
1947 : :
2415 tmunro@postgresql.or 1948 : 0 : errno = save_errno;
1949 : 0 : return result;
1950 : : }
1951 : :
1952 : : /*
1953 : : * Unlink a file, given a file tag. Write the path into an output
1954 : : * buffer so the caller can use it in error messages.
1955 : : *
1956 : : * Return 0 on success, -1 on failure, with errno set.
1957 : : */
1958 : : int
2669 tmunro@postgresql.or 1959 :CBC 37825 : mdunlinkfiletag(const FileTag *ftag, char *path)
1960 : : {
1961 : : RelPathStr p;
1962 : :
1963 : : /* We only unlink tombstone files through this mechanism */
18 heikki.linnakangas@i 1964 [ + - - + ]:GNC 37825 : Assert(ftag->forknum == MAIN_FORKNUM && ftag->segno == 0);
1965 : :
1966 : : /* Compute the path. */
1480 rhaas@postgresql.org 1967 :CBC 37825 : p = relpathperm(ftag->rlocator, MAIN_FORKNUM);
515 andres@anarazel.de 1968 : 37825 : strlcpy(path, p.str, MAXPGPATH);
1969 : :
1970 : : /* Try to unlink the file. */
2669 tmunro@postgresql.or 1971 : 37825 : return unlink(path);
1972 : : }
1973 : :
1974 : : /*
1975 : : * Check if a given candidate request matches a given tag, when processing
1976 : : * a SYNC_FILTER_REQUEST request. This will be called for all pending
1977 : : * requests to find out whether to forget them.
1978 : : */
1979 : : bool
1980 : 8385 : mdfiletagmatches(const FileTag *ftag, const FileTag *candidate)
1981 : : {
1982 : : /*
1983 : : * For now we only use filter requests as a way to drop all scheduled
1984 : : * callbacks relating to a given database, when dropping the database.
1985 : : * We'll return true for all candidates that have the same database OID as
1986 : : * the ftag from the SYNC_FILTER_REQUEST request, so they're forgotten.
1987 : : */
1480 rhaas@postgresql.org 1988 : 8385 : return ftag->rlocator.dbOid == candidate->rlocator.dbOid;
1989 : : }
1990 : :
1991 : : /*
1992 : : * AIO completion callback for mdstartreadv().
1993 : : */
1994 : : static PgAioResult
483 andres@anarazel.de 1995 : 1312444 : md_readv_complete(PgAioHandle *ioh, PgAioResult prior_result, uint8 cb_data)
1996 : : {
1997 : 1312444 : PgAioTargetData *td = pgaio_io_get_target_data(ioh);
1998 : 1312444 : PgAioResult result = prior_result;
1999 : :
2000 [ + + ]: 1312444 : if (prior_result.result < 0)
2001 : : {
2002 : 9 : result.status = PGAIO_RS_ERROR;
2003 : 9 : result.id = PGAIO_HCB_MD_READV;
2004 : : /* For "hard" errors, track the error number in error_data */
2005 : 9 : result.error_data = -prior_result.result;
2006 : 9 : result.result = 0;
2007 : :
2008 : : /*
2009 : : * Immediately log a message about the IO error, but only to the
2010 : : * server log. The reason to do so immediately is that the originator
2011 : : * might not process the query result immediately (because it is busy
2012 : : * doing another part of query processing) or at all (e.g. if it was
2013 : : * cancelled or errored out due to another IO also failing). The
2014 : : * definer of the IO will emit an ERROR when processing the IO's
2015 : : * results
2016 : : */
2017 : 9 : pgaio_result_report(result, td, LOG_SERVER_ONLY);
2018 : :
2019 : 9 : return result;
2020 : : }
2021 : :
2022 : : /*
2023 : : * As explained above smgrstartreadv(), the smgr API operates on the level
2024 : : * of blocks, rather than bytes. Convert.
2025 : : */
2026 : 1312435 : result.result /= BLCKSZ;
2027 : :
2028 [ - + ]: 1312435 : Assert(result.result <= td->smgr.nblocks);
2029 : :
2030 [ + + ]: 1312435 : if (result.result == 0)
2031 : : {
2032 : : /* consider 0 blocks read a failure */
2033 : 2 : result.status = PGAIO_RS_ERROR;
2034 : 2 : result.id = PGAIO_HCB_MD_READV;
2035 : 2 : result.error_data = 0;
2036 : :
2037 : : /* see comment above the "hard error" case */
2038 : 2 : pgaio_result_report(result, td, LOG_SERVER_ONLY);
2039 : :
2040 : 2 : return result;
2041 : : }
2042 : :
2043 [ + - ]: 1312433 : if (result.status != PGAIO_RS_ERROR &&
2044 [ + + ]: 1312433 : result.result < td->smgr.nblocks)
2045 : : {
2046 : : /* partial reads should be retried at upper level */
2047 : 111 : result.status = PGAIO_RS_PARTIAL;
2048 : 111 : result.id = PGAIO_HCB_MD_READV;
2049 : : }
2050 : :
2051 : 1312433 : return result;
2052 : : }
2053 : :
2054 : : /*
2055 : : * AIO error reporting callback for mdstartreadv().
2056 : : *
2057 : : * Errors are encoded as follows:
2058 : : * - PgAioResult.error_data != 0 encodes IO that failed with that errno
2059 : : * - PgAioResult.error_data == 0 encodes IO that didn't read all data
2060 : : */
2061 : : static void
2062 : 131 : md_readv_report(PgAioResult result, const PgAioTargetData *td, int elevel)
2063 : : {
2064 : : RelPathStr path;
2065 : :
2066 [ - + ]: 131 : path = relpathbackend(td->smgr.rlocator,
2067 : : td->smgr.is_temp ? MyProcNumber : INVALID_PROC_NUMBER,
2068 : : td->smgr.forkNum);
2069 : :
2070 [ + + ]: 131 : if (result.error_data != 0)
2071 : : {
2072 : : /* for errcode_for_file_access() and %m */
2073 : 18 : errno = result.error_data;
2074 : :
2075 [ + - ]: 18 : ereport(elevel,
2076 : : errcode_for_file_access(),
2077 : : errmsg("could not read blocks %u..%u in file \"%s\": %m",
2078 : : td->smgr.blockNum,
2079 : : td->smgr.blockNum + td->smgr.nblocks - 1,
2080 : : path.str));
2081 : : }
2082 : : else
2083 : : {
2084 : : /*
2085 : : * NB: This will typically only be output in debug messages, while
2086 : : * retrying a partial IO.
2087 : : */
2088 [ + - ]: 113 : ereport(elevel,
2089 : : errcode(ERRCODE_DATA_CORRUPTED),
2090 : : errmsg("could not read blocks %u..%u in file \"%s\": read only %zu of %zu bytes",
2091 : : td->smgr.blockNum,
2092 : : td->smgr.blockNum + td->smgr.nblocks - 1,
2093 : : path.str,
2094 : : result.result * (size_t) BLCKSZ,
2095 : : td->smgr.nblocks * (size_t) BLCKSZ));
2096 : : }
2097 : 120 : }
|