PostgreSQL Source Code git master
Loading...
Searching...
No Matches
snapbuild.c File Reference
#include "postgres.h"
#include <sys/stat.h>
#include <unistd.h>
#include "access/heapam_xlog.h"
#include "access/transam.h"
#include "access/xact.h"
#include "common/file_utils.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/logical.h"
#include "replication/reorderbuffer.h"
#include "replication/snapbuild.h"
#include "replication/snapbuild_internal.h"
#include "storage/fd.h"
#include "storage/lmgr.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "storage/standby.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
#include "utils/snapmgr.h"
#include "utils/snapshot.h"
#include "utils/wait_event.h"
Include dependency graph for snapbuild.c:

Go to the source code of this file.

Macros

#define SnapBuildOnDiskConstantSize    offsetof(SnapBuildOnDisk, builder)
 
#define SnapBuildOnDiskNotChecksummedSize    offsetof(SnapBuildOnDisk, version)
 
#define SNAPBUILD_MAGIC   0x51A1E001
 
#define SNAPBUILD_VERSION   6
 

Functions

static void SnapBuildPurgeOlderTxn (SnapBuild *builder)
 
static Snapshot SnapBuildBuildSnapshot (SnapBuild *builder)
 
static void SnapBuildFreeSnapshot (Snapshot snap)
 
static void SnapBuildSnapIncRefcount (Snapshot snap)
 
static void SnapBuildDistributeSnapshotAndInval (SnapBuild *builder, XLogRecPtr lsn, TransactionId xid)
 
static bool SnapBuildXidHasCatalogChanges (SnapBuild *builder, TransactionId xid, uint32 xinfo)
 
static bool SnapBuildFindSnapshot (SnapBuild *builder, XLogRecPtr lsn, xl_running_xacts *running)
 
static void SnapBuildWaitSnapshot (xl_running_xacts *running, TransactionId cutoff)
 
static void SnapBuildSerialize (SnapBuild *builder, XLogRecPtr lsn)
 
static bool SnapBuildRestore (SnapBuild *builder, XLogRecPtr lsn)
 
static void SnapBuildRestoreContents (int fd, void *dest, Size size, const char *path)
 
SnapBuildAllocateSnapshotBuilder (ReorderBuffer *reorder, TransactionId xmin_horizon, XLogRecPtr start_lsn, bool need_full_snapshot, bool in_slot_creation, XLogRecPtr two_phase_at)
 
void FreeSnapshotBuilder (SnapBuild *builder)
 
SnapBuildState SnapBuildCurrentState (SnapBuild *builder)
 
XLogRecPtr SnapBuildGetTwoPhaseAt (SnapBuild *builder)
 
void SnapBuildSetTwoPhaseAt (SnapBuild *builder, XLogRecPtr ptr)
 
bool SnapBuildXactNeedsSkip (SnapBuild *builder, XLogRecPtr ptr)
 
void SnapBuildSnapDecRefcount (Snapshot snap)
 
Snapshot SnapBuildInitialSnapshot (SnapBuild *builder)
 
const charSnapBuildExportSnapshot (SnapBuild *builder)
 
Snapshot SnapBuildGetOrBuildSnapshot (SnapBuild *builder)
 
void SnapBuildClearExportedSnapshot (void)
 
void SnapBuildResetExportedSnapshotState (void)
 
bool SnapBuildProcessChange (SnapBuild *builder, TransactionId xid, XLogRecPtr lsn)
 
void SnapBuildProcessNewCid (SnapBuild *builder, TransactionId xid, XLogRecPtr lsn, xl_heap_new_cid *xlrec)
 
static void SnapBuildAddCommittedTxn (SnapBuild *builder, TransactionId xid)
 
void SnapBuildCommitTxn (SnapBuild *builder, XLogRecPtr lsn, TransactionId xid, int nsubxacts, TransactionId *subxacts, uint32 xinfo)
 
void SnapBuildProcessRunningXacts (SnapBuild *builder, XLogRecPtr lsn, xl_running_xacts *running)
 
void SnapBuildSerializationPoint (SnapBuild *builder, XLogRecPtr lsn)
 
bool SnapBuildRestoreSnapshot (SnapBuildOnDisk *ondisk, XLogRecPtr lsn, MemoryContext context, bool missing_ok)
 
void CheckPointSnapBuild (void)
 
bool SnapBuildSnapshotExists (XLogRecPtr lsn)
 

Variables

static ResourceOwner SavedResourceOwnerDuringExport = NULL
 
static bool ExportInProgress = false
 

Macro Definition Documentation

◆ SNAPBUILD_MAGIC

#define SNAPBUILD_MAGIC   0x51A1E001

Definition at line 1477 of file snapbuild.c.

◆ SNAPBUILD_VERSION

#define SNAPBUILD_VERSION   6

Definition at line 1478 of file snapbuild.c.

◆ SnapBuildOnDiskConstantSize

#define SnapBuildOnDiskConstantSize    offsetof(SnapBuildOnDisk, builder)

Definition at line 1472 of file snapbuild.c.

