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

1743{
1744 int fd;
1745 pg_crc32c checksum;
1746 Size sz;
1747 char path[MAXPGPATH];
1748
1749 sprintf(path, "%s/%X-%X.snap",
1751 LSN_FORMAT_ARGS(lsn));
1752
1753 fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
1754
1755 if (fd < 0)
1756 {
1757 if (missing_ok && errno == ENOENT)
1758 return false;
1759
1760 ereport(ERROR,
1762 errmsg("could not open file \"%s\": %m", path)));
1763 }
1764
1765 /* ----
1766 * Make sure the snapshot had been stored safely to disk, that's normally
1767 * cheap.
1768 * Note that we do not need PANIC here, nobody will be able to use the
1769 * slot without fsyncing, and saving it won't succeed without an fsync()
1770 * either...
1771 * ----
1772 */
1773 fsync_fname(path, false);
1775
1776 /* read statically sized portion of snapshot */
1778
1779 if (ondisk->magic != SNAPBUILD_MAGIC)
1780 ereport(ERROR,
1782 errmsg("snapbuild state file \"%s\" has wrong magic number: %u instead of %u",
1783 path, ondisk->magic, SNAPBUILD_MAGIC)));
1784
1785 if (ondisk->version != SNAPBUILD_VERSION)
1786 ereport(ERROR,
1788 errmsg("snapbuild state file \"%s\" has unsupported version: %u instead of %u",
1789 path, ondisk->version, SNAPBUILD_VERSION)));
1790
1791 INIT_CRC32C(checksum);
1792 COMP_CRC32C(checksum,
1793 ((char *) ondisk) + SnapBuildOnDiskNotChecksummedSize,
1795
1796 /* read SnapBuild */
1797 SnapBuildRestoreContents(fd, &ondisk->builder, sizeof(SnapBuild), path);
1798 COMP_CRC32C(checksum, &ondisk->builder, sizeof(SnapBuild));
1799
1800 /* restore committed xacts information */
1801 if (ondisk->builder.committed.xcnt > 0)
1802 {
1803 sz = sizeof(TransactionId) * ondisk->builder.committed.xcnt;
1804 ondisk->builder.committed.xip = MemoryContextAllocZero(context, sz);
1805 SnapBuildRestoreContents(fd, ondisk->builder.committed.xip, sz, path);
1806 COMP_CRC32C(checksum, ondisk->builder.committed.xip, sz);
1807 }
1808
1809 /* restore catalog modifying xacts information */
1810 if (ondisk->builder.catchange.xcnt > 0)
1811 {
1812 sz = sizeof(TransactionId) * ondisk->builder.catchange.xcnt;
1813 ondisk->builder.catchange.xip = MemoryContextAllocZero(context, sz);
1814 SnapBuildRestoreContents(fd, ondisk->builder.catchange.xip, sz, path);
1815 COMP_CRC32C(checksum, ondisk->builder.catchange.xip, sz);
1816 }
1817
1818 if (CloseTransientFile(fd) != 0)
1819 ereport(ERROR,
1821 errmsg("could not close file \"%s\": %m", path)));
1822
1823 FIN_CRC32C(checksum);
1824
1825 /* verify checksum of what we've read */
1826 if (!EQ_CRC32C(checksum, ondisk->checksum))
1827 ereport(ERROR,
1829 errmsg("checksum mismatch for snapbuild state file \"%s\": is %u, should be %u",
1830 path, checksum, ondisk->checksum)));
1831
1832 return true;
1833}
#define PG_BINARY
Definition: c.h:1271
uint32 TransactionId
Definition: c.h:671
size_t Size
Definition: c.h:624
int errcode_for_file_access(void)
Definition: elog.c:886
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
int CloseTransientFile(int fd)
Definition: fd.c:2851
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:753
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2674
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1263
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:42
#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:262
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:1474
#define SnapBuildOnDiskNotChecksummedSize
Definition: snapbuild.c:1470
#define SNAPBUILD_MAGIC
Definition: snapbuild.c:1473
#define SnapBuildOnDiskConstantSize
Definition: snapbuild.c:1468
static void SnapBuildRestoreContents(int fd, void *dest, Size size, const char *path)
Definition: snapbuild.c:1932
struct SnapBuild::@112 committed
struct SnapBuild::@113 catchange
TransactionId * xip
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:47

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().