Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * buffile.c
4 : : * Management of large buffered temporary files.
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : * IDENTIFICATION
10 : : * src/backend/storage/file/buffile.c
11 : : *
12 : : * NOTES:
13 : : *
14 : : * BufFiles provide a very incomplete emulation of stdio atop virtual Files
15 : : * (as managed by fd.c). Currently, we only support the buffered-I/O
16 : : * aspect of stdio: a read or write of the low-level File occurs only
17 : : * when the buffer is filled or emptied. This is an even bigger win
18 : : * for virtual Files than for ordinary kernel files, since reducing the
19 : : * frequency with which a virtual File is touched reduces "thrashing"
20 : : * of opening/closing file descriptors.
21 : : *
22 : : * Note that BufFile structs are allocated with palloc(), and therefore
23 : : * will go away automatically at query/transaction end. Since the underlying
24 : : * virtual Files are made with OpenTemporaryFile, all resources for
25 : : * the file are certain to be cleaned up even if processing is aborted
26 : : * by ereport(ERROR). The data structures required are made in the
27 : : * palloc context that was current when the BufFile was created, and
28 : : * any external resources such as temp files are owned by the ResourceOwner
29 : : * that was current at that time.
30 : : *
31 : : * BufFile also supports temporary files that exceed the OS file size limit
32 : : * (by opening multiple fd.c temporary files). This is an essential feature
33 : : * for sorts and hashjoins on large amounts of data.
34 : : *
35 : : * BufFile supports temporary files that can be shared with other backends, as
36 : : * infrastructure for parallel execution. Such files need to be created as a
37 : : * member of a SharedFileSet that all participants are attached to.
38 : : *
39 : : * BufFile also supports temporary files that can be used by the single backend
40 : : * when the corresponding files need to be survived across the transaction and
41 : : * need to be opened and closed multiple times. Such files need to be created
42 : : * as a member of a FileSet.
43 : : *-------------------------------------------------------------------------
44 : : */
45 : :
46 : : #include "postgres.h"
47 : :
48 : : #include "commands/tablespace.h"
49 : : #include "executor/instrument.h"
50 : : #include "miscadmin.h"
51 : : #include "pgstat.h"
52 : : #include "storage/buffile.h"
53 : : #include "storage/bufmgr.h"
54 : : #include "storage/fd.h"
55 : : #include "utils/resowner.h"
56 : : #include "utils/wait_event.h"
57 : :
58 : : /*
59 : : * We break BufFiles into gigabyte-sized segments, regardless of RELSEG_SIZE.
60 : : * The reason is that we'd like large BufFiles to be spread across multiple
61 : : * tablespaces when available.
62 : : */
63 : : #define MAX_PHYSICAL_FILESIZE 0x40000000
64 : : #define BUFFILE_SEG_SIZE (MAX_PHYSICAL_FILESIZE / BLCKSZ)
65 : :
66 : : /*
67 : : * This data structure represents a buffered file that consists of one or
68 : : * more physical files (each accessed through a virtual file descriptor
69 : : * managed by fd.c).
70 : : */
71 : : struct BufFile
72 : : {
73 : : int numFiles; /* number of physical files in set */
74 : : /* all files except the last have length exactly MAX_PHYSICAL_FILESIZE */
75 : : File *files; /* palloc'd array with numFiles entries */
76 : :
77 : : bool isInterXact; /* keep open over transactions? */
78 : : bool dirty; /* does buffer need to be written? */
79 : : bool readOnly; /* has the file been set to read only? */
80 : :
81 : : FileSet *fileset; /* space for fileset based segment files */
82 : : const char *name; /* name of fileset based BufFile */
83 : :
84 : : /*
85 : : * resowner is the ResourceOwner to use for underlying temp files. (We
86 : : * don't need to remember the memory context we're using explicitly,
87 : : * because after creation we only repalloc our arrays larger.)
88 : : */
89 : : ResourceOwner resowner;
90 : :
91 : : /*
92 : : * "current pos" is position of start of buffer within the logical file.
93 : : * Position as seen by user of BufFile is (curFile, curOffset + pos).
94 : : */
95 : : int curFile; /* file index (0..n) part of current pos */
96 : : pgoff_t curOffset; /* offset part of current pos */
97 : : int64 pos; /* next read/write position in buffer */
98 : : int64 nbytes; /* total # of valid bytes in buffer */
99 : :
100 : : /*
101 : : * XXX Should ideally use PGIOAlignedBlock, but might need a way to avoid
102 : : * wasting per-file alignment padding when some users create many files.
103 : : */
104 : : PGAlignedBlock buffer;
105 : : };
106 : :
107 : : static BufFile *makeBufFileCommon(int nfiles);
108 : : static BufFile *makeBufFile(File firstfile);
109 : : static void extendBufFile(BufFile *file);
110 : : static void BufFileLoadBuffer(BufFile *file);
111 : : static void BufFileDumpBuffer(BufFile *file);
112 : : static void BufFileFlush(BufFile *file);
113 : : static File MakeNewFileSetSegment(BufFile *buffile, int segment);
114 : :
115 : : /*
116 : : * Create BufFile and perform the common initialization.
117 : : */
118 : : static BufFile *
2829 ishii@postgresql.org 119 :CBC 5196 : makeBufFileCommon(int nfiles)
120 : : {
95 michael@paquier.xyz 121 :GNC 5196 : BufFile *file = palloc_object(BufFile);
122 : :
2829 ishii@postgresql.org 123 :CBC 5196 : file->numFiles = nfiles;
6860 tgl@sss.pgh.pa.us 124 : 5196 : file->isInterXact = false;
9647 125 : 5196 : file->dirty = false;
4517 126 : 5196 : file->resowner = CurrentResourceOwner;
9647 127 : 5196 : file->curFile = 0;
1082 peter@eisentraut.org 128 : 5196 : file->curOffset = 0;
9647 tgl@sss.pgh.pa.us 129 : 5196 : file->pos = 0;
130 : 5196 : file->nbytes = 0;
131 : :
2829 ishii@postgresql.org 132 : 5196 : return file;
133 : : }
134 : :
135 : : /*
136 : : * Create a BufFile given the first underlying physical file.
137 : : * NOTE: caller must set isInterXact if appropriate.
138 : : */
139 : : static BufFile *
140 : 1591 : makeBufFile(File firstfile)
141 : : {
142 : 1591 : BufFile *file = makeBufFileCommon(1);
143 : :
95 michael@paquier.xyz 144 :GNC 1591 : file->files = palloc_object(File);
2829 ishii@postgresql.org 145 :CBC 1591 : file->files[0] = firstfile;
3026 andres@anarazel.de 146 : 1591 : file->readOnly = false;
147 : 1591 : file->fileset = NULL;
148 : 1591 : file->name = NULL;
149 : :
9650 tgl@sss.pgh.pa.us 150 : 1591 : return file;
151 : : }
152 : :
153 : : /*
154 : : * Add another component temp file.
155 : : */
156 : : static void
9647 tgl@sss.pgh.pa.us 157 :UBC 0 : extendBufFile(BufFile *file)
158 : : {
159 : : File pfile;
160 : : ResourceOwner oldowner;
161 : :
162 : : /* Be sure to associate the file with the BufFile's resource owner */
4517 163 : 0 : oldowner = CurrentResourceOwner;
164 : 0 : CurrentResourceOwner = file->resowner;
165 : :
3026 andres@anarazel.de 166 [ # # ]: 0 : if (file->fileset == NULL)
167 : 0 : pfile = OpenTemporaryFile(file->isInterXact);
168 : : else
1658 akapila@postgresql.o 169 : 0 : pfile = MakeNewFileSetSegment(file, file->numFiles);
170 : :
9650 tgl@sss.pgh.pa.us 171 [ # # ]: 0 : Assert(pfile >= 0);
172 : :
4517 173 : 0 : CurrentResourceOwner = oldowner;
174 : :
9650 175 : 0 : file->files = (File *) repalloc(file->files,
9468 bruce@momjian.us 176 : 0 : (file->numFiles + 1) * sizeof(File));
9650 tgl@sss.pgh.pa.us 177 : 0 : file->files[file->numFiles] = pfile;
178 : 0 : file->numFiles++;
179 : 0 : }
180 : :
181 : : /*
182 : : * Create a BufFile for a new temporary file (which will expand to become
183 : : * multiple temporary files if more than MAX_PHYSICAL_FILESIZE bytes are
184 : : * written to it).
185 : : *
186 : : * If interXact is true, the temp file will not be automatically deleted
187 : : * at end of transaction.
188 : : *
189 : : * Note: if interXact is true, the caller had better be calling us in a
190 : : * memory context, and with a resource owner, that will survive across
191 : : * transaction boundaries.
192 : : */
193 : : BufFile *
6856 tgl@sss.pgh.pa.us 194 :CBC 1591 : BufFileCreateTemp(bool interXact)
195 : : {
196 : : BufFile *file;
197 : : File pfile;
198 : :
199 : : /*
200 : : * Ensure that temp tablespaces are set up for OpenTemporaryFile to use.
201 : : * Possibly the caller will have done this already, but it seems useful to
202 : : * double-check here. Failure to do this at all would result in the temp
203 : : * files always getting placed in the default tablespace, which is a
204 : : * pretty hard-to-detect bug. Callers may prefer to do it earlier if they
205 : : * want to be sure that any required catalog access is done in some other
206 : : * resource context.
207 : : */
2493 208 : 1591 : PrepareTempTablespaces();
209 : :
6856 210 : 1591 : pfile = OpenTemporaryFile(interXact);
9650 211 [ - + ]: 1591 : Assert(pfile >= 0);
212 : :
9647 213 : 1591 : file = makeBufFile(pfile);
8356 214 : 1591 : file->isInterXact = interXact;
215 : :
9647 216 : 1591 : return file;
217 : : }
218 : :
219 : : /*
220 : : * Build the name for a given segment of a given BufFile.
221 : : */
222 : : static void
1658 akapila@postgresql.o 223 : 7874 : FileSetSegmentName(char *name, const char *buffile_name, int segment)
224 : : {
3026 andres@anarazel.de 225 : 7874 : snprintf(name, MAXPGPATH, "%s.%d", buffile_name, segment);
226 : 7874 : }
227 : :
228 : : /*
229 : : * Create a new segment file backing a fileset based BufFile.
230 : : */
231 : : static File
1658 akapila@postgresql.o 232 : 1581 : MakeNewFileSetSegment(BufFile *buffile, int segment)
233 : : {
234 : : char name[MAXPGPATH];
235 : : File file;
236 : :
237 : : /*
238 : : * It is possible that there are files left over from before a crash
239 : : * restart with the same name. In order for BufFileOpenFileSet() not to
240 : : * get confused about how many segments there are, we'll unlink the next
241 : : * segment number if it already exists.
242 : : */
243 : 1581 : FileSetSegmentName(name, buffile->name, segment + 1);
244 : 1581 : FileSetDelete(buffile->fileset, name, true);
245 : :
246 : : /* Create the new segment. */
247 : 1581 : FileSetSegmentName(name, buffile->name, segment);
248 : 1581 : file = FileSetCreate(buffile->fileset, name);
249 : :
250 : : /* FileSetCreate would've errored out */
3026 andres@anarazel.de 251 [ - + ]: 1581 : Assert(file > 0);
252 : :
253 : 1581 : return file;
254 : : }
255 : :
256 : : /*
257 : : * Create a BufFile that can be discovered and opened read-only by other
258 : : * backends that are attached to the same SharedFileSet using the same name.
259 : : *
260 : : * The naming scheme for fileset based BufFiles is left up to the calling code.
261 : : * The name will appear as part of one or more filenames on disk, and might
262 : : * provide clues to administrators about which subsystem is generating
263 : : * temporary file data. Since each SharedFileSet object is backed by one or
264 : : * more uniquely named temporary directory, names don't conflict with
265 : : * unrelated SharedFileSet objects.
266 : : */
267 : : BufFile *
1658 akapila@postgresql.o 268 : 1581 : BufFileCreateFileSet(FileSet *fileset, const char *name)
269 : : {
270 : : BufFile *file;
271 : :
2829 ishii@postgresql.org 272 : 1581 : file = makeBufFileCommon(1);
3026 andres@anarazel.de 273 : 1581 : file->fileset = fileset;
274 : 1581 : file->name = pstrdup(name);
95 michael@paquier.xyz 275 :GNC 1581 : file->files = palloc_object(File);
1658 akapila@postgresql.o 276 :CBC 1581 : file->files[0] = MakeNewFileSetSegment(file, 0);
3026 andres@anarazel.de 277 : 1581 : file->readOnly = false;
278 : :
279 : 1581 : return file;
280 : : }
281 : :
282 : : /*
283 : : * Open a file that was previously created in another backend (or this one)
284 : : * with BufFileCreateFileSet in the same FileSet using the same name.
285 : : * The backend that created the file must have called BufFileClose() or
286 : : * BufFileExportFileSet() to make sure that it is ready to be opened by other
287 : : * backends and render it read-only. If missing_ok is true, which indicates
288 : : * that missing files can be safely ignored, then return NULL if the BufFile
289 : : * with the given name is not found, otherwise, throw an error.
290 : : */
291 : : BufFile *
1655 akapila@postgresql.o 292 : 2297 : BufFileOpenFileSet(FileSet *fileset, const char *name, int mode,
293 : : bool missing_ok)
294 : : {
295 : : BufFile *file;
296 : : char segment_name[MAXPGPATH];
3026 andres@anarazel.de 297 : 2297 : Size capacity = 16;
298 : : File *files;
299 : 2297 : int nfiles = 0;
300 : :
95 michael@paquier.xyz 301 :GNC 2297 : files = palloc_array(File, capacity);
302 : :
303 : : /*
304 : : * We don't know how many segments there are, so we'll probe the
305 : : * filesystem to find out.
306 : : */
307 : : for (;;)
308 : : {
309 : : /* See if we need to expand our file segment array. */
3026 andres@anarazel.de 310 [ - + ]:CBC 4321 : if (nfiles + 1 > capacity)
311 : : {
3026 andres@anarazel.de 312 :UBC 0 : capacity *= 2;
95 michael@paquier.xyz 313 :UNC 0 : files = repalloc_array(files, File, capacity);
314 : : }
315 : : /* Try to load a segment. */
1658 akapila@postgresql.o 316 :CBC 4321 : FileSetSegmentName(segment_name, name, nfiles);
317 : 4321 : files[nfiles] = FileSetOpen(fileset, segment_name, mode);
3026 andres@anarazel.de 318 [ + + ]: 4321 : if (files[nfiles] <= 0)
319 : 2297 : break;
320 : 2024 : ++nfiles;
321 : :
322 [ + + ]: 2024 : CHECK_FOR_INTERRUPTS();
323 : : }
324 : :
325 : : /*
326 : : * If we didn't find any files at all, then no BufFile exists with this
327 : : * name.
328 : : */
329 [ + + ]: 2297 : if (nfiles == 0)
330 : : {
331 : : /* free the memory */
1655 akapila@postgresql.o 332 : 273 : pfree(files);
333 : :
334 [ + - ]: 273 : if (missing_ok)
335 : 273 : return NULL;
336 : :
3014 andres@anarazel.de 337 [ # # ]:UBC 0 : ereport(ERROR,
338 : : (errcode_for_file_access(),
339 : : errmsg("could not open temporary file \"%s\" from BufFile \"%s\": %m",
340 : : segment_name, name)));
341 : : }
342 : :
2829 ishii@postgresql.org 343 :CBC 2024 : file = makeBufFileCommon(nfiles);
3026 andres@anarazel.de 344 : 2024 : file->files = files;
1649 michael@paquier.xyz 345 : 2024 : file->readOnly = (mode == O_RDONLY);
3026 andres@anarazel.de 346 : 2024 : file->fileset = fileset;
347 : 2024 : file->name = pstrdup(name);
348 : :
349 : 2024 : return file;
350 : : }
351 : :
352 : : /*
353 : : * Delete a BufFile that was created by BufFileCreateFileSet in the given
354 : : * FileSet using the given name.
355 : : *
356 : : * It is not necessary to delete files explicitly with this function. It is
357 : : * provided only as a way to delete files proactively, rather than waiting for
358 : : * the FileSet to be cleaned up.
359 : : *
360 : : * Only one backend should attempt to delete a given name, and should know
361 : : * that it exists and has been exported or closed otherwise missing_ok should
362 : : * be passed true.
363 : : */
364 : : void
1655 akapila@postgresql.o 365 : 352 : BufFileDeleteFileSet(FileSet *fileset, const char *name, bool missing_ok)
366 : : {
367 : : char segment_name[MAXPGPATH];
3026 andres@anarazel.de 368 : 352 : int segment = 0;
369 : 352 : bool found = false;
370 : :
371 : : /*
372 : : * We don't know how many segments the file has. We'll keep deleting
373 : : * until we run out. If we don't manage to find even an initial segment,
374 : : * raise an error.
375 : : */
376 : : for (;;)
377 : : {
1658 akapila@postgresql.o 378 : 391 : FileSetSegmentName(segment_name, name, segment);
379 [ + + ]: 391 : if (!FileSetDelete(fileset, segment_name, true))
3026 andres@anarazel.de 380 : 352 : break;
381 : 39 : found = true;
382 : 39 : ++segment;
383 : :
384 [ - + ]: 39 : CHECK_FOR_INTERRUPTS();
385 : : }
386 : :
1655 akapila@postgresql.o 387 [ + + - + ]: 352 : if (!found && !missing_ok)
1658 akapila@postgresql.o 388 [ # # ]:UBC 0 : elog(ERROR, "could not delete unknown BufFile \"%s\"", name);
3026 andres@anarazel.de 389 :CBC 352 : }
390 : :
391 : : /*
392 : : * BufFileExportFileSet --- flush and make read-only, in preparation for sharing.
393 : : */
394 : : void
1658 akapila@postgresql.o 395 : 290 : BufFileExportFileSet(BufFile *file)
396 : : {
397 : : /* Must be a file belonging to a FileSet. */
3026 andres@anarazel.de 398 [ - + ]: 290 : Assert(file->fileset != NULL);
399 : :
400 : : /* It's probably a bug if someone calls this twice. */
401 [ - + ]: 290 : Assert(!file->readOnly);
402 : :
403 : 290 : BufFileFlush(file);
404 : 290 : file->readOnly = true;
405 : 290 : }
406 : :
407 : : /*
408 : : * Close a BufFile
409 : : *
410 : : * Like fclose(), this also implicitly FileCloses the underlying File.
411 : : */
412 : : void
9650 tgl@sss.pgh.pa.us 413 : 5071 : BufFileClose(BufFile *file)
414 : : {
415 : : int i;
416 : :
417 : : /* flush any unwritten data */
418 : 5071 : BufFileFlush(file);
419 : : /* close and delete the underlying file(s) */
9647 420 [ + + ]: 10259 : for (i = 0; i < file->numFiles; i++)
421 : 5188 : FileClose(file->files[i]);
422 : : /* release the buffer space */
423 : 5071 : pfree(file->files);
9650 424 : 5071 : pfree(file);
425 : 5071 : }
426 : :
427 : : /*
428 : : * BufFileLoadBuffer
429 : : *
430 : : * Load some data into buffer, if possible, starting from curOffset.
431 : : * At call, must have dirty = false, pos and nbytes = 0.
432 : : * On exit, nbytes is number of bytes loaded.
433 : : */
434 : : static void
435 : 54120 : BufFileLoadBuffer(BufFile *file)
436 : : {
437 : : File thisfile;
438 : : instr_time io_start;
439 : : instr_time io_time;
440 : :
441 : : /*
442 : : * Advance to next component file if necessary and possible.
443 : : */
444 [ - + ]: 54120 : if (file->curOffset >= MAX_PHYSICAL_FILESIZE &&
9468 bruce@momjian.us 445 [ # # ]:UBC 0 : file->curFile + 1 < file->numFiles)
446 : : {
9650 tgl@sss.pgh.pa.us 447 : 0 : file->curFile++;
1082 peter@eisentraut.org 448 : 0 : file->curOffset = 0;
449 : : }
450 : :
1437 michael@paquier.xyz 451 :CBC 54120 : thisfile = file->files[file->curFile];
452 : :
453 [ - + ]: 54120 : if (track_io_timing)
1437 michael@paquier.xyz 454 :UBC 0 : INSTR_TIME_SET_CURRENT(io_start);
455 : : else
1150 andres@anarazel.de 456 :CBC 54120 : INSTR_TIME_SET_ZERO(io_start);
457 : :
458 : : /*
459 : : * Read whatever we can get, up to a full bufferload.
460 : : */
3284 rhaas@postgresql.org 461 : 108240 : file->nbytes = FileRead(thisfile,
2752 tgl@sss.pgh.pa.us 462 : 54120 : file->buffer.data,
463 : : sizeof(file->buffer.data),
464 : : file->curOffset,
465 : : WAIT_EVENT_BUFFILE_READ);
9650 466 [ - + ]: 54120 : if (file->nbytes < 0)
467 : : {
9650 tgl@sss.pgh.pa.us 468 :UBC 0 : file->nbytes = 0;
2098 tmunro@postgresql.or 469 [ # # ]: 0 : ereport(ERROR,
470 : : (errcode_for_file_access(),
471 : : errmsg("could not read file \"%s\": %m",
472 : : FilePathName(thisfile))));
473 : : }
474 : :
1437 michael@paquier.xyz 475 [ - + ]:CBC 54120 : if (track_io_timing)
476 : : {
1437 michael@paquier.xyz 477 :UBC 0 : INSTR_TIME_SET_CURRENT(io_time);
1081 andres@anarazel.de 478 : 0 : INSTR_TIME_ACCUM_DIFF(pgBufferUsage.temp_blk_read_time, io_time, io_start);
479 : : }
480 : :
481 : : /* we choose not to advance curOffset here */
482 : :
3057 rhaas@postgresql.org 483 [ + + ]:CBC 54120 : if (file->nbytes > 0)
484 : 52792 : pgBufferUsage.temp_blks_read++;
9650 tgl@sss.pgh.pa.us 485 : 54120 : }
486 : :
487 : : /*
488 : : * BufFileDumpBuffer
489 : : *
490 : : * Dump buffer contents starting at curOffset.
491 : : * At call, should have dirty = true, nbytes > 0.
492 : : * On exit, dirty is cleared if successful write, and curOffset is advanced.
493 : : */
494 : : static void
495 : 60994 : BufFileDumpBuffer(BufFile *file)
496 : : {
79 michael@paquier.xyz 497 :GNC 60994 : int64 wpos = 0;
498 : : int64 bytestowrite;
499 : : File thisfile;
500 : :
501 : : /*
502 : : * Unlike BufFileLoadBuffer, we must dump the whole buffer even if it
503 : : * crosses a component-file boundary; so we need a loop.
504 : : */
9650 tgl@sss.pgh.pa.us 505 [ + + ]:CBC 121988 : while (wpos < file->nbytes)
506 : : {
507 : : int64 availbytes;
508 : : instr_time io_start;
509 : : instr_time io_time;
510 : :
511 : : /*
512 : : * Advance to next component file if necessary and possible.
513 : : */
3041 andres@anarazel.de 514 [ - + ]: 60994 : if (file->curOffset >= MAX_PHYSICAL_FILESIZE)
515 : : {
9468 bruce@momjian.us 516 [ # # ]:UBC 0 : while (file->curFile + 1 >= file->numFiles)
9647 tgl@sss.pgh.pa.us 517 : 0 : extendBufFile(file);
9650 518 : 0 : file->curFile++;
1082 peter@eisentraut.org 519 : 0 : file->curOffset = 0;
520 : : }
521 : :
522 : : /*
523 : : * Determine how much we need to write into this file.
524 : : */
9650 tgl@sss.pgh.pa.us 525 :CBC 60994 : bytestowrite = file->nbytes - wpos;
3041 andres@anarazel.de 526 : 60994 : availbytes = MAX_PHYSICAL_FILESIZE - file->curOffset;
527 : :
79 michael@paquier.xyz 528 [ - + ]:GNC 60994 : if (bytestowrite > availbytes)
79 michael@paquier.xyz 529 :UNC 0 : bytestowrite = availbytes;
530 : :
9647 tgl@sss.pgh.pa.us 531 :CBC 60994 : thisfile = file->files[file->curFile];
532 : :
1437 michael@paquier.xyz 533 [ - + ]: 60994 : if (track_io_timing)
1437 michael@paquier.xyz 534 :UBC 0 : INSTR_TIME_SET_CURRENT(io_start);
535 : : else
1150 andres@anarazel.de 536 :CBC 60994 : INSTR_TIME_SET_ZERO(io_start);
537 : :
3284 rhaas@postgresql.org 538 : 60994 : bytestowrite = FileWrite(thisfile,
2752 tgl@sss.pgh.pa.us 539 : 60994 : file->buffer.data + wpos,
540 : : bytestowrite,
541 : : file->curOffset,
542 : : WAIT_EVENT_BUFFILE_WRITE);
9650 543 [ - + ]: 60994 : if (bytestowrite <= 0)
2098 tmunro@postgresql.or 544 [ # # ]:UBC 0 : ereport(ERROR,
545 : : (errcode_for_file_access(),
546 : : errmsg("could not write to file \"%s\": %m",
547 : : FilePathName(thisfile))));
548 : :
1437 michael@paquier.xyz 549 [ - + ]:CBC 60994 : if (track_io_timing)
550 : : {
1437 michael@paquier.xyz 551 :UBC 0 : INSTR_TIME_SET_CURRENT(io_time);
1081 andres@anarazel.de 552 : 0 : INSTR_TIME_ACCUM_DIFF(pgBufferUsage.temp_blk_write_time, io_time, io_start);
553 : : }
554 : :
9650 tgl@sss.pgh.pa.us 555 :CBC 60994 : file->curOffset += bytestowrite;
556 : 60994 : wpos += bytestowrite;
557 : :
5934 rhaas@postgresql.org 558 : 60994 : pgBufferUsage.temp_blks_written++;
559 : : }
9650 tgl@sss.pgh.pa.us 560 : 60994 : file->dirty = false;
561 : :
562 : : /*
563 : : * At this point, curOffset has been advanced to the end of the buffer,
564 : : * ie, its original value + nbytes. We need to make it point to the
565 : : * logical file position, ie, original value + pos, in case that is less
566 : : * (as could happen due to a small backwards seek in a dirty buffer!)
567 : : */
568 : 60994 : file->curOffset -= (file->nbytes - file->pos);
569 [ - + ]: 60994 : if (file->curOffset < 0) /* handle possible segment crossing */
570 : : {
9650 tgl@sss.pgh.pa.us 571 :UBC 0 : file->curFile--;
572 [ # # ]: 0 : Assert(file->curFile >= 0);
573 : 0 : file->curOffset += MAX_PHYSICAL_FILESIZE;
574 : : }
575 : :
576 : : /*
577 : : * Now we can set the buffer empty without changing the logical position
578 : : */
9650 tgl@sss.pgh.pa.us 579 :CBC 60994 : file->pos = 0;
580 : 60994 : file->nbytes = 0;
581 : 60994 : }
582 : :
583 : : /*
584 : : * BufFileRead variants
585 : : *
586 : : * Like fread() except we assume 1-byte element size and report I/O errors via
587 : : * ereport().
588 : : *
589 : : * If 'exact' is true, then an error is also raised if the number of bytes
590 : : * read is not exactly 'size' (no short reads). If 'exact' and 'eofOK' are
591 : : * true, then reading zero bytes is ok.
592 : : */
593 : : static size_t
1154 peter@eisentraut.org 594 : 16963567 : BufFileReadCommon(BufFile *file, void *ptr, size_t size, bool exact, bool eofOK)
595 : : {
596 : 16963567 : size_t start_size = size;
9650 tgl@sss.pgh.pa.us 597 : 16963567 : size_t nread = 0;
598 : : size_t nthistime;
599 : :
2098 tmunro@postgresql.or 600 : 16963567 : BufFileFlush(file);
601 : :
9650 tgl@sss.pgh.pa.us 602 [ + + ]: 33941857 : while (size > 0)
603 : : {
604 [ + + ]: 16979618 : if (file->pos >= file->nbytes)
605 : : {
606 : : /* Try to load more data into buffer. */
607 : 54120 : file->curOffset += file->pos;
608 : 54120 : file->pos = 0;
609 : 54120 : file->nbytes = 0;
610 : 54120 : BufFileLoadBuffer(file);
611 [ + + ]: 54120 : if (file->nbytes <= 0)
612 : 1328 : break; /* no more data available */
613 : : }
614 : :
615 : 16978290 : nthistime = file->nbytes - file->pos;
616 [ + + ]: 16978290 : if (nthistime > size)
617 : 16927127 : nthistime = size;
618 [ - + ]: 16978290 : Assert(nthistime > 0);
619 : :
2752 620 : 16978290 : memcpy(ptr, file->buffer.data + file->pos, nthistime);
621 : :
9650 622 : 16978290 : file->pos += nthistime;
1171 peter@eisentraut.org 623 : 16978290 : ptr = (char *) ptr + nthistime;
9650 tgl@sss.pgh.pa.us 624 : 16978290 : size -= nthistime;
625 : 16978290 : nread += nthistime;
626 : : }
627 : :
1154 peter@eisentraut.org 628 [ + - + + ]: 16963567 : if (exact &&
629 [ + - - + ]: 1328 : (nread != start_size && !(nread == 0 && eofOK)))
1154 peter@eisentraut.org 630 [ # # # # ]:UBC 0 : ereport(ERROR,
631 : : errcode_for_file_access(),
632 : : file->name ?
633 : : errmsg("could not read from file set \"%s\": read only %zu of %zu bytes",
634 : : file->name, nread, start_size) :
635 : : errmsg("could not read from temporary file: read only %zu of %zu bytes",
636 : : nread, start_size));
637 : :
9650 tgl@sss.pgh.pa.us 638 :CBC 16963567 : return nread;
639 : : }
640 : :
641 : : /*
642 : : * Legacy interface where the caller needs to check for end of file or short
643 : : * reads.
644 : : */
645 : : size_t
1154 peter@eisentraut.org 646 :UBC 0 : BufFileRead(BufFile *file, void *ptr, size_t size)
647 : : {
648 : 0 : return BufFileReadCommon(file, ptr, size, false, false);
649 : : }
650 : :
651 : : /*
652 : : * Require read of exactly the specified size.
653 : : */
654 : : void
1154 peter@eisentraut.org 655 :CBC 10324642 : BufFileReadExact(BufFile *file, void *ptr, size_t size)
656 : : {
657 : 10324642 : BufFileReadCommon(file, ptr, size, true, false);
658 : 10324642 : }
659 : :
660 : : /*
661 : : * Require read of exactly the specified size, but optionally allow end of
662 : : * file (in which case 0 is returned).
663 : : */
664 : : size_t
665 : 6638925 : BufFileReadMaybeEOF(BufFile *file, void *ptr, size_t size, bool eofOK)
666 : : {
667 : 6638925 : return BufFileReadCommon(file, ptr, size, true, eofOK);
668 : : }
669 : :
670 : : /*
671 : : * BufFileWrite
672 : : *
673 : : * Like fwrite() except we assume 1-byte element size and report errors via
674 : : * ereport().
675 : : */
676 : : void
1171 677 : 15918229 : BufFileWrite(BufFile *file, const void *ptr, size_t size)
678 : : {
679 : : size_t nthistime;
680 : :
3026 andres@anarazel.de 681 [ - + ]: 15918229 : Assert(!file->readOnly);
682 : :
9650 tgl@sss.pgh.pa.us 683 [ + + ]: 31861430 : while (size > 0)
684 : : {
685 [ + + ]: 15943201 : if (file->pos >= BLCKSZ)
686 : : {
687 : : /* Buffer full, dump it out */
688 [ + + ]: 40461 : if (file->dirty)
689 : 40227 : BufFileDumpBuffer(file);
690 : : else
691 : : {
692 : : /* Hmm, went directly from reading to writing? */
693 : 234 : file->curOffset += file->pos;
694 : 234 : file->pos = 0;
695 : 234 : file->nbytes = 0;
696 : : }
697 : : }
698 : :
699 : 15943201 : nthistime = BLCKSZ - file->pos;
700 [ + + ]: 15943201 : if (nthistime > size)
701 : 15884130 : nthistime = size;
702 [ - + ]: 15943201 : Assert(nthistime > 0);
703 : :
2752 704 : 15943201 : memcpy(file->buffer.data + file->pos, ptr, nthistime);
705 : :
9650 706 : 15943201 : file->dirty = true;
707 : 15943201 : file->pos += nthistime;
708 [ + + ]: 15943201 : if (file->nbytes < file->pos)
709 : 15941269 : file->nbytes = file->pos;
1171 peter@eisentraut.org 710 : 15943201 : ptr = (const char *) ptr + nthistime;
9650 tgl@sss.pgh.pa.us 711 : 15943201 : size -= nthistime;
712 : : }
713 : 15918229 : }
714 : :
715 : : /*
716 : : * BufFileFlush
717 : : *
718 : : * Like fflush(), except that I/O errors are reported with ereport().
719 : : */
720 : : static void
721 : 16995807 : BufFileFlush(BufFile *file)
722 : : {
723 [ + + ]: 16995807 : if (file->dirty)
724 : 20767 : BufFileDumpBuffer(file);
725 : :
2098 tmunro@postgresql.or 726 [ - + ]: 16995807 : Assert(!file->dirty);
9650 tgl@sss.pgh.pa.us 727 : 16995807 : }
728 : :
729 : : /*
730 : : * BufFileSeek
731 : : *
732 : : * Like fseek(), except that target position needs two values in order to
733 : : * work when logical filesize exceeds maximum value representable by pgoff_t.
734 : : * We do not support relative seeks across more than that, however.
735 : : * I/O errors are reported by ereport().
736 : : *
737 : : * Result is 0 if OK, EOF if not. Logical position is not moved if an
738 : : * impossible seek is attempted.
739 : : */
740 : : int
82 michael@paquier.xyz 741 :GNC 56109 : BufFileSeek(BufFile *file, int fileno, pgoff_t offset, int whence)
742 : : {
743 : : int newFile;
744 : : pgoff_t newOffset;
745 : :
9650 tgl@sss.pgh.pa.us 746 [ + - + - ]:CBC 56109 : switch (whence)
747 : : {
748 : 55774 : case SEEK_SET:
9644 749 [ - + ]: 55774 : if (fileno < 0)
9650 tgl@sss.pgh.pa.us 750 :UBC 0 : return EOF;
9650 tgl@sss.pgh.pa.us 751 :CBC 55774 : newFile = fileno;
752 : 55774 : newOffset = offset;
753 : 55774 : break;
9650 tgl@sss.pgh.pa.us 754 :UBC 0 : case SEEK_CUR:
755 : :
756 : : /*
757 : : * Relative seek considers only the signed offset, ignoring
758 : : * fileno.
759 : : */
760 : 0 : newFile = file->curFile;
761 : 0 : newOffset = (file->curOffset + file->pos) + offset;
762 : 0 : break;
9650 tgl@sss.pgh.pa.us 763 :CBC 335 : case SEEK_END:
764 : :
765 : : /*
766 : : * The file size of the last file gives us the end offset of that
767 : : * file.
768 : : */
2027 akapila@postgresql.o 769 : 335 : newFile = file->numFiles - 1;
770 : 335 : newOffset = FileSize(file->files[file->numFiles - 1]);
771 [ - + ]: 335 : if (newOffset < 0)
2027 akapila@postgresql.o 772 [ # # ]:UBC 0 : ereport(ERROR,
773 : : (errcode_for_file_access(),
774 : : errmsg("could not determine size of temporary file \"%s\" from BufFile \"%s\": %m",
775 : : FilePathName(file->files[file->numFiles - 1]),
776 : : file->name)));
9650 tgl@sss.pgh.pa.us 777 :CBC 335 : break;
9650 tgl@sss.pgh.pa.us 778 :UBC 0 : default:
8270 779 [ # # ]: 0 : elog(ERROR, "invalid whence: %d", whence);
780 : : return EOF;
781 : : }
9650 tgl@sss.pgh.pa.us 782 [ - + ]:CBC 56109 : while (newOffset < 0)
783 : : {
9650 tgl@sss.pgh.pa.us 784 [ # # ]:UBC 0 : if (--newFile < 0)
785 : 0 : return EOF;
786 : 0 : newOffset += MAX_PHYSICAL_FILESIZE;
787 : : }
9650 tgl@sss.pgh.pa.us 788 [ + + ]:CBC 56109 : if (newFile == file->curFile &&
789 [ + + ]: 55992 : newOffset >= file->curOffset &&
790 [ + + ]: 41324 : newOffset <= file->curOffset + file->nbytes)
791 : : {
792 : : /*
793 : : * Seek is to a point within existing buffer; we can just adjust
794 : : * pos-within-buffer, without flushing buffer. Note this is OK
795 : : * whether reading or writing, but buffer remains dirty if we were
796 : : * writing.
797 : : */
79 michael@paquier.xyz 798 :GNC 29230 : file->pos = (int64) (newOffset - file->curOffset);
9650 tgl@sss.pgh.pa.us 799 :CBC 29230 : return 0;
800 : : }
801 : : /* Otherwise, must reposition buffer, so flush any dirty data */
2098 tmunro@postgresql.or 802 : 26879 : BufFileFlush(file);
803 : :
804 : : /*
805 : : * At this point and no sooner, check for seek past last segment. The
806 : : * above flush could have created a new segment, so checking sooner would
807 : : * not work (at least not with this code).
808 : : */
809 : :
810 : : /* convert seek to "start of next seg" to "end of last seg" */
3041 andres@anarazel.de 811 [ - + - - ]: 26879 : if (newFile == file->numFiles && newOffset == 0)
812 : : {
3041 andres@anarazel.de 813 :UBC 0 : newFile--;
814 : 0 : newOffset = MAX_PHYSICAL_FILESIZE;
815 : : }
3041 andres@anarazel.de 816 [ - + ]:CBC 26879 : while (newOffset > MAX_PHYSICAL_FILESIZE)
817 : : {
3041 andres@anarazel.de 818 [ # # ]:UBC 0 : if (++newFile >= file->numFiles)
819 : 0 : return EOF;
820 : 0 : newOffset -= MAX_PHYSICAL_FILESIZE;
821 : : }
9644 tgl@sss.pgh.pa.us 822 [ - + ]:CBC 26879 : if (newFile >= file->numFiles)
9644 tgl@sss.pgh.pa.us 823 :UBC 0 : return EOF;
824 : : /* Seek is OK! */
9650 tgl@sss.pgh.pa.us 825 :CBC 26879 : file->curFile = newFile;
826 : 26879 : file->curOffset = newOffset;
827 : 26879 : file->pos = 0;
828 : 26879 : file->nbytes = 0;
829 : 26879 : return 0;
830 : : }
831 : :
832 : : void
82 michael@paquier.xyz 833 :GNC 88661 : BufFileTell(BufFile *file, int *fileno, pgoff_t *offset)
834 : : {
9650 tgl@sss.pgh.pa.us 835 :CBC 88661 : *fileno = file->curFile;
836 : 88661 : *offset = file->curOffset + file->pos;
837 : 88661 : }
838 : :
839 : : /*
840 : : * BufFileSeekBlock --- block-oriented seek
841 : : *
842 : : * Performs absolute seek to the start of the n'th BLCKSZ-sized block of
843 : : * the file. Note that users of this interface will fail if their files
844 : : * exceed BLCKSZ * PG_INT64_MAX bytes, but that is quite a lot; we don't
845 : : * work with tables bigger than that, either...
846 : : *
847 : : * Result is 0 if OK, EOF if not. Logical position is not moved if an
848 : : * impossible seek is attempted.
849 : : */
850 : : int
849 michael@paquier.xyz 851 : 54275 : BufFileSeekBlock(BufFile *file, int64 blknum)
852 : : {
9647 tgl@sss.pgh.pa.us 853 : 108550 : return BufFileSeek(file,
6579 854 : 54275 : (int) (blknum / BUFFILE_SEG_SIZE),
82 michael@paquier.xyz 855 :GNC 54275 : (pgoff_t) (blknum % BUFFILE_SEG_SIZE) * BLCKSZ,
856 : : SEEK_SET);
857 : : }
858 : :
859 : : /*
860 : : * Returns the amount of data in the given BufFile, in bytes.
861 : : *
862 : : * Returned value includes the size of any holes left behind by BufFileAppend.
863 : : * ereport()s on failure.
864 : : */
865 : : int64
2963 rhaas@postgresql.org 866 :CBC 222 : BufFileSize(BufFile *file)
867 : : {
868 : : int64 lastFileSize;
869 : :
870 : : /* Get the size of the last physical file. */
2685 tmunro@postgresql.or 871 : 222 : lastFileSize = FileSize(file->files[file->numFiles - 1]);
2874 heikki.linnakangas@i 872 [ - + ]: 222 : if (lastFileSize < 0)
2664 pg@bowt.ie 873 [ # # ]:UBC 0 : ereport(ERROR,
874 : : (errcode_for_file_access(),
875 : : errmsg("could not determine size of temporary file \"%s\" from BufFile \"%s\": %m",
876 : : FilePathName(file->files[file->numFiles - 1]),
877 : : file->name)));
878 : :
2677 tmunro@postgresql.or 879 :CBC 222 : return ((file->numFiles - 1) * (int64) MAX_PHYSICAL_FILESIZE) +
880 : : lastFileSize;
881 : : }
882 : :
883 : : /*
884 : : * Append the contents of the source file to the end of the target file.
885 : : *
886 : : * Note that operation subsumes ownership of underlying resources from
887 : : * "source". Caller should never call BufFileClose against source having
888 : : * called here first. Resource owners for source and target must match,
889 : : * too.
890 : : *
891 : : * This operation works by manipulating lists of segment files, so the
892 : : * file content is always appended at a MAX_PHYSICAL_FILESIZE-aligned
893 : : * boundary, typically creating empty holes before the boundary. These
894 : : * areas do not contain any interesting data, and cannot be read from by
895 : : * caller.
896 : : *
897 : : * Returns the block number within target where the contents of source
898 : : * begins. Caller should apply this as an offset when working off block
899 : : * positions that are in terms of the original BufFile space.
900 : : */
901 : : int64
2963 rhaas@postgresql.org 902 : 117 : BufFileAppend(BufFile *target, BufFile *source)
903 : : {
846 michael@paquier.xyz 904 : 117 : int64 startBlock = (int64) target->numFiles * BUFFILE_SEG_SIZE;
2963 rhaas@postgresql.org 905 : 117 : int newNumFiles = target->numFiles + source->numFiles;
906 : : int i;
907 : :
908 [ - + ]: 117 : Assert(source->readOnly);
909 [ - + ]: 117 : Assert(!source->dirty);
910 : :
911 [ - + ]: 117 : if (target->resowner != source->resowner)
2963 rhaas@postgresql.org 912 [ # # ]:UBC 0 : elog(ERROR, "could not append BufFile with non-matching resource owner");
913 : :
2963 rhaas@postgresql.org 914 :CBC 117 : target->files = (File *)
915 : 117 : repalloc(target->files, sizeof(File) * newNumFiles);
916 [ + + ]: 234 : for (i = target->numFiles; i < newNumFiles; i++)
917 : 117 : target->files[i] = source->files[i - target->numFiles];
918 : 117 : target->numFiles = newNumFiles;
919 : :
920 : 117 : return startBlock;
921 : : }
922 : :
923 : : /*
924 : : * Truncate a BufFile created by BufFileCreateFileSet up to the given fileno
925 : : * and the offset.
926 : : */
927 : : void
82 michael@paquier.xyz 928 :GNC 9 : BufFileTruncateFileSet(BufFile *file, int fileno, pgoff_t offset)
929 : : {
2027 akapila@postgresql.o 930 :CBC 9 : int numFiles = file->numFiles;
931 : 9 : int newFile = fileno;
82 michael@paquier.xyz 932 :GNC 9 : pgoff_t newOffset = file->curOffset;
933 : : char segment_name[MAXPGPATH];
934 : : int i;
935 : :
936 : : /*
937 : : * Loop over all the files up to the given fileno and remove the files
938 : : * that are greater than the fileno and truncate the given file up to the
939 : : * offset. Note that we also remove the given fileno if the offset is 0
940 : : * provided it is not the first file in which we truncate it.
941 : : */
2027 akapila@postgresql.o 942 [ + + ]:CBC 18 : for (i = file->numFiles - 1; i >= fileno; i--)
943 : : {
944 [ + - - + : 9 : if ((i != fileno || offset == 0) && i != 0)
- - ]
945 : : {
1658 akapila@postgresql.o 946 :UBC 0 : FileSetSegmentName(segment_name, file->name, i);
2027 947 : 0 : FileClose(file->files[i]);
1658 948 [ # # ]: 0 : if (!FileSetDelete(file->fileset, segment_name, true))
2027 949 [ # # ]: 0 : ereport(ERROR,
950 : : (errcode_for_file_access(),
951 : : errmsg("could not delete fileset \"%s\": %m",
952 : : segment_name)));
953 : 0 : numFiles--;
954 : 0 : newOffset = MAX_PHYSICAL_FILESIZE;
955 : :
956 : : /*
957 : : * This is required to indicate that we have deleted the given
958 : : * fileno.
959 : : */
960 [ # # ]: 0 : if (i == fileno)
961 : 0 : newFile--;
962 : : }
963 : : else
964 : : {
2027 akapila@postgresql.o 965 [ - + ]:CBC 9 : if (FileTruncate(file->files[i], offset,
966 : : WAIT_EVENT_BUFFILE_TRUNCATE) < 0)
2027 akapila@postgresql.o 967 [ # # ]:UBC 0 : ereport(ERROR,
968 : : (errcode_for_file_access(),
969 : : errmsg("could not truncate file \"%s\": %m",
970 : : FilePathName(file->files[i]))));
2027 akapila@postgresql.o 971 :CBC 9 : newOffset = offset;
972 : : }
973 : : }
974 : :
975 : 9 : file->numFiles = numFiles;
976 : :
977 : : /*
978 : : * If the truncate point is within existing buffer then we can just adjust
979 : : * pos within buffer.
980 : : */
981 [ + - ]: 9 : if (newFile == file->curFile &&
982 [ + - ]: 9 : newOffset >= file->curOffset &&
983 [ - + ]: 9 : newOffset <= file->curOffset + file->nbytes)
984 : : {
985 : : /* No need to reset the current pos if the new pos is greater. */
2027 akapila@postgresql.o 986 [ # # ]:UBC 0 : if (newOffset <= file->curOffset + file->pos)
79 michael@paquier.xyz 987 :UNC 0 : file->pos = (int64) newOffset - file->curOffset;
988 : :
989 : : /* Adjust the nbytes for the current buffer. */
990 : 0 : file->nbytes = (int64) newOffset - file->curOffset;
991 : : }
2027 akapila@postgresql.o 992 [ + - ]:CBC 9 : else if (newFile == file->curFile &&
993 [ - + ]: 9 : newOffset < file->curOffset)
994 : : {
995 : : /*
996 : : * The truncate point is within the existing file but prior to the
997 : : * current position, so we can forget the current buffer and reset the
998 : : * current position.
999 : : */
2027 akapila@postgresql.o 1000 :UBC 0 : file->curOffset = newOffset;
1001 : 0 : file->pos = 0;
1002 : 0 : file->nbytes = 0;
1003 : : }
2027 akapila@postgresql.o 1004 [ - + ]:CBC 9 : else if (newFile < file->curFile)
1005 : : {
1006 : : /*
1007 : : * The truncate point is prior to the current file, so need to reset
1008 : : * the current position accordingly.
1009 : : */
2027 akapila@postgresql.o 1010 :UBC 0 : file->curFile = newFile;
1011 : 0 : file->curOffset = newOffset;
1012 : 0 : file->pos = 0;
1013 : 0 : file->nbytes = 0;
1014 : : }
1015 : : /* Nothing to do, if the truncate point is beyond current file. */
2027 akapila@postgresql.o 1016 :CBC 9 : }
|