/* *cloneFile() * *Clones/reflinksarelationfilefromsrctodst. * *schemaName/relNamearerelation'sSQLname(usedforerrormessagesonly).
*/ void
cloneFile(constchar *src, constchar *dst, constchar *schemaName, constchar *relName)
{ #ifdefined(HAVE_COPYFILE) && defined(COPYFILE_CLONE_FORCE) if (copyfile(src, dst, NULL, COPYFILE_CLONE_FORCE) < 0)
pg_fatal("error while cloning relation \"%s.%s\" (\"%s\" to \"%s\"): %m",
schemaName, relName, src, dst); #elifdefined(__linux__) && defined(FICLONE) int src_fd; int dest_fd;
if ((src_fd = open(src, O_RDONLY | PG_BINARY, 0)) < 0)
pg_fatal("error while cloning relation \"%s.%s\": could not open file \"%s\": %m",
schemaName, relName, src);
if ((dest_fd = open(dst, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
pg_file_create_mode)) < 0)
pg_fatal("error while cloning relation \"%s.%s\": could not create file \"%s\": %m",
schemaName, relName, dst);
if (ioctl(dest_fd, FICLONE, src_fd) < 0)
{ int save_errno = errno;
unlink(dst);
pg_fatal("error while cloning relation \"%s.%s\" (\"%s\" to \"%s\"): %s",
schemaName, relName, src, dst, strerror(save_errno));
}
if ((src_fd = open(src, O_RDONLY | PG_BINARY, 0)) < 0)
pg_fatal("error while copying relation \"%s.%s\": could not open file \"%s\": %m",
schemaName, relName, src);
if ((dest_fd = open(dst, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
pg_file_create_mode)) < 0)
pg_fatal("error while copying relation \"%s.%s\": could not create file \"%s\": %m",
schemaName, relName, dst);
/* copy in fairly large chunks for best efficiency */ #define COPY_BUF_SIZE (50 * BLCKSZ)
buffer = (char *) pg_malloc(COPY_BUF_SIZE);
/* perform data copying i.e read src source, write to destination */ while (true)
{
ssize_t nbytes = read(src_fd, buffer, COPY_BUF_SIZE);
if (nbytes < 0)
pg_fatal("error while copying relation \"%s.%s\": could not read file \"%s\": %m",
schemaName, relName, src);
if (nbytes == 0) break;
errno = 0; if (write(dest_fd, buffer, nbytes) != nbytes)
{ /* if write didn't set errno, assume problem is no disk space */ if (errno == 0)
errno = ENOSPC;
pg_fatal("error while copying relation \"%s.%s\": could not write file \"%s\": %m",
schemaName, relName, dst);
}
}
pg_free(buffer);
close(src_fd);
close(dest_fd);
#else/* WIN32 */
if (CopyFile(src, dst, true) == 0)
{
_dosmaperr(GetLastError());
pg_fatal("error while copying relation \"%s.%s\" (\"%s\" to \"%s\"): %m",
schemaName, relName, src, dst);
}
if ((src_fd = open(src, O_RDONLY | PG_BINARY, 0)) < 0)
pg_fatal("error while copying relation \"%s.%s\": could not open file \"%s\": %m",
schemaName, relName, src);
if ((dest_fd = open(dst, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
pg_file_create_mode)) < 0)
pg_fatal("error while copying relation \"%s.%s\": could not create file \"%s\": %m",
schemaName, relName, dst);
do
{
nbytes = copy_file_range(src_fd, NULL, dest_fd, NULL, SSIZE_MAX, 0); if (nbytes < 0)
pg_fatal("error while copying relation \"%s.%s\": could not copy file range from \"%s\" to \"%s\": %m",
schemaName, relName, src, dst);
} while (nbytes > 0);
close(src_fd);
close(dest_fd); #endif
}
/* *linkFile() * *Hard-linksarelationfilefromsrctodst. *schemaName/relNamearerelation'sSQLname(usedforerrormessagesonly).
*/ void
linkFile(constchar *src, constchar *dst, constchar *schemaName, constchar *relName)
{ if (link(src, dst) < 0)
pg_fatal("error while creating link for relation \"%s.%s\" (\"%s\" to \"%s\"): %m",
schemaName, relName, src, dst);
}
/* Compute number of old-format bytes per new page */
rewriteVmBytesPerPage = (BLCKSZ - SizeOfPageHeaderData) / 2;
if ((src_fd = open(fromfile, O_RDONLY | PG_BINARY, 0)) < 0)
pg_fatal("error while copying relation \"%s.%s\": could not open file \"%s\": %m",
schemaName, relName, fromfile);
if (fstat(src_fd, &statbuf) != 0)
pg_fatal("error while copying relation \"%s.%s\": could not stat file \"%s\": %m",
schemaName, relName, fromfile);
if ((dst_fd = open(tofile, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
pg_file_create_mode)) < 0)
pg_fatal("error while copying relation \"%s.%s\": could not create file \"%s\": %m",
schemaName, relName, tofile);
/* Save old file size */
src_filesize = statbuf.st_size;
/* First, copy old page header to new page */
memcpy(new_vmbuf.data, &pageheader, SizeOfPageHeaderData);
/* Rewriting the last part of the last old page? */
old_lastpart = old_lastblk && (old_break == old_blkend);
new_cur = new_vmbuf.data + SizeOfPageHeaderData;
/* Process old page bytes one by one, and turn it into new page. */ while (old_cur < old_break)
{
uint8 byte = *(uint8 *) old_cur;
uint16 new_vmbits = 0; int i;
/* Generate new format bits while keeping old information */ for (i = 0; i < BITS_PER_BYTE; i++)
{ if (byte & (1 << i))
{
empty = false;
new_vmbits |=
VISIBILITYMAP_ALL_VISIBLE << (BITS_PER_HEAPBLOCK * i);
}
}
/* If the last part of the last page is empty, skip writing it */ if (old_lastpart && empty) break;
/* Set new checksum for visibility map page, if enabled */ if (new_cluster.controldata.data_checksum_version != 0)
((PageHeader) new_vmbuf.data)->pd_checksum =
pg_checksum_page(new_vmbuf.data, new_blkno);
errno = 0; if (write(dst_fd, new_vmbuf.data, BLCKSZ) != BLCKSZ)
{ /* if write didn't set errno, assume problem is no disk space */ if (errno == 0)
errno = ENOSPC;
pg_fatal("error while copying relation \"%s.%s\": could not write file \"%s\": %m",
schemaName, relName, tofile);
}
/* Advance for next new page */
old_break += rewriteVmBytesPerPage;
new_blkno++;
}
}
#ifdefined(HAVE_COPYFILE) && defined(COPYFILE_CLONE_FORCE) if (copyfile(existing_file, new_link_file, NULL, COPYFILE_CLONE_FORCE) < 0)
pg_fatal("could not clone file between old and new data directories: %m"); #elifdefined(__linux__) && defined(FICLONE)
{ int src_fd; int dest_fd;
if ((src_fd = open(existing_file, O_RDONLY | PG_BINARY, 0)) < 0)
pg_fatal("could not open file \"%s\": %m",
existing_file);
if (link(existing_file, new_link_file) < 0)
{ if (transfer_mode == TRANSFER_MODE_LINK)
pg_fatal("could not create hard link between old and new data directories: %m\n" "In link mode the old and new data directories must be on the same file system."); elseif (transfer_mode == TRANSFER_MODE_SWAP)
pg_fatal("could not create hard link between old and new data directories: %m\n" "In swap mode the old and new data directories must be on the same file system."); else
pg_fatal("unrecognized transfer mode");
}
unlink(new_link_file);
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.17 Sekunden
(vorverarbeitet am 2026-08-07)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.