1488{
1489 if (builder->state < SNAPBUILD_CONSISTENT)
1490 SnapBuildRestore(builder, lsn);
1491 else
1492 SnapBuildSerialize(builder, lsn);
1493}
1494
1495/*
1496 * Serialize the snapshot 'builder' at the location 'lsn' if it hasn't already
1497 * been done by another decoding process.
1498 */
1499static void
1501{
1503 SnapBuildOnDisk *ondisk = NULL;
1506 size_t catchange_xcnt;
1507 char *ondisk_c;
1508 int fd;
1509 char tmppath[MAXPGPATH];
1510 char path[MAXPGPATH];
1511 int ret;
1512 struct stat stat_buf;
1513 Size sz;
1514
1517 builder->last_serialized_snapshot <= lsn);
1518
1519 /*
1520 * no point in serializing if we cannot continue to work immediately after
1521 * restoring the snapshot
1522 */
1523 if (builder->state < SNAPBUILD_CONSISTENT)
1524 return;
1525
1526 /* consistent snapshots have no next phase */
1528
1529 /*
1530 * We identify snapshots by the LSN they are valid for. We don't need to
1531 * include timelines in the name as each LSN maps to exactly one timeline
1532 * unless the user used pg_resetwal or similar. If a user did so, there's
1533 * no hope continuing to decode anyway.
1534 */
1535 sprintf(path, "%s/%X-%X.snap",
1537 LSN_FORMAT_ARGS(lsn));
1538
1539 /*
1540 * first check whether some other backend already has written the snapshot
1541 * for this LSN. It's perfectly fine if there's none, so we accept ENOENT
1542 * as a valid state. Everything else is an unexpected error.
1543 */
1544 ret = stat(path, &stat_buf);
1545
1546 if (ret != 0 && errno != ENOENT)
1547 ereport(ERROR,
1549 errmsg("could not stat file \"%s\": %m", path)));
1550
1551 else if (ret == 0)
1552 {
1553 /*
1554 * somebody else has already serialized to this point, don't overwrite
1555 * but remember location, so we don't need to read old data again.
1556 *
1557 * To be sure it has been synced to disk after the rename() from the
1558 * tempfile filename to the real filename, we just repeat the fsync.
1559 * That ought to be cheap because in most scenarios it should already
1560 * be safely on disk.
1561 */
1562 fsync_fname(path, false);
1564
1565 builder->last_serialized_snapshot = lsn;
1566 goto out;
1567 }
1568
1569 /*
1570 * there is an obvious race condition here between the time we stat(2) the
1571 * file and us writing the file. But we rename the file into place
1572 * atomically and all files created need to contain the same data anyway,
1573 * so this is perfectly fine, although a bit of a resource waste. Locking
1574 * seems like pointless complication.
1575 */
1576 elog(DEBUG1, "serializing snapshot to %s", path);
1577
1578 /* to make sure only we will write to this tempfile, include pid */
1579 sprintf(tmppath, "%s/%X-%X.snap.%d.tmp",
1582
1583 /*
1584 * Unlink temporary file if it already exists, needs to have been before a
1585 * crash/error since we won't enter this function twice from within a
1586 * single decoding slot/backend and the temporary file contains the pid of
1587 * the current process.
1588 */
1589 if (unlink(tmppath) != 0 && errno != ENOENT)
1590 ereport(ERROR,
1592 errmsg("could not remove file \"%s\": %m", tmppath)));
1593
1595
1596 /* Get the catalog modifying transactions that are yet not committed */
1599
1600 needed_length = sizeof(SnapBuildOnDisk) +
1601 sizeof(TransactionId) * (builder->committed.xcnt + catchange_xcnt);
1602
1604 ondisk = (SnapBuildOnDisk *) ondisk_c;
1605 ondisk->magic = SNAPBUILD_MAGIC;
1606 ondisk->version = SNAPBUILD_VERSION;
1607 ondisk->length = needed_length;
1608 INIT_CRC32C(ondisk->checksum);
1609 COMP_CRC32C(ondisk->checksum,
1610 ((char *) ondisk) + SnapBuildOnDiskNotChecksummedSize,
1612 ondisk_c += sizeof(SnapBuildOnDisk);
1613
1614 memcpy(&ondisk->builder, builder, sizeof(SnapBuild));
1615 /* NULL-ify memory-only data */
1616 ondisk->builder.context = NULL;
1617 ondisk->builder.snapshot = NULL;
1618 ondisk->builder.reorder = NULL;
1619 ondisk->builder.committed.xip = NULL;
1620 ondisk->builder.catchange.xip = NULL;
1621 /* update catchange only on disk data */
1623
1624 COMP_CRC32C(ondisk->checksum,
1625 &ondisk->builder,
1626 sizeof(SnapBuild));
1627
1628 /* copy committed xacts */
1629 if (builder->committed.xcnt > 0)
1630 {
1631 sz = sizeof(TransactionId) * builder->committed.xcnt;
1632 memcpy(ondisk_c, builder->committed.xip, sz);
1633 COMP_CRC32C(ondisk->checksum, ondisk_c, sz);
1634 ondisk_c += sz;
1635 }
1636
1637 /* copy catalog modifying xacts */
1638 if (catchange_xcnt > 0)
1639 {
1640 sz = sizeof(TransactionId) * catchange_xcnt;
1642 COMP_CRC32C(ondisk->checksum, ondisk_c, sz);
1643 ondisk_c += sz;
1644 }
1645
1646 FIN_CRC32C(ondisk->checksum);
1647
1648 /* we have valid data now, open tempfile and write it there */
1651 if (fd < 0)
1652 ereport(ERROR,
1654 errmsg("could not open file \"%s\": %m", tmppath)));
1655
1656 errno = 0;
1658 if ((write(fd, ondisk, needed_length)) != needed_length)
1659 {
1660 int save_errno = errno;
1661
1663
1664 /* if write didn't set errno, assume problem is no disk space */
1666 ereport(ERROR,
1668 errmsg("could not write to file \"%s\": %m", tmppath)));
1669 }
1671
1672 /*
1673 * fsync the file before renaming so that even if we crash after this we
1674 * have either a fully valid file or nothing.
1675 *
1676 * It's safe to just ERROR on fsync() here because we'll retry the whole
1677 * operation including the writes.
1678 *
1679 * TODO: Do the fsync() via checkpoints/restartpoints, doing it here has
1680 * some noticeable overhead since it's performed synchronously during
1681 * decoding?
1682 */
1684 if (pg_fsync(fd) != 0)
1685 {
1686 int save_errno = errno;
1687
1689 errno = save_errno;
1690 ereport(ERROR,
1692 errmsg("could not fsync file \"%s\": %m", tmppath)));
1693 }
1695
1696 if (CloseTransientFile(fd) != 0)
1697 ereport(ERROR,
1699 errmsg("could not close file \"%s\": %m", tmppath)));
1700
1702
1703 /*
1704 * We may overwrite the work from some other backend, but that's ok, our
1705 * snapshot is valid as well, we'll just have done some superfluous work.
1706 */
1707 if (rename(tmppath, path) != 0)
1708 {
1709 ereport(ERROR,
1711 errmsg("could not rename file \"%s\" to \"%s\": %m",
1712 tmppath, path)));
1713 }
1714
1715 /* make sure we persist */
1716 fsync_fname(path, false);
1718
1719 /*
1720 * Now there's no way we can lose the dumped state anymore, remember this
1721 * as a serialization point.
1722 */
1723 builder->last_serialized_snapshot = lsn;
1724
1726
1727out:
1729 builder->last_serialized_snapshot);
1730 /* be tidy */
1731 if (ondisk)
1732 pfree(ondisk);
1733 if (catchange_xip)
1735}
1736
1737/*
1738 * Restore the logical snapshot file contents to 'ondisk'.
1739 *
1740 * 'context' is the memory context where the catalog modifying/committed xid
1741 * will live.
1742 * If 'missing_ok' is true, will not throw an error if the file is not found.
1743 */
1744bool
1746 MemoryContext context, bool missing_ok)
1747{
1748 int fd;
1749 pg_crc32c checksum;
1750 Size sz;
1751 char path[MAXPGPATH];
1752
1753 sprintf(path, "%s/%X-%X.snap",
1755 LSN_FORMAT_ARGS(lsn));
1756
1758
1759 if (fd < 0)
1760 {
1761 if (missing_ok && errno == ENOENT)
1762 return false;
1763
1764 ereport(ERROR,
1766 errmsg("could not open file \"%s\": %m", path)));
1767 }
1768
1769 /* ----
1770 * Make sure the snapshot had been stored safely to disk, that's normally
1771 * cheap.
1772 * Note that we do not need PANIC here, nobody will be able to use the
1773 * slot without fsyncing, and saving it won't succeed without an fsync()
1774 * either...
1775 * ----
1776 */
1777 fsync_fname(path, false);
1779
1780 /* read statically sized portion of snapshot */
1782
1783 if (ondisk->magic != SNAPBUILD_MAGIC)
1784 ereport(ERROR,
1786 errmsg("snapbuild state file \"%s\" has wrong magic number: %u instead of %u",
1787 path, ondisk->magic, SNAPBUILD_MAGIC)));
1788
1789 if (ondisk->version != SNAPBUILD_VERSION)
1790 ereport(ERROR,
1792 errmsg("snapbuild state file \"%s\" has unsupported version: %u instead of %u",
1793 path, ondisk->version, SNAPBUILD_VERSION)));
1794
1795 INIT_CRC32C(checksum);
1796 COMP_CRC32C(checksum,
1797 ((char *) ondisk) + SnapBuildOnDiskNotChecksummedSize,
1799
1800 /* read SnapBuild */
1801 SnapBuildRestoreContents(fd, &ondisk->builder, sizeof(SnapBuild), path);
1802 COMP_CRC32C(checksum, &ondisk->builder, sizeof(SnapBuild));
1803
1804 /* restore committed xacts information */
1805 if (ondisk->builder.committed.xcnt > 0)
1806 {
1807 sz = sizeof(TransactionId) * ondisk->builder.committed.xcnt;
1808 ondisk->builder.committed.xip = MemoryContextAllocZero(context, sz);
1810 COMP_CRC32C(checksum, ondisk->builder.committed.xip, sz);
1811 }
1812
1813 /* restore catalog modifying xacts information */
1814 if (ondisk->builder.catchange.xcnt > 0)
1815 {
1816 sz = sizeof(TransactionId) * ondisk->builder.catchange.xcnt;
1817 ondisk->builder.catchange.xip = MemoryContextAllocZero(context, sz);
1819 COMP_CRC32C(checksum, ondisk->builder.catchange.xip, sz);
1820 }
1821
1822 if (CloseTransientFile(fd) != 0)
1823 ereport(ERROR,
1825 errmsg("could not close file \"%s\": %m", path)));
1826
1827 FIN_CRC32C(checksum);
1828
1829 /* verify checksum of what we've read */
1830 if (!EQ_CRC32C(checksum, ondisk->checksum))
1831 ereport(ERROR,
1833 errmsg("checksum mismatch for snapbuild state file \"%s\": is %u, should be %u",
1834 path, checksum, ondisk->checksum)));
1835
1836 return true;
1837}
1838
1839/*
1840 * Restore a snapshot into 'builder' if previously one has been stored at the
1841 * location indicated by 'lsn'. Returns true if successful, false otherwise.
1842 */
1843static bool
1845{
1846 SnapBuildOnDisk ondisk;
1847
1848 /* no point in loading a snapshot if we're already there */
1849 if (builder->state == SNAPBUILD_CONSISTENT)
1850 return false;
1851
1852 /* validate and restore the snapshot to 'ondisk' */
1853 if (!SnapBuildRestoreSnapshot(&ondisk, lsn, builder->context, true))
1854 return false;
1855
1856 /*
1857 * ok, we now have a sensible snapshot here, figure out if it has more
1858 * information than we have.
1859 */
1860
1861 /*
1862 * We are only interested in consistent snapshots for now, comparing
1863 * whether one incomplete snapshot is more "advanced" seems to be
1864 * unnecessarily complex.
1865 */
1866 if (ondisk.builder.state < SNAPBUILD_CONSISTENT)
1868
1869 /*
1870 * Don't use a snapshot that requires an xmin that we cannot guarantee to
1871 * be available.
1872 */
1875
1876 /*
1877 * Consistent snapshots have no next phase. Reset next_phase_at as it is
1878 * possible that an old value may remain.
1879 */
1882
1883 /* ok, we think the snapshot is sensible, copy over everything important */
1884 builder->xmin = ondisk.builder.xmin;
1885 builder->xmax = ondisk.builder.xmax;
1886 builder->state = ondisk.builder.state;
1887
1888 builder->committed.xcnt = ondisk.builder.committed.xcnt;
1889 /* We only allocated/stored xcnt, not xcnt_space xids ! */
1890 /* don't overwrite preallocated xip, if we don't have anything here */
1891 if (builder->committed.xcnt > 0)
1892 {
1893 pfree(builder->committed.xip);
1894 builder->committed.xcnt_space = ondisk.builder.committed.xcnt;
1895 builder->committed.xip = ondisk.builder.committed.xip;
1896 }
1897 ondisk.builder.committed.xip = NULL;
1898
1899 /* set catalog modifying transactions */
1900 if (builder->catchange.xip)
1901 pfree(builder->catchange.xip);
1902 builder->catchange.xcnt = ondisk.builder.catchange.xcnt;
1903 builder->catchange.xip = ondisk.builder.catchange.xip;
1904 ondisk.builder.catchange.xip = NULL;
1905
1906 /* our snapshot is not interesting anymore, build a new one */
1907 if (builder->snapshot != NULL)
1908 {
1910 }
1911 builder->snapshot = SnapBuildBuildSnapshot(builder);
1913
1915
1916 Assert(builder->state == SNAPBUILD_CONSISTENT);
1917
1918 ereport(LOG,
1919 errmsg("logical decoding found consistent point at %X/%08X",
1920 LSN_FORMAT_ARGS(lsn)),
1921 errdetail("Logical decoding will begin using saved snapshot."));
1922 return true;
1923
1925 if (ondisk.builder.committed.xip != NULL)
1926 pfree(ondisk.builder.committed.xip);
1927 if (ondisk.builder.catchange.xip != NULL)
1928 pfree(ondisk.builder.catchange.xip);
1929 return false;
1930}
1931
1932/*
1933 * Read the contents of the serialized snapshot to 'dest'.
1934 */
1935static void
1936SnapBuildRestoreContents(int fd, void *dest, Size size, const char *path)
1937{
1938 int readBytes;
1939
1941 readBytes = read(fd, dest, size);
1943 if (readBytes != size)
1944 {
1945 int save_errno = errno;
1946
1948
1949 if (readBytes < 0)
1950 {
1951 errno = save_errno;
1952 ereport(ERROR,
1954 errmsg("could not read file \"%s\": %m", path)));
1955 }
1956 else
1957 ereport(ERROR,
1959 errmsg("could not read file \"%s\": read %d of %zu",
1960 path, readBytes, size)));
1961 }
1962}
1963
1964/*
1965 * Remove all serialized snapshots that are not required anymore because no
1966 * slot can need them. This doesn't actually have to run during a checkpoint,
1967 * but it's a convenient point to schedule this.
1968 *
1969 * NB: We run this during checkpoints even if logical decoding is disabled so
1970 * we cleanup old slots at some point after it got disabled.
1971 */
1972void
1974{
1975 XLogRecPtr cutoff;
1976 XLogRecPtr redo;
1977 DIR *snap_dir;
1978 struct dirent *snap_de;
1979 char path[MAXPGPATH + sizeof(PG_LOGICAL_SNAPSHOTS_DIR)];
1980
1981 /*
1982 * We start off with a minimum of the last redo pointer. No new
1983 * replication slot will start before that, so that's a safe upper bound
1984 * for removal.
1985 */
1986 redo = GetRedoRecPtr();
1987
1988 /* now check for the restart ptrs from existing slots */
1990
1991 /* don't start earlier than the restart lsn */
1992 if (redo < cutoff)
1993 cutoff = redo;
1994
1997 {
1998 uint32 hi;
1999 uint32 lo;
2000 XLogRecPtr lsn;
2002
2003 if (strcmp(snap_de->d_name, ".") == 0 ||
2004 strcmp(snap_de->d_name, "..") == 0)
2005 continue;
2006
2007 snprintf(path, sizeof(path), "%s/%s", PG_LOGICAL_SNAPSHOTS_DIR, snap_de->d_name);
2008 de_type = get_dirent_type(path, snap_de, false, DEBUG1);
2009
2011 {
2012 elog(DEBUG1, "only regular files expected: %s", path);
2013 continue;
2014 }
2015
2016 /*
2017 * temporary filenames from SnapBuildSerialize() include the LSN and
2018 * everything but are postfixed by .$pid.tmp. We can just remove them
2019 * the same as other files because there can be none that are
2020 * currently being written that are older than cutoff.
2021 *
2022 * We just log a message if a file doesn't fit the pattern, it's
2023 * probably some editors lock/state file or similar...
2024 */
2025 if (sscanf(snap_de->d_name, "%X-%X.snap", &hi, &lo) != 2)
2026 {
2027 ereport(LOG,
2028 (errmsg("could not parse file name \"%s\"", path)));
2029 continue;
2030 }
2031
2032 lsn = ((uint64) hi) << 32 | lo;
2033
2034 /* check whether we still need it */
2035 if (lsn < cutoff || !XLogRecPtrIsValid(cutoff))
2036 {
2037 elog(DEBUG1, "removing snapbuild snapshot %s", path);
2038
2039 /*
2040 * It's not particularly harmful, though strange, if we can't
2041 * remove the file here. Don't prevent the checkpoint from
2042 * completing, that'd be a cure worse than the disease.
2043 */
2044 if (unlink(path) < 0)
2045 {
2046 ereport(LOG,
2048 errmsg("could not remove file \"%s\": %m",
2049 path)));
2050 continue;
2051 }
2052 }
2053 }
2055}
2056
2057/*
2058 * Check if a logical snapshot at the specified point has been serialized.
2059 */
2060bool
2062{
2063 char path[MAXPGPATH];
2064 int ret;
2065 struct stat stat_buf;
2066
2067 sprintf(path, "%s/%X-%X.snap",
2069 LSN_FORMAT_ARGS(lsn));
2070
2071 ret = stat(path, &stat_buf);
2072
2073 if (ret != 0 && errno != ENOENT)
2074 ereport(ERROR,
2076 errmsg("could not stat file \"%s\": %m", path)));
2077
2078 return ret == 0;
2079}
#define Assert(condition)
Definition c.h:927
#define PG_BINARY
Definition c.h:1358
uint64_t uint64
Definition c.h:601
uint32_t uint32
Definition c.h:600
uint32 TransactionId
Definition c.h:720
size_t Size
Definition c.h:673
int errcode_for_file_access(void)
Definition elog.c:897
int errcode(int sqlerrcode)
Definition elog.c:874
#define LOG
Definition elog.h:31
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define DEBUG1
Definition elog.h:30
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
int FreeDir(DIR *dir)
Definition fd.c:3009
int CloseTransientFile(int fd)
Definition fd.c:2855
void fsync_fname(const char *fname, bool isdir)
Definition fd.c:757
DIR * AllocateDir(const char *dirname)
Definition fd.c:2891
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition fd.c:2957
int pg_fsync(int fd)
Definition fd.c:390
int OpenTransientFile(const char *fileName, int fileFlags)
Definition fd.c:2678
PGFileType get_dirent_type(const char *path, const struct dirent *de, bool look_through_symlinks, int elevel)
Definition file_utils.c:547
PGFileType
Definition file_utils.h:19
@ PGFILETYPE_REG
Definition file_utils.h:22
@ PGFILETYPE_ERROR
Definition file_utils.h:20
int MyProcPid
Definition globals.c:47
static uint32 dclist_count(const dclist_head *head)
Definition ilist.h:932
#define write(a, b, c)
Definition win32.h:14
#define read(a, b, c)
Definition win32.h:13
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition mcxt.c:1266
void pfree(void *pointer)
Definition mcxt.c:1616
void * palloc0(Size size)
Definition mcxt.c:1417
static char * errmsg
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
#define ERRCODE_DATA_CORRUPTED
#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
#define snprintf
Definition port.h:260
static int fd(const char *x, int i)
static int fb(int x)
TransactionId * ReorderBufferGetCatalogChangesXacts(ReorderBuffer *rb)
void ReorderBufferSetRestartPoint(ReorderBuffer *rb, XLogRecPtr ptr)
#define PG_LOGICAL_SNAPSHOTS_DIR
XLogRecPtr ReplicationSlotsComputeLogicalRestartLSN(void)
Definition slot.c:1371
static void SnapBuildSerialize(SnapBuild *builder, XLogRecPtr lsn)
Definition snapbuild.c:1500
void SnapBuildSnapDecRefcount(Snapshot snap)
Definition snapbuild.c:331
bool SnapBuildRestoreSnapshot(SnapBuildOnDisk *ondisk, XLogRecPtr lsn, MemoryContext context, bool missing_ok)
Definition snapbuild.c:1745
#define SNAPBUILD_VERSION
Definition snapbuild.c:1478
static void SnapBuildSnapIncRefcount(Snapshot snap)
Definition snapbuild.c:319
#define SnapBuildOnDiskNotChecksummedSize
Definition snapbuild.c:1474
bool SnapBuildSnapshotExists(XLogRecPtr lsn)
Definition snapbuild.c:2061
void CheckPointSnapBuild(void)
Definition snapbuild.c:1973
#define SNAPBUILD_MAGIC
Definition snapbuild.c:1477
static Snapshot SnapBuildBuildSnapshot(SnapBuild *builder)
Definition snapbuild.c:363
#define SnapBuildOnDiskConstantSize
Definition snapbuild.c:1472
static void SnapBuildRestoreContents(int fd, void *dest, Size size, const char *path)
Definition snapbuild.c:1936
static bool SnapBuildRestore(SnapBuild *builder, XLogRecPtr lsn)
Definition snapbuild.c:1844
@ SNAPBUILD_CONSISTENT
Definition snapbuild.h:50
Definition dirent.c:26
dclist_head catchange_txns
SnapBuildState state
TransactionId xmin
TransactionId initial_xmin_horizon
TransactionId xmax
TransactionId * xip
Snapshot snapshot
struct SnapBuild::@122 catchange
struct SnapBuild::@119 committed
TransactionId next_phase_at
XLogRecPtr last_serialized_snapshot
MemoryContext context
ReorderBuffer * reorder
#define InvalidTransactionId
Definition transam.h:31
static bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition transam.h:263
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition wait_event.h:69
static void pgstat_report_wait_end(void)
Definition wait_event.h:85
#define stat
Definition win32_port.h:74
XLogRecPtr GetRedoRecPtr(void)
Definition xlog.c:6547
#define XLogRecPtrIsValid(r)
Definition xlogdefs.h:29
#define LSN_FORMAT_ARGS(lsn)
Definition xlogdefs.h:47
uint64 XLogRecPtr
Definition xlogdefs.h:21

