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, XLogRecPtr lsn, MemoryContext context, bool missing_ok)
 

Typedef Documentation

◆ SnapBuildOnDisk

Function Documentation

◆ SnapBuildRestoreSnapshot()

bool SnapBuildRestoreSnapshot ( SnapBuildOnDisk ondisk,
XLogRecPtr  lsn,
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 char path[MAXPGPATH];
1701
1702 sprintf(path, "%s/%X-%X.snap",
1704 LSN_FORMAT_ARGS(lsn));
1705
1706 fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
1707
1708 if (fd < 0)
1709 {
1710 if (missing_ok && errno == ENOENT)
1711 return false;
1712
1713 ereport(ERROR,
1715 errmsg("could not open file \"%s\": %m", path)));
1716 }
1717
1718 /* ----
1719 * Make sure the snapshot had been stored safely to disk, that's normally
1720 * cheap.
1721 * Note that we do not need PANIC here, nobody will be able to use the
1722 * slot without fsyncing, and saving it won't succeed without an fsync()
1723 * either...
1724 * ----
1725 */
1726 fsync_fname(path, false);
1728
1729 /* read statically sized portion of snapshot */
1731
1732 if (ondisk->magic != SNAPBUILD_MAGIC)
1733 ereport(ERROR,
1735 errmsg("snapbuild state file \"%s\" has wrong magic number: %u instead of %u",
1736 path, ondisk->magic, SNAPBUILD_MAGIC)));
1737
1738 if (ondisk->version != SNAPBUILD_VERSION)
1739 ereport(ERROR,
1741 errmsg("snapbuild state file \"%s\" has unsupported version: %u instead of %u",
1742 path, ondisk->version, SNAPBUILD_VERSION)));
1743
1744 INIT_CRC32C(checksum);
1745 COMP_CRC32C(checksum,
1746 ((char *) ondisk) + SnapBuildOnDiskNotChecksummedSize,
1748
1749 /* read SnapBuild */
1750 SnapBuildRestoreContents(fd, &ondisk->builder, sizeof(SnapBuild), path);
1751 COMP_CRC32C(checksum, &ondisk->builder, sizeof(SnapBuild));
1752
1753 /* restore committed xacts information */
1754 if (ondisk->builder.committed.xcnt > 0)
1755 {
1756 sz = sizeof(TransactionId) * ondisk->builder.committed.xcnt;
1757 ondisk->builder.committed.xip = MemoryContextAllocZero(context, sz);
1758 SnapBuildRestoreContents(fd, ondisk->builder.committed.xip, sz, path);
1759 COMP_CRC32C(checksum, ondisk->builder.committed.xip, sz);
1760 }
1761
1762 /* restore catalog modifying xacts information */
1763 if (ondisk->builder.catchange.xcnt > 0)
1764 {
1765 sz = sizeof(TransactionId) * ondisk->builder.catchange.xcnt;
1766 ondisk->builder.catchange.xip = MemoryContextAllocZero(context, sz);
1767 SnapBuildRestoreContents(fd, ondisk->builder.catchange.xip, sz, path);
1768 COMP_CRC32C(checksum, ondisk->builder.catchange.xip, sz);
1769 }
1770
1771 if (CloseTransientFile(fd) != 0)
1772 ereport(ERROR,
1774 errmsg("could not close file \"%s\": %m", path)));
1775
1776 FIN_CRC32C(checksum);
1777
1778 /* verify checksum of what we've read */
1779 if (!EQ_CRC32C(checksum, ondisk->checksum))
1780 ereport(ERROR,
1782 errmsg("checksum mismatch for snapbuild state file \"%s\": is %u, should be %u",
1783 path, checksum, ondisk->checksum)));
1784
1785 return true;
1786}
#define PG_BINARY
Definition: c.h:1244
uint32 TransactionId
Definition: c.h:623
size_t Size
Definition: c.h:576
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
#define MAXPGPATH
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
#define sprintf
Definition: port.h:241
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#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, void *dest, Size size, const char *path)
Definition: snapbuild.c:1885
struct SnapBuild::@117 committed
TransactionId * xip
struct SnapBuild::@118 catchange
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43

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

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