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 1734 of file snapbuild.c.

1736{
1737 int fd;
1738 pg_crc32c checksum;
1739 Size sz;
1740 char path[MAXPGPATH];
1741
1742 sprintf(path, "%s/%X-%X.snap",
1744 LSN_FORMAT_ARGS(lsn));
1745
1746 fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
1747
1748 if (fd < 0)
1749 {
1750 if (missing_ok && errno == ENOENT)
1751 return false;
1752
1753 ereport(ERROR,
1755 errmsg("could not open file \"%s\": %m", path)));
1756 }
1757
1758 /* ----
1759 * Make sure the snapshot had been stored safely to disk, that's normally
1760 * cheap.
1761 * Note that we do not need PANIC here, nobody will be able to use the
1762 * slot without fsyncing, and saving it won't succeed without an fsync()
1763 * either...
1764 * ----
1765 */
1766 fsync_fname(path, false);
1768
1769 /* read statically sized portion of snapshot */
1771
1772 if (ondisk->magic != SNAPBUILD_MAGIC)
1773 ereport(ERROR,
1775 errmsg("snapbuild state file \"%s\" has wrong magic number: %u instead of %u",
1776 path, ondisk->magic, SNAPBUILD_MAGIC)));
1777
1778 if (ondisk->version != SNAPBUILD_VERSION)
1779 ereport(ERROR,
1781 errmsg("snapbuild state file \"%s\" has unsupported version: %u instead of %u",
1782 path, ondisk->version, SNAPBUILD_VERSION)));
1783
1784 INIT_CRC32C(checksum);
1785 COMP_CRC32C(checksum,
1786 ((char *) ondisk) + SnapBuildOnDiskNotChecksummedSize,
1788
1789 /* read SnapBuild */
1790 SnapBuildRestoreContents(fd, &ondisk->builder, sizeof(SnapBuild), path);
1791 COMP_CRC32C(checksum, &ondisk->builder, sizeof(SnapBuild));
1792
1793 /* restore committed xacts information */
1794 if (ondisk->builder.committed.xcnt > 0)
1795 {
1796 sz = sizeof(TransactionId) * ondisk->builder.committed.xcnt;
1797 ondisk->builder.committed.xip = MemoryContextAllocZero(context, sz);
1798 SnapBuildRestoreContents(fd, ondisk->builder.committed.xip, sz, path);
1799 COMP_CRC32C(checksum, ondisk->builder.committed.xip, sz);
1800 }
1801
1802 /* restore catalog modifying xacts information */
1803 if (ondisk->builder.catchange.xcnt > 0)
1804 {
1805 sz = sizeof(TransactionId) * ondisk->builder.catchange.xcnt;
1806 ondisk->builder.catchange.xip = MemoryContextAllocZero(context, sz);
1807 SnapBuildRestoreContents(fd, ondisk->builder.catchange.xip, sz, path);
1808 COMP_CRC32C(checksum, ondisk->builder.catchange.xip, sz);
1809 }
1810
1811 if (CloseTransientFile(fd) != 0)
1812 ereport(ERROR,
1814 errmsg("could not close file \"%s\": %m", path)));
1815
1816 FIN_CRC32C(checksum);
1817
1818 /* verify checksum of what we've read */
1819 if (!EQ_CRC32C(checksum, ondisk->checksum))
1820 ereport(ERROR,
1822 errmsg("checksum mismatch for snapbuild state file \"%s\": is %u, should be %u",
1823 path, checksum, ondisk->checksum)));
1824
1825 return true;
1826}
#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:877
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
int CloseTransientFile(int fd)
Definition: fd.c:2871
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:756
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2694
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1294
#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:153
#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:158
#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:1467
#define SnapBuildOnDiskNotChecksummedSize
Definition: snapbuild.c:1463
#define SNAPBUILD_MAGIC
Definition: snapbuild.c:1466
#define SnapBuildOnDiskConstantSize
Definition: snapbuild.c:1461
static void SnapBuildRestoreContents(int fd, void *dest, Size size, const char *path)
Definition: snapbuild.c:1925
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().