◆ SnapBuildOnDiskNotChecksummedSize

#define SnapBuildOnDiskNotChecksummedSize    offsetof(SnapBuildOnDisk, version)

Definition at line 1474 of file snapbuild.c.

Function Documentation

◆ AllocateSnapshotBuilder()

SnapBuild * AllocateSnapshotBuilder ( ReorderBuffer reorder,
TransactionId  xmin_horizon,
XLogRecPtr  start_lsn,
bool  need_full_snapshot,
bool  in_slot_creation,
XLogRecPtr  two_phase_at 
)

Definition at line 188 of file snapbuild.c.

194{
195 MemoryContext context;
196 MemoryContext oldcontext;
197 SnapBuild *builder;
198
199 /* allocate memory in own context, to have better accountability */
201 "snapshot builder context",
203 oldcontext = MemoryContextSwitchTo(context);
204
205 builder = palloc0_object(SnapBuild);
206
207 builder->state = SNAPBUILD_START;
208 builder->context = context;
209 builder->reorder = reorder;
210 /* Other struct members initialized by zeroing via palloc0 above */
211
212 builder->committed.xcnt = 0;
213 builder->committed.xcnt_space = 128; /* arbitrary number */
214 builder->committed.xip =
216 builder->committed.includes_all_transactions = true;
217
218 builder->catchange.xcnt = 0;
219 builder->catchange.xip = NULL;
220
222 builder->start_decoding_at = start_lsn;
223 builder->in_slot_creation = in_slot_creation;
225 builder->two_phase_at = two_phase_at;
226
227 MemoryContextSwitchTo(oldcontext);
228
229 return builder;
230}
#define palloc0_array(type, count)
Definition fe_memutils.h:77
#define palloc0_object(type)
Definition fe_memutils.h:75
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
@ SNAPBUILD_START
Definition snapbuild.h:27
XLogRecPtr start_decoding_at
XLogRecPtr two_phase_at
bool building_full_snapshot
bool includes_all_transactions

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, SnapBuild::building_full_snapshot, SnapBuild::catchange, SnapBuild::committed, SnapBuild::context, CurrentMemoryContext, fb(), SnapBuild::in_slot_creation, SnapBuild::includes_all_transactions, SnapBuild::initial_xmin_horizon, MemoryContextSwitchTo(), palloc0_array, palloc0_object, SnapBuild::reorder, SNAPBUILD_START, SnapBuild::start_decoding_at, SnapBuild::state, SnapBuild::two_phase_at, SnapBuild::xcnt, SnapBuild::xcnt_space, and SnapBuild::xip.

Referenced by StartupDecodingContext().

◆ CheckPointSnapBuild()

void CheckPointSnapBuild ( void  )

Definition at line 1973 of file snapbuild.c.

1974{
1975 XLogRecPtr cutoff;
1976 XLogRecPtr redo;
1977 DIR *snap_dir;
1978 struct dirent *snap_de;
1979 char path[MAXPGPATH + sizeof(PG_LOGICAL_SNAPSHOTS_DIR)];
1980
1981 /*
1982 * We start off with a minimum of the last redo pointer. No new
1983 * replication slot will start before that, so that's a safe upper bound
1984 * for removal.
1985 */
1986 redo = GetRedoRecPtr();
1987
1988 /* now check for the restart ptrs from existing slots */
1990
1991 /* don't start earlier than the restart lsn */
1992 if (redo < cutoff)
1993 cutoff = redo;
1994
1997 {
1998 uint32 hi;
1999 uint32 lo;
2000 XLogRecPtr lsn;
2002
2003 if (strcmp(snap_de->d_name, ".") == 0 ||
2004 strcmp(snap_de->d_name, "..") == 0)
2005 continue;
2006
2007 snprintf(path, sizeof(path), "%s/%s", PG_LOGICAL_SNAPSHOTS_DIR, snap_de->d_name);
2008 de_type = get_dirent_type(path, snap_de, false, DEBUG1);
2009
2011 {
2012 elog(DEBUG1, "only regular files expected: %s", path);
2013 continue;
2014 }
2015
2016 /*
2017 * temporary filenames from SnapBuildSerialize() include the LSN and
2018 * everything but are postfixed by .$pid.tmp. We can just remove them
2019 * the same as other files because there can be none that are
2020 * currently being written that are older than cutoff.
2021 *
2022 * We just log a message if a file doesn't fit the pattern, it's
2023 * probably some editors lock/state file or similar...
2024 */
2025 if (sscanf(snap_de->d_name, "%X-%X.snap", &hi, &lo) != 2)
2026 {
2027 ereport(LOG,
2028 (errmsg("could not parse file name \"%s\"", path)));
2029 continue;
2030 }
2031
2032 lsn = ((uint64) hi) << 32 | lo;
2033
2034 /* check whether we still need it */
2035 if (lsn < cutoff || !XLogRecPtrIsValid(cutoff))
2036 {
2037 elog(DEBUG1, "removing snapbuild snapshot %s", path);
2038
2039 /*
2040 * It's not particularly harmful, though strange, if we can't
2041 * remove the file here. Don't prevent the checkpoint from
2042 * completing, that'd be a cure worse than the disease.
2043 */
2044 if (unlink(path) < 0)
2045 {
2046 ereport(LOG,
2048 errmsg("could not remove file \"%s\": %m",
2049 path)));
2050 continue;
2051 }
2052 }
2053 }
2055}

References AllocateDir(), DEBUG1, elog, ereport, errcode_for_file_access(), errmsg, fb(), FreeDir(), get_dirent_type(), GetRedoRecPtr(), LOG, MAXPGPATH, PG_LOGICAL_SNAPSHOTS_DIR, PGFILETYPE_ERROR, PGFILETYPE_REG, ReadDir(), ReplicationSlotsComputeLogicalRestartLSN(), snprintf, and XLogRecPtrIsValid.

Referenced by CheckPointGuts().

◆ FreeSnapshotBuilder()

void FreeSnapshotBuilder ( SnapBuild builder)

Definition at line 236 of file snapbuild.c.

237{
238 MemoryContext context = builder->context;
239
240 /* free snapshot explicitly, that contains some error checking */
241 if (builder->snapshot != NULL)
242 {
244 builder->snapshot = NULL;
245 }
246
247 /* other resources are deallocated via memory context reset */
248 MemoryContextDelete(context);
249}
void MemoryContextDelete(MemoryContext context)
Definition mcxt.c:472

References SnapBuild::context, fb(), MemoryContextDelete(), SnapBuildSnapDecRefcount(), and SnapBuild::snapshot.

Referenced by FreeDecodingContext().

◆ SnapBuildAddCommittedTxn()

static void SnapBuildAddCommittedTxn ( SnapBuild builder,
TransactionId  xid 
)
static

Definition at line 831 of file snapbuild.c.

832{
834
835 if (builder->committed.xcnt == builder->committed.xcnt_space)
836 {
837 builder->committed.xcnt_space = builder->committed.xcnt_space * 2 + 1;
838
839 elog(DEBUG1, "increasing space for committed transactions to %u",
840 (uint32) builder->committed.xcnt_space);
841
842 builder->committed.xip = repalloc_array(builder->committed.xip,
844 builder->committed.xcnt_space);
845 }
846
847 /*
848 * TODO: It might make sense to keep the array sorted here instead of
849 * doing it every time we build a new snapshot. On the other hand this
850 * gets called repeatedly when a transaction with subtransactions commits.
851 */
852 builder->committed.xip[builder->committed.xcnt++] = xid;
853}
#define repalloc_array(pointer, type, count)
Definition fe_memutils.h:78
#define TransactionIdIsValid(xid)
Definition transam.h:41

References Assert, SnapBuild::committed, DEBUG1, elog, repalloc_array, TransactionIdIsValid, SnapBuild::xcnt, SnapBuild::xcnt_space, and SnapBuild::xip.

Referenced by SnapBuildCommitTxn().

◆ SnapBuildBuildSnapshot()

static Snapshot SnapBuildBuildSnapshot ( SnapBuild builder)
static

Definition at line 363 of file snapbuild.c.

364{
365 Snapshot snapshot;
366 Size ssize;
367
369
370 ssize = sizeof(SnapshotData)
371 + sizeof(TransactionId) * builder->committed.xcnt
372 + sizeof(TransactionId) * 1 /* toplevel xid */ ;
373
374 snapshot = MemoryContextAllocZero(builder->context, ssize);
375
377
378 /*
379 * We misuse the original meaning of SnapshotData's xip and subxip fields
380 * to make the more fitting for our needs.
381 *
382 * In the 'xip' array we store transactions that have to be treated as
383 * committed. Since we will only ever look at tuples from transactions
384 * that have modified the catalog it's more efficient to store those few
385 * that exist between xmin and xmax (frequently there are none).
386 *
387 * Snapshots that are used in transactions that have modified the catalog
388 * also use the 'subxip' array to store their toplevel xid and all the
389 * subtransaction xids so we can recognize when we need to treat rows as
390 * visible that are not in xip but still need to be visible. Subxip only
391 * gets filled when the transaction is copied into the context of a
392 * catalog modifying transaction since we otherwise share a snapshot
393 * between transactions. As long as a txn hasn't modified the catalog it
394 * doesn't need to treat any uncommitted rows as visible, so there is no
395 * need for those xids.
396 *
397 * Both arrays are qsort'ed so that we can use bsearch() on them.
398 */
401
402 snapshot->xmin = builder->xmin;
403 snapshot->xmax = builder->xmax;
404
405 /* store all transactions to be treated as committed by this snapshot */
406 snapshot->xip =
407 (TransactionId *) ((char *) snapshot + sizeof(SnapshotData));
408 snapshot->xcnt = builder->committed.xcnt;
409 memcpy(snapshot->xip,
410 builder->committed.xip,
411 builder->committed.xcnt * sizeof(TransactionId));
412
413 /* sort so we can bsearch() */
414 qsort(snapshot->xip, snapshot->xcnt, sizeof(TransactionId), xidComparator);
415
416 /*
417 * Initially, subxip is empty, i.e. it's a snapshot to be used by
418 * transactions that don't modify the catalog. Will be filled by
419 * ReorderBufferCopySnap() if necessary.
420 */
421 snapshot->subxcnt = 0;
422 snapshot->subxip = NULL;
423
424 snapshot->suboverflowed = false;
425 snapshot->takenDuringRecovery = false;
426 snapshot->copied = false;
427 snapshot->curcid = FirstCommandId;
428 snapshot->active_count = 0;
429 snapshot->regd_count = 0;
430 snapshot->snapXactCompletionCount = 0;
431
432 return snapshot;
433}
#define FirstCommandId
Definition c.h:736
#define qsort(a, b, c, d)
Definition port.h:495
@ SNAPBUILD_FULL_SNAPSHOT
Definition snapbuild.h:43
@ SNAPSHOT_HISTORIC_MVCC
Definition snapshot.h:105
TransactionId xmin
Definition snapshot.h:153
int32 subxcnt
Definition snapshot.h:177
uint32 regd_count
Definition snapshot.h:201
uint32 active_count
Definition snapshot.h:200
CommandId curcid
Definition snapshot.h:183
uint32 xcnt
Definition snapshot.h:165
TransactionId * subxip
Definition snapshot.h:176
uint64 snapXactCompletionCount
Definition snapshot.h:209
TransactionId xmax
Definition snapshot.h:154
SnapshotType snapshot_type
Definition snapshot.h:140
TransactionId * xip
Definition snapshot.h:164
bool suboverflowed
Definition snapshot.h:178
bool takenDuringRecovery
Definition snapshot.h:180
#define TransactionIdIsNormal(xid)
Definition transam.h:42
int xidComparator(const void *arg1, const void *arg2)
Definition xid.c:152

References SnapshotData::active_count, Assert, SnapBuild::committed, SnapBuild::context, SnapshotData::copied, SnapshotData::curcid, fb(), FirstCommandId, MemoryContextAllocZero(), qsort, SnapshotData::regd_count, SNAPBUILD_FULL_SNAPSHOT, SNAPSHOT_HISTORIC_MVCC, SnapshotData::snapshot_type, SnapshotData::snapXactCompletionCount, SnapBuild::state, SnapshotData::suboverflowed, SnapshotData::subxcnt, SnapshotData::subxip, SnapshotData::takenDuringRecovery, TransactionIdIsNormal, SnapBuild::xcnt, SnapshotData::xcnt, xidComparator(), SnapBuild::xip, SnapshotData::xip, SnapBuild::xmax, SnapshotData::xmax, SnapBuild::xmin, and SnapshotData::xmin.

