PostgreSQL Source Code  git master
snapbuild_internal.h File Reference
Include dependency graph for snapbuild_internal.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SnapBuild
 
struct  SnapBuildOnDisk
 

Typedefs

typedef struct SnapBuildOnDisk SnapBuildOnDisk
 

Functions

bool SnapBuildRestoreSnapshot (SnapBuildOnDisk *ondisk, const char *path, MemoryContext context, bool missing_ok)
 

Typedef Documentation

◆ SnapBuildOnDisk

Function Documentation

◆ SnapBuildRestoreSnapshot()

bool SnapBuildRestoreSnapshot ( SnapBuildOnDisk ondisk,
const char *  path,
MemoryContext  context,
bool  missing_ok 
)

Definition at line 1694 of file snapbuild.c.

1696 {
1697  int fd;
1698  pg_crc32c checksum;
1699  Size sz;
1700 
1701  fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
1702 
1703  if (fd < 0)
1704  {
1705  if (missing_ok && errno == ENOENT)
1706  return false;
1707 
1708  ereport(ERROR,
1710  errmsg("could not open file \"%s\": %m", path)));
1711  }
1712 
1713  /* ----
1714  * Make sure the snapshot had been stored safely to disk, that's normally
1715  * cheap.
1716  * Note that we do not need PANIC here, nobody will be able to use the
1717  * slot without fsyncing, and saving it won't succeed without an fsync()
1718  * either...
1719  * ----
1720  */
1721  fsync_fname(path, false);
1723 
1724  /* read statically sized portion of snapshot */
1725  SnapBuildRestoreContents(fd, (char *) ondisk, SnapBuildOnDiskConstantSize, path);
1726 
1727  if (ondisk->magic != SNAPBUILD_MAGIC)
1728  ereport(ERROR,
1730  errmsg("snapbuild state file \"%s\" has wrong magic number: %u instead of %u",
1731  path, ondisk->magic, SNAPBUILD_MAGIC)));
1732 
1733  if (ondisk->version != SNAPBUILD_VERSION)
1734  ereport(ERROR,
1736  errmsg("snapbuild state file \"%s\" has unsupported version: %u instead of %u",
1737  path, ondisk->version, SNAPBUILD_VERSION)));
1738 
1739  INIT_CRC32C(checksum);
1740  COMP_CRC32C(checksum,
1741  ((char *) ondisk) + SnapBuildOnDiskNotChecksummedSize,
1743 
1744  /* read SnapBuild */
1745  SnapBuildRestoreContents(fd, (char *) &ondisk->builder, sizeof(SnapBuild), path);
1746  COMP_CRC32C(checksum, &ondisk->builder, sizeof(SnapBuild));
1747 
1748  /* restore committed xacts information */
1749  if (ondisk->builder.committed.xcnt > 0)
1750  {
1751  sz = sizeof(TransactionId) * ondisk->builder.committed.xcnt;
1753  SnapBuildRestoreContents(fd, (char *) ondisk->builder.committed.xip, sz, path);
1754  COMP_CRC32C(checksum, ondisk->builder.committed.xip, sz);
1755  }
1756 
1757  /* restore catalog modifying xacts information */
1758  if (ondisk->builder.catchange.xcnt > 0)
1759  {
1760  sz = sizeof(TransactionId) * ondisk->builder.catchange.xcnt;
1762  SnapBuildRestoreContents(fd, (char *) ondisk->builder.catchange.xip, sz, path);
1763  COMP_CRC32C(checksum, ondisk->builder.catchange.xip, sz);
1764  }
1765 
1766  if (CloseTransientFile(fd) != 0)
1767  ereport(ERROR,
1769  errmsg("could not close file \"%s\": %m", path)));
1770 
1771  FIN_CRC32C(checksum);
1772 
1773  /* verify checksum of what we've read */
1774  if (!EQ_CRC32C(checksum, ondisk->checksum))
1775  ereport(ERROR,
1777  errmsg("checksum mismatch for snapbuild state file \"%s\": is %u, should be %u",
1778  path, checksum, ondisk->checksum)));
1779 
1780  return true;
1781 }
#define PG_BINARY
Definition: c.h:1227
uint32 TransactionId
Definition: c.h:606
size_t Size
Definition: c.h:559
int errcode_for_file_access(void)
Definition: elog.c:876
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
int CloseTransientFile(int fd)
Definition: fd.c:2831
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:755
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2655
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1215
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:41
uint32 pg_crc32c
Definition: pg_crc32c.h:38
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:98
#define EQ_CRC32C(c1, c2)
Definition: pg_crc32c.h:42
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:103
static int fd(const char *x, int i)
Definition: preproc-init.c:105
tree context
Definition: radixtree.h:1837
#define PG_LOGICAL_SNAPSHOTS_DIR
Definition: reorderbuffer.h:24
#define SNAPBUILD_VERSION
Definition: snapbuild.c:1427
#define SnapBuildOnDiskNotChecksummedSize
Definition: snapbuild.c:1423
#define SNAPBUILD_MAGIC
Definition: snapbuild.c:1426
#define SnapBuildOnDiskConstantSize
Definition: snapbuild.c:1421
static void SnapBuildRestoreContents(int fd, char *dest, Size size, const char *path)
Definition: snapbuild.c:1885
struct SnapBuild::@116 catchange
TransactionId * xip
struct SnapBuild::@115 committed

References SnapBuildOnDisk::builder, SnapBuild::catchange, SnapBuildOnDisk::checksum, CloseTransientFile(), SnapBuild::committed, COMP_CRC32C, context, EQ_CRC32C, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errcode_for_file_access(), errmsg(), ERROR, fd(), FIN_CRC32C, fsync_fname(), INIT_CRC32C, SnapBuildOnDisk::magic, MemoryContextAllocZero(), OpenTransientFile(), PG_BINARY, PG_LOGICAL_SNAPSHOTS_DIR, SNAPBUILD_MAGIC, SNAPBUILD_VERSION, SnapBuildOnDiskConstantSize, SnapBuildOnDiskNotChecksummedSize, SnapBuildRestoreContents(), SnapBuildOnDisk::version, SnapBuild::xcnt, and SnapBuild::xip.

Referenced by pg_get_logical_snapshot_info(), pg_get_logical_snapshot_meta(), and SnapBuildRestore().