Referenced by SnapBuildCommitTxn(), SnapBuildGetOrBuildSnapshot(), SnapBuildInitialSnapshot(), SnapBuildProcessChange(), and SnapBuildRestore().

◆ SnapBuildClearExportedSnapshot()

void SnapBuildClearExportedSnapshot ( void  )

Definition at line 602 of file snapbuild.c.

603{
605
606 /* nothing exported, that is the usual case */
607 if (!ExportInProgress)
608 return;
609
610 if (!IsTransactionState())
611 elog(ERROR, "clearing exported snapshot in wrong transaction state");
612
613 /*
614 * AbortCurrentTransaction() takes care of resetting the snapshot state,
615 * so remember SavedResourceOwnerDuringExport.
616 */
618
619 /* make sure nothing could have ever happened */
621
623}
ResourceOwner CurrentResourceOwner
Definition resowner.c:173
static ResourceOwner SavedResourceOwnerDuringExport
Definition snapbuild.c:154
static bool ExportInProgress
Definition snapbuild.c:155
bool IsTransactionState(void)
Definition xact.c:389
void AbortCurrentTransaction(void)
Definition xact.c:3473

References AbortCurrentTransaction(), CurrentResourceOwner, elog, ERROR, ExportInProgress, fb(), IsTransactionState(), and SavedResourceOwnerDuringExport.

Referenced by exec_replication_command().

◆ SnapBuildCommitTxn()

void SnapBuildCommitTxn ( SnapBuild builder,
XLogRecPtr  lsn,
TransactionId  xid,
int  nsubxacts,
TransactionId subxacts,
uint32  xinfo 
)

Definition at line 943 of file snapbuild.c.

945{
946 int nxact;
947
948 bool needs_snapshot = false;
949 bool needs_timetravel = false;
950 bool sub_needs_timetravel = false;
951
952 TransactionId xmax = xid;
953
954 /*
955 * Transactions preceding BUILDING_SNAPSHOT will neither be decoded, nor
956 * will they be part of a snapshot. So we don't need to record anything.
957 */
958 if (builder->state == SNAPBUILD_START ||
959 (builder->state == SNAPBUILD_BUILDING_SNAPSHOT &&
960 TransactionIdPrecedes(xid, builder->next_phase_at)))
961 {
962 /* ensure that only commits after this are getting replayed */
963 if (builder->start_decoding_at <= lsn)
964 builder->start_decoding_at = lsn + 1;
965 return;
966 }
967
968 if (builder->state < SNAPBUILD_CONSISTENT)
969 {
970 /* ensure that only commits after this are getting replayed */
971 if (builder->start_decoding_at <= lsn)
972 builder->start_decoding_at = lsn + 1;
973
974 /*
975 * If building an exportable snapshot, force xid to be tracked, even
976 * if the transaction didn't modify the catalog.
977 */
978 if (builder->building_full_snapshot)
979 {
980 needs_timetravel = true;
981 }
982 }
983
984 for (nxact = 0; nxact < nsubxacts; nxact++)
985 {
986 TransactionId subxid = subxacts[nxact];
987
988 /*
989 * Add subtransaction to base snapshot if catalog modifying, we don't
990 * distinguish to toplevel transactions there.
991 */
992 if (SnapBuildXidHasCatalogChanges(builder, subxid, xinfo))
993 {
995 needs_snapshot = true;
996
997 elog(DEBUG1, "found subtransaction %u:%u with catalog changes",
998 xid, subxid);
999
1000 SnapBuildAddCommittedTxn(builder, subxid);
1001
1002 if (NormalTransactionIdFollows(subxid, xmax))
1003 xmax = subxid;
1004 }
1005
1006 /*
1007 * If we're forcing timetravel we also need visibility information
1008 * about subtransaction, so keep track of subtransaction's state, even
1009 * if not catalog modifying. Don't need to distribute a snapshot in
1010 * that case.
1011 */
1012 else if (needs_timetravel)
1013 {
1014 SnapBuildAddCommittedTxn(builder, subxid);
1015 if (NormalTransactionIdFollows(subxid, xmax))
1016 xmax = subxid;
1017 }
1018 }
1019
1020 /* if top-level modified catalog, it'll need a snapshot */
1021 if (SnapBuildXidHasCatalogChanges(builder, xid, xinfo))
1022 {
1023 elog(DEBUG2, "found top level transaction %u, with catalog changes",
1024 xid);
1025 needs_snapshot = true;
1026 needs_timetravel = true;
1027 SnapBuildAddCommittedTxn(builder, xid);
1028 }
1029 else if (sub_needs_timetravel)
1030 {
1031 /* track toplevel txn as well, subxact alone isn't meaningful */
1032 elog(DEBUG2, "forced transaction %u to do timetravel due to one of its subtransactions",
1033 xid);
1034 needs_timetravel = true;
1035 SnapBuildAddCommittedTxn(builder, xid);
1036 }
1037 else if (needs_timetravel)
1038 {
1039 elog(DEBUG2, "forced transaction %u to do timetravel", xid);
1040
1041 SnapBuildAddCommittedTxn(builder, xid);
1042 }
1043
1044 if (!needs_timetravel)
1045 {
1046 /* record that we cannot export a general snapshot anymore */
1047 builder->committed.includes_all_transactions = false;
1048 }
1049
1051
1052 /*
1053 * Adjust xmax of the snapshot builder, we only do that for committed,
1054 * catalog modifying, transactions, everything else isn't interesting for
1055 * us since we'll never look at the respective rows.
1056 */
1057 if (needs_timetravel &&
1058 (!TransactionIdIsValid(builder->xmax) ||
1059 TransactionIdFollowsOrEquals(xmax, builder->xmax)))
1060 {
1061 builder->xmax = xmax;
1062 TransactionIdAdvance(builder->xmax);
1063 }
1064
1065 /* if there's any reason to build a historic snapshot, do so now */
1066 if (needs_snapshot)
1067 {
1068 /*
1069 * If we haven't built a complete snapshot yet there's no need to hand
1070 * it out, it wouldn't (and couldn't) be used anyway.
1071 */
1072 if (builder->state < SNAPBUILD_FULL_SNAPSHOT)
1073 return;
1074
1075 /*
1076 * Decrease the snapshot builder's refcount of the old snapshot, note
1077 * that it still will be used if it has been handed out to the
1078 * reorderbuffer earlier.
1079 */
1080 if (builder->snapshot)
1082
1083 builder->snapshot = SnapBuildBuildSnapshot(builder);
1084
1085 /* we might need to execute invalidations, add snapshot */
1086 if (!ReorderBufferXidHasBaseSnapshot(builder->reorder, xid))
1087 {
1089 ReorderBufferSetBaseSnapshot(builder->reorder, xid, lsn,
1090 builder->snapshot);
1091 }
1092
1093 /* refcount of the snapshot builder for the new snapshot */
1095
1096 /*
1097 * Add a new catalog snapshot and invalidations messages to all
1098 * currently running transactions.
1099 */
1100 SnapBuildDistributeSnapshotAndInval(builder, lsn, xid);
1101 }
1102}
#define DEBUG2
Definition elog.h:29
void ReorderBufferSetBaseSnapshot(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, Snapshot snap)
bool ReorderBufferXidHasBaseSnapshot(ReorderBuffer *rb, TransactionId xid)
static void SnapBuildAddCommittedTxn(SnapBuild *builder, TransactionId xid)
Definition snapbuild.c:831
static bool SnapBuildXidHasCatalogChanges(SnapBuild *builder, TransactionId xid, uint32 xinfo)
Definition snapbuild.c:1109
static void SnapBuildDistributeSnapshotAndInval(SnapBuild *builder, XLogRecPtr lsn, TransactionId xid)
Definition snapbuild.c:733
@ SNAPBUILD_BUILDING_SNAPSHOT
Definition snapbuild.h:33
static bool TransactionIdFollowsOrEquals(TransactionId id1, TransactionId id2)
Definition transam.h:312
#define NormalTransactionIdFollows(id1, id2)
Definition transam.h:152
#define TransactionIdAdvance(dest)
Definition transam.h:91

References Assert, SnapBuild::building_full_snapshot, SnapBuild::committed, DEBUG1, DEBUG2, elog, fb(), SnapBuild::includes_all_transactions, SnapBuild::next_phase_at, NormalTransactionIdFollows, SnapBuild::reorder, ReorderBufferSetBaseSnapshot(), ReorderBufferXidHasBaseSnapshot(), SNAPBUILD_BUILDING_SNAPSHOT, SNAPBUILD_CONSISTENT, SNAPBUILD_FULL_SNAPSHOT, SNAPBUILD_START, SnapBuildAddCommittedTxn(), SnapBuildBuildSnapshot(), SnapBuildDistributeSnapshotAndInval(), SnapBuildSnapDecRefcount(), SnapBuildSnapIncRefcount(), SnapBuildXidHasCatalogChanges(), SnapBuild::snapshot, SnapBuild::start_decoding_at, SnapBuild::state, TransactionIdAdvance, TransactionIdFollowsOrEquals(), TransactionIdIsValid, TransactionIdPrecedes(), and SnapBuild::xmax.

Referenced by DecodeCommit().

◆ SnapBuildCurrentState()

SnapBuildState SnapBuildCurrentState ( SnapBuild builder)

◆ SnapBuildDistributeSnapshotAndInval()

static void SnapBuildDistributeSnapshotAndInval ( SnapBuild builder,
XLogRecPtr  lsn,
TransactionId  xid 
)
static

Definition at line 733 of file snapbuild.c.

734{
736 ReorderBufferTXN *txn;
737
738 /*
739 * Iterate through all toplevel transactions. This can include
740 * subtransactions which we just don't yet know to be that, but that's
741 * fine, they will just get an unnecessary snapshot and invalidations
742 * queued.
743 */
745 {
746 txn = dlist_container(ReorderBufferTXN, node, txn_i.cur);
747
749
750 /*
751 * If we don't have a base snapshot yet, there are no changes in this
752 * transaction which in turn implies we don't yet need a snapshot at
753 * all. We'll add a snapshot when the first change gets queued.
754 *
755 * Similarly, we don't need to add invalidations to a transaction
756 * whose base snapshot is not yet set. Once a base snapshot is built,
757 * it will include the xids of committed transactions that have
758 * modified the catalog, thus reflecting the new catalog contents. The
759 * existing catalog cache will have already been invalidated after
760 * processing the invalidations in the transaction that modified
761 * catalogs, ensuring that a fresh cache is constructed during
762 * decoding.
763 *
764 * NB: This works correctly even for subtransactions because
765 * ReorderBufferAssignChild() takes care to transfer the base snapshot
766 * to the top-level transaction, and while iterating the changequeue
767 * we'll get the change from the subtxn.
768 */
769 if (!ReorderBufferXidHasBaseSnapshot(builder->reorder, txn->xid))
770 continue;
771
772 /*
773 * We don't need to add snapshot or invalidations to prepared
774 * transactions as they should not see the new catalog contents.
775 */
776 if (rbtxn_is_prepared(txn))
777 continue;
778
779 elog(DEBUG2, "adding a new snapshot and invalidations to %u at %X/%08X",
780 txn->xid, LSN_FORMAT_ARGS(lsn));
781
782 /*
783 * increase the snapshot's refcount for the transaction we are handing
784 * it out to
785 */
787 ReorderBufferAddSnapshot(builder->reorder, txn->xid, lsn,
788 builder->snapshot);
789
790 /*
791 * Add invalidation messages to the reorder buffer of in-progress
792 * transactions except the current committed transaction, for which we
793 * will execute invalidations at the end.
794 *
795 * It is required, otherwise, we will end up using the stale catcache
796 * contents built by the current transaction even after its decoding,
797 * which should have been invalidated due to concurrent catalog
798 * changing transaction.
799 *
800 * Distribute only the invalidation messages generated by the current
801 * committed transaction. Invalidation messages received from other
802 * transactions would have already been propagated to the relevant
803 * in-progress transactions. This transaction would have processed
804 * those invalidations, ensuring that subsequent transactions observe
805 * a consistent cache state.
806 */
807 if (txn->xid != xid)
808 {
809 uint32 ninvalidations;
811
812 ninvalidations = ReorderBufferGetInvalidations(builder->reorder,
813 xid, &msgs);
814
815 if (ninvalidations > 0)
816 {
817 Assert(msgs != NULL);
818
820 txn->xid, lsn,
821 ninvalidations, msgs);
822 }
823 }
824 }
825}
#define dlist_foreach(iter, lhead)
Definition ilist.h:623
#define dlist_container(type, membername, ptr)
Definition ilist.h:593
uint32 ReorderBufferGetInvalidations(ReorderBuffer *rb, TransactionId xid, SharedInvalidationMessage **msgs)
void ReorderBufferAddDistributedInvalidations(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, Size nmsgs, SharedInvalidationMessage *msgs)
void ReorderBufferAddSnapshot(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, Snapshot snap)
#define rbtxn_is_prepared(txn)
TransactionId xid
dlist_head toplevel_by_lsn

References Assert, DEBUG2, dlist_container, dlist_foreach, elog, fb(), LSN_FORMAT_ARGS, rbtxn_is_prepared, SnapBuild::reorder, ReorderBufferAddDistributedInvalidations(), ReorderBufferAddSnapshot(), ReorderBufferGetInvalidations(), ReorderBufferXidHasBaseSnapshot(), SnapBuildSnapIncRefcount(), SnapBuild::snapshot, ReorderBuffer::toplevel_by_lsn, TransactionIdIsValid, and ReorderBufferTXN::xid.

Referenced by SnapBuildCommitTxn().

◆ SnapBuildExportSnapshot()

const char * SnapBuildExportSnapshot ( SnapBuild builder)

Definition at line 541 of file snapbuild.c.

542{
544 char *snapname;
545
547 elog(ERROR, "cannot export a snapshot from within a transaction");
548
550 elog(ERROR, "can only export one snapshot at a time");
551
553 ExportInProgress = true;
554
556
557 /* There doesn't seem to a nice API to set these */
559 XactReadOnly = true;
560
562
563 /*
564 * now that we've built a plain snapshot, make it active and use the
565 * normal mechanisms for exporting it
566 */
568
569 ereport(LOG,
570 (errmsg_plural("exported logical decoding snapshot: \"%s\" with %u transaction ID",
571 "exported logical decoding snapshot: \"%s\" with %u transaction IDs",
572 snap->xcnt,
573 snapname, snap->xcnt)));
574 return snapname;
575}
int int int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...) pg_attribute_printf(1
Snapshot SnapBuildInitialSnapshot(SnapBuild *builder)
Definition snapbuild.c:443
char * ExportSnapshot(Snapshot snapshot)
Definition snapmgr.c:1115
bool IsTransactionOrTransactionBlock(void)
Definition xact.c:5012
bool XactReadOnly
Definition xact.c:84
void StartTransactionCommand(void)
Definition xact.c:3081
int XactIsoLevel
Definition xact.c:81
#define XACT_REPEATABLE_READ
Definition xact.h:38

References CurrentResourceOwner, elog, ereport, errmsg_plural(), ERROR, ExportInProgress, ExportSnapshot(), fb(), IsTransactionOrTransactionBlock(), LOG, SavedResourceOwnerDuringExport, SnapBuildInitialSnapshot(), StartTransactionCommand(), XACT_REPEATABLE_READ, XactIsoLevel, and XactReadOnly.

Referenced by CreateReplicationSlot().

◆ SnapBuildFindSnapshot()

static bool SnapBuildFindSnapshot ( SnapBuild builder,
XLogRecPtr  lsn,
xl_running_xacts running 
)
static

Definition at line 1241 of file snapbuild.c.

1242{
1243 /* ---
1244 * Build catalog decoding snapshot incrementally using information about
1245 * the currently running transactions. There are several ways to do that:
1246 *
1247 * a) There were no running transactions when the xl_running_xacts record
1248 * was inserted, jump to CONSISTENT immediately. We might find such a
1249 * state while waiting on c)'s sub-states.
1250 *
1251 * b) This (in a previous run) or another decoding slot serialized a
1252 * snapshot to disk that we can use. Can't use this method while finding
1253 * the start point for decoding changes as the restart LSN would be an
1254 * arbitrary LSN but we need to find the start point to extract changes
1255 * where we won't see the data for partial transactions. Also, we cannot
1256 * use this method when a slot needs a full snapshot for export or direct
1257 * use, as that snapshot will only contain catalog modifying transactions.
1258 *
1259 * c) First incrementally build a snapshot for catalog tuples
1260 * (BUILDING_SNAPSHOT), that requires all, already in-progress,
1261 * transactions to finish. Every transaction starting after that
1262 * (FULL_SNAPSHOT state), has enough information to be decoded. But
1263 * for older running transactions no viable snapshot exists yet, so
1264 * CONSISTENT will only be reached once all of those have finished.
1265 * ---
1266 */
1267
1268 /*
1269 * xl_running_xacts record is older than what we can use, we might not
1270 * have all necessary catalog rows anymore.
1271 */
1274 builder->initial_xmin_horizon))
1275 {
1277 errmsg_internal("skipping snapshot at %X/%08X while building logical decoding snapshot, xmin horizon too low",
1278 LSN_FORMAT_ARGS(lsn)),
1279 errdetail_internal("initial xmin horizon of %u vs the snapshot's %u",
1280 builder->initial_xmin_horizon, running->oldestRunningXid));
1281
1282
1284
1285 return true;
1286 }
1287
1288 /*
1289 * a) No transaction were running, we can jump to consistent.
1290 *
1291 * This is not affected by races around xl_running_xacts, because we can
1292 * miss transaction commits, but currently not transactions starting.
1293 *
1294 * NB: We might have already started to incrementally assemble a snapshot,
1295 * so we need to be careful to deal with that.
1296 */
1297 if (running->oldestRunningXid == running->nextXid)
1298 {
1299 if (!XLogRecPtrIsValid(builder->start_decoding_at) ||
1300 builder->start_decoding_at <= lsn)
1301 /* can decode everything after this */
1302 builder->start_decoding_at = lsn + 1;
1303
1304 /* As no transactions were running xmin/xmax can be trivially set. */
1305 builder->xmin = running->nextXid; /* < are finished */
1306 builder->xmax = running->nextXid; /* >= are running */
1307
1308 /* so we can safely use the faster comparisons */
1311
1312 builder->state = SNAPBUILD_CONSISTENT;
1314
1315 ereport(LOG,
1316 errmsg("logical decoding found consistent point at %X/%08X",
1317 LSN_FORMAT_ARGS(lsn)),
1318 errdetail("There are no running transactions."));
1319
1320 return false;
1321 }
1322
1323 /*
1324 * b) valid on disk state and while neither building full snapshot nor
1325 * creating a slot.
1326 */
1327 else if (!builder->building_full_snapshot &&
1328 !builder->in_slot_creation &&
1329 SnapBuildRestore(builder, lsn))
1330 {
1331 /* there won't be any state to cleanup */
1332 return false;
1333 }
1334
1335 /*
1336 * c) transition from START to BUILDING_SNAPSHOT.
1337 *
1338 * In START state, and a xl_running_xacts record with running xacts is
1339 * encountered. In that case, switch to BUILDING_SNAPSHOT state, and
1340 * record xl_running_xacts->nextXid. Once all running xacts have finished
1341 * (i.e. they're all >= nextXid), we have a complete catalog snapshot. It
1342 * might look that we could use xl_running_xacts's ->xids information to
1343 * get there quicker, but that is problematic because transactions marked
1344 * as running, might already have inserted their commit record - it's
1345 * infeasible to change that with locking.
1346 */
1347 else if (builder->state == SNAPBUILD_START)
1348 {
1350 builder->next_phase_at = running->nextXid;
1351
1352 /*
1353 * Start with an xmin/xmax that's correct for future, when all the
1354 * currently running transactions have finished. We'll update both
1355 * while waiting for the pending transactions to finish.
1356 */
1357 builder->xmin = running->nextXid; /* < are finished */
1358 builder->xmax = running->nextXid; /* >= are running */
1359
1360 /* so we can safely use the faster comparisons */
1363
1364 ereport(LOG,
1365 errmsg("logical decoding found initial starting point at %X/%08X",
1366 LSN_FORMAT_ARGS(lsn)),
1367 errdetail("Waiting for transactions (approximately %d) older than %u to end.",
1368 running->xcnt, running->nextXid));
1369
1370 SnapBuildWaitSnapshot(running, running->nextXid);
1371 }
1372
1373 /*
1374 * c) transition from BUILDING_SNAPSHOT to FULL_SNAPSHOT.
1375 *
1376 * In BUILDING_SNAPSHOT state, and this xl_running_xacts' oldestRunningXid
1377 * is >= than nextXid from when we switched to BUILDING_SNAPSHOT. This
1378 * means all transactions starting afterwards have enough information to
1379 * be decoded. Switch to FULL_SNAPSHOT.
1380 */
1381 else if (builder->state == SNAPBUILD_BUILDING_SNAPSHOT &&
1383 running->oldestRunningXid))
1384 {
1385 builder->state = SNAPBUILD_FULL_SNAPSHOT;
1386 builder->next_phase_at = running->nextXid;
1387
1388 ereport(LOG,
1389 errmsg("logical decoding found initial consistent point at %X/%08X",
1390 LSN_FORMAT_ARGS(lsn)),
1391 errdetail("Waiting for transactions (approximately %d) older than %u to end.",
1392 running->xcnt, running->nextXid));
1393
1394 SnapBuildWaitSnapshot(running, running->nextXid);
1395 }
1396
1397 /*
1398 * c) transition from FULL_SNAPSHOT to CONSISTENT.
1399 *
1400 * In FULL_SNAPSHOT state, and this xl_running_xacts' oldestRunningXid is
1401 * >= than nextXid from when we switched to FULL_SNAPSHOT. This means all
1402 * transactions that are currently in progress have a catalog snapshot,
1403 * and all their changes have been collected. Switch to CONSISTENT.
1404 */
1405 else if (builder->state == SNAPBUILD_FULL_SNAPSHOT &&
1407 running->oldestRunningXid))
1408 {
1409 builder->state = SNAPBUILD_CONSISTENT;
1411
1412 ereport(LOG,
1413 errmsg("logical decoding found consistent point at %X/%08X",
1414 LSN_FORMAT_ARGS(lsn)),
1415 errdetail("There are no old transactions anymore."));
1416 }
1417
1418 /*
1419 * We already started to track running xacts and need to wait for all
1420 * in-progress ones to finish. We fall through to the normal processing of
1421 * records so incremental cleanup can be performed.
1422 */
1423 return true;
1424}
int int errdetail_internal(const char *fmt,...) pg_attribute_printf(1
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
static void SnapBuildWaitSnapshot(xl_running_xacts *running, TransactionId cutoff)
Definition snapbuild.c:1438
TransactionId oldestRunningXid
Definition standbydefs.h:53
TransactionId nextXid
Definition standbydefs.h:52
static bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition transam.h:282
#define NormalTransactionIdPrecedes(id1, id2)
Definition transam.h:147

References Assert, SnapBuild::building_full_snapshot, DEBUG1, ereport, errdetail(), errdetail_internal(), errmsg, errmsg_internal(), SnapBuild::in_slot_creation, SnapBuild::initial_xmin_horizon, InvalidTransactionId, LOG, LSN_FORMAT_ARGS, SnapBuild::next_phase_at, xl_running_xacts::nextXid, NormalTransactionIdPrecedes, xl_running_xacts::oldestRunningXid, SNAPBUILD_BUILDING_SNAPSHOT, SNAPBUILD_CONSISTENT, SNAPBUILD_FULL_SNAPSHOT, SNAPBUILD_START, SnapBuildRestore(), SnapBuildWaitSnapshot(), SnapBuild::start_decoding_at, SnapBuild::state, TransactionIdIsNormal, TransactionIdPrecedesOrEquals(), xl_running_xacts::xcnt, XLogRecPtrIsValid, SnapBuild::xmax, and SnapBuild::xmin.

Referenced by SnapBuildProcessRunningXacts().

◆ SnapBuildFreeSnapshot()

static void SnapBuildFreeSnapshot ( Snapshot  snap)
static

Definition at line 255 of file snapbuild.c.

256{
257 /* make sure we don't get passed an external snapshot */
258 Assert(snap->snapshot_type == SNAPSHOT_HISTORIC_MVCC);
259
260 /* make sure nobody modified our snapshot */
261 Assert(snap->curcid == FirstCommandId);
262 Assert(!snap->suboverflowed);
263 Assert(!snap->takenDuringRecovery);
264 Assert(snap->regd_count == 0);
265
266 /* slightly more likely, so it's checked even without c-asserts */
267 if (snap->copied)
268 elog(ERROR, "cannot free a copied snapshot");
269
270 if (snap->active_count)
271 elog(ERROR, "cannot free an active snapshot");
272
273 pfree(snap);
274}

References Assert, elog, ERROR, fb(), FirstCommandId, pfree(), and SNAPSHOT_HISTORIC_MVCC.

Referenced by SnapBuildSnapDecRefcount().

◆ SnapBuildGetOrBuildSnapshot()

Snapshot SnapBuildGetOrBuildSnapshot ( SnapBuild builder)

Definition at line 581 of file snapbuild.c.

582{
583 Assert(builder->state == SNAPBUILD_CONSISTENT);
584
585 /* only build a new snapshot if we don't have a prebuilt one */
586 if (builder->snapshot == NULL)
587 {
588 builder->snapshot = SnapBuildBuildSnapshot(builder);
589 /* increase refcount for the snapshot builder */
591 }
592
593 return builder->snapshot;
594}

References Assert, fb(), SNAPBUILD_CONSISTENT, SnapBuildBuildSnapshot(), SnapBuildSnapIncRefcount(), SnapBuild::snapshot, and SnapBuild::state.

Referenced by logicalmsg_decode().

◆ SnapBuildGetTwoPhaseAt()

XLogRecPtr SnapBuildGetTwoPhaseAt ( SnapBuild builder)

Definition at line 289 of file snapbuild.c.

290{
291 return builder->two_phase_at;
292}

References SnapBuild::two_phase_at.

Referenced by DecodeCommit().

◆ SnapBuildInitialSnapshot()

Snapshot SnapBuildInitialSnapshot ( SnapBuild builder)

Definition at line 443 of file snapbuild.c.

444{
446 TransactionId xid;
449 int newxcnt = 0;
450
453
454 /* don't allow older snapshots */
455 InvalidateCatalogSnapshot(); /* about to overwrite MyProc->xmin */
457 elog(ERROR, "cannot build an initial slot snapshot when snapshots exist");
459
460 if (builder->state != SNAPBUILD_CONSISTENT)
461 elog(ERROR, "cannot build an initial slot snapshot before reaching a consistent state");
462
464 elog(ERROR, "cannot build an initial slot snapshot, not all transactions are monitored anymore");
465
466 /* so we don't overwrite the existing value */
468 elog(ERROR, "cannot build an initial slot snapshot when MyProc->xmin already is valid");
469
470 snap = SnapBuildBuildSnapshot(builder);
471
472 /*
473 * We know that snap->xmin is alive, enforced by the logical xmin
474 * mechanism. Due to that we can do this without locks, we're only
475 * changing our own value.
476 *
477 * Building an initial snapshot is expensive and an unenforced xmin
478 * horizon would have bad consequences, therefore always double-check that
479 * the horizon is enforced.
480 */
484
486 elog(ERROR, "cannot build an initial slot snapshot as oldest safe xid %u follows snapshot's xmin %u",
487 safeXid, snap->xmin);
488
489 MyProc->xmin = snap->xmin;
490
491 /* allocate in transaction context */
493
494 /*
495 * snapbuild.c builds transactions in an "inverted" manner, which means it
496 * stores committed transactions in ->xip, not ones in progress. Build a
497 * classical snapshot by marking all non-committed transactions as
498 * in-progress. This can be expensive.
499 */
500 for (xid = snap->xmin; NormalTransactionIdPrecedes(xid, snap->xmax);)
501 {
502 void *test;
503
504 /*
505 * Check whether transaction committed using the decoding snapshot
506 * meaning of ->xip.
507 */
508 test = bsearch(&xid, snap->xip, snap->xcnt,
510
511 if (test == NULL)
512 {
516 errmsg("initial slot snapshot too large")));
517
518 newxip[newxcnt++] = xid;
519 }
520
522 }
523
524 /* adjust remaining snapshot fields as needed */
525 snap->snapshot_type = SNAPSHOT_MVCC;
526 snap->xcnt = newxcnt;
527 snap->xip = newxip;
528
529 return snap;
530}
#define palloc_array(type, count)
Definition fe_memutils.h:76
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1177
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1794
@ LW_SHARED
Definition lwlock.h:113
#define ERRCODE_T_R_SERIALIZATION_FAILURE
Definition pgbench.c:77
static void test(void)
TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly)
Definition procarray.c:2906
int GetMaxSnapshotXidCount(void)
Definition procarray.c:2016
bool HistoricSnapshotActive(void)
Definition snapmgr.c:1692
bool HaveRegisteredOrActiveSnapshot(void)
Definition snapmgr.c:1644
void InvalidateCatalogSnapshot(void)
Definition snapmgr.c:455
@ SNAPSHOT_MVCC
Definition snapshot.h:46
PGPROC * MyProc
Definition proc.c:68
TransactionId xmin
Definition proc.h:239
static bool TransactionIdFollows(TransactionId id1, TransactionId id2)
Definition transam.h:297

References Assert, SnapBuild::building_full_snapshot, SnapBuild::committed, elog, ereport, errcode(), ERRCODE_T_R_SERIALIZATION_FAILURE, errmsg, ERROR, fb(), GetMaxSnapshotXidCount(), GetOldestSafeDecodingTransactionId(), HaveRegisteredOrActiveSnapshot(), HistoricSnapshotActive(), SnapBuild::includes_all_transactions, InvalidateCatalogSnapshot(), LW_SHARED, LWLockAcquire(), LWLockRelease(), MyProc, NormalTransactionIdPrecedes, palloc_array, SNAPBUILD_CONSISTENT, SnapBuildBuildSnapshot(), SNAPSHOT_MVCC, SnapBuild::state, test(), TransactionIdAdvance, TransactionIdFollows(), TransactionIdIsValid, XACT_REPEATABLE_READ, XactIsoLevel, xidComparator(), and PGPROC::xmin.

Referenced by CreateReplicationSlot(), and SnapBuildExportSnapshot().

◆ SnapBuildProcessChange()

bool SnapBuildProcessChange ( SnapBuild builder,
TransactionId  xid,
XLogRecPtr  lsn 
)

Definition at line 641 of file snapbuild.c.

642{
643 /*
644 * We can't handle data in transactions if we haven't built a snapshot
645 * yet, so don't store them.
646 */
647 if (builder->state < SNAPBUILD_FULL_SNAPSHOT)
648 return false;
649
650 /*
651 * No point in keeping track of changes in transactions that we don't have
652 * enough information about to decode. This means that they started before
653 * we got into the SNAPBUILD_FULL_SNAPSHOT state.
654 */
655 if (builder->state < SNAPBUILD_CONSISTENT &&
657 return false;
658
659 /*
660 * If the reorderbuffer doesn't yet have a snapshot, add one now, it will
661 * be needed to decode the change we're currently processing.
662 */
663 if (!ReorderBufferXidHasBaseSnapshot(builder->reorder, xid))
664 {
665 /* only build a new snapshot if we don't have a prebuilt one */
666 if (builder->snapshot == NULL)
667 {
668 builder->snapshot = SnapBuildBuildSnapshot(builder);
669 /* increase refcount for the snapshot builder */
671 }
672
673 /*
674 * Increase refcount for the transaction we're handing the snapshot
675 * out to.
676 */
678 ReorderBufferSetBaseSnapshot(builder->reorder, xid, lsn,
679 builder->snapshot);
680 }
681
682 return true;
683}

References fb(), SnapBuild::next_phase_at, SnapBuild::reorder, ReorderBufferSetBaseSnapshot(), ReorderBufferXidHasBaseSnapshot(), SNAPBUILD_CONSISTENT, SNAPBUILD_FULL_SNAPSHOT, SnapBuildBuildSnapshot(), SnapBuildSnapIncRefcount(), SnapBuild::snapshot, SnapBuild::state, and TransactionIdPrecedes().

Referenced by heap2_decode(), heap_decode(), and logicalmsg_decode().

◆ SnapBuildProcessNewCid()

void SnapBuildProcessNewCid ( SnapBuild builder,
TransactionId  xid,
XLogRecPtr  lsn,
xl_heap_new_cid xlrec 
)

Definition at line 691 of file snapbuild.c.

693{
695
696 /*
697 * we only log new_cid's if a catalog tuple was modified, so mark the
698 * transaction as containing catalog modifications
699 */
700 ReorderBufferXidSetCatalogChanges(builder->reorder, xid, lsn);
701
702 ReorderBufferAddNewTupleCids(builder->reorder, xlrec->top_xid, lsn,
703 xlrec->target_locator, xlrec->target_tid,
704 xlrec->cmin, xlrec->cmax,
705 xlrec->combocid);
706
707 /* figure out new command id */
708 if (xlrec->cmin != InvalidCommandId &&
709 xlrec->cmax != InvalidCommandId)
710 cid = Max(xlrec->cmin, xlrec->cmax);
711 else if (xlrec->cmax != InvalidCommandId)
712 cid = xlrec->cmax;
713 else if (xlrec->cmin != InvalidCommandId)
714 cid = xlrec->cmin;
715 else
716 {
717 cid = InvalidCommandId; /* silence compiler */
718 elog(ERROR, "xl_heap_new_cid record without a valid CommandId");
719 }
720
721 ReorderBufferAddNewCommandId(builder->reorder, xid, lsn, cid + 1);
722}
#define InvalidCommandId
Definition c.h:737
#define Max(x, y)
Definition c.h:1069
uint32 CommandId
Definition c.h:734
void ReorderBufferXidSetCatalogChanges(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
void ReorderBufferAddNewCommandId(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, CommandId cid)
void ReorderBufferAddNewTupleCids(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, RelFileLocator locator, ItemPointerData tid, CommandId cmin, CommandId cmax, CommandId combocid)

References elog, ERROR, fb(), InvalidCommandId, Max, SnapBuild::reorder, ReorderBufferAddNewCommandId(), ReorderBufferAddNewTupleCids(), and ReorderBufferXidSetCatalogChanges().

Referenced by heap2_decode().

◆ SnapBuildProcessRunningXacts()

void SnapBuildProcessRunningXacts ( SnapBuild builder,
XLogRecPtr  lsn,
xl_running_xacts running 
)

Definition at line 1139 of file snapbuild.c.

1140{
1141 ReorderBufferTXN *txn;
1142 TransactionId xmin;
1143
1144 /*
1145 * If we're not consistent yet, inspect the record to see whether it
1146 * allows to get closer to being consistent. If we are consistent, dump
1147 * our snapshot so others or we, after a restart, can use it.
1148 */
1149 if (builder->state < SNAPBUILD_CONSISTENT)
1150 {
1151 /* returns false if there's no point in performing cleanup just yet */
1152 if (!SnapBuildFindSnapshot(builder, lsn, running))
1153 return;
1154 }
1155 else
1156 SnapBuildSerialize(builder, lsn);
1157
1158 /*
1159 * Update range of interesting xids based on the running xacts
1160 * information. We don't increase ->xmax using it, because once we are in
1161 * a consistent state we can do that ourselves and much more efficiently
1162 * so, because we only need to do it for catalog transactions since we
1163 * only ever look at those.
1164 *
1165 * NB: We only increase xmax when a catalog modifying transaction commits
1166 * (see SnapBuildCommitTxn). Because of this, xmax can be lower than
1167 * xmin, which looks odd but is correct and actually more efficient, since
1168 * we hit fast paths in heapam_visibility.c.
1169 */
1170 builder->xmin = running->oldestRunningXid;
1171
1172 /* Remove transactions we don't need to keep track off anymore */
1173 SnapBuildPurgeOlderTxn(builder);
1174
1175 /*
1176 * Advance the xmin limit for the current replication slot, to allow
1177 * vacuum to clean up the tuples this slot has been protecting.
1178 *
1179 * The reorderbuffer might have an xmin among the currently running
1180 * snapshots; use it if so. If not, we need only consider the snapshots
1181 * we'll produce later, which can't be less than the oldest running xid in
1182 * the record we're reading now.
1183 */
1184 xmin = ReorderBufferGetOldestXmin(builder->reorder);
1185 if (xmin == InvalidTransactionId)
1186 xmin = running->oldestRunningXid;
1187 elog(DEBUG3, "xmin: %u, xmax: %u, oldest running: %u, oldest xmin: %u",
1188 builder->xmin, builder->xmax, running->oldestRunningXid, xmin);
1189 LogicalIncreaseXminForSlot(lsn, xmin);
1190
1191 /*
1192 * Also tell the slot where we can restart decoding from. We don't want to
1193 * do that after every commit because changing that implies an fsync of
1194 * the logical slot's state file, so we only do it every time we see a
1195 * running xacts record.
1196 *
1197 * Do so by looking for the oldest in progress transaction (determined by
1198 * the first LSN of any of its relevant records). Every transaction
1199 * remembers the last location we stored the snapshot to disk before its
1200 * beginning. That point is where we can restart from.
1201 */
1202
1203 /*
1204 * Can't know about a serialized snapshot's location if we're not
1205 * consistent.
1206 */
1207 if (builder->state < SNAPBUILD_CONSISTENT)
1208 return;
1209
1210 txn = ReorderBufferGetOldestTXN(builder->reorder);
1211
1212 /*
1213 * oldest ongoing txn might have started when we didn't yet serialize
1214 * anything because we hadn't reached a consistent state yet.
1215 */
1216 if (txn != NULL && XLogRecPtrIsValid(txn->restart_decoding_lsn))
1218
1219 /*
1220 * No in-progress transaction, can reuse the last serialized snapshot if
1221 * we have one.
1222 */
1223 else if (txn == NULL &&
1227 builder->last_serialized_snapshot);
1228}
#define DEBUG3
Definition elog.h:28
void LogicalIncreaseRestartDecodingForSlot(XLogRecPtr current_lsn, XLogRecPtr restart_lsn)
Definition logical.c:1735
void LogicalIncreaseXminForSlot(XLogRecPtr current_lsn, TransactionId xmin)
Definition logical.c:1667
TransactionId ReorderBufferGetOldestXmin(ReorderBuffer *rb)
ReorderBufferTXN * ReorderBufferGetOldestTXN(ReorderBuffer *rb)
static bool SnapBuildFindSnapshot(SnapBuild *builder, XLogRecPtr lsn, xl_running_xacts *running)
Definition snapbuild.c:1241
static void SnapBuildPurgeOlderTxn(SnapBuild *builder)
Definition snapbuild.c:866
XLogRecPtr restart_decoding_lsn
XLogRecPtr current_restart_decoding_lsn

References ReorderBuffer::current_restart_decoding_lsn, DEBUG3, elog, fb(), InvalidTransactionId, SnapBuild::last_serialized_snapshot, LogicalIncreaseRestartDecodingForSlot(), LogicalIncreaseXminForSlot(), xl_running_xacts::oldestRunningXid, SnapBuild::reorder, ReorderBufferGetOldestTXN(), ReorderBufferGetOldestXmin(), ReorderBufferTXN::restart_decoding_lsn, SNAPBUILD_CONSISTENT, SnapBuildFindSnapshot(), SnapBuildPurgeOlderTxn(), SnapBuildSerialize(), SnapBuild::state, XLogRecPtrIsValid, SnapBuild::xmax, and SnapBuild::xmin.

Referenced by standby_decode().

◆ SnapBuildPurgeOlderTxn()

static void SnapBuildPurgeOlderTxn ( SnapBuild builder)
static

Definition at line 866 of file snapbuild.c.

867{
868 int off;
869 TransactionId *workspace;
870 int surviving_xids = 0;
871
872 /* not ready yet */
873 if (!TransactionIdIsNormal(builder->xmin))
874 return;
875
876 /* TODO: Neater algorithm than just copying and iterating? */
877 workspace =
879 builder->committed.xcnt * sizeof(TransactionId));
880
881 /* copy xids that still are interesting to workspace */
882 for (off = 0; off < builder->committed.xcnt; off++)
883 {
884 if (NormalTransactionIdPrecedes(builder->committed.xip[off],
885 builder->xmin))
886 ; /* remove */
887 else
888 workspace[surviving_xids++] = builder->committed.xip[off];
889 }
890
891 /* copy workspace back to persistent state */
892 memcpy(builder->committed.xip, workspace,
893 surviving_xids * sizeof(TransactionId));
894
895 elog(DEBUG3, "purged committed transactions from %u to %u, xmin: %u, xmax: %u",
897 builder->xmin, builder->xmax);
898 builder->committed.xcnt = surviving_xids;
899
900 pfree(workspace);
901
902 /*
903 * Purge xids in ->catchange as well. The purged array must also be sorted
904 * in xidComparator order.
905 */
906 if (builder->catchange.xcnt > 0)
907 {
908 /*
909 * Since catchange.xip is sorted, we find the lower bound of xids that
910 * are still interesting.
911 */
912 for (off = 0; off < builder->catchange.xcnt; off++)
913 {
915 builder->xmin))
916 break;
917 }
918
919 surviving_xids = builder->catchange.xcnt - off;
920
921 if (surviving_xids > 0)
922 {
923 memmove(builder->catchange.xip, &(builder->catchange.xip[off]),
924 surviving_xids * sizeof(TransactionId));
925 }
926 else
927 {
928 pfree(builder->catchange.xip);
929 builder->catchange.xip = NULL;
930 }
931
932 elog(DEBUG3, "purged catalog modifying transactions from %u to %u, xmin: %u, xmax: %u",
934 builder->xmin, builder->xmax);
935 builder->catchange.xcnt = surviving_xids;
936 }
937}
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232

References SnapBuild::catchange, SnapBuild::committed, SnapBuild::context, DEBUG3, elog, fb(), MemoryContextAlloc(), NormalTransactionIdPrecedes, pfree(), TransactionIdFollowsOrEquals(), TransactionIdIsNormal, SnapBuild::xcnt, SnapBuild::xip, SnapBuild::xmax, and SnapBuild::xmin.

Referenced by SnapBuildProcessRunningXacts().

◆ SnapBuildResetExportedSnapshotState()

void SnapBuildResetExportedSnapshotState ( void  )

Definition at line 629 of file snapbuild.c.

630{
632 ExportInProgress = false;
633}

References ExportInProgress, fb(), and SavedResourceOwnerDuringExport.

Referenced by AbortTransaction().

◆ SnapBuildRestore()

static bool SnapBuildRestore ( SnapBuild builder,
XLogRecPtr  lsn 
)
static

Definition at line 1844 of file snapbuild.c.

1845{
1846 SnapBuildOnDisk ondisk;
1847
1848 /* no point in loading a snapshot if we're already there */
1849 if (builder->state == SNAPBUILD_CONSISTENT)
1850 return false;
1851
1852 /* validate and restore the snapshot to 'ondisk' */
1853 if (!SnapBuildRestoreSnapshot(&ondisk, lsn, builder->context, true))
1854 return false;
1855
1856 /*
1857 * ok, we now have a sensible snapshot here, figure out if it has more
1858 * information than we have.
1859 */
1860
1861 /*
1862 * We are only interested in consistent snapshots for now, comparing
1863 * whether one incomplete snapshot is more "advanced" seems to be
1864 * unnecessarily complex.
1865 */
1866 if (ondisk.builder.state < SNAPBUILD_CONSISTENT)
1868
1869 /*
1870 * Don't use a snapshot that requires an xmin that we cannot guarantee to
1871 * be available.
1872 */
1875
1876 /*
1877 * Consistent snapshots have no next phase. Reset next_phase_at as it is
1878 * possible that an old value may remain.
1879 */
1882
1883 /* ok, we think the snapshot is sensible, copy over everything important */
1884 builder->xmin = ondisk.builder.xmin;
1885 builder->xmax = ondisk.builder.xmax;
1886 builder->state = ondisk.builder.state;
1887
1888 builder->committed.xcnt = ondisk.builder.committed.xcnt;
1889 /* We only allocated/stored xcnt, not xcnt_space xids ! */
1890 /* don't overwrite preallocated xip, if we don't have anything here */
1891 if (builder->committed.xcnt > 0)
1892 {
1893 pfree(builder->committed.xip);
1894 builder->committed.xcnt_space = ondisk.builder.committed.xcnt;
1895 builder->committed.xip = ondisk.builder.committed.xip;
1896 }
1897 ondisk.builder.committed.xip = NULL;
1898
1899 /* set catalog modifying transactions */
1900 if (builder->catchange.xip)
1901 pfree(builder->catchange.xip);
1902 builder->catchange.xcnt = ondisk.builder.catchange.xcnt;
1903 builder->catchange.xip = ondisk.builder.catchange.xip;
1904 ondisk.builder.catchange.xip = NULL;
1905
1906 /* our snapshot is not interesting anymore, build a new one */
1907 if (builder->snapshot != NULL)
1908 {
1910 }
1911 builder->snapshot = SnapBuildBuildSnapshot(builder);
1913
1915
1916 Assert(builder->state == SNAPBUILD_CONSISTENT);
1917
1918 ereport(LOG,
1919 errmsg("logical decoding found consistent point at %X/%08X",
1920 LSN_FORMAT_ARGS(lsn)),
1921 errdetail("Logical decoding will begin using saved snapshot."));
1922 return true;
1923
1925 if (ondisk.builder.committed.xip != NULL)
1926 pfree(ondisk.builder.committed.xip);
1927 if (ondisk.builder.catchange.xip != NULL)
1928 pfree(ondisk.builder.catchange.xip);
1929 return false;
1930}

References Assert, SnapBuildOnDisk::builder, SnapBuild::catchange, SnapBuild::committed, SnapBuild::context, ereport, errdetail(), errmsg, fb(), SnapBuild::initial_xmin_horizon, InvalidTransactionId, LOG, LSN_FORMAT_ARGS, SnapBuild::next_phase_at, pfree(), SnapBuild::reorder, ReorderBufferSetRestartPoint(), SNAPBUILD_CONSISTENT, SnapBuildBuildSnapshot(), SnapBuildRestoreSnapshot(), SnapBuildSnapDecRefcount(), SnapBuildSnapIncRefcount(), SnapBuild::snapshot, SnapBuild::state, TransactionIdPrecedes(), SnapBuild::xcnt, SnapBuild::xcnt_space, SnapBuild::xip, SnapBuild::xmax, and SnapBuild::xmin.

Referenced by SnapBuildFindSnapshot(), and SnapBuildSerializationPoint().

◆ SnapBuildRestoreContents()

static void SnapBuildRestoreContents ( int  fd,
void dest,
Size  size,
const char path 
)
static

Definition at line 1936 of file snapbuild.c.

1937{
1938 int readBytes;
1939
1941 readBytes = read(fd, dest, size);
1943 if (readBytes != size)
1944 {
1945 int save_errno = errno;
1946
1948
1949 if (readBytes < 0)
1950 {
1951 errno = save_errno;
1952 ereport(ERROR,
1954 errmsg("could not read file \"%s\": %m", path)));
1955 }
1956 else
1957 ereport(ERROR,
1959 errmsg("could not read file \"%s\": read %d of %zu",
1960 path, readBytes, size)));
1961 }
1962}

References CloseTransientFile(), ereport, errcode(), ERRCODE_DATA_CORRUPTED, errcode_for_file_access(), errmsg, ERROR, fb(), fd(), pgstat_report_wait_end(), pgstat_report_wait_start(), and read.

Referenced by SnapBuildRestoreSnapshot().

◆ SnapBuildRestoreSnapshot()

bool SnapBuildRestoreSnapshot ( SnapBuildOnDisk ondisk,
XLogRecPtr  lsn,
MemoryContext  context,
bool  missing_ok 
)

Definition at line 1745 of file snapbuild.c.

1747{
1748 int fd;
1749 pg_crc32c checksum;
1750 Size sz;
1751 char path[MAXPGPATH];
1752
1753 sprintf(path, "%s/%X-%X.snap",
1755 LSN_FORMAT_ARGS(lsn));
1756
1758
1759 if (fd < 0)
1760 {
1761 if (missing_ok && errno == ENOENT)
1762 return false;
1763
1764 ereport(ERROR,
1766 errmsg("could not open file \"%s\": %m", path)));
1767 }
1768
1769 /* ----
1770 * Make sure the snapshot had been stored safely to disk, that's normally
1771 * cheap.
1772 * Note that we do not need PANIC here, nobody will be able to use the
1773 * slot without fsyncing, and saving it won't succeed without an fsync()
1774 * either...
1775 * ----
1776 */
1777 fsync_fname(path, false);
1779
1780 /* read statically sized portion of snapshot */
1782
1783 if (ondisk->magic != SNAPBUILD_MAGIC)
1784 ereport(ERROR,
1786 errmsg("snapbuild state file \"%s\" has wrong magic number: %u instead of %u",
1787 path, ondisk->magic, SNAPBUILD_MAGIC)));
1788
1789 if (ondisk->version != SNAPBUILD_VERSION)
1790 ereport(ERROR,
1792 errmsg("snapbuild state file \"%s\" has unsupported version: %u instead of %u",
1793 path, ondisk->version, SNAPBUILD_VERSION)));
1794
1795 INIT_CRC32C(checksum);
1796 COMP_CRC32C(checksum,
1797 ((char *) ondisk) + SnapBuildOnDiskNotChecksummedSize,
1799
1800 /* read SnapBuild */
1801 SnapBuildRestoreContents(fd, &ondisk->builder, sizeof(SnapBuild), path);
1802 COMP_CRC32C(checksum, &ondisk->builder, sizeof(SnapBuild));
1803
1804 /* restore committed xacts information */
1805 if (ondisk->builder.committed.xcnt > 0)
1806 {
1807 sz = sizeof(TransactionId) * ondisk->builder.committed.xcnt;
1808 ondisk->builder.committed.xip = MemoryContextAllocZero(context, sz);
1810 COMP_CRC32C(checksum, ondisk->builder.committed.xip, sz);
1811 }
1812
1813 /* restore catalog modifying xacts information */
1814 if (ondisk->builder.catchange.xcnt > 0)
1815 {
1816 sz = sizeof(TransactionId) * ondisk->builder.catchange.xcnt;
1817 ondisk->builder.catchange.xip = MemoryContextAllocZero(context, sz);
1819 COMP_CRC32C(checksum, ondisk->builder.catchange.xip, sz);
1820 }
1821
1822 if (CloseTransientFile(fd) != 0)
1823 ereport(ERROR,
1825 errmsg("could not close file \"%s\": %m", path)));
1826
1827 FIN_CRC32C(checksum);
1828
1829 /* verify checksum of what we've read */
1830 if (!EQ_CRC32C(checksum, ondisk->checksum))
1831 ereport(ERROR,
1833 errmsg("checksum mismatch for snapbuild state file \"%s\": is %u, should be %u",
1834 path, checksum, ondisk->checksum)));
1835
1836 return true;
1837}

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

◆ SnapBuildSerializationPoint()

void SnapBuildSerializationPoint ( SnapBuild builder,
XLogRecPtr  lsn 
)

Definition at line 1487 of file snapbuild.c.

1488{
1489 if (builder->state < SNAPBUILD_CONSISTENT)
1490 SnapBuildRestore(builder, lsn);
1491 else
1492 SnapBuildSerialize(builder, lsn);
1493}

References SNAPBUILD_CONSISTENT, SnapBuildRestore(), SnapBuildSerialize(), and SnapBuild::state.

Referenced by xlog_decode().

◆ SnapBuildSerialize()

static void SnapBuildSerialize ( SnapBuild builder,
XLogRecPtr  lsn 
)
static

Definition at line 1500 of file snapbuild.c.

1501{
1503 SnapBuildOnDisk *ondisk = NULL;
1506 size_t catchange_xcnt;
1507 char *ondisk_c;
1508 int fd;
1509 char tmppath[MAXPGPATH];
1510 char path[MAXPGPATH];
1511 int ret;
1512 struct stat stat_buf;
1513 Size sz;
1514
1517 builder->last_serialized_snapshot <= lsn);
1518
1519 /*
1520 * no point in serializing if we cannot continue to work immediately after
1521 * restoring the snapshot
1522 */
1523 if (builder->state < SNAPBUILD_CONSISTENT)
1524 return;
1525
1526 /* consistent snapshots have no next phase */
1528
1529 /*
1530 * We identify snapshots by the LSN they are valid for. We don't need to
1531 * include timelines in the name as each LSN maps to exactly one timeline
1532 * unless the user used pg_resetwal or similar. If a user did so, there's
1533 * no hope continuing to decode anyway.
1534 */
1535 sprintf(path, "%s/%X-%X.snap",
1537 LSN_FORMAT_ARGS(lsn));
1538
1539 /*
1540 * first check whether some other backend already has written the snapshot
1541 * for this LSN. It's perfectly fine if there's none, so we accept ENOENT
1542 * as a valid state. Everything else is an unexpected error.
1543 */
1544 ret = stat(path, &stat_buf);
1545
1546 if (ret != 0 && errno != ENOENT)
1547 ereport(ERROR,
1549 errmsg("could not stat file \"%s\": %m", path)));
1550
1551 else if (ret == 0)
1552 {
1553 /*
1554 * somebody else has already serialized to this point, don't overwrite
1555 * but remember location, so we don't need to read old data again.
1556 *
1557 * To be sure it has been synced to disk after the rename() from the
1558 * tempfile filename to the real filename, we just repeat the fsync.
1559 * That ought to be cheap because in most scenarios it should already
1560 * be safely on disk.
1561 */
1562 fsync_fname(path, false);
1564
1565 builder->last_serialized_snapshot = lsn;
1566 goto out;
1567 }
1568
1569 /*
1570 * there is an obvious race condition here between the time we stat(2) the
1571 * file and us writing the file. But we rename the file into place
1572 * atomically and all files created need to contain the same data anyway,
1573 * so this is perfectly fine, although a bit of a resource waste. Locking
1574 * seems like pointless complication.
1575 */
1576 elog(DEBUG1, "serializing snapshot to %s", path);
1577
1578 /* to make sure only we will write to this tempfile, include pid */
1579 sprintf(tmppath, "%s/%X-%X.snap.%d.tmp",
1582
1583 /*
1584 * Unlink temporary file if it already exists, needs to have been before a
1585 * crash/error since we won't enter this function twice from within a
1586 * single decoding slot/backend and the temporary file contains the pid of
1587 * the current process.
1588 */
1589 if (unlink(tmppath) != 0 && errno != ENOENT)
1590 ereport(ERROR,
1592 errmsg("could not remove file \"%s\": %m", tmppath)));
1593
1595
1596 /* Get the catalog modifying transactions that are yet not committed */
1599
1600 needed_length = sizeof(SnapBuildOnDisk) +
1601 sizeof(TransactionId) * (builder->committed.xcnt + catchange_xcnt);
1602
1604 ondisk = (SnapBuildOnDisk *) ondisk_c;
1605 ondisk->magic = SNAPBUILD_MAGIC;
1606 ondisk->version = SNAPBUILD_VERSION;
1607 ondisk->length = needed_length;
1608 INIT_CRC32C(ondisk->checksum);
1609 COMP_CRC32C(ondisk->checksum,
1610 ((char *) ondisk) + SnapBuildOnDiskNotChecksummedSize,
1612 ondisk_c += sizeof(SnapBuildOnDisk);
1613
1614 memcpy(&ondisk->builder, builder, sizeof(SnapBuild));
1615 /* NULL-ify memory-only data */
1616 ondisk->builder.context = NULL;
1617 ondisk->builder.snapshot = NULL;
1618 ondisk->builder.reorder = NULL;
1619 ondisk->builder.committed.xip = NULL;
1620 ondisk->builder.catchange.xip = NULL;
1621 /* update catchange only on disk data */
1623
1624 COMP_CRC32C(ondisk->checksum,
1625 &ondisk->builder,
1626 sizeof(SnapBuild));
1627
1628 /* copy committed xacts */
1629 if (builder->committed.xcnt > 0)
1630 {
1631 sz = sizeof(TransactionId) * builder->committed.xcnt;
1632 memcpy(ondisk_c, builder->committed.xip, sz);
1633 COMP_CRC32C(ondisk->checksum, ondisk_c, sz);
1634 ondisk_c += sz;
1635 }
1636
1637 /* copy catalog modifying xacts */
1638 if (catchange_xcnt > 0)
1639 {
1640 sz = sizeof(TransactionId) * catchange_xcnt;
1642 COMP_CRC32C(ondisk->checksum, ondisk_c, sz);
1643 ondisk_c += sz;
1644 }
1645
1646 FIN_CRC32C(ondisk->checksum);
1647
1648 /* we have valid data now, open tempfile and write it there */
1651 if (fd < 0)
1652 ereport(ERROR,
1654 errmsg("could not open file \"%s\": %m", tmppath)));
1655
1656 errno = 0;
1658 if ((write(fd, ondisk, needed_length)) != needed_length)
1659 {
1660 int save_errno = errno;
1661
1663
1664 /* if write didn't set errno, assume problem is no disk space */
1666 ereport(ERROR,
1668 errmsg("could not write to file \"%s\": %m", tmppath)));
1669 }
1671
1672 /*
1673 * fsync the file before renaming so that even if we crash after this we
1674 * have either a fully valid file or nothing.
1675 *
1676 * It's safe to just ERROR on fsync() here because we'll retry the whole
1677 * operation including the writes.
1678 *
1679 * TODO: Do the fsync() via checkpoints/restartpoints, doing it here has
1680 * some noticeable overhead since it's performed synchronously during
1681 * decoding?
1682 */
1684 if (pg_fsync(fd) != 0)
1685 {
1686 int save_errno = errno;
1687
1689 errno = save_errno;
1690 ereport(ERROR,
1692 errmsg("could not fsync file \"%s\": %m", tmppath)));
1693 }
1695
1696 if (CloseTransientFile(fd) != 0)
1697 ereport(ERROR,
1699 errmsg("could not close file \"%s\": %m", tmppath)));
1700
1702
1703 /*
1704 * We may overwrite the work from some other backend, but that's ok, our
1705 * snapshot is valid as well, we'll just have done some superfluous work.
1706 */
1707 if (rename(tmppath, path) != 0)
1708 {
1709 ereport(ERROR,
1711 errmsg("could not rename file \"%s\" to \"%s\": %m",
1712 tmppath, path)));
1713 }
1714
1715 /* make sure we persist */
1716 fsync_fname(path, false);
1718
1719 /*
1720 * Now there's no way we can lose the dumped state anymore, remember this
1721 * as a serialization point.
1722 */
1723 builder->last_serialized_snapshot = lsn;
1724
1726
1727out:
1729 builder->last_serialized_snapshot);
1730 /* be tidy */
1731 if (ondisk)
1732 pfree(ondisk);
1733 if (catchange_xip)
1735}

References Assert, SnapBuildOnDisk::builder, SnapBuild::catchange, ReorderBuffer::catchange_txns, SnapBuildOnDisk::checksum, CloseTransientFile(), SnapBuild::committed, COMP_CRC32C, SnapBuild::context, dclist_count(), DEBUG1, elog, ereport, errcode_for_file_access(), errmsg, ERROR, fb(), fd(), FIN_CRC32C, fsync_fname(), INIT_CRC32C, InvalidTransactionId, SnapBuild::last_serialized_snapshot, SnapBuildOnDisk::length, LSN_FORMAT_ARGS, SnapBuildOnDisk::magic, MAXPGPATH, MemoryContextSwitchTo(), MyProcPid, SnapBuild::next_phase_at, OpenTransientFile(), palloc0(), pfree(), PG_BINARY, pg_fsync(), PG_LOGICAL_SNAPSHOTS_DIR, pgstat_report_wait_end(), pgstat_report_wait_start(), SnapBuild::reorder, ReorderBufferGetCatalogChangesXacts(), ReorderBufferSetRestartPoint(), SNAPBUILD_CONSISTENT, SNAPBUILD_MAGIC, SNAPBUILD_VERSION, SnapBuildOnDiskConstantSize, SnapBuildOnDiskNotChecksummedSize, SnapBuild::snapshot, sprintf, stat, SnapBuild::state, SnapBuildOnDisk::version, write, SnapBuild::xcnt, SnapBuild::xip, and XLogRecPtrIsValid.

Referenced by SnapBuildProcessRunningXacts(), and SnapBuildSerializationPoint().

◆ SnapBuildSetTwoPhaseAt()

void SnapBuildSetTwoPhaseAt ( SnapBuild builder,
XLogRecPtr  ptr 
)

Definition at line 298 of file snapbuild.c.

299{
300 builder->two_phase_at = ptr;
301}

References SnapBuild::two_phase_at.

Referenced by CreateDecodingContext().

◆ SnapBuildSnapDecRefcount()

void SnapBuildSnapDecRefcount ( Snapshot  snap)

Definition at line 331 of file snapbuild.c.

332{
333 /* make sure we don't get passed an external snapshot */
334 Assert(snap->snapshot_type == SNAPSHOT_HISTORIC_MVCC);
335
336 /* make sure nobody modified our snapshot */
337 Assert(snap->curcid == FirstCommandId);
338 Assert(!snap->suboverflowed);
339 Assert(!snap->takenDuringRecovery);
340
341 Assert(snap->regd_count == 0);
342
343 Assert(snap->active_count > 0);
344
345 /* slightly more likely, so it's checked even without casserts */
346 if (snap->copied)
347 elog(ERROR, "cannot free a copied snapshot");
348
349 snap->active_count--;
350 if (snap->active_count == 0)
352}
static void SnapBuildFreeSnapshot(Snapshot snap)
Definition snapbuild.c:255

References Assert, elog, ERROR, fb(), FirstCommandId, SnapBuildFreeSnapshot(), and SNAPSHOT_HISTORIC_MVCC.

Referenced by FreeSnapshotBuilder(), ReorderBufferCleanupTXN(), ReorderBufferFreeSnap(), ReorderBufferTransferSnapToParent(), SnapBuildCommitTxn(), and SnapBuildRestore().

◆ SnapBuildSnapIncRefcount()

static void SnapBuildSnapIncRefcount ( Snapshot  snap)
static

Definition at line 319 of file snapbuild.c.

320{
321 snap->active_count++;
322}

References fb().

Referenced by SnapBuildCommitTxn(), SnapBuildDistributeSnapshotAndInval(), SnapBuildGetOrBuildSnapshot(), SnapBuildProcessChange(), and SnapBuildRestore().

◆ SnapBuildSnapshotExists()

bool SnapBuildSnapshotExists ( XLogRecPtr  lsn)

Definition at line 2061 of file snapbuild.c.

2062{
2063 char path[MAXPGPATH];
2064 int ret;
2065 struct stat stat_buf;
2066
2067 sprintf(path, "%s/%X-%X.snap",
2069 LSN_FORMAT_ARGS(lsn));
2070
2071 ret = stat(path, &stat_buf);
2072
2073 if (ret != 0 && errno != ENOENT)
2074 ereport(ERROR,
2076 errmsg("could not stat file \"%s\": %m", path)));
2077
2078 return ret == 0;
2079}

References ereport, errcode_for_file_access(), errmsg, ERROR, fb(), LSN_FORMAT_ARGS, MAXPGPATH, PG_LOGICAL_SNAPSHOTS_DIR, sprintf, and stat.

Referenced by update_local_synced_slot().

◆ SnapBuildWaitSnapshot()

static void SnapBuildWaitSnapshot ( xl_running_xacts running,
TransactionId  cutoff 
)
static

Definition at line 1438 of file snapbuild.c.

1439{
1440 int off;
1441
1442 for (off = 0; off < running->xcnt; off++)
1443 {
1444 TransactionId xid = running->xids[off];
1445
1446 /*
1447 * Upper layers should prevent that we ever need to wait on ourselves.
1448 * Check anyway, since failing to do so would either result in an
1449 * endless wait or an Assert() failure.
1450 */
1452 elog(ERROR, "waiting for ourselves");
1453
1454 if (TransactionIdFollows(xid, cutoff))
1455 continue;
1456
1458 }
1459
1460 /*
1461 * All transactions we needed to finish finished - try to ensure there is
1462 * another xl_running_xacts record in a timely manner, without having to
1463 * wait for bgwriter or checkpointer to log one. During recovery we can't
1464 * enforce that, so we'll have to wait.
1465 */
1466 if (!RecoveryInProgress())
1467 {
1469 }
1470}
void XactLockTableWait(TransactionId xid, Relation rel, const ItemPointerData *ctid, XLTW_Oper oper)
Definition lmgr.c:663
@ XLTW_None
Definition lmgr.h:26
XLogRecPtr LogStandbySnapshot(void)
Definition standby.c:1283
TransactionId xids[FLEXIBLE_ARRAY_MEMBER]
Definition standbydefs.h:56
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition xact.c:943
bool RecoveryInProgress(void)
Definition xlog.c:6444

References elog, ERROR, fb(), LogStandbySnapshot(), RecoveryInProgress(), TransactionIdFollows(), TransactionIdIsCurrentTransactionId(), XactLockTableWait(), xl_running_xacts::xcnt, xl_running_xacts::xids, and XLTW_None.

Referenced by SnapBuildFindSnapshot().

◆ SnapBuildXactNeedsSkip()

bool SnapBuildXactNeedsSkip ( SnapBuild builder,
XLogRecPtr  ptr 
)

Definition at line 307 of file snapbuild.c.

308{
309 return ptr < builder->start_decoding_at;
310}

References SnapBuild::start_decoding_at.

Referenced by AssertTXNLsnOrder(), DecodeTXNNeedSkip(), logicalmsg_decode(), and ReorderBufferCanStartStreaming().

◆ SnapBuildXidHasCatalogChanges()

static bool SnapBuildXidHasCatalogChanges ( SnapBuild builder,
TransactionId  xid,
uint32  xinfo 
)
inlinestatic

Definition at line 1109 of file snapbuild.c.

1111{
1112 if (ReorderBufferXidHasCatalogChanges(builder->reorder, xid))
1113 return true;
1114
1115 /*
1116 * The transactions that have changed catalogs must have invalidation
1117 * info.
1118 */
1119 if (!(xinfo & XACT_XINFO_HAS_INVALS))
1120 return false;
1121
1122 /* Check the catchange XID array */
1123 return ((builder->catchange.xcnt > 0) &&
1124 (bsearch(&xid, builder->catchange.xip, builder->catchange.xcnt,
1125 sizeof(TransactionId), xidComparator) != NULL));
1126}
bool ReorderBufferXidHasCatalogChanges(ReorderBuffer *rb, TransactionId xid)
#define XACT_XINFO_HAS_INVALS
Definition xact.h:192

References SnapBuild::catchange, fb(), SnapBuild::reorder, ReorderBufferXidHasCatalogChanges(), XACT_XINFO_HAS_INVALS, SnapBuild::xcnt, xidComparator(), and SnapBuild::xip.

Referenced by SnapBuildCommitTxn().

Variable Documentation

◆ ExportInProgress

bool ExportInProgress = false
static

◆ SavedResourceOwnerDuringExport

ResourceOwner SavedResourceOwnerDuringExport = NULL
static