Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wyk4x-003av7-2s for pgsql-hackers@arkaria.postgresql.org; Tue, 25 Aug 2026 05:52:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wyk4w-005YfU-2f for pgsql-hackers@arkaria.postgresql.org; Tue, 25 Aug 2026 05:52:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wyk4w-005YVt-0g for pgsql-hackers@lists.postgresql.org; Tue, 25 Aug 2026 05:52:10 +0000 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wyk4s-000000018A0-3JQo for pgsql-hackers@postgresql.org; Tue, 25 Aug 2026 05:52:09 +0000 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-2cacb8416a1so37796365ad.1 for ; Mon, 24 Aug 2026 22:52:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787637123; x=1788241923; darn=postgresql.org; h=to:cc:date:message-id:subject:mime-version:content-type:from:from :to:cc:subject:date:message-id:reply-to:content-type; bh=C7M5JwLIUNMemwACOQLn1ZNJJkzV7dNYUVu9a+QKZqE=; b=JBZfSYNZ4zRT0q91RimjHILLxFldsVePdXE8RNvl4trO676SzlzyjDDFiMa6hgjxeO cxovFzzj8Y+JD8hmJD4a5azQ3mMHwz5CIAEphmYiz/PCfTILBaXkGCWRK6BGM8G7XCCV J+dsomyxHhRrSFAKBvbpRuog17DbCwzYZ86zhGSfghEg1/xKUJG7V9TkHL5LKgeijc0X xyEX4LDv/Dic1np2NpzAhM3p1q+24wNwYjkFjAJshe85Czsb8NLwxON6O/Xldlvb2F2z ZN/wcG1ixyQ9xMwB6vwC5sdoLfdMn1VGgg4+hptI1j2gW0eJ5xQFq5qmJL299p0nCLm9 NvKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787637123; x=1788241923; h=to:cc:date:message-id:subject:mime-version:content-type:from :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to:content-type; bh=C7M5JwLIUNMemwACOQLn1ZNJJkzV7dNYUVu9a+QKZqE=; b=ICyAj+FcU+w9gcOMSFHW1VF1e4Endixk2qMwr8UcbLLPl91qmboHCCv1oVjTY6ur2e k/2x7EvlTUNGzAdAT8olw4vDrXFxUY0esQ1Be6sjNlJc9k8T2x2QvW5pBgBzHhwQY0lu stK6Cq//mgZvvLpkUDmWutg8d8Gb79xtH5j8aRD3+QEbWNs6hUlWQ3zeLnta9xfvyAYC eJzzfd1NfK0xLLAycKjlz/3dvb8HhngYJADtT3a6KsS6p2X4/1D6QdgN7+jrINlPYIdD L7t6i0iGmnyew0h6kH1iUiTx4v1KXqxW4b11OnSVyuW6ZpfFoMI73z0tgOrzh9JbKTtR yx4A== X-Gm-Message-State: AFuF++k6gsieV8f0fBU4HLffzYPx7pPNwHbSKBSV82taUMHpLZ4AhJ2e 0HJCmf+3YdevHMbtV0fNgx4XdwjKHVWwjxHdNqy/7RA/9pTdHNAIpNJp3Y3p1Q== X-Gm-Gg: AR+sD11v+XJ+0/f3VKPnGxN8az4gM7rPoBvy2HvSJ9/rtLkYpM4HmT33oFk+Ng6jrIV uOO7n8pdn1nkCO7rJYKi312NDMi+9hrsctMI4AegmQcKuaN52f0qPtZ3MTNQ6dFExnD7Ownlkp4 YCuRARVWMIHablgB97DvtV0VPq5i4hy5858OKyqdpK4ShcxVdYleX2jVjBDVw5MZKBCayAhqdir YyL/T4WP0C0323JtI+cLRlEm5596+tHo6hX+rkYvANQySkTzeflai4Yu+ahbcDDefoc679Z6A9Z lWiO6prC9hkSVznUEmOzH1eWZDQW8qHPKMFc4P0Oqs4t8J6QGhovXl/Rr9kCvGCUjmryd+MkYOa UmXWsMPmmuPTOE6AltriqAbV3N1Tj2FP4aOQ3/1aVljgdSuPaeLyz8EdkwnbceG14BVtBxzmK4L fT011GXo7cRwpX6ORNRh7nGQ2UanMV2FGJtqLW2jthDYU4ODi2XLAOLhMAj1FIFGMkelHXdw== X-Received: by 2002:a17:903:b0c:b0:2d3:160b:c01f with SMTP id d9443c01a7336-2d6dcaae0dcmr85568935ad.6.1787637122565; Mon, 24 Aug 2026 22:52:02 -0700 (PDT) Received: from smtpclient.apple ([185.135.79.125]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2d6768957c7sm24105115ad.51.2026.08.24.22.51.58 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 24 Aug 2026 22:52:01 -0700 (PDT) From: Chao Li Content-Type: multipart/mixed; boundary="Apple-Mail=_C08FF66D-40CC-4A7F-8D7F-AB3E6CF59BE1" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.600.51.1.1\)) Subject: Reduce logical WAL volume with restricted replication slots Message-Id: Date: Tue, 25 Aug 2026 13:51:24 +0800 Cc: Melanie Plageman , Masahiko Sawada To: PostgreSQL-development X-Mailer: Apple Mail (2.3864.600.51.1.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_C08FF66D-40CC-4A7F-8D7F-AB3E6CF59BE1 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Hi, # To Reviewers This is the biggest feature I have worked on so far. I would not be = surprised if I have made mistakes or missed some cases. I would greatly = appreciate anyone who reviews this patch. Given the size of the patch, I = believe it will take a long journey of revisions, so for the initial = rounds, please focus on the design, though any kind of comments are = still appreciated. Sorry for not splitting this large patch into multiple commits. The = design and implementation evolved significantly during testing and = debugging, and many of the necessary pieces were discovered along the = way. So that, it ended up with a single large commit. If this makes the = patch difficult to review, please let me know, and I will try to split = it into a more manageable series. # Motivation PG19 introduced a new feature, effective_wal_level [1], that can toggle = logical decoding dynamically based on the presence of logical slots. = While reviewing that patch, I got the initial idea for this feature and = talked to some DBAs about it. Currently, once a logical slot exists, all = eligible tables write the extra WAL data required for logical decoding. = So, the basic idea is: can we write the extra WAL data only for tables = that are being replicated? When I discussed the idea with some DBAs, they expressed strong = interest. Their most direct reaction was that it could save money. They = confirmed two things: * They have use cases where they logically replicate only a small set of = tables. * Although PG has a mechanism to automatically clean up WAL files, in = practice they manually back up WAL files, using various tools, and = retain them for years. Nowadays, long-term storage costs can be significant for many users, so = reducing WAL volume would help relieve that pain. I also briefly discussed the idea with Masahiko in a review thread [2], = and he expressed some interest as well. # Design Goal The first thing I considered was whether this feature would be generally = helpful to all deployments. My answer was no. For example, if a = deployment: * logically replicates most tables, this feature will not save = significant storage; * has mostly INSERT operations, the additional logical WAL overhead may = be relatively small; * does not retain WAL data for a long time, temporarily generating extra = WAL data will not be a major concern; Therefore, some users might be excited about this feature, while others = might not. The first decision I made was that this feature should be = opt-in. A DBA should be able to turn it on as easily as possible and = without much burden. In particular, it should NOT require a server = restart. Second, because this feature is opt-in, when it is not enabled, it = should add minimal overhead, ideally zero overhead. Third, this feature is based on effective_wal_level. If a cluster sets = wal_level =3D logical, this feature has no effect. Therefore, this = feature should be built on the infrastructure established for = effective_wal_level. # Gap effective_wal_level is raised from replica to logical based on the = presence of logical slots, but a logical replication slot does not = currently map to any relations. =46rom a slot alone, there is no way to = determine which relations are replicated through it. # Design ## How to make the feature opt-in? My idea is to add an attribute to replication slots, currently called = unrestricted: * If true, which is the default, the slot retains the behavior from = before this feature. An unrestricted slot causes effective_wal_level to = be raised from replica to logical, and all eligible tables write the = extra logical WAL data. * If false, the slot is restricted and does not raise = effective_wal_level. Only relations mapped to the slot write the extra = logical WAL data. ## User experience This feature is intended not to change the data replication semantics, = subject to the restrictions described below. Currently, restricted slots have the following restrictions: * They currently support only pgoutput; this is enforced when a = restricted slot is created. * They cannot be created from FOR ALL TABLES publications. Replicating = all tables should use an unrestricted slot instead. * The publication list of a restricted subscription can only be reduced. = Once a publication is removed, it cannot be added again without = recreating the subscription. * pg_create_logical_replication_slot() cannot create a restricted slot = inside an explicit transaction block. The expected user experience is: 1. When the cluster's wal_level is set to replica and there are no = logical slots, no logical WAL data is written. 2. When a user decides to logically replicate a table, they follow the = normal procedure to create a publication on the publisher and enable = this feature from the subscriber when creating the subscription. For = example: ``` CREATE SUBSCRIPT sub=20 CONNECTION =E2=80=98connection_str=E2=80=99=20 PUBLICATION pub=20 WITH (other opts, unrestricted_slot=3Dfalse); ``` 3. Only tables mapped to the restricted slot will start writing the = extra logical WAL data. 4. If the user wants to replicate another table, they simply add the = table to an existing publication associated with the slot. Note that, = here I just mean the relation will start to write logical WAL data, = ALTER SUBSCRIPTION sub REFRESH pub command is still required as a normal = workflow. 5. If a publication includes all tables in a schema, then when a new = table is created in that schema, the table should automatically start = writing the extra WAL data. 6. For a partitioned table in the publication, if a new partition is = created or attached, the new partition should automatically start = writing the extra WAL data. 7. If table t belongs to a restricted slot and the user adds a varchar = column that causes a TOAST table to be created, the new TOAST table = should automatically start writing the extra WAL data. 8. Disabling a subscription only pauses replication; it does not stop = logical WAL data from being written. Therefore, when the subscription is = enabled again, changes made in the meantime can still be replicated. 9. Dropping the subscription normally removes its associated slot. = Logical WAL may continue to be written conservatively until stale = mappings are cleaned up, or indefinitely if another slot still covers = the relation. ## restricted_wal_level effective_wal_level has established useful infrastructure, including how = to enable logical decoding and where to decide whether logical WAL data = should be written. I am therefore trying to build this feature on that = infrastructure. Initially, I considered reusing effective_wal_level with = some additional flags. After further investigation, however, I now think = keeping effective_wal_level unchanged may be the best approach. = Therefore, once an unrestricted slot is created, effective_wal_level is = raised to logical, preserving the current behavior. The key point for deciding whether logical WAL should be written for a = relation is RelationIsLogicallyLogged(relation). It roughly checks: * If the common conditions are not met, for example for an unlogged = table, return false. * If wal_level >=3D logical, return true. * If effective_wal_level >=3D logical, return true. * Otherwise, return false. With this feature, RelationIsLogicallyLogged(relation) is extended to = behave roughly as follows: * If the common conditions are not met, for example for an unlogged = table, return false. * If wal_level >=3D logical, return true. * If effective_wal_level >=3D logical, return true. * If restricted_wal_level < logical, return false. * If relation->rd_rel->relhasrestrictedslots is true, return true. * Otherwise, return false. So, when effective_wal_level >=3D logical, this feature has no effect. restricted_wal_level is raised to logical when there is a restricted = slot. A relation's relhasrestrictedslots flag is true when the relation = has one or more current restricted-slot mappings. So, the core of this = feature is to maintain restricted_wal_level and relhasrestrictedslots = efficiently. ## Slot scope A subscription normally maps to one slot. A subscription maps to one or = more publications, and a publication maps to one or more tables. A table = may in turn map to multiple physical relations, such as partitions and = TOAST relations. Therefore, from a subscription, we can determine the = relations for which its slots may require logical WAL. However, this = chain is long, so the information needs to be maintained efficiently at = runtime. This patch introduces the term SlotScope for the mechanism that = maintains the mapping between a restricted slot and its relations. = Accordingly, the main implementation is in slotscope.h and slotscope.c. The SlotScope mapping determines where logical tuple WAL may be = required. It doesn't determine which relations are emitted by pgoutput; = the current publication definitions and normal pgoutput filtering remain = responsible for that. For simplicity, throughout the rest of this doc, I use the terms = =E2=80=9Cscoped table=E2=80=9D and =E2=80=9Cunscoped table=E2=80=9D. A = scoped table is one included in the scope of at least one restricted = logical replication slot. The key components of the SlotScope module include: ### Publication sidecar file A slot can be associated with publications through a subscription. The = difficulty is that the slot exists on the publisher, while the = subscription information is stored in the subscriber's pg_subscription = catalog. There is no easy way for the publisher to query the = subscriber's pg_subscription, so the mapping must also be stored on the = publisher. Because slots are stored on disk, this feature adds a sidecar file, = pg_replslot//publications, to store the immutable publication OIDs = associated with the slot. The membership of those publications may still = change normally. ### A new pg_restricted_slot_relation catalog On the publisher, the new catalog stores the slot name, relation OID, = and slot incarnation. It maintains the conservative writer-side mapping = used to determine whether a relation may belong to a restricted slot. A relation may be included in multiple restricted slots, so the catalog = may contain multiple entries for the same relation. A special tuple, (slot_name, InvalidOid, slot_incarnation), acts as a = completion marker for restricted-slot initialization. This is necessary = because slot info is stored on disk and is not transactional. = Restricted-slot initialization involves approximately these steps: Step 1: Persist the slot information to disk. Step 2: Populate pg_restricted_slot_relation transactionally. Step 3: Persist the slot's ready state to disk. If a crash occurs before step 3, recovery needs to determine whether = step 2 committed. Because a publication may currently contain no = relations, the absence of ordinary rows for the slot in = pg_restricted_slot_relation cannot distinguish an empty scope from a = failed step 2. ### Additional fields added to slot =E2=80=A2 bool unrestricted: indicates whether the slot is unrestricted. =E2=80=A2 uint64 restricted_scope_incarnation: identifies a particular = creation of the slot and distinguishes slot-name reuse. For example, if = a slot is dropped and recreated with the same name, it is a different = slot and all existing mappings must be rebuilt. =E2=80=A2 bool restricted_scope_ready: indicates whether initialization = of the restricted slot has completed. If step 2 above fails, no extra = logical WAL will be written for the corresponding tables, so the slot is = not ready for replication. This flag is set to true by the transaction = commit callback after the mapping transaction commits. ### Key functions #### Creating a restricted slot =E2=80=A2 LogicalSlotScopePrepareFromPublications() resolves publication = names to publication OIDs and then calls = LogicalSlotScopePrepareFromPublicationOids(). =E2=80=A2 LogicalSlotScopePrepareFromPublicationOids() marks the slot as = restricted and incomplete and persists the publication sidecar file. It = then calls the existing EnsureLogicalDecodingEnabled() to make logical = decoding available at the cluster level. There is a short period between = slot creation and the commit of the transaction that populates = pg_restricted_slot_relation, so the function temporarily enables full = logical WAL logging, equivalent to raising effective_wal_level to = logical. It then enables restricted mode and marks the slot state dirty = so that it will be persisted. =E2=80=A2 LogicalSlotScopeFinishCreate() is called after = initialize_logical_replication_slot(). It populates = pg_restricted_slot_relation and adds the slot to pending_ready_slots. =E2=80=A2 When the transaction that populates = pg_restricted_slot_relation commits, slot_scope_xact_callback() marks = the pending restricted slot as ready. It then requests removal of the = temporary full WAL requirement, allowing restricted_wal_level to control = WAL logging unless another unrestricted slot still requires full logical = WAL. #### Handle publication changes LogicalSlotScopePublicationAddRelations() finds restricted slots = containing the changed publication, expands the added relations, and = installs their mappings. Removing a relation from a publication does not immediately remove its = writer-side mapping. The stale mapping is safe because it causes only = unnecessary logical WAL, and normal publication filtering prevents the = relation from being emitted. #### Handle partition attachment CheckLogicalSlotScopeHierarchyChange() finds mappings associated with = the parent relation and applies them to the attached partition's = physical closure. #### Handle TOAST creation LogicalSlotScopeNoteToastCreation() finds slots associated with the = owning table and adds the new TOAST relation to those slot incarnations. #### Handle relation drops LogicalSlotScopeRelationDrop() deletes mappings for a relation when the = relation itself is dropped. #### Lazy cleanup LogicalSlotScopeCleanup() removes mappings whose (slot name, = incarnation) no longer corresponds to a live, valid restricted slot. After a restricted slot is dropped, its mapping data is not deleted = immediately. Instead, autovacuum lazily calls LogicalSlotScopeCleanup() = to remove the stale data. This may cause some extra logical WAL data to = be written after dropping a restricted slot, but that is safe and = matches the lazy strategy used when dropping a slot under = effective_wal_level. #### WAL insertion decision The main entry point, RelationIsLogicallyLogged(relation), remains = unchanged, but it now calls RelationNeedsLogicalTupleWAL(). #### Recovery LogicalSlotScopeReconcileDatabase() reconciles incomplete = restricted-slot state after a restart using the completion marker in = pg_restricted_slot_relation. #### Concurrency and locking Scope initialization and scope-changing DDL must be serialized = carefully. A missing mapping could cause required logical tuple WAL to = be omitted, while inconsistent lock ordering could deadlock slot = creation with publication, partition, or TOAST changes. So we use a database-level scope lock to serialize changes to the = mapping catalog. DDL operations acquire and release this lock at command = level, rather than retaining it until transaction end. A separate = transaction-lifetime scope-change marker records that the transaction = has performed publication or structural DDL. Multiple DDL transactions = can hold this marker concurrently, but restricted-slot initialization = conditionally acquires it in exclusive mode and fails with a retryable = error if uncommitted scope-changing DDL exists. When a physical relation closure contains multiple relations, the = relations are discovered first, sorted by OID, and then locked in = deterministic order. DDL takes the required relation locks before the = database scope lock. Restricted-slot initialization already holds the = database scope lock, so it acquires relation locks conditionally and = asks the user to retry slot creation if a conflicting relation lock = exists. This avoids waiting for a relation lock while blocking DDL on = the scope lock. Publication and partition hooks first perform a relevance check under = the scope lock. If no restricted slot uses the publication or parent = relation, they return without strongly locking the physical closure. = When the change is relevant, they lock the closure, reacquire the scope = lock, recheck the relevant mappings, and then install the additions. ## Other changes Most of the other changes add calls to functions defined in slotscope.h. = Some notable changes are highlighted below. ### rel.h Change RelationIsLogicallyLogged(relation) to call = RelationNeedsLogicalTupleWAL(relation). Add a new macro, RelationCanBeLogicallyLogged(relation), which wraps the = common eligibility conditions. RelationNeedsLogicalTupleWAL(relation) = calls this macro. ### pg_class.h Add a new field, relhasrestrictedslots, to pg_class, indicating whether = the relation has one or more current or stale restricted-slot mappings. ### xlog.h Add XLogRestrictedInfo, which serves a role similar to XLogLogicalInfo = for effective_wal_level. ### GUC Add restricted_wal_level. ### pg_restricted_slot_relation.h Add a new catalog table that stores the conservative writer-side = mappings from restricted slots to relation OIDs. ### subscriptioncmds.c On the subscriber, add a new unrestricted_slot option to CREATE = SUBSCRIPTION. So far, this patch only allows the creation of new restricted slots. = Switching an existing slot between restricted and unrestricted is not = supported. For a restricted subscription, the publication list can only = be reduced; adding a new publication requires recreating the = subscription. ### libpqwalreceiver.c On the subscriber, extend the CREATE_REPLICATION_SLOT protocol command = to pass publication names. The command now looks like: ``` CREATE_REPLICATION_SLOT "pg_16419_sync_16420_7676018444727456626" = LOGICAL pgoutput (PUBLICATION_NAMES 'schema_pub', SNAPSHOT 'use') ``` ### walsender.c On the publisher, create a restricted slot when a = CREATE_REPLICATION_SLOT command contains PUBLICATION_NAMES. ### slotfuncs.c Extend pg_create_logical_replication_slot() to accept a list of = publication names. When publication names are supplied, it creates a = restricted slot. Extend pg_get_replication_slots() to show restricted-slot info. One thing to note is that pg_create_logical_replication_slot() rejects = the creation of a restricted slot inside an explicit transaction block. = A restricted slot should normally be created through CREATE SUBSCRIPTION = on the subscriber. For testing purposes, users may manually call = pg_create_logical_replication_slot(), but that does not need to be done = inside a transaction block. Supporting cases such as the following would = make the implementation considerably more complicated: ``` BEGIN pg_create_logical_replication_slot(=E2=80=99slot-name=E2=80=99, = =E2=80=98pgoutput=E2=80=99, publications=3D>[=E2=80=99pub']) ALTER TABLE t ADD COLUMN str varchar; # add a toast table ALTER TABLE p ATTACHE PARTITION p1; ABORT; ``` ### slot.c Add the new restricted-scope fields to replication slots, as described = above. ### pgoutput.c Validate that a restricted slot is ready and that the publications = requested by pgoutput are contained in the slot's immutable publication = set. Normal publication filtering continues to determine which relations = are emitted. ### slotsync.c Synchronize restricted-slot metadata for failover slots. The = restricted_scope_ready_lsn field records a WAL position at or after the = mapping transaction. A standby does not persist a synchronized = restricted slot as ready until it has flushed through this position, = ensuring that the corresponding mapping catalog changes are locally = durable before promotion. ### heapam.c Add special handling for speculative insertions. Even when the target = table is outside the restricted scope, write the logical WAL required = for a speculative insertion because the reorder buffer needs to process = its confirmation record. ### reorderbuffer.c Replace RelationIsLogicallyLogged() with RelationCanBeLogicallyLogged() = while processing changes, including TRUNCATE. ### logicalctl.c Handle LogicalDecodingCtl->xlog_restricted_info. ### autovacuum.c Call LogicalSlotScopeCleanup() to lazily clean up stale restricted-slot = mappings. ### tablecmds.c In StoreCatalogInheritance1(), add a call to = CheckLogicalSlotScopeHierarchyChange(). When a partition is created to = scoped partitioned table, add the partition to the restricted slot to = relation mapping. When ALTER TABLE ... SET SCHEMA moves a table into a schema included in = a publication, update the restricted-slot-to-relation mapping after the = namespace change becomes command-visible. ### publicationcmds.c When a new table or schema is added to a publication, update the = restricted-slot-to-relation mapping. ### toasting.c When creating a TOAST table, if its owning table is scoped, make the = TOAST table scoped as well. ### heap.c In heap_create_with_catalog(), when creating a table in a schema = included in a publication associated with a restricted slot, make the = new table scoped. # A Basic Demo ## Init two clusters One cluster is called pubdb, and the other is called subdb. Create a = database in both clusters. In this demo, the database is called = evantest. pubdb runs on the standard port, 5432, and subdb runs on port 55432. ## In pubdb, create two tables ``` CREATE TABLE scoped_table ( id integer PRIMARY KEY, payload text ); =20 CREATE TABLE unscoped_table ( id integer PRIMARY KEY, payload text ); ``` ## In pubdb, create a publication ``` CREATE PUBLICATION test_pub FOR TABLE scoped_table; ``` ## In pubdb, create a role to use for replication ``` CREATE ROLE repl; ALTER ROLE repl PASSWORD 'xxx'; GRANT USAGE ON SCHEMA public TO repl; GRANT SELECT ON scoped_table TO repl; ALTER ROLE repl WITH LOGIN REPLICATION; ``` ## In subdb, create the scoped table ``` CREATE TABLE scoped_table ( id integer PRIMARY KEY, payload text ); ``` ## In subdb, create a restricted subscription, which will create a = restricted slot in pubdb ``` CREATE SUBSCRIPTION test_sub CONNECTION 'host=3D127.0.0.1 port=3D5432 dbname=3Devantest user=3Drepl = password=3Dxxx' PUBLICATION test_pub WITH ( create_slot =3D true, copy_data =3D true, unrestricted_slot =3D false ); ``` ## In pubdb, check the restricted slot status ``` evantest=3D# SELECT slot_name, plugin, slot_type, database, = unrestricted, publication_oids, restricted_scope_ready, = restricted_scope_incarnation, restricted_scope_ready_lsn FROM = pg_replication_slots; slot_name | plugin | slot_type | database | unrestricted | = publication_oids | restricted_scope_ready | restricted_scope_incarnation = | restricted_scope_ready_lsn = -----------+----------+-----------+----------+--------------+-------------= -----+------------------------+------------------------------+------------= ---------------- test_sub | pgoutput | logical | evantest | f | {16401} = | t | -5474929704368200547 | = 0/01C414A8 (1 row) evantest=3D# select rsrslotname, rsrrelid::regclass, rsrincarnation from = pg_restricted_slot_relation; rsrslotname | rsrrelid | rsrincarnation -------------+-------------------------+---------------------- test_sub | scoped_table | -5474929704368200547 test_sub | pg_toast.pg_toast_16385 | -5474929704368200547 test_sub | - | -5474929704368200547 (3 rows) evantest=3D# SHOW effective_wal_level; effective_wal_level --------------------- replica (1 row) evantest=3D# SHOW restricted_wal_level; restricted_wal_level ---------------------- logical (1 row) ``` ## In pubdb, insert a row to both scoped_table and unscoped_table ``` INSERT INTO scoped_table VALUES (1, 'a=E2=80=99); INSERT INTO unscoped_table VALUES (1, 'a'); ``` ## In subdb, check the scoped_table, the row should be replicated ``` evantest=3D# select * from scoped_table; id | payload ----+--------- 1 | a (1 row) ``` ## In pubdb, dump WAL data ``` evantest=3D# SELECT pg_current_wal_flush_lsn() AS end_lsn; end_lsn ------------ 0/01C476D0 (1 row) ``` Then use pg_waldump: ``` % pg_waldump -p /Users/chaol/Downloads/pg_data/pubdb -s '0/01C414A8' -e = '0/01C476D0' rmgr: Heap len (rec/tot): 61/ 61, tx: 707, lsn: = 0/01C43DB0, prev 0/01C41D90, desc: INSERT+INIT off: 1, flags: 0x08, = blkref #0: rel 1663/16384/16385 blk 0 rmgr: Heap len (rec/tot): 61/ 61, tx: 708, lsn: = 0/01C47438, prev 0/01C45B38, desc: INSERT+INIT off: 1, flags: 0x00, = blkref #0: rel 1663/16384/16393 blk 0 ``` As shown above, the first INSERT has flags 0x08, where 0x08 is = XLH_INSERT_CONTAINS_NEW_TUPLE, indicating that the new tuple required = for logical decoding is included. The second INSERT, into = unscoped_table, doesn't have XLH_INSERT_CONTAINS_NEW_TUPLE. This = demonstrates the selective logical WAL behavior implemented by this = patch. [1] = https://git.postgresql.org/cgit/postgresql.git/commit/?id=3D67c20979ce72b8= c236622e5603f9775968ff501c [2] = https://www.postgresql.org/message-id/CAD21AoAAJ2tgL7%2BTeR4gRo5q28Gvx1hfC= 7BQ2nTrqL_O3GdU6w%40mail.gmail.com Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/ --Apple-Mail=_C08FF66D-40CC-4A7F-8D7F-AB3E6CF59BE1 Content-Disposition: attachment; filename=v1-0001-Support-selective-logical-WAL-for-restricted-repl.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="v1-0001-Support-selective-logical-WAL-for-restricted-repl.patch" Content-Transfer-Encoding: quoted-printable =46rom=207a4ab83094fa1b243a23f74ffe9c6287b25bb425=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20"Chao=20Li=20(Evan)"=20=0A= Date:=20Fri,=2014=20Aug=202026=2006:27:37=20+0800=0ASubject:=20[PATCH=20= v1]=20Support=20selective=20logical=20WAL=20for=20restricted=20= replication=0A=20slots=0A=0AThe=20presence=20of=20a=20logical=20= replication=20slot=20currently=20causes=20the=0Aextra=20WAL=20= information=20required=20for=20logical=20decoding=20to=20be=20written=0A= for=20every=20eligible=20relation.=20This=20can=20generate=20substantial=0A= unnecessary=20WAL=20when=20only=20a=20small=20subset=20of=20tables=20is=20= replicated.=0A=0AAdd=20restricted=20logical=20replication=20slots,=20= whose=20WAL=20requirements=0Aare=20derived=20from=20an=20immutable=20set=20= of=20publications.=20Maintain=20a=0Aconservative=20mapping=20from=20= restricted=20slots=20to=20their=20physical=0Arelations,=20including=20= partitions=20and=20TOAST=20relations,=20and=20mark=0Amapped=20relations=20= in=20pg_class.=20A=20new=20restricted=20WAL=20mode=20writes=0Alogical=20= tuple=20information=20only=20for=20marked=20relations=20when=20full=0A= logical=20WAL=20is=20not=20otherwise=20required.=0A=0AKeep=20existing=20= slots=20unrestricted=20by=20default,=20preserving=20the=20current=0A= behavior.=20Restricted=20slots=20can=20be=20requested=20through=20CREATE=0A= SUBSCRIPTION,=20the=20replication=20protocol,=20or=0A= pg_create_logical_replication_slot().=20Currently,=20restricted=20slots=0A= support=20only=20pgoutput=20and=20reject=20FOR=20ALL=20TABLES=20= publications.=0A=0AUpdate=20mappings=20transactionally=20when=20= publications=20expand,=20tables=0Aare=20created=20in=20published=20= schemas,=20partitions=20are=20attached,=20or=0ATOAST=20relations=20are=20= created.=20Stale=20mappings=20are=20safe=20because=0Athey=20cause=20only=20= unnecessary=20WAL=20and=20are=20removed=20lazily=20by=0Aautovacuum.=0A=0A= Persist=20publication=20identities=20and=20restricted-slot=20= initialization=0Astate,=20including=20crash-recovery=20markers.=20Also=20= synchronize=0Arestricted-slot=20metadata=20for=20failover=20slots=20and=20= validate=20the=0Arequested=20publication=20set=20when=20pgoutput=20= starts.=0A=0AAuthor:=20Chao=20Li=20=0A---=0A=20= doc/src/sgml/catalogs.sgml=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20|=20=20=2069=20+=0A=20doc/src/sgml/config.sgml=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20|=20=20=2018=20+=0A=20= doc/src/sgml/func/func-admin.sgml=20=20=20=20=20=20=20=20=20=20=20=20=20= |=20=20=2030=20+-=0A=20doc/src/sgml/protocol.sgml=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20|=20=20=2012=20+=0A=20= doc/src/sgml/ref/alter_subscription.sgml=20=20=20=20=20=20|=20=20=20=207=20= +=0A=20doc/src/sgml/ref/create_subscription.sgml=20=20=20=20=20|=20=20=20= 20=20+=0A=20doc/src/sgml/system-views.sgml=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20|=20=20=2053=20+=0A=20src/backend/access/heap/heapam.c=20=20= =20=20=20=20=20=20=20=20=20=20=20=20|=20=20=20=209=20+-=0A=20= src/backend/access/transam/xlog.c=20=20=20=20=20=20=20=20=20=20=20=20=20= |=20=20=2010=20+-=0A=20src/backend/catalog/heap.c=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20|=20=20=2027=20+=0A=20= src/backend/catalog/pg_subscription.c=20=20=20=20=20=20=20=20=20|=20=20=20= =201=20+=0A=20src/backend/catalog/system_views.sql=20=20=20=20=20=20=20=20= =20=20|=20=20=20=208=20+-=0A=20src/backend/catalog/toasting.c=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20|=20=20=20=202=20+=0A=20= src/backend/commands/publicationcmds.c=20=20=20=20=20=20=20=20|=20=20=20= 10=20+=0A=20src/backend/commands/subscriptioncmds.c=20=20=20=20=20=20=20= |=20=20=2057=20+-=0A=20src/backend/commands/tablecmds.c=20=20=20=20=20=20= =20=20=20=20=20=20=20=20|=20=20=2023=20+=0A=20= src/backend/postmaster/autovacuum.c=20=20=20=20=20=20=20=20=20=20=20|=20=20= =20=203=20+=0A=20.../libpqwalreceiver/libpqwalreceiver.c=20=20=20=20=20=20= =20|=20=20=2028=20+=0A=20src/backend/replication/logical/Makefile=20=20=20= =20=20=20|=20=20=20=201=20+=0A=20= src/backend/replication/logical/logicalctl.c=20=20|=20=20=2077=20+-=0A=20= src/backend/replication/logical/meson.build=20=20=20|=20=20=20=201=20+=0A= =20.../replication/logical/reorderbuffer.c=20=20=20=20=20=20=20|=20=20=20= =205=20+-=0A=20src/backend/replication/logical/slotscope.c=20=20=20|=20= 1423=20+++++++++++++++++=0A=20src/backend/replication/logical/slotsync.c=20= =20=20=20|=20=20=2097=20+-=0A=20= src/backend/replication/logical/tablesync.c=20=20=20|=20=20=20=201=20+=0A= =20src/backend/replication/pgoutput/pgoutput.c=20=20=20|=20=20=2037=20+=0A= =20src/backend/replication/slot.c=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20|=20=20111=20+-=0A=20src/backend/replication/slotfuncs.c=20=20=20=20= =20=20=20=20=20=20=20|=20=20234=20++-=0A=20= src/backend/replication/walreceiver.c=20=20=20=20=20=20=20=20=20|=20=20=20= =203=20+-=0A=20src/backend/replication/walsender.c=20=20=20=20=20=20=20=20= =20=20=20|=20=20=2068=20+-=0A=20src/backend/utils/init/postinit.c=20=20=20= =20=20=20=20=20=20=20=20=20=20|=20=20=20=204=20+=0A=20= src/backend/utils/misc/guc_parameters.dat=20=20=20=20=20|=20=20=20=209=20= +=0A=20src/backend/utils/misc/guc_tables.c=20=20=20=20=20=20=20=20=20=20=20= |=20=20=20=201=20+=0A=20src/bin/pg_dump/pg_dump.c=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20|=20=20=2014=20+=0A=20= src/bin/pg_dump/pg_dump.h=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20|=20=20=20=201=20+=0A=20src/bin/pg_dump/t/002_pg_dump.pl=20= =20=20=20=20=20=20=20=20=20=20=20=20=20|=20=20=20=205=20+-=0A=20= src/include/access/xlog.h=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20|=20=20=20=207=20+-=0A=20src/include/catalog/Makefile=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20|=20=20=20=201=20+=0A=20= src/include/catalog/meson.build=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20|=20=20=20=201=20+=0A=20src/include/catalog/pg_class.h=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20|=20=20=20=203=20+=0A=20= src/include/catalog/pg_proc.dat=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20|=20=20=2018=20+-=0A=20.../catalog/pg_restricted_slot_relation.h=20=20= =20=20=20|=20=20=2036=20+=0A=20src/include/catalog/pg_subscription.h=20=20= =20=20=20=20=20=20=20|=20=20=20=205=20+=0A=20= src/include/replication/logicalctl.h=20=20=20=20=20=20=20=20=20=20|=20=20= =20=203=20+=0A=20src/include/replication/slot.h=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20|=20=20=2016=20+=0A=20= src/include/replication/slotscope.h=20=20=20=20=20=20=20=20=20=20=20|=20=20= =2049=20+=0A=20src/include/replication/walreceiver.h=20=20=20=20=20=20=20= =20=20|=20=20=2018=20+-=0A=20src/include/utils/guc_hooks.h=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20|=20=20=20=201=20+=0A=20= src/include/utils/rel.h=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20|=20=20=2025=20+-=0A=20src/test/recovery/meson.build=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20|=20=20=20=201=20+=0A=20= .../t/040_standby_failover_slots_sync.pl=20=20=20=20=20=20|=20=20147=20= ++=0A=20src/test/recovery/t/056_logical_slot_scope.pl=20|=20=20647=20= ++++++++=0A=20src/test/regress/expected/oidjoins.out=20=20=20=20=20=20=20= =20|=20=20=20=201=20+=0A=20src/test/regress/expected/rules.out=20=20=20=20= =20=20=20=20=20=20=20|=20=20=20=209=20+-=0A=20= src/tools/pgindent/typedefs.list=20=20=20=20=20=20=20=20=20=20=20=20=20=20= |=20=20=20=204=20+=0A=2055=20files=20changed,=203357=20insertions(+),=20= 114=20deletions(-)=0A=20create=20mode=20100644=20= src/backend/replication/logical/slotscope.c=0A=20create=20mode=20100644=20= src/include/catalog/pg_restricted_slot_relation.h=0A=20create=20mode=20= 100644=20src/include/replication/slotscope.h=0A=20create=20mode=20100644=20= src/test/recovery/t/056_logical_slot_scope.pl=0A=0Adiff=20--git=20= a/doc/src/sgml/catalogs.sgml=20b/doc/src/sgml/catalogs.sgml=0Aindex=20= 6066c4784f4..0a9d5f39aea=20100644=0A---=20a/doc/src/sgml/catalogs.sgml=0A= +++=20b/doc/src/sgml/catalogs.sgml=0A@@=20-280,6=20+280,11=20@@=0A=20=20=20= =20=20=20=20relation=20to=20publication=20mapping=0A=20=20= =20=20=20=20=0A=20=0A+=20=20=20=20=20=0A+=20=20=20=20=20=20= pg_restricted_= slot_relation=0A+=20=20=20=20=20=20= restricted=20logical=20replication=20slot=20to=20relation=20= mapping=0A+=20=20=20=20=20=0A+=0A=20=20=20=20=20=20=0A= =20=20=20=20=20=20=20pg_range=0A=20=20=20=20=20=20=20information=20about=20range=20= types=0A@@=20-2131,6=20+2136,17=20@@=20= SCRAM-SHA-256$<iteration=20= count>:&l=0A=20=20=20=20=20=20=20= =0A=20=20=20=20=20=20=0A=20=0A+=20=20=20=20=20=0A= +=20=20=20=20=20=20=0A+=20=20=20=20=20=20=20= relhasrestrictedslots=20bool=0A+=20= =20=20=20=20=20=0A+=20=20=20=20=20=20=0A+=20=20=20=20=20=20=20= True=20if=20this=20relation=20is=20(or=20recently=20was)=20required=20by=20= a=20restricted=0A+=20=20=20=20=20=20=20logical=20replication=20slot.=20= This=20flag=20can=20remain=20true=20until=20autovacuum=0A+=20=20=20=20=20= =20=20removes=20obsolete=20slot=20mappings.=0A+=20=20=20=20=20=20= =0A+=20=20=20=20=20=0A+=0A=20=20=20=20=20=20=0A= =20=20=20=20=20=20=20=0A=20=20=20=20=20=20=20=20= relisshared=20bool=0A@@=20= -7136,6=20+7152,48=20@@=20SCRAM-SHA-256$<iteration=20= count>:&l=0A=20=20=20=20=20=20=0A=20=20= =20=20=20=0A=20=20=20=20=0A+=20=0A+=20=0A= +=0A+=20=0A+=20=20= <structname>pg_restricted_slot_relation</structname>=0A+=0A= +=20=20=0A+=20=20= =20pg_restricted_slot_relation=0A+=20=20=0A= +=0A+=20=20=0A+=20=20=20The=20catalog=20= pg_restricted_slot_relation=20records=20a=0A+=20= =20=20conservative=20mapping=20from=20restricted=20logical=20replication=20= slots=20to=0A+=20=20=20relations=20that=20must=20write=20logical=20tuple=20= information.=20Rows=20belonging=20to=0A+=20=20=20dropped=20or=20replaced=20= slot=20incarnations=20can=20remain=20until=20autovacuum=20cleanup.=0A+=20= =20=20Rows=20made=20obsolete=20by=20publication=20contraction=20or=20= partition=20detach=20are=0A+=20=20=20retained=20conservatively=20and=20= can=20remain=20indefinitely.=0A+=20=20=0A+=0A+=20=20=0A+=20= =20=20<structname>pg_restricted_slot_relation</structname>=20= Columns=0A+=20=20=20=0A+=20=20=20=20=0A= +=20=20=20=20=20=0A+=20=20=20=20=20=20Column=20Type=0A+=20=20=20= =20=20Description=0A+=20=20=20=20= =0A+=20=20=20=20=0A+=20=20=20=20=20=0A+=20=20= =20=20=20=20rsrslotname=20name=0A= +=20=20=20=20=20Name=20of=20the=20restricted=20replication=20= slot=0A+=20=20=20=20=20=0A+=20=20= =20=20=20=20rsrrelid=20oid=0A+=20= =20=20=20=20=20(references=20pg_class.oid)=0A+=20=20=20=20=20OID=20of=20a=20= relation=20that=20must=20write=20logical=20tuple=20information,=0A+=20=20= =20=20=20=20or=20zero=20for=20a=20committed-initialization=20= marker=0A+=20=20=20=20=20=0A+=20=20= =20=20=20rsrincarnation=20int8=0A= +=20=20=20=20=20Restricted=20slot=20incarnation=20= associated=20with=20this=20mapping=20or=0A+=20=20=20=20=20=20= initialization=20marker=0A+=20=20=20=20=0A+=20= =20=20=0A=20=20=20
=0A=20=20=0A=20=0A@@=20= -8690,6=20+8748,17=20@@=20SCRAM-SHA-256$<iteration=20= count>:&l=0A=20=20=20=20=20=20=20= =0A=20=20=20=20=20=20=0A=20=0A+=20=20=20=20=20=0A= +=20=20=20=20=20=20=0A+=20=20=20=20=20=20=20= subunrestricted=20bool=0A+=20=20=20= =20=20=20=0A+=20=20=20=20=20=20=0A+=20=20=20=20=20=20=20If=20= true,=20the=20associated=20replication=20slots=20on=20the=20upstream=20= database=20are=0A+=20=20=20=20=20=20=20unrestricted.=20If=20false,=20= they=20are=20scoped=20to=20the=20subscription's=0A+=20=20=20=20=20=20=20= publications.=0A+=20=20=20=20=20=20=0A+=20=20=20=20=20= =0A+=0A=20=20=20=20=20=20=0A=20=20=20=20=20=20=20=0A=20=20= =20=20=20=20=20=20subretaindeadtuples=20= bool=0Adiff=20--git=20a/doc/src/sgml/config.sgml=20= b/doc/src/sgml/config.sgml=0Aindex=20596fd45a3db..2aebbeb5697=20100644=0A= ---=20a/doc/src/sgml/config.sgml=0A+++=20b/doc/src/sgml/config.sgml=0A@@=20= -12453,6=20+12453,24=20@@=20dynamic_library_path=20=3D=20= '/usr/local/lib/postgresql:$libdir'=0A=20=20=20=20=20=20=20=20=20the=20= value=20of=20effective_wal_level=20from=20the=20most=0A= =20=20=20=20=20=20=20=20=20upstream=20server=20in=20the=20replication=20= chain.=0A=20=20=20=20=20=20=20=20=0A+=20=20=20=20=20=0A= +=20=20=20=20=20=0A+=0A+=20=20=20=20=20=0A= +=20=20=20=20=20=20restricted_wal_level=20= (enum)=0A+=20=20=20=20=20=20=0A+=20=20=20=20=20=20= =20restricted_wal_level=20configuration=20= parameter=0A+=20=20=20=20=20=20=0A+=20=20=20=20=20=20= =0A+=20=20=20=20=20=20=0A+=20=20=20=20=20=20=20=0A= +=20=20=20=20=20=20=20=20Reports=20whether=20restricted=20logical=20= tuple=20logging=20is=20active.=20When=20this=0A+=20=20=20=20=20=20=20=20= value=20is=20logical=20while=0A+=20=20=20=20=20=20=20=20= =20is=20below=0A+=20=20=20=20= =20=20=20=20logical,=20logical=20tuple=20information=20= is=20written=20only=0A+=20=20=20=20=20=20=20=20for=20relations=20marked=20= as=20required=20by=20a=20restricted=20logical=20replication=0A+=20=20=20=20= =20=20=20=20slot.=20This=20is=20a=20read-only=20parameter.=0A+=20=20=20=20= =20=20=20=0A=20=20=20=20=20=20=20=0A=20=20=20=20=20=20= =0A=20=0Adiff=20--git=20= a/doc/src/sgml/func/func-admin.sgml=20= b/doc/src/sgml/func/func-admin.sgml=0Aindex=200eae1c1f616..90416e396a0=20= 100644=0A---=20a/doc/src/sgml/func/func-admin.sgml=0A+++=20= b/doc/src/sgml/func/func-admin.sgml=0A@@=20-1077,7=20+1077,7=20@@=20= postgres=3D#=20SELECT=20'0/0'::pg_lsn=20+=20pd.segment_number=20*=20= ps.setting::int=20+=20:offset=0A=20=20=20=20=20=20=20=20=20=0A= =20=20=20=20=20=20=20=20=20=20= pg_create_logical_replication_slot=0A=20=20=20=20=20=20= =20=20=20=0A-=20=20=20=20=20=20=20=20= pg_create_logical_replication_slot=20(=20= slot_name=20name,=20= plugin=20name=20,=20= temporary=20boolean,=20= twophase=20boolean,=20= failover=20boolean=20=20)=0A= +=20=20=20=20=20=20=20=20= pg_create_logical_replication_slot=20(=20= slot_name=20name,=20= plugin=20name=20,=20= temporary=20boolean,=20= twophase=20boolean,=20= failover=20boolean,=20= publications=20text[]=20=20= )=0A=20=20=20=20=20=20=20=20=20record=0A=20=20= =20=20=20=20=20=20=20(=20slot_name=20= name,=0A=20=20=20=20=20=20=20=20=20= lsn=20pg_lsn=20)=0A@@=20-1098,9=20= +1098,32=20@@=20postgres=3D#=20SELECT=20'0/0'::pg_lsn=20+=20= pd.segment_number=20*=20ps.setting::int=20+=20:offset=0A=20=20=20=20=20=20= =20=20=20failover,=20when=20set=20to=20true,=0A=20= =20=20=20=20=20=20=20=20specifies=20that=20this=20slot=20is=20enabled=20= to=20be=20synced=20to=20the=0A=20=20=20=20=20=20=20=20=20standbys=20so=20= that=20logical=20replication=20can=20be=20resumed=20after=0A-=20=20=20=20= =20=20=20=20failover.=20A=20call=20to=20this=20function=20has=20the=20= same=20effect=20as=0A+=20=20=20=20=20=20=20=20failover.=20When=20the=20= optional=20publications=20array=0A+=20=20=20=20=20= =20=20=20is=20supplied,=20it=20must=20contain=20at=20least=20one=20= publication=20name=20and=20the=0A+=20=20=20=20=20=20=20=20output=20= plugin=20must=20be=20pgoutput.=20Such=20a=20slot=20is=0A= +=20=20=20=20=20=20=20=20restricted:=20logical=20tuple=20information=20= is=20written=20only=20for=20relations=0A+=20=20=20=20=20=20=20=20covered=20= by=20one=20of=20the=20stored=20publications.=20Omitting=20the=20array=20= creates=0A+=20=20=20=20=20=20=20=20an=20unrestricted=20slot.=20A=20named=20= publication=20that=20currently=20contains=20no=0A+=20=20=20=20=20=20=20=20= relations=20is=20allowed.=20The=20restriction=0A+=20=20=20=20=20=20=20=20= is=20retained=20when=0A+=20=20=20=20=20=20=20=20=20is=20logical,=20even=0A= +=20=20=20=20=20=20=20=20though=20that=20setting=20causes=20logical=20= tuple=20information=20to=20be=20written=0A+=20=20=20=20=20=20=20=20for=20= every=20eligible=20relation.=20A=20call=20to=20this=20function=20has=20= the=20same=20effect=20as=0A=20=20=20=20=20=20=20=20=20the=20replication=20= protocol=20command=0A=20=20=20=20=20=20=20=20=20= CREATE_REPLICATION_SLOT=20...=20LOGICAL.=0A+=20=20=20=20= =20=20=20=20Restricted=20slots=20cannot=20be=20created=20by=20this=20= function=20inside=20an=20explicit=0A+=20=20=20=20=20=20=20=20transaction=20= block=20or=20subtransaction.=0A+=20=20=20=20=20=20=20=0A+=20=20=20= =20=20=20=20=0A+=20=20=20=20=20=20=20=20The=20slot=20stores=20= publication=20identities,=20not=20a=20fixed=20relation=20list.=0A+=20=20=20= =20=20=20=20=20Publication=20expansion,=20partition=20attachment,=20and=20= TOAST=20creation=20add=0A+=20=20=20=20=20=20=20=20writer-side=20relation=20= mappings=20synchronously.=20Mappings=20made=20obsolete=0A+=20=20=20=20=20= =20=20=20by=20dropping=20or=20recreating=20a=20slot=20are=20cleaned=20up=20= later=20by=20autovacuum.=0A+=20=20=20=20=20=20=20=20Mappings=20made=20= obsolete=20by=20publication=20contraction=20or=20partition=20detach=0A+=20= =20=20=20=20=20=20=20are=20retained=20indefinitely;=20this=20is=20safe=20= but=20can=20cause=20additional=0A+=20=20=20=20=20=20=20=20logical=20= tuple=20information=20to=20be=20written.=20At=20decoding=20startup,=0A+=20= =20=20=20=20=20=20=20pgoutput=20requires=20every=20= requested=20publication=20to=20be=0A+=20=20=20=20=20=20=20=20one=20of=20= the=20publications=20stored=20by=20the=20slot.=0A=20=20=20=20=20=20=20=20= =0A=20=20=20=20=20=20=20=0A=20=0A@@=20-1151,7=20= +1174,8=20@@=20postgres=3D#=20SELECT=20'0/0'::pg_lsn=20+=20= pd.segment_number=20*=20ps.setting::int=20+=20:offset=0A=20=20=20=20=20=20= =20=20=20is=20not=20copied=20and=20is=20set=20to=20= false=20by=20default.=20This=0A=20=20=20=20=20=20=20=20= =20is=20to=20avoid=20the=20risk=20of=20being=20unable=20to=20continue=20= logical=20replication=0A=20=20=20=20=20=20=20=20=20after=20failover=20to=20= standby=20where=20the=20slot=20is=20being=20synchronized.=20Copy=20of=0A= -=20=20=20=20=20=20=20=20an=20invalidated=20slot=20is=20not=20allowed.=0A= +=20=20=20=20=20=20=20=20an=20invalidated=20slot=20is=20not=20allowed.=20= A=20restricted=20logical=20slot=20cannot=20be=0A+=20=20=20=20=20=20=20=20= copied=20inside=20an=20explicit=20transaction=20block=20or=20= subtransaction.=0A=20=20=20=20=20=20=20=20=0A=20=20=20=20=20= =20=20=0A=20=0Adiff=20--git=20a/doc/src/sgml/protocol.sgml=20= b/doc/src/sgml/protocol.sgml=0Aindex=2049f81676712..45418274cb6=20100644=0A= ---=20a/doc/src/sgml/protocol.sgml=0A+++=20b/doc/src/sgml/protocol.sgml=0A= @@=20-2446,6=20+2446,18=20@@=20psql=20"dbname=3Dpostgres=20= replication=3Ddatabase"=20-c=20"IDENTIFY_SYSTEM;"=0A=20=20=20=20=20=20=20= =20=20=20=0A=20=20=20=20=20=20=20=20=20=0A=20=20=20=20=20= =20=20=20=0A+=0A+=20=20=20=20=20=20=20=0A+=20= =20=20=20=20=20=20=20PUBLICATION_NAMES=20publication_names=0A+=20= =20=20=20=20=20=20=20=0A+=20=20=20=20=20=20=20=20=20=0A+=20= =20=20=20=20=20=20=20=20=20A=20nonempty=20comma-separated=20list=20of=20= publication=20names.=20Supplying=20this=0A+=20=20=20=20=20=20=20=20=20=20= option=20creates=20a=20restricted=20logical=20slot=20and=20requires=20= the=0A+=20=20=20=20=20=20=20=20=20=20pgoutput=20= output=20plugin.=20Omitting=20it=20creates=20an=0A+=20=20=20=20=20=20=20=20= =20=20unrestricted=20slot.=0A+=20=20=20=20=20=20=20=20=20=0A+=20=20= =20=20=20=20=20=20=0A+=20=20=20=20=20=20=20=0A=20= =20=20=20=20=20=20=0A=20=0A=20=20=20=20=20=20=20=0A= diff=20--git=20a/doc/src/sgml/ref/alter_subscription.sgml=20= b/doc/src/sgml/ref/alter_subscription.sgml=0Aindex=20= 545264e8a0a..651bd107fef=20100644=0A---=20= a/doc/src/sgml/ref/alter_subscription.sgml=0A+++=20= b/doc/src/sgml/ref/alter_subscription.sgml=0A@@=20-143,6=20+143,13=20@@=20= ALTER=20SUBSCRIPTION=20name=20RENAME=20TO=20<=0A=20=20=20=20=20= =20=20REFRESH=20PUBLICATION.=0A=20=20=20=20=20=20= =0A=20=0A+=20=20=20=20=20=0A+=20=20=20=20=20=20The=20= publication=20list=20of=20a=20subscription=20created=20with=0A+=20=20=20=20= =20=20unrestricted_slot=20=3D=20false=20can=20only=20= be=20reduced.=0A+=20=20=20=20=20=20Publications=20removed=20from=20the=20= subscription=20cannot=20be=20added=20again=20without=0A+=20=20=20=20=20=20= recreating=20the=20subscription.=0A+=20=20=20=20=20=0A+=0A=20=20=20= =20=20=20=0A=20=20=20=20=20=20=20= publication_option=20specifies=20additional=0A= =20=20=20=20=20=20=20options=20for=20this=20operation.=20=20The=20= supported=20options=20are:=0Adiff=20--git=20= a/doc/src/sgml/ref/create_subscription.sgml=20= b/doc/src/sgml/ref/create_subscription.sgml=0Aindex=20= 25a81e2e62a..b707f88b171=20100644=0A---=20= a/doc/src/sgml/ref/create_subscription.sgml=0A+++=20= b/doc/src/sgml/ref/create_subscription.sgml=0A@@=20-164,6=20+164,26=20@@=20= CREATE=20SUBSCRIPTION=20subscription_name=0A=20= =20=20=20=20=20=20=20=20=20=20for=20examples.=0A=20=20=20=20=20=20=20=20=20= =20=0A+=20=20=20=20=20=20=20=0A+=20=20=20=20=20=20=20= =0A+=0A+=20=20=20=20=20=20=20=0A+=20=20=20= =20=20=20=20=20unrestricted_slot=20= (boolean)=0A+=20=20=20=20=20=20=20=20=0A+=20= =20=20=20=20=20=20=20=20=0A+=20=20=20=20=20=20=20=20=20=20= Specifies=20whether=20the=20replication=20slot=20created=20on=20the=20= publisher=0A+=20=20=20=20=20=20=20=20=20=20writes=20logical=20tuple=20= information=20for=20every=20eligible=20relation.=20The=0A+=20=20=20=20=20= =20=20=20=20=20default=20is=20true.=20When=20set=20to=20= false,=0A+=20=20=20=20=20=20=20=20=20=20the=20slot=20= is=20scoped=20to=20the=20relations=20currently=20included=20in=20the=0A+=20= =20=20=20=20=20=20=20=20=20subscription's=20publications.=0A+=20=20=20=20= =20=20=20=20=20=0A+=20=20=20=20=20=20=20=20=20=0A+=20=20=20=20= =20=20=20=20=20=20When=20create_slot=20is=20= true,=20this=0A+=20=20=20=20=20=20=20=20=20=20option=20= also=20controls=20the=20main=20slot=20created=20on=20the=20publisher.=20= When=0A+=20=20=20=20=20=20=20=20=20=20create_slot=20= is=20false,=20it=20does=0A+=20=20=20=20=20=20=20=20=20= =20not=20alter=20the=20separately=20created=20main=20slot,=20but=20still=20= controls=20any=0A+=20=20=20=20=20=20=20=20=20=20table=20synchronization=20= slots=20created=20by=20this=20subscription.=0A+=20=20=20=20=20=20=20=20=20= =0A=20=20=20=20=20=20=20=20=20=0A=20=20=20=20=20=20=20=20= =0A=20=0Adiff=20--git=20a/doc/src/sgml/system-views.sgml=20= b/doc/src/sgml/system-views.sgml=0Aindex=205ea19d68622..8e5e4879b4f=20= 100644=0A---=20a/doc/src/sgml/system-views.sgml=0A+++=20= b/doc/src/sgml/system-views.sgml=0A@@=20-3175,6=20+3175,59=20@@=20SELECT=20= *=20FROM=20pg_locks=20pl=20LEFT=20JOIN=20pg_prepared_xacts=20ppx=0A=20=20= =20=20=20=20=20=20=0A=20=20=20=20=20=20=20=0A= =20=20=20=20=20=20=0A+=0A+=20=20=20=20=20=0A+=20=20=20=20=20=20= =0A= +=20=20=20=20=20=20=20unrestricted=20= bool=0A+=20=20=20=20=20=20=0A+=20=20=20=20=20=20= =0A+=20=20=20=20=20=20=20True=20if=20the=20logical=20slot=20writes=20= logical=20tuple=20information=20for=20every=0A+=20=20=20=20=20=20=20= eligible=20relation,=20false=20if=20it=20is=20restricted=20by=20its=20= stored=0A+=20=20=20=20=20=20=20publications,=20or=20= NULL=20for=20a=20physical=20slot.=0A+=20=20=20=20=20=20= =0A+=20=20=20=20=20=0A+=0A+=20=20=20=20=20=0A+=20= =20=20=20=20=20=0A+=20=20=20=20=20=20=20= publication_oids=20oid[]=0A+=20=20= =20=20=20=20=0A+=20=20=20=20=20=20=0A+=20=20=20=20=20=20=20= Publications=20stored=20by=20a=20restricted=20logical=20slot,=20or=20= NULL=0A+=20=20=20=20=20=20=20for=20an=20unrestricted=20= or=20physical=20slot.=0A+=20=20=20=20=20=20=0A+=20=20=20=20= =20=0A+=0A+=20=20=20=20=20=0A+=20=20=20=20=20=20=0A+=20=20= =20=20=20=20=20restricted_scope_ready=20= bool=0A+=20=20=20=20=20=20=0A+=20=20=20=20=20=20= =0A+=20=20=20=20=20=20=20True=20when=20a=20restricted=20logical=20= slot's=20initial=20relation=20mappings=20have=0A+=20=20=20=20=20=20=20= committed=20and=20the=20slot=20can=20be=20used,=20false=20while=20= initialization=20is=20in=0A+=20=20=20=20=20=20=20progress,=20or=20= NULL=20for=20an=20unrestricted=20or=20physical=20= slot.=0A+=20=20=20=20=20=20=0A+=20=20=20=20=20=0A+=0A= +=20=20=20=20=20=0A+=20=20=20=20=20=20=0A+=20=20= =20=20=20=20=20restricted_scope_incarnation=20= bigint=0A+=20=20=20=20=20=20=0A+=20=20=20=20=20=20= =0A+=20=20=20=20=20=20=20Internal=20identity=20of=20a=20restricted=20= slot=20initialization,=20or=0A+=20=20=20=20=20=20=20= NULL=20for=20an=20unrestricted=20or=20physical=20= slot.=0A+=20=20=20=20=20=20=0A+=20=20=20=20=20=0A+=0A= +=20=20=20=20=20=0A+=20=20=20=20=20=20=0A+=20=20= =20=20=20=20=20restricted_scope_ready_lsn=20= pg_lsn=0A+=20=20=20=20=20=20=0A+=20=20=20=20=20=20= =0A+=20=20=20=20=20=20=20WAL=20location=20following=20the=20commit=20= of=20a=20restricted=20slot's=20initial=0A+=20=20=20=20=20=20=20relation=20= mappings,=20or=20NULL=20while=20initialization=20is=0A= +=20=20=20=20=20=20=20incomplete=20or=20for=20an=20unrestricted=20or=20= physical=20slot.=0A+=20=20=20=20=20=20=0A+=20=20=20=20=20= =0A=20=20=20=20=20=0A=20=20=20=20=0A=20=20=20= =0Adiff=20--git=20a/src/backend/access/heap/heapam.c=20= b/src/backend/access/heap/heapam.c=0Aindex=2072d6541734c..7ee61132223=20= 100644=0A---=20a/src/backend/access/heap/heapam.c=0A+++=20= b/src/backend/access/heap/heapam.c=0A@@=20-2136,9=20+2136,14=20@@=20= heap_insert(Relation=20relation,=20HeapTuple=20tup,=20CommandId=20cid,=0A= =20=09=09/*=0A=20=09=09=20*=20For=20logical=20decoding,=20we=20need=20= the=20tuple=20even=20if=20we're=20doing=20a=20full=0A=20=09=09=20*=20= page=20write,=20so=20make=20sure=20it's=20included=20even=20if=20we=20= take=20a=20full-page=0A-=09=09=20*=20image.=20(XXX=20We=20could=20= alternatively=20store=20a=20pointer=20into=20the=20FPW).=0A+=09=09=20*=20= image.=20=20A=20speculative=20confirmation=20has=20no=20logical-data=20= flag=20of=20its=0A+=09=09=20*=20own,=20so=20retain=20the=20speculative=20= insertion=20whenever=20logical=20decoding=0A+=09=09=20*=20is=20active,=20= even=20if=20the=20relation=20is=20outside=20a=20restricted=20slot's=0A+=09= =09=20*=20scope.=20(XXX=20We=20could=20alternatively=20store=20a=20= pointer=20into=20the=20FPW).=0A=20=09=09=20*/=0A-=09=09if=20= (RelationIsLogicallyLogged(relation)=20&&=0A+=09=09if=20= ((RelationIsLogicallyLogged(relation)=20||=0A+=09=09=09=20((options=20&=20= HEAP_INSERT_SPECULATIVE)=20&&=0A+=09=09=09=20=20= XLogLogicalInfoActive()))=20&&=0A=20=09=09=09!(options=20&=20= HEAP_INSERT_NO_LOGICAL))=0A=20=09=09{=0A=20=09=09=09xlrec.flags=20|=3D=20= XLH_INSERT_CONTAINS_NEW_TUPLE;=0Adiff=20--git=20= a/src/backend/access/transam/xlog.c=20= b/src/backend/access/transam/xlog.c=0Aindex=20c3baca5193b..651f6c30314=20= 100644=0A---=20a/src/backend/access/transam/xlog.c=0A+++=20= b/src/backend/access/transam/xlog.c=0A@@=20-5250,7=20+5250,15=20@@=20= show_effective_wal_level(void)=0A=20=09if=20(RecoveryInProgress())=0A=20=09= =09return=20IsXLogLogicalInfoEnabled()=20?=20"logical"=20:=20"replica";=0A= =20=0A-=09return=20XLogLogicalInfoActive()=20?=20"logical"=20:=20= "replica";=0A+=09return=20XLogFullLogicalInfoActive()=20?=20"logical"=20= :=20"replica";=0A+}=0A+=0A+const=20char=20*=0A= +show_restricted_wal_level(void)=0A+{=0A+=09if=20(wal_level=20=3D=3D=20= WAL_LEVEL_MINIMAL)=0A+=09=09return=20"minimal";=0A+=09return=20= IsXLogRestrictedInfoEnabled()=20?=20"logical"=20:=20"replica";=0A=20}=0A=20= =0A=20/*=0Adiff=20--git=20a/src/backend/catalog/heap.c=20= b/src/backend/catalog/heap.c=0Aindex=20b018f26545b..aeb95eb4b96=20100644=0A= ---=20a/src/backend/catalog/heap.c=0A+++=20b/src/backend/catalog/heap.c=0A= @@=20-49,6=20+49,7=20@@=0A=20#include=20"catalog/pg_namespace.h"=0A=20= #include=20"catalog/pg_opclass.h"=0A=20#include=20= "catalog/pg_partitioned_table.h"=0A+#include=20= "catalog/pg_publication.h"=0A=20#include=20"catalog/pg_statistic.h"=0A=20= #include=20"catalog/pg_subscription_rel.h"=0A=20#include=20= "catalog/pg_tablespace.h"=0A@@=20-67,6=20+68,7=20@@=0A=20#include=20= "parser/parsetree.h"=0A=20#include=20"partitioning/partdesc.h"=0A=20= #include=20"pgstat.h"=0A+#include=20"replication/slotscope.h"=0A=20= #include=20"storage/lmgr.h"=0A=20#include=20"storage/predicate.h"=0A=20= #include=20"utils/array.h"=0A@@=20-968,6=20+970,8=20@@=20= InsertPgClassTuple(Relation=20pg_class_desc,=0A=20=09= values[Anum_pg_class_relispopulated=20-=201]=20=3D=20= BoolGetDatum(rd_rel->relispopulated);=0A=20=09= values[Anum_pg_class_relreplident=20-=201]=20=3D=20= CharGetDatum(rd_rel->relreplident);=0A=20=09= values[Anum_pg_class_relispartition=20-=201]=20=3D=20= BoolGetDatum(rd_rel->relispartition);=0A+=09= values[Anum_pg_class_relhasrestrictedslots=20-=201]=20=3D=0A+=09=09= BoolGetDatum(rd_rel->relhasrestrictedslots);=0A=20=09= values[Anum_pg_class_relrewrite=20-=201]=20=3D=20= ObjectIdGetDatum(rd_rel->relrewrite);=0A=20=09= values[Anum_pg_class_relfrozenxid=20-=201]=20=3D=20= TransactionIdGetDatum(rd_rel->relfrozenxid);=0A=20=09= values[Anum_pg_class_relminmxid=20-=201]=20=3D=20= MultiXactIdGetDatum(rd_rel->relminmxid);=0A@@=20-1040,6=20+1044,7=20@@=20= AddNewRelationTuple(Relation=20pg_class_desc,=0A=20=0A=20=09/*=20= relispartition=20is=20always=20set=20by=20updating=20this=20tuple=20= later=20*/=0A=20=09new_rel_reltup->relispartition=20=3D=20false;=0A+=09= new_rel_reltup->relhasrestrictedslots=20=3D=20false;=0A=20=0A=20=09/*=20= fill=20rd_att's=20type=20ID=20with=20something=20sane=20even=20if=20= reltype=20is=20zero=20*/=0A=20=09new_rel_desc->rd_att->tdtypeid=20=3D=20= new_type_oid=20?=20new_type_oid=20:=20RECORDOID;=0A@@=20-1540,6=20= +1545,21=20@@=20heap_create_with_catalog(const=20char=20*relname,=0A=20=09= if=20(oncommit=20!=3D=20ONCOMMIT_NOOP)=0A=20=09=09= register_on_commit_action(relid,=20oncommit);=0A=20=0A+=09/*=20A=20new=20= table=20may=20be=20covered=20immediately=20by=20a=20schema=20= publication.=20*/=0A+=09if=20(!IsBootstrapProcessingMode()=20&&=0A+=09=09= (relkind=20=3D=3D=20RELKIND_RELATION=20||=20relkind=20=3D=3D=20= RELKIND_PARTITIONED_TABLE))=0A+=09{=0A+=09=09List=09=20=20=20= *publications;=0A+=09=09List=09=20=20=20*relations=20=3D=20= list_make1_oid(relid);=0A+=0A+=09=09CommandCounterIncrement();=0A+=09=09= publications=20=3D=20GetSchemaPublications(relnamespace);=0A+=09=09= foreach_oid(pubid,=20publications)=0A+=09=09=09= LogicalSlotScopePublicationAddRelations(pubid,=20relations);=0A+=09=09= list_free(publications);=0A+=09=09list_free(relations);=0A+=09}=0A+=0A=20= =09/*=0A=20=09=20*=20ok,=20the=20relation=20has=20been=20cataloged,=20so=20= close=20our=20relations=20and=20return=0A=20=09=20*=20the=20OID=20of=20= the=20newly=20created=20relation.=0A@@=20-1847,6=20+1867,13=20@@=20= heap_drop_with_catalog(Oid=20relid)=0A=20=09=20*/=0A=20=09rel=20=3D=20= relation_open(relid,=20AccessExclusiveLock);=0A=20=0A+=09/*=0A+=09=20*=20= Remove=20any=20logical=20replication=20slot=20that=20is=20using=20this=20= relation.=20=20This=0A+=09=20*=20must=20be=20done=20before=20we=20remove=20= the=20pg_class=20row,=20else=20the=20slot=20will=20be=0A+=09=20*=20left=20= pointing=20to=20a=20non-existent=20relation.=0A+=09=20*/=0A+=09= LogicalSlotScopeRelationDrop(relid);=0A+=0A=20=09/*=0A=20=09=20*=20There=20= can=20no=20longer=20be=20anyone=20*else*=20touching=20the=20relation,=20= but=20we=0A=20=09=20*=20might=20still=20have=20open=20queries=20or=20= cursors,=20or=20pending=20trigger=20events,=20in=0Adiff=20--git=20= a/src/backend/catalog/pg_subscription.c=20= b/src/backend/catalog/pg_subscription.c=0Aindex=20= f1e8b624d8e..eed3582c806=20100644=0A---=20= a/src/backend/catalog/pg_subscription.c=0A+++=20= b/src/backend/catalog/pg_subscription.c=0A@@=20-129,6=20+129,7=20@@=20= GetSubscription(Oid=20subid,=20bool=20missing_ok)=0A=20=09= sub->passwordrequired=20=3D=20subform->subpasswordrequired;=0A=20=09= sub->runasowner=20=3D=20subform->subrunasowner;=0A=20=09sub->failover=20= =3D=20subform->subfailover;=0A+=09sub->unrestricted=20=3D=20= subform->subunrestricted;=0A=20=09sub->retaindeadtuples=20=3D=20= subform->subretaindeadtuples;=0A=20=09sub->maxretention=20=3D=20= subform->submaxretention;=0A=20=09sub->retentionactive=20=3D=20= subform->subretentionactive;=0Adiff=20--git=20= a/src/backend/catalog/system_views.sql=20= b/src/backend/catalog/system_views.sql=0Aindex=20= 8612d99a890..a6dcfc2be04=20100644=0A---=20= a/src/backend/catalog/system_views.sql=0A+++=20= b/src/backend/catalog/system_views.sql=0A@@=20-1123,7=20+1123,12=20@@=20= CREATE=20VIEW=20pg_replication_slots=20AS=0A=20=20=20=20=20=20=20=20=20=20= =20=20=20L.invalidation_reason,=0A=20=20=20=20=20=20=20=20=20=20=20=20=20= L.failover,=0A=20=20=20=20=20=20=20=20=20=20=20=20=20L.synced,=0A-=20=20=20= =20=20=20=20=20=20=20=20=20L.slotsync_skip_reason=0A+=20=20=20=20=20=20=20= =20=20=20=20=20L.slotsync_skip_reason,=0A+=20=20=20=20=20=20=20=20=20=20=20= =20L.unrestricted,=0A+=20=20=20=20=20=20=20=20=20=20=20=20= L.publication_oids,=0A+=09=09=09L.restricted_scope_ready,=0A+=09=09=09= L.restricted_scope_incarnation,=0A+=09=09=09L.restricted_scope_ready_lsn=0A= =20=20=20=20=20FROM=20pg_get_replication_slots()=20AS=20L=0A=20=20=20=20=20= =20=20=20=20=20=20=20=20LEFT=20JOIN=20pg_database=20D=20ON=20(L.datoid=20= =3D=20D.oid);=0A=20=0A@@=20-1537,6=20+1542,7=20@@=20REVOKE=20ALL=20ON=20= pg_subscription=20FROM=20public;=0A=20GRANT=20SELECT=20(oid,=20subdbid,=20= subskiplsn,=20subname,=20subowner,=20subenabled,=0A=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20subbinary,=20substream,=20subtwophasestate,=20= subdisableonerr,=0A=20=09=09=09=20=20subpasswordrequired,=20= subrunasowner,=20subfailover,=0A+=09=09=09=20=20subunrestricted,=0A=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20subretaindeadtuples,=20= submaxretention,=20subretentionactive,=0A=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20subserver,=20subconflictlogrelid,=20subconflictlogdest,=20= subslotname,=0A=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= subsynccommit,=20subwalrcvtimeout,=20subpublications,=20suborigin)=0A= diff=20--git=20a/src/backend/catalog/toasting.c=20= b/src/backend/catalog/toasting.c=0Aindex=204aa52a4bd25..6822c1177ea=20= 100644=0A---=20a/src/backend/catalog/toasting.c=0A+++=20= b/src/backend/catalog/toasting.c=0A@@=20-30,6=20+30,7=20@@=0A=20#include=20= "catalog/toasting.h"=0A=20#include=20"miscadmin.h"=0A=20#include=20= "nodes/makefuncs.h"=0A+#include=20"replication/slotscope.h"=0A=20= #include=20"utils/fmgroids.h"=0A=20#include=20"utils/rel.h"=0A=20= #include=20"utils/syscache.h"=0A@@=20-397,6=20+398,7=20@@=20= create_toast_table(Relation=20rel,=20Oid=20toastOid,=20Oid=20= toastIndexOid,=0A=20=09=20*=20Make=20changes=20visible=0A=20=09=20*/=0A=20= =09CommandCounterIncrement();=0A+=09= LogicalSlotScopeNoteToastCreation(relOid,=20toast_relid);=0A=20=0A=20=09= return=20true;=0A=20}=0Adiff=20--git=20= a/src/backend/commands/publicationcmds.c=20= b/src/backend/commands/publicationcmds.c=0Aindex=20= 96838730fe1..23c95759bb0=20100644=0A---=20= a/src/backend/commands/publicationcmds.c=0A+++=20= b/src/backend/commands/publicationcmds.c=0A@@=20-38,6=20+38,7=20@@=0A=20= #include=20"parser/parse_collate.h"=0A=20#include=20= "parser/parse_relation.h"=0A=20#include=20"rewrite/rewriteHandler.h"=0A= +#include=20"replication/slotscope.h"=0A=20#include=20"storage/lmgr.h"=0A= =20#include=20"utils/acl.h"=0A=20#include=20"utils/builtins.h"=0A@@=20= -2036,6=20+2037,7=20@@=20PublicationAddTables(Oid=20pubid,=20List=20= *rels,=20bool=20if_not_exists,=0A=20=09=09=09=09=09=20= AlterPublicationStmt=20*stmt)=0A=20{=0A=20=09ListCell=20=20=20*lc;=0A+=09= List=09=20=20=20*relids=20=3D=20NIL;=0A=20=0A=20=09foreach(lc,=20rels)=0A= =20=09{=0A@@=20-2049,6=20+2051,7=20@@=20PublicationAddTables(Oid=20= pubid,=20List=20*rels,=20bool=20if_not_exists,=0A=20=09=09=09=09=09=09=20= =20=20RelationGetRelationName(rel));=0A=20=0A=20=09=09obj=20=3D=20= publication_add_relation(pubid,=20pub_rel,=20if_not_exists,=20stmt);=0A+=09= =09relids=20=3D=20lappend_oid(relids,=20RelationGetRelid(rel));=0A=20=09=09= if=20(stmt)=0A=20=09=09{=0A=20=09=09=09= EventTriggerCollectSimpleCommand(obj,=20InvalidObjectAddress,=0A@@=20= -2058,6=20+2061,8=20@@=20PublicationAddTables(Oid=20pubid,=20List=20= *rels,=20bool=20if_not_exists,=0A=20=09=09=09=09=09=09=09=09=09=20=20=20= obj.objectId,=200);=0A=20=09=09}=0A=20=09}=0A+=09= LogicalSlotScopePublicationAddRelations(pubid,=20relids);=0A+=09= list_free(relids);=0A=20}=0A=20=0A=20/*=0A@@=20-2118,8=20+2123,13=20@@=20= PublicationAddSchemas(Oid=20pubid,=20List=20*schemas,=20bool=20= if_not_exists,=0A=20=09{=0A=20=09=09Oid=09=09=09schemaid=20=3D=20= lfirst_oid(lc);=0A=20=09=09ObjectAddress=20obj;=0A+=09=09List=09=20=20=20= *relations;=0A=20=0A=20=09=09obj=20=3D=20publication_add_schema(pubid,=20= schemaid,=20if_not_exists);=0A+=09=09relations=20=3D=20= GetSchemaPublicationRelations(schemaid,=0A+=09=09=09=09=09=09=09=09=09=09= =09=09=20=20PUBLICATION_PART_ALL);=0A+=09=09= LogicalSlotScopePublicationAddRelations(pubid,=20relations);=0A+=09=09= list_free(relations);=0A=20=09=09if=20(stmt)=0A=20=09=09{=0A=20=09=09=09= EventTriggerCollectSimpleCommand(obj,=20InvalidObjectAddress,=0Adiff=20= --git=20a/src/backend/commands/subscriptioncmds.c=20= b/src/backend/commands/subscriptioncmds.c=0Aindex=20= 6e805394808..dee887a3546=20100644=0A---=20= a/src/backend/commands/subscriptioncmds.c=0A+++=20= b/src/backend/commands/subscriptioncmds.c=0A@@=20-83,6=20+83,7=20@@=0A=20= #define=20SUBOPT_LSN=09=09=09=09=090x00020000=0A=20#define=20= SUBOPT_ORIGIN=09=09=09=090x00040000=0A=20#define=20= SUBOPT_CONFLICT_LOG_DEST=090x00080000=0A+#define=20= SUBOPT_UNRESTRICTED_SLOT=090x00100000=0A=20=0A=20/*=20check=20if=20the=20= 'val'=20has=20'bits'=20set=20*/=0A=20#define=20IsSet(val,=20bits)=20=20= (((val)=20&=20(bits))=20=3D=3D=20(bits))=0A@@=20-108,6=20+109,7=20@@=20= typedef=20struct=20SubOpts=0A=20=09bool=09=09passwordrequired;=0A=20=09= bool=09=09runasowner;=0A=20=09bool=09=09failover;=0A+=09bool=09=09= unrestricted;=0A=20=09bool=09=09retaindeadtuples;=0A=20=09int32=09=09= maxretention;=0A=20=09char=09=20=20=20*origin;=0A@@=20-199,6=20+201,8=20= @@=20parse_subscription_options(ParseState=20*pstate,=20List=20= *stmt_options,=0A=20=09=09opts->runasowner=20=3D=20false;=0A=20=09if=20= (IsSet(supported_opts,=20SUBOPT_FAILOVER))=0A=20=09=09opts->failover=20=3D= =20false;=0A+=09if=20(IsSet(supported_opts,=20SUBOPT_UNRESTRICTED_SLOT))=0A= +=09=09opts->unrestricted=20=3D=20true;=0A=20=09if=20= (IsSet(supported_opts,=20SUBOPT_RETAIN_DEAD_TUPLES))=0A=20=09=09= opts->retaindeadtuples=20=3D=20false;=0A=20=09if=20= (IsSet(supported_opts,=20SUBOPT_MAX_RETENTION_DURATION))=0A@@=20-350,6=20= +354,15=20@@=20parse_subscription_options(ParseState=20*pstate,=20List=20= *stmt_options,=0A=20=09=09=09opts->specified_opts=20|=3D=20= SUBOPT_FAILOVER;=0A=20=09=09=09opts->failover=20=3D=20= defGetBoolean(defel);=0A=20=09=09}=0A+=09=09else=20if=20= (IsSet(supported_opts,=20SUBOPT_UNRESTRICTED_SLOT)=20&&=0A+=09=09=09=09=20= strcmp(defel->defname,=20"unrestricted_slot")=20=3D=3D=200)=0A+=09=09{=0A= +=09=09=09if=20(IsSet(opts->specified_opts,=20SUBOPT_UNRESTRICTED_SLOT))=0A= +=09=09=09=09errorConflictingDefElem(defel,=20pstate);=0A+=0A+=09=09=09= opts->specified_opts=20|=3D=20SUBOPT_UNRESTRICTED_SLOT;=0A+=09=09=09= opts->unrestricted=20=3D=20defGetBoolean(defel);=0A+=09=09}=0A=20=09=09= else=20if=20(IsSet(supported_opts,=20SUBOPT_RETAIN_DEAD_TUPLES)=20&&=0A=20= =09=09=09=09=20strcmp(defel->defname,=20"retain_dead_tuples")=20=3D=3D=20= 0)=0A=20=09=09{=0A@@=20-539,6=20+552,7=20@@=20= parse_subscription_options(ParseState=20*pstate,=20List=20*stmt_options,=0A= =20=09=09=09=09=09=09=09=09"slot_name=20=3D=20NONE",=20"create_slot=20=3D=20= false")));=0A=20=09=09}=0A=20=09}=0A+=0A=20}=0A=20=0A=20/*=0A@@=20-691,6=20= +705,7=20@@=20CreateSubscription(ParseState=20*pstate,=20= CreateSubscriptionStmt=20*stmt,=0A=20=09=20*=20Connection=20and=20= publication=20should=20not=20be=20specified=20here.=0A=20=09=20*/=0A=20=09= supported_opts=20=3D=20(SUBOPT_CONNECT=20|=20SUBOPT_ENABLED=20|=20= SUBOPT_CREATE_SLOT=20|=0A+=09=09=09=09=09=20=20SUBOPT_UNRESTRICTED_SLOT=20= |=0A=20=09=09=09=09=09=20=20SUBOPT_SLOT_NAME=20|=20SUBOPT_COPY_DATA=20|=0A= =20=09=09=09=09=09=20=20SUBOPT_SYNCHRONOUS_COMMIT=20|=20SUBOPT_BINARY=20= |=0A=20=09=09=09=09=09=20=20SUBOPT_STREAMING=20|=20= SUBOPT_TWOPHASE_COMMIT=20|=0A@@=20-864,6=20+879,7=20@@=20= CreateSubscription(ParseState=20*pstate,=20CreateSubscriptionStmt=20= *stmt,=0A=20=09values[Anum_pg_subscription_subpasswordrequired=20-=201]=20= =3D=20BoolGetDatum(opts.passwordrequired);=0A=20=09= values[Anum_pg_subscription_subrunasowner=20-=201]=20=3D=20= BoolGetDatum(opts.runasowner);=0A=20=09= values[Anum_pg_subscription_subfailover=20-=201]=20=3D=20= BoolGetDatum(opts.failover);=0A+=09= values[Anum_pg_subscription_subunrestricted=20-=201]=20=3D=20= BoolGetDatum(opts.unrestricted);=0A=20=09= values[Anum_pg_subscription_subretaindeadtuples=20-=201]=20=3D=0A=20=09=09= BoolGetDatum(opts.retaindeadtuples);=0A=20=09= values[Anum_pg_subscription_submaxretention=20-=201]=20=3D=0A@@=20= -1065,7=20+1081,8=20@@=20CreateSubscription(ParseState=20*pstate,=20= CreateSubscriptionStmt=20*stmt,=0A=20=09=09=09=09=09twophase_enabled=20=3D= =20true;=0A=20=0A=20=09=09=09=09walrcv_create_slot(wrconn,=20= opts.slot_name,=20false,=20twophase_enabled,=0A-=09=09=09=09=09=09=09=09=20= =20=20opts.failover,=20CRS_NOEXPORT_SNAPSHOT,=20NULL);=0A+=09=09=09=09=09= =09=09=09=20=20=20opts.failover,=20opts.unrestricted,=20publications,=0A= +=09=09=09=09=09=09=09=09=20=20=20CRS_NOEXPORT_SNAPSHOT,=20NULL);=0A=20=0A= =20=09=09=09=09if=20(twophase_enabled)=0A=20=09=09=09=09=09= UpdateTwoPhaseState(subid,=20LOGICALREP_TWOPHASE_STATE_ENABLED);=0A@@=20= -1738,6=20+1755,44=20@@=20AlterSubscription(ParseState=20*pstate,=20= AlterSubscriptionStmt=20*stmt,=0A=20=0A=20=09sub=20=3D=20= GetSubscription(subid,=20false);=0A=20=0A+=09/*=0A+=09=20*=20A=20= restricted=20slot=20stores=20an=20immutable=20set=20of=20publication=20= identities.=0A+=09=20*=20The=20subscriber=20does=20not=20retain=20that=20= original=20set,=20so=20only=20allow=20its=0A+=09=20*=20current=20= publication=20list=20to=20be=20reduced.=20=20This=20prevents=20the=20= apply=20worker=0A+=09=20*=20from=20failing=20asynchronously=20on=20an=20= identity=20absent=20from=20the=20slot.=0A+=09=20*/=0A+=09if=20= (!sub->unrestricted=20&&=0A+=09=09(stmt->kind=20=3D=3D=20= ALTER_SUBSCRIPTION_SET_PUBLICATION=20||=0A+=09=09=20stmt->kind=20=3D=3D=20= ALTER_SUBSCRIPTION_ADD_PUBLICATION))=0A+=09{=0A+=09=09if=20(stmt->kind=20= =3D=3D=20ALTER_SUBSCRIPTION_ADD_PUBLICATION)=0A+=09=09=09ereport(ERROR,=0A= +=09=09=09=09=09(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),=0A+=09=09=09=09=09= =20errmsg("cannot=20add=20publications=20to=20restricted=20subscription=20= \"%s\"",=0A+=09=09=09=09=09=09=09stmt->subname),=0A+=09=09=09=09=09=20= errhint("Drop=20and=20recreate=20the=20subscription=20to=20expand=20its=20= publication=20list.")));=0A+=0A+=09=09foreach_node(String,=20= publication,=20stmt->publication)=0A+=09=09{=0A+=09=09=09bool=09=09found=20= =3D=20false;=0A+=0A+=09=09=09foreach_node(String,=20oldpublication,=20= sub->publications)=0A+=09=09=09{=0A+=09=09=09=09if=20= (strcmp(strVal(publication),=20strVal(oldpublication))=20=3D=3D=200)=0A+=09= =09=09=09{=0A+=09=09=09=09=09found=20=3D=20true;=0A+=09=09=09=09=09= break;=0A+=09=09=09=09}=0A+=09=09=09}=0A+=09=09=09if=20(!found)=0A+=09=09= =09=09ereport(ERROR,=0A+=09=09=09=09=09=09= (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),=0A+=09=09=09=09=09=09=20= errmsg("cannot=20add=20publication=20\"%s\"=20to=20restricted=20= subscription=20\"%s\"",=0A+=09=09=09=09=09=09=09=09strVal(publication),=20= stmt->subname),=0A+=09=09=09=09=09=09=20errhint("Drop=20and=20recreate=20= the=20subscription=20to=20expand=20its=20publication=20list.")));=0A+=09=09= }=0A+=09}=0A+=0A=20=09/*=0A=20=09=20*=20Determine=20in=20advance=20= whether=20we=20need=20the=20original=20conninfo=20or=20not,=20so=0A=20=09= =20*=20that=20errors=20are=20generated=20consistently=20in=20cases=20= where=20we=20do=20need=20it;=0Adiff=20--git=20= a/src/backend/commands/tablecmds.c=20b/src/backend/commands/tablecmds.c=0A= index=2012beb37246f..9996014695e=20100644=0A---=20= a/src/backend/commands/tablecmds.c=0A+++=20= b/src/backend/commands/tablecmds.c=0A@@=20-49,6=20+49,7=20@@=0A=20= #include=20"catalog/pg_opclass.h"=0A=20#include=20"catalog/pg_policy.h"=0A= =20#include=20"catalog/pg_proc.h"=0A+#include=20= "catalog/pg_publication.h"=0A=20#include=20= "catalog/pg_publication_rel.h"=0A=20#include=20"catalog/pg_rewrite.h"=0A=20= #include=20"catalog/pg_statistic_ext.h"=0A@@=20-92,6=20+93,7=20@@=0A=20= #include=20"rewrite/rewriteDefine.h"=0A=20#include=20= "rewrite/rewriteHandler.h"=0A=20#include=20"rewrite/rewriteManip.h"=0A= +#include=20"replication/slotscope.h"=0A=20#include=20"storage/bufmgr.h"=0A= =20#include=20"storage/lmgr.h"=0A=20#include=20"storage/lock.h"=0A@@=20= -3658,6=20+3660,11=20@@=20StoreCatalogInheritance1(Oid=20relationId,=20= Oid=20parentOid,=0A=20=0A=20=09/*=20store=20the=20pg_inherits=20row=20*/=0A= =20=09StoreSingleInheritance(relationId,=20parentOid,=20seqNumber);=0A+=09= if=20(child_is_partition)=0A+=09{=0A+=09=09CommandCounterIncrement();=0A= +=09=09CheckLogicalSlotScopeHierarchyChange(relationId,=20parentOid);=0A= +=09}=0A=20=0A=20=09/*=0A=20=09=20*=20Store=20a=20dependency=20too=0A@@=20= -19803,6=20+19810,22=20@@=20AlterTableNamespaceInternal(Relation=20rel,=20= Oid=20oldNspOid,=20Oid=20nspOid,=0A=20=09=09=09=09=09=09=09=20=20false,=20= objsMoved);=0A=20=0A=20=09table_close(classRel,=20RowExclusiveLock);=0A+=0A= +=09/*=20Moving=20a=20table=20can=20make=20it=20a=20member=20of=20a=20= schema=20publication.=20*/=0A+=09if=20(!IsBootstrapProcessingMode()=20&&=20= oldNspOid=20!=3D=20nspOid=20&&=0A+=09=09(rel->rd_rel->relkind=20=3D=3D=20= RELKIND_RELATION=20||=0A+=09=09=20rel->rd_rel->relkind=20=3D=3D=20= RELKIND_PARTITIONED_TABLE))=0A+=09{=0A+=09=09List=09=20=20=20= *publications;=0A+=09=09List=09=20=20=20*relations=20=3D=20= list_make1_oid(RelationGetRelid(rel));=0A+=0A+=09=09= CommandCounterIncrement();=0A+=09=09publications=20=3D=20= GetSchemaPublications(nspOid);=0A+=09=09foreach_oid(pubid,=20= publications)=0A+=09=09=09LogicalSlotScopePublicationAddRelations(pubid,=20= relations);=0A+=09=09list_free(publications);=0A+=09=09= list_free(relations);=0A+=09}=0A=20}=0A=20=0A=20/*=0Adiff=20--git=20= a/src/backend/postmaster/autovacuum.c=20= b/src/backend/postmaster/autovacuum.c=0Aindex=20874454891d3..5e297d4592d=20= 100644=0A---=20a/src/backend/postmaster/autovacuum.c=0A+++=20= b/src/backend/postmaster/autovacuum.c=0A@@=20-89,6=20+89,7=20@@=0A=20= #include=20"postmaster/autovacuum.h"=0A=20#include=20= "postmaster/interrupt.h"=0A=20#include=20"postmaster/postmaster.h"=0A= +#include=20"replication/slotscope.h"=0A=20#include=20= "storage/aio_subsys.h"=0A=20#include=20"storage/bufmgr.h"=0A=20#include=20= "storage/ipc.h"=0A@@=20-2667,6=20+2668,8=20@@=20deleted:=0A=20=09if=20= (did_vacuum=20||=20!found_concurrent_worker)=0A=20=09=09= vac_update_datfrozenxid();=0A=20=0A+=09LogicalSlotScopeCleanup();=0A+=0A=20= =09/*=20Finally=20close=20out=20the=20last=20transaction.=20*/=0A=20=09= CommitTransactionCommand();=0A=20}=0Adiff=20--git=20= a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c=20= b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c=0Aindex=20= 029990d9fce..998ef9cba04=20100644=0A---=20= a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c=0A+++=20= b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c=0A@@=20= -42,6=20+42,9=20@@=20PG_MODULE_MAGIC_EXT(=0A=20=09=09=09=09=09.version=20= =3D=20PG_VERSION=0A=20);=0A=20=0A+/*=20First=20server=20version=20= supporting=20UNRESTRICTED=20and=20PUBLICATION_NAMES.=20*/=0A+#define=20= LOGICAL_SLOT_SCOPE_VERSION_NUM=20200000=0A+=0A=20struct=20= WalReceiverConn=0A=20{=0A=20=09/*=20Current=20connection=20to=20the=20= primary,=20if=20any=20*/=0A@@=20-85,6=20+88,8=20@@=20static=20char=20= *libpqrcv_create_slot(WalReceiverConn=20*conn,=0A=20=09=09=09=09=09=09=09= =09=20=20bool=20temporary,=0A=20=09=09=09=09=09=09=09=09=20=20bool=20= two_phase,=0A=20=09=09=09=09=09=09=09=09=20=20bool=20failover,=0A+=09=09=09= =09=09=09=09=09=20=20bool=20unrestricted,=0A+=09=09=09=09=09=09=09=09=20=20= List=20*publications,=0A=20=09=09=09=09=09=09=09=09=20=20= CRSSnapshotAction=20snapshot_action,=0A=20=09=09=09=09=09=09=09=09=20=20= XLogRecPtr=20*lsn);=0A=20static=20void=20= libpqrcv_alter_slot(WalReceiverConn=20*conn,=20const=20char=20*slotname,=0A= @@=20-924,6=20+929,7=20@@=20libpqrcv_send(WalReceiverConn=20*conn,=20= const=20char=20*buffer,=20int=20nbytes)=0A=20static=20char=20*=0A=20= libpqrcv_create_slot(WalReceiverConn=20*conn,=20const=20char=20= *slotname,=0A=20=09=09=09=09=09=20bool=20temporary,=20bool=20two_phase,=20= bool=20failover,=0A+=09=09=09=09=09=20bool=20unrestricted,=20List=20= *publications,=0A=20=09=09=09=09=09=20CRSSnapshotAction=20= snapshot_action,=20XLogRecPtr=20*lsn)=0A=20{=0A=20=09PGresult=20=20=20= *res;=0A@@=20-932,6=20+938,10=20@@=20= libpqrcv_create_slot(WalReceiverConn=20*conn,=20const=20char=20= *slotname,=0A=20=09int=09=09=09use_new_options_syntax;=0A=20=0A=20=09= use_new_options_syntax=20=3D=20(PQserverVersion(conn->streamConn)=20>=3D=20= 150000);=0A+=09if=20(!unrestricted=20&&=20= PQserverVersion(conn->streamConn)=20<=20LOGICAL_SLOT_SCOPE_VERSION_NUM)=0A= +=09=09ereport(ERROR,=0A+=09=09=09=09= (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),=0A+=09=09=09=09=20= errmsg("publisher=20does=20not=20support=20scoped=20logical=20= replication=20slots")));=0A=20=0A=20=09initStringInfo(&cmd);=0A=20=0A@@=20= -943,6=20+953,9=20@@=20libpqrcv_create_slot(WalReceiverConn=20*conn,=20= const=20char=20*slotname,=0A=20=0A=20=09if=20(conn->logical)=0A=20=09{=0A= +=09=09StringInfoData=20pubnames;=0A+=09=09ListCell=20=20=20*lc;=0A+=0A=20= =09=09appendStringInfoString(&cmd,=20"=20LOGICAL=20pgoutput=20");=0A=20=09= =09if=20(use_new_options_syntax)=0A=20=09=09=09= appendStringInfoChar(&cmd,=20'(');=0A@@=20-964,6=20+977,21=20@@=20= libpqrcv_create_slot(WalReceiverConn=20*conn,=20const=20char=20= *slotname,=0A=20=09=09=09=09appendStringInfoChar(&cmd,=20'=20');=0A=20=09= =09}=0A=20=0A+=09=09if=20(use_new_options_syntax=20&&=20!unrestricted)=0A= +=09=09{=0A+=09=09=09initStringInfo(&pubnames);=0A+=09=09=09foreach(lc,=20= publications)=0A+=09=09=09{=0A+=09=09=09=09if=20(lc=20!=3D=20= list_head(publications))=0A+=09=09=09=09=09= appendStringInfoChar(&pubnames,=20',');=0A+=09=09=09=09= appendStringInfoString(&pubnames,=0A+=09=09=09=09=09=09=09=09=09=20= quote_identifier(strVal(lfirst(lc))));=0A+=09=09=09}=0A+=09=09=09= appendStringInfo(&cmd,=20"PUBLICATION_NAMES=20%s,=20",=0A+=09=09=09=09=09= =09=09=20quote_literal_cstr(pubnames.data));=0A+=09=09=09= pfree(pubnames.data);=0A+=09=09}=0A+=0A=20=09=09if=20= (use_new_options_syntax)=0A=20=09=09{=0A=20=09=09=09switch=20= (snapshot_action)=0Adiff=20--git=20= a/src/backend/replication/logical/Makefile=20= b/src/backend/replication/logical/Makefile=0Aindex=20= 455768a57f0..f064790b5a1=20100644=0A---=20= a/src/backend/replication/logical/Makefile=0A+++=20= b/src/backend/replication/logical/Makefile=0A@@=20-21,6=20+21,7=20@@=20= OBJS=20=3D=20\=0A=20=09launcher.o=20\=0A=20=09logical.o=20\=0A=20=09= logicalctl.o=20\=0A+=09slotscope.o=20\=0A=20=09logicalfuncs.o=20\=0A=20=09= message.o=20\=0A=20=09origin.o=20\=0Adiff=20--git=20= a/src/backend/replication/logical/logicalctl.c=20= b/src/backend/replication/logical/logicalctl.c=0Aindex=20= e5340880fa7..96d0460961a=20100644=0A---=20= a/src/backend/replication/logical/logicalctl.c=0A+++=20= b/src/backend/replication/logical/logicalctl.c=0A@@=20-89,6=20+89,7=20@@=20= typedef=20struct=20LogicalDecodingCtlData=0A=20=09=20*=20caches=20this=20= value=20in=20XLogLogicalInfo=20for=20better=20performance.=0A=20=09=20*/=0A= =20=09bool=09=09xlog_logical_info;=0A+=09bool=09=09xlog_restricted_info;=0A= =20=0A=20=09/*=20True=20if=20logical=20decoding=20is=20available=20in=20= the=20system=20*/=0A=20=09bool=09=09logical_decoding_enabled;=0A@@=20= -113,6=20+114,7=20@@=20const=20ShmemCallbacks=20= LogicalDecodingCtlShmemCallbacks=20=3D=20{=0A=20=20*=20transaction=20= ends.=20See=20the=20comments=20for=20XLogLogicalInfoUpdatePending=20for=20= details.=0A=20=20*/=0A=20bool=09=09XLogLogicalInfo=20=3D=20false;=0A= +bool=09=09XLogRestrictedInfo=20=3D=20false;=0A=20=0A=20/*=0A=20=20*=20= When=20receiving=20the=20PROCSIGNAL_BARRIER_UPDATE_XLOG_LOGICAL_INFO=20= signal,=20if=0A@@=20-164,6=20+166,7=20@@=20static=20inline=20void=0A=20= update_xlog_logical_info(void)=0A=20{=0A=20=09XLogLogicalInfo=20=3D=20= IsXLogLogicalInfoEnabled();=0A+=09XLogRestrictedInfo=20=3D=20= IsXLogRestrictedInfoEnabled();=0A=20}=0A=20=0A=20/*=0A@@=20-226,6=20= +229,60=20@@=20IsXLogLogicalInfoEnabled(void)=0A=20=09return=20= xlog_logical_info;=0A=20}=0A=20=0A+/*=0A+=20*=20Returns=20true=20if=20= restricted=20logical=20WAL=20logging=20is=20enabled=20based=20on=20the=20= shared=20memory=0A+=20*=20status.=0A+=20*/=0A+bool=0A= +IsXLogRestrictedInfoEnabled(void)=0A+{=0A+=09bool=09=09enabled;=0A+=0A+=09= LWLockAcquire(LogicalDecodingControlLock,=20LW_SHARED);=0A+=09enabled=20= =3D=20LogicalDecodingCtl->xlog_restricted_info;=0A+=09= LWLockRelease(LogicalDecodingControlLock);=0A+=09return=20enabled;=0A+}=0A= +=0A+/*=0A+=20*=20Synchronously=20raise=20effective_wal_level=20to=20= logical.=0A+=20*/=0A+void=0A+EnsureRestrictedLogicalWAL(void)=0A+{=0A+=09= if=20(wal_level=20>=3D=20WAL_LEVEL_LOGICAL)=0A+=09=09return;=0A+=09= Assert(wal_level=20>=3D=20WAL_LEVEL_REPLICA);=0A+=09= LWLockAcquire(LogicalDecodingControlLock,=20LW_EXCLUSIVE);=0A+=09if=20= (LogicalDecodingCtl->xlog_restricted_info)=0A+=09{=0A+=09=09= LWLockRelease(LogicalDecodingControlLock);=0A+=09=09return;=0A+=09}=0A+=09= LogicalDecodingCtl->xlog_restricted_info=20=3D=20true;=0A+=09= LWLockRelease(LogicalDecodingControlLock);=0A+=09= WaitForProcSignalBarrier(EmitProcSignalBarrier(PROCSIGNAL_BARRIER_UPDATE_X= LOG_LOGICAL_INFO));=0A+}=0A+=0A+/*=0A+=20*=20Synchronously=20raise=20= effective_wal_level=20to=20logical.=0A+=20*/=0A+void=0A= +EnsureFullLogicalWAL(void)=0A+{=0A+=09if=20(wal_level=20>=3D=20= WAL_LEVEL_LOGICAL)=0A+=09=09return;=0A+=09= LWLockAcquire(LogicalDecodingControlLock,=20LW_EXCLUSIVE);=0A+=09if=20= (LogicalDecodingCtl->xlog_logical_info)=0A+=09{=0A+=09=09= LWLockRelease(LogicalDecodingControlLock);=0A+=09=09return;=0A+=09}=0A+=09= LogicalDecodingCtl->xlog_logical_info=20=3D=20true;=0A+=09= LWLockRelease(LogicalDecodingControlLock);=0A+=09= WaitForProcSignalBarrier(EmitProcSignalBarrier(PROCSIGNAL_BARRIER_UPDATE_X= LOG_LOGICAL_INFO));=0A+}=0A+=0A=20/*=0A=20=20*=20Reset=20the=20local=20= cache=20at=20end=20of=20the=20transaction.=0A=20=20*/=0A@@=20-524,6=20= +581,8=20@@=20DisableLogicalDecoding(void)=0A=20{=0A=20=09bool=09=09= in_recovery=20=3D=20RecoveryInProgress();=0A=20=09bool=09=09was_enabled;=0A= +=09bool=09=09has_logical_slots=20=3D=20false;=0A+=09bool=09=09= has_restricted_slots=20=3D=20false;=0A=20=0A=20=09= LWLockAcquire(LogicalDecodingControlLock,=20LW_EXCLUSIVE);=0A=20=0A@@=20= -534,9=20+593,15=20@@=20DisableLogicalDecoding(void)=0A=20=09=20*=20= (skip=20the=20slot=20check=20during=20recovery=20because=20the=20= existing=20slots=20will=20be=0A=20=09=20*=20invalidated=20after=20= disabling=20logical=20decoding.)=0A=20=09=20*/=0A+=09if=20(!in_recovery)=0A= +=09{=0A+=09=09has_logical_slots=20=3D=20CheckLogicalSlotExists();=0A+=09= =09has_restricted_slots=20=3D=20CheckRestrictedLogicalSlotExists();=0A+=09= }=0A=20=09if=20((!LogicalDecodingCtl->logical_decoding_enabled=20&&=0A-=09= =09=20!LogicalDecodingCtl->xlog_logical_info)=20||=0A-=09=09= (!in_recovery=20&&=20CheckLogicalSlotExists()))=0A+=09=09=20= !LogicalDecodingCtl->xlog_logical_info=20&&=0A+=09=09=20= !LogicalDecodingCtl->xlog_restricted_info)=20||=0A+=09=09(!in_recovery=20= &&=20CheckFullWalLogicalSlotExists()))=0A=20=09{=0A=20=09=09= LogicalDecodingCtl->pending_disable=20=3D=20false;=0A=20=09=09= LWLockRelease(LogicalDecodingControlLock);=0A@@=20-557,14=20+622,16=20@@=20= DisableLogicalDecoding(void)=0A=20=09=20*=20information=20WAL=20logging=20= in=20order=20to=20ensure=20that=20no=20logical=20decoding=0A=20=09=20*=20= processes=20WAL=20records=20with=20insufficient=20information.=0A=20=09=20= */=0A-=09LogicalDecodingCtl->logical_decoding_enabled=20=3D=20false;=0A+=09= LogicalDecodingCtl->logical_decoding_enabled=20=3D=20has_logical_slots;=0A= =20=0A=20=09/*=20Write=20the=20WAL=20to=20disable=20logical=20decoding=20= on=20standbys=20too=20*/=0A-=09if=20(!in_recovery=20&&=20was_enabled)=0A= +=09if=20(!in_recovery=20&&=20was_enabled=20&&=20!has_logical_slots)=0A=20= =09=09write_logical_decoding_status_update_record(false);=0A=20=0A=20=09= /*=20Now=20disable=20logical=20information=20WAL=20logging=20*/=0A=20=09= LogicalDecodingCtl->xlog_logical_info=20=3D=20false;=0A+=09if=20= (!has_restricted_slots)=0A+=09=09= LogicalDecodingCtl->xlog_restricted_info=20=3D=20false;=0A=20=09= LogicalDecodingCtl->pending_disable=20=3D=20false;=0A=20=0A=20=09= END_CRIT_SECTION();=0A@@=20-574,7=20+641,7=20@@=20= DisableLogicalDecoding(void)=0A=20=09=20*=20the=20server=20log=20before=20= its=20eventual=20"is=20enabled",=20making=20server=20log=0A=20=09=20*=20= diagnostics=20easy.=0A=20=09=20*/=0A-=09if=20(!in_recovery=20&&=20= was_enabled)=0A+=09if=20(!in_recovery=20&&=20was_enabled=20&&=20= !has_logical_slots)=0A=20=09=09ereport(LOG,=0A=20=09=09=09=09= errmsg("logical=20decoding=20is=20disabled=20because=20there=20are=20no=20= valid=20logical=20replication=20slots"));=0A=20=0Adiff=20--git=20= a/src/backend/replication/logical/meson.build=20= b/src/backend/replication/logical/meson.build=0Aindex=20= 47a68b660d2..327a6f7e221=20100644=0A---=20= a/src/backend/replication/logical/meson.build=0A+++=20= b/src/backend/replication/logical/meson.build=0A@@=20-7,6=20+7,7=20@@=20= backend_sources=20+=3D=20files(=0A=20=20=20'launcher.c',=0A=20=20=20= 'logical.c',=0A=20=20=20'logicalctl.c',=0A+=20=20'slotscope.c',=0A=20=20=20= 'logicalfuncs.c',=0A=20=20=20'message.c',=0A=20=20=20'origin.c',=0Adiff=20= --git=20a/src/backend/replication/logical/reorderbuffer.c=20= b/src/backend/replication/logical/reorderbuffer.c=0Aindex=20= 6aed6346366..cfbcd119900=20100644=0A---=20= a/src/backend/replication/logical/reorderbuffer.c=0A+++=20= b/src/backend/replication/logical/reorderbuffer.c=0A@@=20-103,6=20+103,7=20= @@=0A=20#include=20"replication/logical.h"=0A=20#include=20= "replication/reorderbuffer.h"=0A=20#include=20"replication/slot.h"=0A= +#include=20"replication/slotscope.h"=0A=20#include=20= "replication/snapbuild.h"=09/*=20just=20for=20SnapBuildSnapDecRefcount=20= */=0A=20#include=20"storage/bufmgr.h"=0A=20#include=20"storage/fd.h"=0A= @@=20-2353,7=20+2354,7=20@@=20ReorderBufferProcessTXN(ReorderBuffer=20= *rb,=20ReorderBufferTXN=20*txn,=0A=20=09=09=09=09=09=09=09=20= relpathperm(change->data.tp.rlocator,=0A=20=09=09=09=09=09=09=09=09=09=09= =20MAIN_FORKNUM).str);=0A=20=0A-=09=09=09=09=09if=20= (!RelationIsLogicallyLogged(relation))=0A+=09=09=09=09=09if=20= (!RelationCanBeLogicallyLogged(relation))=0A=20=09=09=09=09=09=09goto=20= change_done;=0A=20=0A=20=09=09=09=09=09/*=0A@@=20-2491,7=20+2492,7=20@@=20= ReorderBufferProcessTXN(ReorderBuffer=20*rb,=20ReorderBufferTXN=20*txn,=0A= =20=09=09=09=09=09=09=09if=20(!RelationIsValid(rel))=0A=20=09=09=09=09=09= =09=09=09elog(ERROR,=20"could=20not=20open=20relation=20with=20OID=20= %u",=20relid);=0A=20=0A-=09=09=09=09=09=09=09if=20= (!RelationIsLogicallyLogged(rel))=0A+=09=09=09=09=09=09=09if=20= (!RelationCanBeLogicallyLogged(rel))=0A=20=09=09=09=09=09=09=09=09= continue;=0A=20=0A=20=09=09=09=09=09=09=09relations[nrelations++]=20=3D=20= rel;=0Adiff=20--git=20a/src/backend/replication/logical/slotscope.c=20= b/src/backend/replication/logical/slotscope.c=0Anew=20file=20mode=20= 100644=0Aindex=2000000000000..a2bae22f017=0A---=20/dev/null=0A+++=20= b/src/backend/replication/logical/slotscope.c=0A@@=20-0,0=20+1,1423=20@@=0A= +/*-----------------------------------------------------------------------= --=0A+=20*=0A+=20*=20slotscope.c=0A+=20*=09=20=20Publication-backed=20= relation=20scopes=20for=20restricted=20logical=20slots.=0A+=20*=0A+=20*=20= A=20restricted=20logical=20replication=20slot=20writes=20logical=20tuple=20= WAL=20only=20for=0A+=20*=20relations=20that=20may=20be=20needed=20by=20= the=20slot's=20publications.=20=20The=20slot=20stores=0A+=20*=20its=20= immutable=20publication=20OIDs=20in=20a=20sidecar=20file,=20while=0A+=20= *=20pg_restricted_slot_relation=20maintains=20the=20current=20physical=20= mappings=20from=0A+=20*=20slot=20incarnations=20to=20relations.=20=20= pg_class.relhasrestrictedslots=20provides=0A+=20*=20the=20fast=20= relation-level=20decision=20used=20during=20WAL=20insertion.=0A+=20*=0A+=20= *=20The=20writer-side=20mapping=20is=20deliberately=20conservative.=20=20= Stale=20mappings=20may=0A+=20*=20cause=20unnecessary=20logical=20WAL=20= to=20be=20written=20until=20they=20are=20cleaned=20up,=20but=0A+=20*=20a=20= required=20mapping=20must=20never=20be=20missing.=20=20This=20mapping=20= does=20not=20determine=0A+=20*=20which=20changes=20are=20emitted=20by=20= an=20output=20plugin;=20pgoutput=20continues=20to=20apply=0A+=20*=20the=20= current=20publication=20definitions=20when=20decoding.=0A+=20*=0A+=20*=20= Publication=20expansion,=20schema=20membership,=20partition=20= attachment,=20and=20TOAST=0A+=20*=20creation=20update=20the=20mappings=20= transactionally.=20=20Obsolete=20mappings=20are=0A+=20*=20removed=20= lazily=20after=20their=20slot=20name=20and=20incarnation=20no=20longer=20= identify=20a=0A+=20*=20live=20restricted=20slot.=0A+=20*=0A+=20*=20Slot=20= files=20and=20catalog=20mappings=20cannot=20be=20persisted=20atomically.=20= =20Restricted=0A+=20*=20slot=20creation=20therefore=20temporarily=20= enables=20full=20logical=20WAL,=20installs=20the=0A+=20*=20mappings=20= and=20a=20completion=20marker=20in=20a=20transaction,=20and=20marks=20= the=20slot=20ready=0A+=20*=20after=20that=20transaction=20commits.=20=20= After=20a=20restart,=20incomplete=20slot=20state=20is=0A+=20*=20= reconciled=20using=20the=20durable=20completion=20marker.=0A+=20*=0A+=20= *=0A+=20*=20Copyright=20(c)=202026,=20PostgreSQL=20Global=20Development=20= Group=0A+=20*=0A+=20*=0A+=20*=20IDENTIFICATION=0A+=20*=09=20=20= src/backend/replication/logical/slotscope.c=0A+=20= *-------------------------------------------------------------------------= =0A+=20*/=0A+#include=20"postgres.h"=0A+=0A+#include=20=0A= +#include=20=0A+=0A+#include=20"access/genam.h"=0A+#include=20= "access/heapam.h"=0A+#include=20"access/table.h"=0A+#include=20= "access/xact.h"=0A+#include=20"catalog/indexing.h"=0A+#include=20= "catalog/pg_class.h"=0A+#include=20"catalog/pg_inherits.h"=0A+#include=20= "catalog/pg_publication.h"=0A+#include=20= "catalog/pg_restricted_slot_relation.h"=0A+#include=20= "common/file_utils.h"=0A+#include=20"common/pg_prng.h"=0A+#include=20= "miscadmin.h"=0A+#include=20"replication/logicalctl.h"=0A+#include=20= "replication/slot.h"=0A+#include=20"replication/slotscope.h"=0A+#include=20= "storage/fd.h"=0A+#include=20"storage/lmgr.h"=0A+#include=20= "utils/fmgroids.h"=0A+#include=20"utils/builtins.h"=0A+#include=20= "utils/lsyscache.h"=0A+#include=20"utils/memutils.h"=0A+#include=20= "utils/rel.h"=0A+#include=20"utils/syscache.h"=0A+=0A+#define=20= SLOT_PUBLICATIONS_MAGIC=200x51C0B11EU=0A+#define=20= SLOT_PUBLICATIONS_VERSION=201=0A+#define=20SLOT_PUBLICATIONS_FILE=20= "publications"=0A+#define=20SLOT_SCOPE_CHANGE_LOCK_SUBID=201=0A+=0A= +typedef=20struct=20LogicalSlotPublicationsOnDisk=0A+{=0A+=09uint32=09=09= magic;=0A+=09pg_crc32c=09checksum;=0A+=09uint32=09=09version;=0A+=09= uint32=09=09npublications;=0A+=09Oid=09=09=09= publications[FLEXIBLE_ARRAY_MEMBER];=0A+}=09=09=09= LogicalSlotPublicationsOnDisk;=0A+=0A+static=20List=20= *read_publications_file(ReplicationSlot=20*slot);=0A+static=20List=20= *publication_relation_closure(List=20*publications,=0A+=09=09=09=09=09=09= =09=09=09=09=20=20bool=20conditional);=0A+static=20List=20= *restricted_slots_for_publication(Oid=20pubid);=0A+static=20void=20= restricted_relation_add(const=20char=20*slotname,=20Oid=20relid,=0A+=09=09= =09=09=09=09=09=09=09bool=20conditional);=0A+static=20void=20= restricted_ready_marker_add(const=20char=20*slotname,=0A+=09=09=09=09=09=09= =09=09=09=09uint64=20incarnation);=0A+static=20List=20= *pending_ready_slots;=0A+static=20bool=20callbacks_registered;=0A+=0A= +typedef=20struct=20PendingReadySlot=0A+{=0A+=09ReplicationSlot=20*slot;=0A= +=09NameData=09name;=0A+=09bool=09=09finalize_at_xact_end;=0A+}=09=09=09= PendingReadySlot;=0A+=0A+typedef=20struct=20ReconcileSlot=0A+{=0A+=09= NameData=09name;=0A+=09uint64=09=09incarnation;=0A+}=09=09=09= ReconcileSlot;=0A+=0A+/*=0A+=20*=20Acquire=20the=20database-wide=20lock=20= that=20serializes=20restricted-slot=20scope=0A+=20*=20operations.=20=20= DDL=20paths=20release=20this=20lock=20explicitly=20after=20updating=20= scope=0A+=20*=20mappings,=20while=20initialization=20and=20maintenance=20= may=20retain=20it=20until=0A+=20*=20transaction=20end.=0A+=20*/=0A= +static=20void=0A+lock_database_scope(void)=0A+{=0A+=09= LockDatabaseObject(RestrictedSlotRelationRelationId,=20MyDatabaseId,=20= 0,=0A+=09=09=09=09=09=20=20=20ExclusiveLock);=0A+}=0A+=0A+/*=0A+=20*=20= Release=20a=20database=20scope=20lock=20acquired=20for=20a=20single=20= DDL=20scope=20operation.=0A+=20*=20The=20transaction-level=20= scope-change=20marker,=20if=20any,=20remains=20held=20until=0A+=20*=20= transaction=20end.=0A+=20*/=0A+static=20void=0A= +unlock_database_scope(void)=0A+{=0A+=09= UnlockDatabaseObject(RestrictedSlotRelationRelationId,=20MyDatabaseId,=20= 0,=0A+=09=09=09=09=09=09=20ExclusiveLock);=0A+}=0A+=0A+/*=0A+=20*=20Mark=20= the=20current=20transaction=20as=20containing=20an=20uncommitted=20= scope-changing=0A+=20*=20DDL=20operation.=0A+=20*=0A+=20*=20DDL=20= transactions=20acquire=20this=20marker=20in=20ShareLock=20mode=20and=20= retain=20it=20until=0A+=20*=20transaction=20end.=20=20Restricted-slot=20= initialization=20conditionally=20requests=20an=0A+=20*=20ExclusiveLock=20= on=20the=20same=20lock=20tag,=20preventing=20it=20from=20constructing=20= an=0A+=20*=20initial=20scope=20while=20relevant=20catalog=20changes=20= remain=20uncommitted.=0A+=20*/=0A+static=20void=0A= +mark_database_scope_change(void)=0A+{=0A+=09= LockDatabaseObject(RestrictedSlotRelationRelationId,=20MyDatabaseId,=0A+=09= =09=09=09=09=20=20=20SLOT_SCOPE_CHANGE_LOCK_SUBID,=20ShareLock);=0A+}=0A= +=0A+/*=0A+=20*=20Ensure=20that=20no=20other=20transaction=20has=20an=20= uncommitted=20scope-changing=20DDL=0A+=20*=20operation.=0A+=20*=0A+=20*=20= Acquire=20the=20transaction-level=20scope-change=20marker=20= conditionally=20in=0A+=20*=20ExclusiveLock=20mode.=20=20Fail=20with=20a=20= retryable=20error=20instead=20of=20waiting,=20since=0A+=20*=20the=20= conflicting=20DDL=20may=20already=20hold=20relation=20locks=20needed=20= by=20slot=0A+=20*=20initialization.=0A+=20*/=0A+static=20void=0A= +check_no_database_scope_change(void)=0A+{=0A+=09if=20= (!ConditionalLockDatabaseObject(RestrictedSlotRelationRelationId,=0A+=09=09= =09=09=09=09=09=09=09=20=20=20MyDatabaseId,=0A+=09=09=09=09=09=09=09=09=09= =20=20=20SLOT_SCOPE_CHANGE_LOCK_SUBID,=0A+=09=09=09=09=09=09=09=09=09=20=20= =20ExclusiveLock))=0A+=09=09ereport(ERROR,=0A+=09=09=09=09= (errcode(ERRCODE_LOCK_NOT_AVAILABLE),=0A+=09=09=09=09=20errmsg("could=20= not=20initialize=20restricted=20logical=20replication=20slot=20due=20to=20= concurrent=20activity"),=0A+=09=09=09=09=20errhint("Retry=20creating=20= the=20replication=20slot.")));=0A+}=0A+=0A+/*=0A+=20*=20Acquire=20= ShareRowExclusiveLock=20on=20a=20relation=20while=20constructing=20or=20= updating=0A+=20*=20a=20restricted=20slot's=20physical=20scope.=0A+=20*=0A= +=20*=20When=20conditional=20is=20true,=20fail=20with=20a=20retryable=20= error=20rather=20than=20waiting.=0A+=20*=20Slot=20initialization=20uses=20= conditional=20locking=20because=20it=20already=20holds=20the=0A+=20*=20= database=20scope=20lock=20and=20waiting=20for=20DDL-held=20relation=20= locks=20could=20deadlock.=0A+=20*=20DDL=20paths=20use=20normal=20= blocking=20locks=20before=20acquiring=20the=20scope=20lock.=0A+=20*/=0A= +static=20void=0A+lock_scope_relation(Oid=20relid,=20bool=20conditional)=0A= +{=0A+=09if=20(conditional)=0A+=09{=0A+=09=09if=20= (ConditionalLockRelationOid(relid,=20ShareRowExclusiveLock))=0A+=09=09=09= return;=0A+=09=09ereport(ERROR,=0A+=09=09=09=09= (errcode(ERRCODE_LOCK_NOT_AVAILABLE),=0A+=09=09=09=09=20errmsg("could=20= not=20initialize=20restricted=20logical=20replication=20slot=20due=20to=20= concurrent=20activity"),=0A+=09=09=09=09=20errhint("Retry=20creating=20= the=20replication=20slot.")));=0A+=09}=0A+=09LockRelationOid(relid,=20= ShareRowExclusiveLock);=0A+}=0A+=0A+/*=0A+=20*=20Complete=20pending=20= restricted-slot=20initializations=20at=20transaction=20end.=0A+=20*=0A+=20= *=20The=20initial=20relation=20mappings=20and=20readiness=20marker=20are=20= transactional,=20but=0A+=20*=20replication-slot=20state=20is=20not.=20=20= After=20commit,=20mark=20each=20pending=20slot=20ready=0A+=20*=20at=20= the=20end=20of=20the=20commit=20record=20and=20optionally=20release=20a=20= slot=20whose=20caller=0A+=20*=20delegated=20finalization=20to=20this=20= callback.=20=20Request=20asynchronous=20removal=20of=0A+=20*=20the=20= temporary=20full-WAL=20requirement=20once=20the=20restricted=20mappings=20= are=0A+=20*=20committed.=0A+=20*=0A+=20*=20On=20abort,=20release=20slots=20= delegated=20to=20this=20callback=20as=20ephemeral=20so=20that=0A+=20*=20= their=20incomplete=20on-disk=20state=20is=20removed.=20=20Other=20= callers=20retain=0A+=20*=20responsibility=20for=20releasing=20their=20= slots.=0A+=20*/=0A+static=20void=0A+slot_scope_xact_callback(XactEvent=20= event,=20void=20*arg)=0A+{=0A+=09if=20(event=20=3D=3D=20= XACT_EVENT_COMMIT)=0A+=09{=0A+=09=09foreach_ptr(PendingReadySlot,=20= pending,=20pending_ready_slots)=0A+=09=09{=0A+=09=09=09ReplicationSlot=20= *slot=20=3D=20pending->slot;=0A+=0A+=09=09=09if=20(slot->in_use=20&&=0A+=09= =09=09=09strcmp(NameStr(slot->data.name),=20NameStr(pending->name))=20=3D=3D= =200)=0A+=09=09=09{=0A+=09=09=09=09SpinLockAcquire(&slot->mutex);=0A+=09=09= =09=09slot->data.restricted_scope_ready=20=3D=20true;=0A+=09=09=09=09= slot->data.restricted_scope_ready_lsn=20=3D=20XactLastCommitEnd;=0A+=09=09= =09=09slot->just_dirtied=20=3D=20true;=0A+=09=09=09=09slot->dirty=20=3D=20= true;=0A+=09=09=09=09SpinLockRelease(&slot->mutex);=0A+=09=09=09=09if=20= (pending->finalize_at_xact_end=20&&=20MyReplicationSlot=20=3D=3D=20slot)=0A= +=09=09=09=09=09ReplicationSlotRelease();=0A+=09=09=09}=0A+=09=09}=0A+=09= =09if=20(pending_ready_slots=20!=3D=20NIL)=0A+=09=09=09= RequestDisableLogicalDecoding();=0A+=09}=0A+=09else=20if=20(event=20=3D=3D= =20XACT_EVENT_ABORT)=0A+=09{=0A+=09=09foreach_ptr(PendingReadySlot,=20= pending,=20pending_ready_slots)=0A+=09=09{=0A+=09=09=09ReplicationSlot=20= *slot=20=3D=20pending->slot;=0A+=0A+=09=09=09if=20= (!pending->finalize_at_xact_end=20||=20MyReplicationSlot=20!=3D=20slot)=0A= +=09=09=09=09continue;=0A+=09=09=09SpinLockAcquire(&slot->mutex);=0A+=09=09= =09slot->data.persistency=20=3D=20RS_EPHEMERAL;=0A+=09=09=09= SpinLockRelease(&slot->mutex);=0A+=09=09=09ReplicationSlotRelease();=0A+=09= =09}=0A+=09}=0A+=09if=20(event=20=3D=3D=20XACT_EVENT_COMMIT=20||=20event=20= =3D=3D=20XACT_EVENT_ABORT)=0A+=09{=0A+=09=09= list_free_deep(pending_ready_slots);=0A+=09=09pending_ready_slots=20=3D=20= NIL;=0A+=09}=0A+}=0A+=0A+/*=20Register=20the=20transaction=20callback=20= used=20by=20restricted-slot=20initialization.=20*/=0A+void=0A= +LogicalSlotScopeInitialize(void)=0A+{=0A+=09if=20= (!callbacks_registered)=0A+=09{=0A+=09=09= RegisterXactCallback(slot_scope_xact_callback,=20NULL);=0A+=09=09= callbacks_registered=20=3D=20true;=0A+=09}=0A+}=0A+=0A+/*=0A+=20*=20= Atomically=20replace=20a=20restricted=20slot's=20publication=20side=20= file.=0A+=20*=0A+=20*=20The=20file=20stores=20the=20immutable=20= publication=20identities=20associated=20with=20the=0A+=20*=20slot.=20=20= Write=20and=20fsync=20a=20temporary=20file,=20then=20durably=20rename=20= it=20over=20the=0A+=20*=20previous=20file.=20=20The=20slot=20I/O=20lock=20= serializes=20this=20operation=20with=20readers=0A+=20*=20and=20other=20= slot-file=20operations.=0A+=20*/=0A+static=20void=0A= +write_publications_file(ReplicationSlot=20*slot,=20List=20= *publications)=0A+{=0A+=09LogicalSlotPublicationsOnDisk=20*ondisk;=0A+=09= char=09=09path[MAXPGPATH];=0A+=09char=09=09tmppath[MAXPGPATH];=0A+=09= Size=09=09size;=0A+=09int=09=09=09fd;=0A+=09int=09=09=09i=20=3D=200;=0A+=0A= +=09size=20=3D=20offsetof(LogicalSlotPublicationsOnDisk,=20publications)=20= +=0A+=09=09list_length(publications)=20*=20sizeof(Oid);=0A+=09ondisk=20=3D= =20palloc0(size);=0A+=09ondisk->magic=20=3D=20SLOT_PUBLICATIONS_MAGIC;=0A= +=09ondisk->version=20=3D=20SLOT_PUBLICATIONS_VERSION;=0A+=09= ondisk->npublications=20=3D=20list_length(publications);=0A+=09= foreach_oid(pubid,=20publications)=0A+=09=09ondisk->publications[i++]=20= =3D=20pubid;=0A+=09INIT_CRC32C(ondisk->checksum);=0A+=09= COMP_CRC32C(ondisk->checksum,=0A+=09=09=09=09(char=20*)=20ondisk=20+=20= offsetof(LogicalSlotPublicationsOnDisk,=20version),=0A+=09=09=09=09size=20= -=20offsetof(LogicalSlotPublicationsOnDisk,=20version));=0A+=09= FIN_CRC32C(ondisk->checksum);=0A+=0A+=09snprintf(path,=20sizeof(path),=20= "%s/%s/%s",=20PG_REPLSLOT_DIR,=0A+=09=09=09=20NameStr(slot->data.name),=20= SLOT_PUBLICATIONS_FILE);=0A+=09snprintf(tmppath,=20sizeof(tmppath),=20= "%s.tmp",=20path);=0A+=09LWLockAcquire(&slot->io_in_progress_lock,=20= LW_EXCLUSIVE);=0A+=09if=20(unlink(tmppath)=20<=200=20&&=20errno=20!=3D=20= ENOENT)=0A+=09=09ereport(ERROR,=0A+=09=09=09=09= (errcode_for_file_access(),=0A+=09=09=09=09=20errmsg("could=20not=20= remove=20file=20\"%s\":=20%m",=20tmppath)));=0A+=09fd=20=3D=20= OpenTransientFile(tmppath,=20O_CREAT=20|=20O_EXCL=20|=20O_WRONLY=20|=20= PG_BINARY);=0A+=09if=20(fd=20<=200)=0A+=09=09ereport(ERROR,=0A+=09=09=09=09= (errcode_for_file_access(),=0A+=09=09=09=09=20errmsg("could=20not=20= create=20file=20\"%s\":=20%m",=20tmppath)));=0A+=09if=20(write(fd,=20= ondisk,=20size)=20!=3D=20size=20||=20pg_fsync(fd)=20!=3D=200=20||=0A+=09=09= CloseTransientFile(fd)=20!=3D=200)=0A+=09=09ereport(ERROR,=0A+=09=09=09=09= (errcode_for_file_access(),=0A+=09=09=09=09=20errmsg("could=20not=20= write=20file=20\"%s\":=20%m",=20tmppath)));=0A+=09(void)=20= durable_rename(tmppath,=20path,=20ERROR);=0A+=09= LWLockRelease(&slot->io_in_progress_lock);=0A+=09pfree(ondisk);=0A+}=0A+=0A= +/*=0A+=20*=20Read=20and=20validate=20a=20restricted=20slot's=20= publication=20side=20file.=0A+=20*=0A+=20*=20Verify=20the=20file=20= header,=20size,=20version,=20and=20checksum=20before=20returning=20its=0A= +=20*=20publication=20OIDs.=20=20The=20slot=20I/O=20lock=20prevents=20= the=20file=20from=20being=20replaced=0A+=20*=20while=20it=20is=20being=20= read.=20=20Unrestricted=20slots=20have=20no=20publication=20file=20and=0A= +=20*=20return=20an=20empty=20list.=0A+=20*/=0A+static=20List=20*=0A= +read_publications_file(ReplicationSlot=20*slot)=0A+{=0A+=09= LogicalSlotPublicationsOnDisk=20*ondisk;=0A+=09pg_crc32c=09checksum;=0A+=09= struct=20stat=20st;=0A+=09char=09=09path[MAXPGPATH];=0A+=09List=09=20=20=20= *result=20=3D=20NIL;=0A+=09Size=09=09expected;=0A+=09int=09=09=09fd;=0A+=0A= +=09if=20(slot->data.unrestricted)=0A+=09=09return=20NIL;=0A+=09= snprintf(path,=20sizeof(path),=20"%s/%s/%s",=20PG_REPLSLOT_DIR,=0A+=09=09= =09=20NameStr(slot->data.name),=20SLOT_PUBLICATIONS_FILE);=0A+=09= LWLockAcquire(&slot->io_in_progress_lock,=20LW_SHARED);=0A+=09fd=20=3D=20= OpenTransientFile(path,=20O_RDONLY=20|=20PG_BINARY);=0A+=09if=20(fd=20<=20= 0=20||=20fstat(fd,=20&st)=20!=3D=200)=0A+=09=09ereport(ERROR,=0A+=09=09=09= =09(errcode_for_file_access(),=0A+=09=09=09=09=20errmsg("could=20not=20= open=20restricted=20slot=20publication=20file=20\"%s\":=20%m",=20= path)));=0A+=09if=20(st.st_size=20<=20= offsetof(LogicalSlotPublicationsOnDisk,=20publications))=0A+=09=09= ereport(ERROR,=0A+=09=09=09=09(errmsg("restricted=20slot=20publication=20= file=20\"%s\"=20is=20too=20small",=20path)));=0A+=09ondisk=20=3D=20= palloc(st.st_size);=0A+=09if=20(read(fd,=20ondisk,=20st.st_size)=20!=3D=20= st.st_size=20||=0A+=09=09CloseTransientFile(fd)=20!=3D=200)=0A+=09=09= ereport(ERROR,=0A+=09=09=09=09(errcode_for_file_access(),=0A+=09=09=09=09= =20errmsg("could=20not=20read=20restricted=20slot=20publication=20file=20= \"%s\":=20%m",=20path)));=0A+=09= LWLockRelease(&slot->io_in_progress_lock);=0A+=09expected=20=3D=20= offsetof(LogicalSlotPublicationsOnDisk,=20publications)=20+=0A+=09=09= ondisk->npublications=20*=20sizeof(Oid);=0A+=09if=20(ondisk->magic=20!=3D=20= SLOT_PUBLICATIONS_MAGIC=20||=0A+=09=09ondisk->version=20!=3D=20= SLOT_PUBLICATIONS_VERSION=20||=20expected=20!=3D=20st.st_size)=0A+=09=09= ereport(ERROR,=0A+=09=09=09=09(errmsg("invalid=20restricted=20slot=20= publication=20file=20\"%s\"",=20path)));=0A+=09INIT_CRC32C(checksum);=0A= +=09COMP_CRC32C(checksum,=0A+=09=09=09=09(char=20*)=20ondisk=20+=20= offsetof(LogicalSlotPublicationsOnDisk,=20version),=0A+=09=09=09=09= st.st_size=20-=20offsetof(LogicalSlotPublicationsOnDisk,=20version));=0A= +=09FIN_CRC32C(checksum);=0A+=09if=20(!EQ_CRC32C(checksum,=20= ondisk->checksum))=0A+=09=09ereport(ERROR,=0A+=09=09=09=09= (errmsg("checksum=20mismatch=20for=20restricted=20slot=20publication=20= file=20\"%s\"",=20path)));=0A+=09for=20(uint32=20i=20=3D=200;=20i=20<=20= ondisk->npublications;=20i++)=0A+=09=09result=20=3D=20= lappend_oid(result,=20ondisk->publications[i]);=0A+=09pfree(ondisk);=0A+=09= return=20result;=0A+}=0A+=0A+/*=20Return=20the=20publication=20OIDs=20= stored=20for=20a=20restricted=20slot.=20*/=0A+List=20*=0A= +LogicalSlotScopeGetPublications(ReplicationSlot=20*slot)=0A+{=0A+=09= return=20read_publications_file(slot);=0A+}=0A+=0A+/*=0A+=20*=20Build=20= and=20lock=20the=20current=20physical=20relation=20closure=20of=20a=20= publication=20set.=0A+=20*=0A+=20*=20Expand=20explicit=20and=20schema=20= publication=20members=20to=20include=20partition=0A+=20*=20descendants,=20= then=20add=20each=20relation's=20current=20TOAST=20relation.=20=20Ignore=0A= +=20*=20publications=20that=20have=20been=20dropped,=20reject=20FOR=20= ALL=20TABLES=20publications,=0A+=20*=20and=20remove=20duplicate=20= relation=20OIDs.=0A+=20*=0A+=20*=20Acquire=20ShareRowExclusiveLock=20on=20= every=20base=20relation=20so=20no=20writer=20can=20cross=0A+=20*=20scope=20= activation=20using=20stale=20relation=20metadata.=20=20When=20= conditional=20is=20true,=0A+=20*=20fail=20instead=20of=20waiting=20for=20= a=20conflicting=20relation=20lock;=20this=20mode=20is=20used=0A+=20*=20= during=20restricted-slot=20initialization=20to=20avoid=20deadlocks=20= with=20concurrent=0A+=20*=20DDL.=0A+=20*/=0A+static=20List=20*=0A= +publication_relation_closure(List=20*publications,=20bool=20= conditional)=0A+{=0A+=09List=09=20=20=20*relations=20=3D=20NIL;=0A+=09= List=09=20=20=20*base;=0A+=0A+=09foreach_oid(pubid,=20publications)=0A+=09= {=0A+=09=09Publication=20*pub;=0A+=09=09List=09=20=20=20*pubrels;=0A+=0A= +=09=09if=20(!SearchSysCacheExists1(PUBLICATIONOID,=20= ObjectIdGetDatum(pubid)))=0A+=09=09=09continue;=0A+=09=09pub=20=3D=20= GetPublication(pubid);=0A+=09=09if=20(pub->alltables)=0A+=09=09=09= ereport(ERROR,=0A+=09=09=09=09=09= (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),=0A+=09=09=09=09=09=20= errmsg("publication=20\"%s\"=20is=20defined=20FOR=20ALL=20TABLES",=20= pub->name),=0A+=09=09=09=09=09=20errhint("Create=20an=20unrestricted=20= logical=20replication=20slot=20instead.")));=0A+=09=09pubrels=20=3D=20= GetIncludedPublicationRelations(pubid,=20PUBLICATION_PART_ALL);=0A+=09=09= pubrels=20=3D=20list_concat(pubrels,=0A+=09=09=09=09=09=09=09=20=20= GetAllSchemaPublicationRelations(pubid,=20PUBLICATION_PART_ALL));=0A+=09=09= relations=20=3D=20list_concat(relations,=20pubrels);=0A+=09}=0A+=09= list_sort(relations,=20list_oid_cmp);=0A+=09= list_deduplicate_oid(relations);=0A+=0A+=09base=20=3D=20= list_copy(relations);=0A+=09foreach_oid(relid,=20base)=0A+=09{=0A+=09=09= Relation=09rel;=0A+=0A+=09=09lock_scope_relation(relid,=20conditional);=0A= +=09=09rel=20=3D=20table_open(relid,=20NoLock);=0A+=0A+=09=09if=20= (OidIsValid(rel->rd_rel->reltoastrelid))=0A+=09=09=09relations=20=3D=20= lappend_oid(relations,=20rel->rd_rel->reltoastrelid);=0A+=09=09= table_close(rel,=20NoLock);=0A+=09}=0A+=09list_free(base);=0A+=0A+=09= list_sort(relations,=20list_oid_cmp);=0A+=09= list_deduplicate_oid(relations);=0A+=09return=20relations;=0A+}=0A+=0A= +/*=20Set=20pg_class.relhasrestrictedslots=20for=20a=20mapped=20= relation.=20*/=0A+static=20void=0A+set_relation_restricted_flag(Oid=20= relid)=0A+{=0A+=09Relation=09classrel;=0A+=09HeapTuple=09tuple;=0A+=09= Form_pg_class=20classform;=0A+=0A+=09tuple=20=3D=20= SearchSysCache1(RELOID,=20ObjectIdGetDatum(relid));=0A+=09if=20= (!HeapTupleIsValid(tuple))=0A+=09=09ereport(ERROR,=0A+=09=09=09=09= (errcode(ERRCODE_UNDEFINED_TABLE),=0A+=09=09=09=09=20errmsg("relation=20= with=20OID=20%u=20does=20not=20exist",=20relid)));=0A+=0A+=09if=20= (((Form_pg_class)=20GETSTRUCT(tuple))->relhasrestrictedslots)=0A+=09{=0A= +=09=09ReleaseSysCache(tuple);=0A+=09=09return;=0A+=09}=0A+=09= ReleaseSysCache(tuple);=0A+=0A+=09/*=0A+=09=20*=20Fetch=20and=20check=20= the=20tuple=20again=20while=20holding=20the=20relation=20lock=0A+=09=20*=20= acquired=20by=20restricted_relation_add(),=20because=20its=20value=20= might=20have=0A+=09=20*=20changed=20since=20the=20fast-path=20check.=0A+=09= =20*/=0A+=09classrel=20=3D=20table_open(RelationRelationId,=20= RowExclusiveLock);=0A+=09tuple=20=3D=20SearchSysCacheCopy1(RELOID,=20= ObjectIdGetDatum(relid));=0A+=09if=20(!HeapTupleIsValid(tuple))=0A+=09=09= ereport(ERROR,=0A+=09=09=09=09(errcode(ERRCODE_UNDEFINED_TABLE),=0A+=09=09= =09=09=20errmsg("relation=20with=20OID=20%u=20does=20not=20exist",=20= relid)));=0A+=0A+=09classform=20=3D=20(Form_pg_class)=20= GETSTRUCT(tuple);=0A+=09if=20(!classform->relhasrestrictedslots)=0A+=09{=0A= +=09=09classform->relhasrestrictedslots=20=3D=20true;=0A+=09=09= CatalogTupleUpdate(classrel,=20&tuple->t_self,=20tuple);=0A+=09=09= CommandCounterIncrement();=0A+=09}=0A+=0A+=09heap_freetuple(tuple);=0A+=09= table_close(classrel,=20RowExclusiveLock);=0A+}=0A+=0A+/*=20Look=20up=20= the=20current=20incarnation=20of=20a=20named=20logical=20replication=20= slot.=20*/=0A+static=20uint64=0A+restricted_slot_incarnation(const=20= char=20*slotname)=0A+{=0A+=09uint64=09=09incarnation=20=3D=200;=0A+=0A+=09= LWLockAcquire(ReplicationSlotAllocationLock,=20LW_SHARED);=0A+=09for=20= (int=20i=20=3D=200;=20i=20<=20max_replication_slots=20+=20= max_repack_replication_slots;=20i++)=0A+=09{=0A+=09=09ReplicationSlot=20= *slot=20=3D=20&ReplicationSlotCtl->replication_slots[i];=0A+=09=09= ReplicationSlotPersistentData=20data;=0A+=0A+=09=09if=20(!slot->in_use)=0A= +=09=09=09continue;=0A+=09=09SpinLockAcquire(&slot->mutex);=0A+=09=09= data=20=3D=20slot->data;=0A+=09=09SpinLockRelease(&slot->mutex);=0A+=09=09= if=20(data.database=20!=3D=20InvalidOid=20&&=0A+=09=09=09= strcmp(NameStr(data.name),=20slotname)=20=3D=3D=200)=0A+=09=09{=0A+=09=09= =09incarnation=20=3D=20data.restricted_scope_incarnation;=0A+=09=09=09= break;=0A+=09=09}=0A+=09}=0A+=09= LWLockRelease(ReplicationSlotAllocationLock);=0A+=09return=20= incarnation;=0A+}=0A+=0A+/*=0A+=20*=20Add=20a=20relation=20mapping=20for=20= the=20current=20incarnation=20of=20a=20restricted=20slot.=0A+=20*=0A+=20= *=20If=20the=20named=20slot=20no=20longer=20exists,=20do=20nothing.=20=20= Otherwise,=20lock=20the=0A+=20*=20relation=20against=20concurrent=20DML,=20= insert=20the=20incarnation-qualified=20mapping=0A+=20*=20if=20it=20is=20= absent,=20and=20set=20pg_class.relhasrestrictedslots.=0A+=20*=0A+=20*=20= The=20relation=20lock=20prevents=20a=20writer=20from=20crossing=20= transaction=20commit=20with=0A+=20*=20a=20stale=20false=20value=20for=20= relhasrestrictedslots.=20=20The=20database-object=20lock=0A+=20*=20= serializes=20mapping=20changes=20with=20cleanup=20and=20other=20scope=20= maintenance.=0A+=20*/=0A+static=20void=0A+restricted_relation_add(const=20= char=20*slotname,=20Oid=20relid,=20bool=20conditional)=0A+{=0A+=09= Relation=09maprel;=0A+=09ScanKeyData=20keys[3];=0A+=09SysScanDesc=20= scan;=0A+=09HeapTuple=09tuple;=0A+=09NameData=09keyname;=0A+=09uint64=09=09= incarnation=20=3D=20restricted_slot_incarnation(slotname);=0A+=0A+=09if=20= (incarnation=20=3D=3D=200)=0A+=09=09return;=0A+=0A+=09/*=20Keep=20DML=20= out=20until=20the=20new=20flag=20and=20mapping=20become=20visible=20at=20= commit.=20*/=0A+=09lock_scope_relation(relid,=20conditional);=0A+=09= maprel=20=3D=20table_open(RestrictedSlotRelationRelationId,=20= RowExclusiveLock);=0A+=09namestrcpy(&keyname,=20slotname);=0A+=09= ScanKeyInit(&keys[0],=20Anum_pg_restricted_slot_relation_rsrslotname,=0A= +=09=09=09=09BTEqualStrategyNumber,=20F_NAMEEQ,=20= NameGetDatum(&keyname));=0A+=09ScanKeyInit(&keys[1],=20= Anum_pg_restricted_slot_relation_rsrrelid,=0A+=09=09=09=09= BTEqualStrategyNumber,=20F_OIDEQ,=20ObjectIdGetDatum(relid));=0A+=09= ScanKeyInit(&keys[2],=20Anum_pg_restricted_slot_relation_rsrincarnation,=0A= +=09=09=09=09BTEqualStrategyNumber,=20F_INT8EQ,=20Int64GetDatum((int64)=20= incarnation));=0A+=09scan=20=3D=20systable_beginscan(maprel,=20= RestrictedSlotRelationSlotRelIndexId,=0A+=09=09=09=09=09=09=09=20=20= true,=20NULL,=203,=20keys);=0A+=09tuple=20=3D=20systable_getnext(scan);=0A= +=09if=20(!HeapTupleIsValid(tuple))=0A+=09{=0A+=09=09Datum=09=09= values[Natts_pg_restricted_slot_relation];=0A+=09=09bool=09=09= nulls[Natts_pg_restricted_slot_relation]=20=3D=20{false};=0A+=09=09= NameData=09name;=0A+=0A+=09=09MemSet(values,=200,=20sizeof(values));=0A+=09= =09namestrcpy(&name,=20slotname);=0A+=09=09= values[Anum_pg_restricted_slot_relation_rsrslotname=20-=201]=20=3D=20= NameGetDatum(&name);=0A+=09=09= values[Anum_pg_restricted_slot_relation_rsrrelid=20-=201]=20=3D=20= ObjectIdGetDatum(relid);=0A+=09=09= values[Anum_pg_restricted_slot_relation_rsrincarnation=20-=201]=20=3D=0A= +=09=09=09Int64GetDatum((int64)=20incarnation);=0A+=09=09tuple=20=3D=20= heap_form_tuple(RelationGetDescr(maprel),=20values,=20nulls);=0A+=09=09= CatalogTupleInsert(maprel,=20tuple);=0A+=09=09heap_freetuple(tuple);=0A+=09= =09CommandCounterIncrement();=0A+=09}=0A+=09systable_endscan(scan);=0A+=09= table_close(maprel,=20RowExclusiveLock);=0A+=09= set_relation_restricted_flag(relid);=0A+}=0A+=0A+/*=0A+=20*=20Add=20the=20= transactional=20readiness=20marker=20for=20a=20restricted-slot=20= incarnation.=0A+=20*=0A+=20*=20The=20marker=20uses=20InvalidOid=20as=20= its=20relation=20OID=20and=20is=20inserted=20in=20the=20same=0A+=20*=20= transaction=20as=20the=20initial=20relation=20mappings.=20=20After=20a=20= crash,=20its=20presence=0A+=20*=20proves=20that=20initialization=20= committed,=20including=20when=20the=20publications=20had=0A+=20*=20no=20= relation=20members.=20=20Its=20absence=20means=20that=20a=20not-ready=20= slot=20must=20be=0A+=20*=20treated=20as=20incomplete.=0A+=20*/=0A+static=20= void=0A+restricted_ready_marker_add(const=20char=20*slotname,=20uint64=20= incarnation)=0A+{=0A+=09Relation=09maprel;=0A+=09ScanKeyData=20keys[3];=0A= +=09SysScanDesc=20scan;=0A+=09HeapTuple=09tuple;=0A+=09NameData=09= keyname;=0A+=0A+=09maprel=20=3D=20= table_open(RestrictedSlotRelationRelationId,=20RowExclusiveLock);=0A+=09= namestrcpy(&keyname,=20slotname);=0A+=09ScanKeyInit(&keys[0],=20= Anum_pg_restricted_slot_relation_rsrslotname,=0A+=09=09=09=09= BTEqualStrategyNumber,=20F_NAMEEQ,=20NameGetDatum(&keyname));=0A+=09= ScanKeyInit(&keys[1],=20Anum_pg_restricted_slot_relation_rsrrelid,=0A+=09= =09=09=09BTEqualStrategyNumber,=20F_OIDEQ,=20= ObjectIdGetDatum(InvalidOid));=0A+=09ScanKeyInit(&keys[2],=20= Anum_pg_restricted_slot_relation_rsrincarnation,=0A+=09=09=09=09= BTEqualStrategyNumber,=20F_INT8EQ,=20Int64GetDatum((int64)=20= incarnation));=0A+=09scan=20=3D=20systable_beginscan(maprel,=20= RestrictedSlotRelationSlotRelIndexId,=0A+=09=09=09=09=09=09=09=20=20= true,=20NULL,=203,=20keys);=0A+=09tuple=20=3D=20systable_getnext(scan);=0A= +=09if=20(!HeapTupleIsValid(tuple))=0A+=09{=0A+=09=09Datum=09=09= values[Natts_pg_restricted_slot_relation];=0A+=09=09bool=09=09= nulls[Natts_pg_restricted_slot_relation]=20=3D=20{false};=0A+=09=09= NameData=09name;=0A+=0A+=09=09MemSet(values,=200,=20sizeof(values));=0A+=09= =09namestrcpy(&name,=20slotname);=0A+=09=09= values[Anum_pg_restricted_slot_relation_rsrslotname=20-=201]=20=3D=0A+=09= =09=09NameGetDatum(&name);=0A+=09=09= values[Anum_pg_restricted_slot_relation_rsrrelid=20-=201]=20=3D=0A+=09=09= =09ObjectIdGetDatum(InvalidOid);=0A+=09=09= values[Anum_pg_restricted_slot_relation_rsrincarnation=20-=201]=20=3D=0A= +=09=09=09Int64GetDatum((int64)=20incarnation);=0A+=09=09tuple=20=3D=20= heap_form_tuple(RelationGetDescr(maprel),=20values,=20nulls);=0A+=09=09= CatalogTupleInsert(maprel,=20tuple);=0A+=09=09heap_freetuple(tuple);=0A+=09= }=0A+=09systable_endscan(scan);=0A+=09table_close(maprel,=20= RowExclusiveLock);=0A+}=0A+=0A+/*=20Return=20the=20names=20of=20= restricted=20slots=20currently=20mapped=20to=20a=20relation.=20*/=0A= +static=20List=20*=0A+restricted_slots_for_relation(Oid=20relid)=0A+{=0A= +=09Relation=09maprel;=0A+=09ScanKeyData=20key;=0A+=09SysScanDesc=20= scan;=0A+=09HeapTuple=09tuple;=0A+=09List=09=20=20=20*result=20=3D=20= NIL;=0A+=0A+=09maprel=20=3D=20= table_open(RestrictedSlotRelationRelationId,=20AccessShareLock);=0A+=09= ScanKeyInit(&key,=20Anum_pg_restricted_slot_relation_rsrrelid,=0A+=09=09=09= =09BTEqualStrategyNumber,=20F_OIDEQ,=20ObjectIdGetDatum(relid));=0A+=09= scan=20=3D=20systable_beginscan(maprel,=20= RestrictedSlotRelationRelIndexId,=0A+=09=09=09=09=09=09=09=20=20true,=20= NULL,=201,=20&key);=0A+=09while=20(HeapTupleIsValid(tuple=20=3D=20= systable_getnext(scan)))=0A+=09{=0A+=09=09= Form_pg_restricted_slot_relation=20form=20=3D=0A+=09=09=09= (Form_pg_restricted_slot_relation)=20GETSTRUCT(tuple);=0A+=0A+=09=09= result=20=3D=20lappend(result,=20pstrdup(NameStr(form->rsrslotname)));=0A= +=09}=0A+=09systable_endscan(scan);=0A+=09table_close(maprel,=20= AccessShareLock);=0A+=09return=20result;=0A+}=0A+=0A+/*=0A+=20*=20Return=20= the=20names=20of=20valid=20restricted=20slots=20in=20the=20current=20= database=20whose=0A+=20*=20stored=20publication=20set=20contains=20= pubid.=0A+=20*=0A+=20*=20Copy=20the=20slot=20names=20while=20holding=20= the=20replication-slot=20allocation=20lock,=20but=0A+=20*=20do=20not=20= retain=20that=20lock=20across=20relation=20locking=20or=20mapping=20= catalog=20changes.=0A+=20*=20Include=20not-yet-ready=20restricted=20= slots=20so=20concurrent=20publication=20expansion=0A+=20*=20cannot=20be=20= missed=20during=20slot=20initialization.=0A+=20*/=0A+static=20List=20*=0A= +restricted_slots_for_publication(Oid=20pubid)=0A+{=0A+=09List=09=20=20=20= *slotnames=20=3D=20NIL;=0A+=0A+=09= LWLockAcquire(ReplicationSlotAllocationLock,=20LW_SHARED);=0A+=09for=20= (int=20i=20=3D=200;=20i=20<=20max_replication_slots=20+=20= max_repack_replication_slots;=20i++)=0A+=09{=0A+=09=09ReplicationSlot=20= *slot=20=3D=20&ReplicationSlotCtl->replication_slots[i];=0A+=09=09= ReplicationSlotPersistentData=20data;=0A+=09=09List=09=20=20=20= *publications;=0A+=0A+=09=09if=20(!slot->in_use)=0A+=09=09=09continue;=0A= +=09=09SpinLockAcquire(&slot->mutex);=0A+=09=09data=20=3D=20slot->data;=0A= +=09=09SpinLockRelease(&slot->mutex);=0A+=09=09if=20(data.database=20!=3D=20= MyDatabaseId=20||=20data.unrestricted=20||=0A+=09=09=09data.invalidated=20= !=3D=20RS_INVAL_NONE)=0A+=09=09=09continue;=0A+=09=09publications=20=3D=20= read_publications_file(slot);=0A+=09=09if=20= (list_member_oid(publications,=20pubid))=0A+=09=09=09slotnames=20=3D=20= lappend(slotnames,=20pstrdup(NameStr(data.name)));=0A+=09=09= list_free(publications);=0A+=09}=0A+=09= LWLockRelease(ReplicationSlotAllocationLock);=0A+=09return=20slotnames;=0A= +}=0A+=0A+/*=0A+=20*=20Build=20and=20lock=20the=20current=20physical=20= closure=20of=20a=20list=20of=20relation=20roots.=0A+=20*=0A+=20*=20= Discover=20every=20root=20and=20partition=20descendant=20under=20= AccessShareLock,=20add=0A+=20*=20their=20current=20TOAST=20relations,=20= and=20sort=20and=20deduplicate=20the=20resulting=20OIDs.=0A+=20*=20= Acquire=20ShareRowExclusiveLock=20in=20OID=20order=20to=20prevent=20= concurrent=20writers=0A+=20*=20from=20crossing=20the=20transactional=20= installation=20of=20scope=20mappings=20and=20to=0A+=20*=20avoid=20= inconsistent=20relation-lock=20ordering=20between=20concurrent=20= operations.=0A+=20*/=0A+static=20List=20*=0A= +relation_physical_closure(List=20*roots)=0A+{=0A+=09List=09=20=20=20= *result=20=3D=20NIL;=0A+=09List=09=20=20=20*base;=0A+=09List=09=20=20=20= *sorted_roots=20=3D=20list_copy(roots);=0A+=0A+=09= list_sort(sorted_roots,=20list_oid_cmp);=0A+=09= list_deduplicate_oid(sorted_roots);=0A+=09foreach_oid(root,=20= sorted_roots)=0A+=09{=0A+=09=09List=09=20=20=20*descendants=20=3D=20= find_all_inheritors(root,=0A+=09=09=09=09=09=09=09=09=09=09=09=09=09=20=20= AccessShareLock,=20NULL);=0A+=0A+=09=09result=20=3D=20= list_concat(result,=20descendants);=0A+=09}=0A+=09= list_free(sorted_roots);=0A+=09list_sort(result,=20list_oid_cmp);=0A+=09= list_deduplicate_oid(result);=0A+=0A+=09base=20=3D=20list_copy(result);=0A= +=09foreach_oid(relid,=20base)=0A+=09{=0A+=09=09Relation=09rel=20=3D=20= table_open(relid,=20NoLock);=0A+=0A+=09=09if=20= (OidIsValid(rel->rd_rel->reltoastrelid))=0A+=09=09=09result=20=3D=20= lappend_oid(result,=20rel->rd_rel->reltoastrelid);=0A+=09=09= table_close(rel,=20NoLock);=0A+=09}=0A+=09list_free(base);=0A+=0A+=09= list_sort(result,=20list_oid_cmp);=0A+=09list_deduplicate_oid(result);=0A= +=09foreach_oid(relid,=20result)=0A+=09=09LockRelationOid(relid,=20= ShareRowExclusiveLock);=0A+=09return=20result;=0A+}=0A+=0A+/*=20Add=20= physical=20relations=20to=20a=20named=20restricted=20slot's=20scope.=20= */=0A+static=20void=0A+logical_slot_scope_add_relations(const=20char=20= *slotname,=20List=20*relations,=0A+=09=09=09=09=09=09=09=09=20bool=20= conditional)=0A+{=0A+=09foreach_oid(relid,=20relations)=0A+=09=09= restricted_relation_add(slotname,=20relid,=20conditional);=0A+}=0A+=0A= +/*=0A+=20*=20Add=20newly=20published=20relations=20to=20restricted=20= slots=20using=20a=20publication.=0A+=20*=0A+=20*=20Find=20valid=20= restricted=20slots=20in=20the=20current=20database=20whose=20stored=0A+=20= *=20publication=20set=20contains=20pubid.=20=20Expand=20the=20supplied=20= relation=20roots=20to=0A+=20*=20their=20partition=20descendants=20and=20= TOAST=20relations,=20then=20add=20the=20resulting=0A+=20*=20mappings=20= to=20each=20matching=20slot.=0A+=20*=0A+=20*=20The=20mappings=20and=20= pg_class=20flags=20are=20changed=20in=20the=20caller's=20transaction,=0A= +=20*=20so=20they=20become=20visible=20atomically=20with=20the=20= publication=20membership=20change.=0A+=20*=20Relation=20locks=20prevent=20= concurrent=20DML=20from=20missing=20logical=20tuple=20WAL=20while=0A+=20= *=20that=20change=20is=20being=20committed.=0A+=20*/=0A+void=0A= +LogicalSlotScopePublicationAddRelations(Oid=20pubid,=20List=20= *relations)=0A+{=0A+=09List=09=20=20=20*slotnames;=0A+=09List=09=20=20=20= *closure;=0A+=0A+=09/*=0A+=09=20*=20To=20avoid=20unconditionally=20doing=20= relation_physical_closure,=20check=20if=0A+=09=20*=20there=20are=20any=20= restricted=20slots=20for=20the=20publication=20first.=20=20If=20not,=20= we=0A+=09=20*=20can=20skip=20the=20closure=20and=20locking=20entirely.=20= =20This=20is=20important=20for=0A+=09=20*=20performance,=20as=20= relation_physical_closure=20can=20be=20expensive=20for=20large=0A+=09=20= *=20relation=20sets.=0A+=09=20*/=0A+=09mark_database_scope_change();=0A+=09= lock_database_scope();=0A+=09slotnames=20=3D=20= restricted_slots_for_publication(pubid);=0A+=09unlock_database_scope();=0A= +=09if=20(slotnames=20=3D=3D=20NIL)=0A+=09=09return;=0A+=09= list_free_deep(slotnames);=0A+=0A+=09closure=20=3D=20= relation_physical_closure(relations);=0A+=09lock_database_scope();=0A+=09= /*=20Recheck=20after=20taking=20relation=20locks=20and=20reacquiring=20= serialization.=20*/=0A+=09slotnames=20=3D=20= restricted_slots_for_publication(pubid);=0A+=09if=20(slotnames=20=3D=3D=20= NIL)=0A+=09{=0A+=09=09list_free(closure);=0A+=09=09= unlock_database_scope();=0A+=09=09return;=0A+=09}=0A+=09= EnsureRestrictedLogicalWAL();=0A+=09foreach_ptr(char,=20slotname,=20= slotnames)=0A+=09=09logical_slot_scope_add_relations(slotname,=20= closure,=20false);=0A+=09list_free(closure);=0A+=09= list_free_deep(slotnames);=0A+=09unlock_database_scope();=0A+}=0A+=0A+/*=0A= +=20*=20Propagate=20an=20owner's=20restricted-slot=20mappings=20to=20a=20= new=20TOAST=20relation.=0A+=20*=0A+=20*=20Add=20the=20TOAST=20relation=20= to=20every=20restricted=20slot=20currently=20mapped=20to=20its=0A+=20*=20= owning=20relation.=20=20The=20mapping=20and=20relhasrestrictedslots=20= flag=20are=20changed=0A+=20*=20in=20the=20transaction=20that=20creates=20= the=20TOAST=20relation,=20so=20subsequent=20TOAST=0A+=20*=20writes=20= cannot=20become=20visible=20without=20the=20required=20logical=20WAL.=0A= +=20*/=0A+void=0A+LogicalSlotScopeNoteToastCreation(Oid=20owner,=20Oid=20= toastrelid)=0A+{=0A+=09List=09=20=20=20*slotnames;=0A+=0A+=09if=20= (IsBootstrapProcessingMode())=0A+=09=09return;=0A+=09= mark_database_scope_change();=0A+=09lock_database_scope();=0A+=09= slotnames=20=3D=20restricted_slots_for_relation(owner);=0A+=09= foreach_ptr(char,=20slotname,=20slotnames)=0A+=09=09= restricted_relation_add(slotname,=20toastrelid,=20false);=0A+=09= list_free_deep(slotnames);=0A+=09unlock_database_scope();=0A+}=0A+=0A+/*=0A= +=20*=20Remove=20all=20restricted-slot=20mappings=20for=20a=20relation=20= being=20dropped.=0A+=20*=0A+=20*=20Delete=20mappings=20for=20every=20= slot=20incarnation=20in=20the=20same=20transaction=20that=0A+=20*=20= drops=20the=20relation.=20=20There=20is=20no=20need=20to=20clear=20= relhasrestrictedslots=0A+=20*=20because=20the=20relation's=20pg_class=20= row=20is=20also=20being=20removed.=0A+=20*/=0A+void=0A= +LogicalSlotScopeRelationDrop(Oid=20relid)=0A+{=0A+=09Relation=09maprel;=0A= +=09ScanKeyData=20key;=0A+=09SysScanDesc=20scan;=0A+=09HeapTuple=09= tuple;=0A+=0A+=09mark_database_scope_change();=0A+=09= lock_database_scope();=0A+=09maprel=20=3D=20= table_open(RestrictedSlotRelationRelationId,=20RowExclusiveLock);=0A+=09= ScanKeyInit(&key,=20Anum_pg_restricted_slot_relation_rsrrelid,=0A+=09=09=09= =09BTEqualStrategyNumber,=20F_OIDEQ,=20ObjectIdGetDatum(relid));=0A+=09= scan=20=3D=20systable_beginscan(maprel,=20= RestrictedSlotRelationRelIndexId,=0A+=09=09=09=09=09=09=09=20=20true,=20= NULL,=201,=20&key);=0A+=09while=20(HeapTupleIsValid(tuple=20=3D=20= systable_getnext(scan)))=0A+=09=09CatalogTupleDelete(maprel,=20= &tuple->t_self);=0A+=09systable_endscan(scan);=0A+=09table_close(maprel,=20= RowExclusiveLock);=0A+=09unlock_database_scope();=0A+}=0A+=0A+/*=20= Return=20whether=20any=20restricted-slot=20mapping=20remains=20for=20a=20= relation.=20*/=0A+static=20bool=0A+relation_has_mapping(Oid=20relid)=0A= +{=0A+=09Relation=09maprel;=0A+=09ScanKeyData=20key;=0A+=09SysScanDesc=20= scan;=0A+=09bool=09=09result;=0A+=0A+=09maprel=20=3D=20= table_open(RestrictedSlotRelationRelationId,=20AccessShareLock);=0A+=09= ScanKeyInit(&key,=20Anum_pg_restricted_slot_relation_rsrrelid,=0A+=09=09=09= =09BTEqualStrategyNumber,=20F_OIDEQ,=20ObjectIdGetDatum(relid));=0A+=09= scan=20=3D=20systable_beginscan(maprel,=20= RestrictedSlotRelationRelIndexId,=0A+=09=09=09=09=09=09=09=20=20true,=20= NULL,=201,=20&key);=0A+=09result=20=3D=20= HeapTupleIsValid(systable_getnext(scan));=0A+=09systable_endscan(scan);=0A= +=09table_close(maprel,=20AccessShareLock);=0A+=09return=20result;=0A+}=0A= +=0A+/*=20Check=20for=20a=20committed=20readiness=20marker=20matching=20= a=20slot=20incarnation.=20*/=0A+static=20bool=0A= +restricted_ready_marker_exists(const=20char=20*slotname,=20uint64=20= incarnation)=0A+{=0A+=09Relation=09maprel;=0A+=09ScanKeyData=20keys[3];=0A= +=09SysScanDesc=20scan;=0A+=09NameData=09name;=0A+=09bool=09=09result;=0A= +=0A+=09maprel=20=3D=20table_open(RestrictedSlotRelationRelationId,=20= AccessShareLock);=0A+=09namestrcpy(&name,=20slotname);=0A+=09= ScanKeyInit(&keys[0],=20Anum_pg_restricted_slot_relation_rsrslotname,=0A= +=09=09=09=09BTEqualStrategyNumber,=20F_NAMEEQ,=20NameGetDatum(&name));=0A= +=09ScanKeyInit(&keys[1],=20Anum_pg_restricted_slot_relation_rsrrelid,=0A= +=09=09=09=09BTEqualStrategyNumber,=20F_OIDEQ,=20= ObjectIdGetDatum(InvalidOid));=0A+=09ScanKeyInit(&keys[2],=20= Anum_pg_restricted_slot_relation_rsrincarnation,=0A+=09=09=09=09= BTEqualStrategyNumber,=20F_INT8EQ,=20Int64GetDatum((int64)=20= incarnation));=0A+=09scan=20=3D=20systable_beginscan(maprel,=20= RestrictedSlotRelationSlotRelIndexId,=0A+=09=09=09=09=09=09=09=20=20= true,=20NULL,=203,=20keys);=0A+=09result=20=3D=20= HeapTupleIsValid(systable_getnext(scan));=0A+=09systable_endscan(scan);=0A= +=09table_close(maprel,=20AccessShareLock);=0A+=09return=20result;=0A+}=0A= +=0A+/*=0A+=20*=20Reconcile=20incomplete=20restricted=20slots=20for=20= the=20current=20database.=0A+=20*=0A+=20*=20Slot=20state=20and=20= transactional=20relation=20mappings=20cannot=20be=20persisted=0A+=20*=20= atomically.=20=20After=20a=20crash,=20a=20slot=20may=20therefore=20= remain=20not=20ready=20even=0A+=20*=20though=20its=20mapping=20= transaction=20committed.=0A+=20*=0A+=20*=20For=20each=20inactive,=20= valid=20restricted=20slot=20that=20is=20not=20ready,=20look=20for=20the=0A= +=20*=20readiness=20marker=20matching=20its=20name=20and=20incarnation.=20= =20If=20the=20marker=20exists,=0A+=20*=20mark=20the=20slot=20ready=20and=20= use=20the=20current=20flush=20position=20as=20a=20conservative=0A+=20*=20= mapping-durability=20boundary.=20=20If=20the=20marker=20does=20not=20= exist,=20drop=20the=20slot=0A+=20*=20as=20an=20aborted=20or=20incomplete=20= creation.=0A+=20*=0A+=20*=20Slot=20state=20is=20rechecked=20after=20= acquiring=20each=20slot=20because=20it=20may=20have=0A+=20*=20changed=20= since=20the=20initial=20scan.=0A+=20*/=0A+void=0A= +LogicalSlotScopeReconcileDatabase(void)=0A+{=0A+=09List=09=20=20=20= *slots=20=3D=20NIL;=0A+=09bool=09=09found=20=3D=20false;=0A+=0A+=09= Assert(IsTransactionState());=0A+=0A+=09/*=20Avoid=20catalog=20locking=20= on=20connections=20with=20nothing=20to=20reconcile.=20*/=0A+=09= LWLockAcquire(ReplicationSlotAllocationLock,=20LW_SHARED);=0A+=09for=20= (int=20i=20=3D=200;=20i=20<=20max_replication_slots=20+=20= max_repack_replication_slots;=20i++)=0A+=09{=0A+=09=09ReplicationSlot=20= *slot=20=3D=20&ReplicationSlotCtl->replication_slots[i];=0A+=09=09= ReplicationSlotPersistentData=20data;=0A+=09=09bool=09=09active;=0A+=0A+=09= =09if=20(!slot->in_use)=0A+=09=09=09continue;=0A+=09=09= SpinLockAcquire(&slot->mutex);=0A+=09=09data=20=3D=20slot->data;=0A+=09=09= active=20=3D=20slot->active_proc=20!=3D=20INVALID_PROC_NUMBER;=0A+=09=09= SpinLockRelease(&slot->mutex);=0A+=09=09if=20(data.database=20!=3D=20= MyDatabaseId=20||=20data.unrestricted=20||=0A+=09=09=09= data.restricted_scope_ready=20||=0A+=09=09=09data.invalidated=20!=3D=20= RS_INVAL_NONE=20||=20active)=0A+=09=09=09continue;=0A+=09=09found=20=3D=20= true;=0A+=09=09break;=0A+=09}=0A+=09= LWLockRelease(ReplicationSlotAllocationLock);=0A+=09if=20(!found)=0A+=09=09= return;=0A+=0A+=09lock_database_scope();=0A+=09= LWLockAcquire(ReplicationSlotAllocationLock,=20LW_SHARED);=0A+=09for=20= (int=20i=20=3D=200;=20i=20<=20max_replication_slots=20+=20= max_repack_replication_slots;=20i++)=0A+=09{=0A+=09=09ReplicationSlot=20= *slot=20=3D=20&ReplicationSlotCtl->replication_slots[i];=0A+=09=09= ReplicationSlotPersistentData=20data;=0A+=09=09ReconcileSlot=20*item;=0A= +=09=09bool=09=09active;=0A+=0A+=09=09if=20(!slot->in_use)=0A+=09=09=09= continue;=0A+=09=09SpinLockAcquire(&slot->mutex);=0A+=09=09data=20=3D=20= slot->data;=0A+=09=09active=20=3D=20slot->active_proc=20!=3D=20= INVALID_PROC_NUMBER;=0A+=09=09SpinLockRelease(&slot->mutex);=0A+=09=09if=20= (data.database=20!=3D=20MyDatabaseId=20||=20data.unrestricted=20||=0A+=09= =09=09data.restricted_scope_ready=20||=0A+=09=09=09data.invalidated=20!=3D= =20RS_INVAL_NONE=20||=20active)=0A+=09=09=09continue;=0A+=09=09item=20=3D=20= palloc(sizeof(*item));=0A+=09=09item->name=20=3D=20data.name;=0A+=09=09= item->incarnation=20=3D=20data.restricted_scope_incarnation;=0A+=09=09= slots=20=3D=20lappend(slots,=20item);=0A+=09}=0A+=09= LWLockRelease(ReplicationSlotAllocationLock);=0A+=0A+=09= foreach_ptr(ReconcileSlot,=20item,=20slots)=0A+=09{=0A+=09=09if=20= (restricted_ready_marker_exists(NameStr(item->name),=20= item->incarnation))=0A+=09=09{=0A+=09=09=09bool=09=09reconciled=20=3D=20= false;=0A+=09=09=09ReplicationSlotPersistentData=20data;=0A+=0A+=09=09=09= if=20(!ReplicationSlotConditionalAcquire(NameStr(item->name),=20false))=0A= +=09=09=09=09continue;=0A+=09=09=09= SpinLockAcquire(&MyReplicationSlot->mutex);=0A+=09=09=09data=20=3D=20= MyReplicationSlot->data;=0A+=09=09=09if=20(data.database=20=3D=3D=20= MyDatabaseId=20&&=20!data.unrestricted=20&&=0A+=09=09=09=09= !data.restricted_scope_ready=20&&=0A+=09=09=09=09data.invalidated=20=3D=3D= =20RS_INVAL_NONE=20&&=0A+=09=09=09=09data.restricted_scope_incarnation=20= =3D=3D=20item->incarnation)=0A+=09=09=09{=0A+=09=09=09=09= MyReplicationSlot->data.restricted_scope_ready=20=3D=20true;=0A+=09=09=09= =09MyReplicationSlot->data.restricted_scope_ready_lsn=20=3D=20= GetFlushRecPtr(NULL);=0A+=09=09=09=09MyReplicationSlot->just_dirtied=20=3D= =20true;=0A+=09=09=09=09MyReplicationSlot->dirty=20=3D=20true;=0A+=09=09=09= =09reconciled=20=3D=20true;=0A+=09=09=09}=0A+=09=09=09= SpinLockRelease(&MyReplicationSlot->mutex);=0A+=09=09=09= ReplicationSlotRelease();=0A+=09=09=09if=20(reconciled)=0A+=09=09=09{=0A= +=09=09=09=09EnableLogicalDecoding();=0A+=09=09=09=09= EnsureRestrictedLogicalWAL();=0A+=09=09=09=09= RequestDisableLogicalDecoding();=0A+=09=09=09}=0A+=09=09}=0A+=09=09else=0A= +=09=09{=0A+=09=09=09ReplicationSlotPersistentData=20data;=0A+=0A+=09=09=09= if=20(!ReplicationSlotConditionalAcquire(NameStr(item->name),=20false))=0A= +=09=09=09=09continue;=0A+=09=09=09= SpinLockAcquire(&MyReplicationSlot->mutex);=0A+=09=09=09data=20=3D=20= MyReplicationSlot->data;=0A+=09=09=09= SpinLockRelease(&MyReplicationSlot->mutex);=0A+=09=09=09if=20= (data.database=20=3D=3D=20MyDatabaseId=20&&=20!data.unrestricted=20&&=0A= +=09=09=09=09!data.restricted_scope_ready=20&&=0A+=09=09=09=09= data.restricted_scope_incarnation=20=3D=3D=20item->incarnation)=0A+=09=09= =09=09ReplicationSlotDropAcquired(true);=0A+=09=09=09else=0A+=09=09=09=09= ReplicationSlotRelease();=0A+=09=09}=0A+=09}=0A+=09= list_free_deep(slots);=0A+}=0A+=0A+/*=20Test=20whether=20a=20list=20= contains=20a=20matching=20slot=20name=20and=20incarnation.=20*/=0A= +static=20bool=0A+slot_identity_list_contains(List=20*slots,=20const=20= char=20*slotname,=0A+=09=09=09=09=09=09=09uint64=20incarnation)=0A+{=0A+=09= foreach_ptr(ReconcileSlot,=20candidate,=20slots)=0A+=09=09if=20= (strcmp(NameStr(candidate->name),=20slotname)=20=3D=3D=200=20&&=0A+=09=09= =09candidate->incarnation=20=3D=3D=20incarnation)=0A+=09=09return=20= true;=0A+=09return=20false;=0A+}=0A+=0A+/*=20Clear=20= relhasrestrictedslots=20after=20a=20relation=20loses=20its=20last=20= mapping.=20*/=0A+static=20void=0A+clear_relation_restricted_flag(Oid=20= relid)=0A+{=0A+=09Relation=09classrel;=0A+=09HeapTuple=09tuple;=0A+=0A+=09= /*=20A=20stale=20true=20flag=20is=20safe;=20maintenance=20must=20not=20= wait=20behind=20DDL.=20*/=0A+=09if=20(!ConditionalLockRelationOid(relid,=20= ShareRowExclusiveLock))=0A+=09=09return;=0A+=09if=20= (relation_has_mapping(relid))=0A+=09=09return;=0A+=09classrel=20=3D=20= table_open(RelationRelationId,=20RowExclusiveLock);=0A+=09tuple=20=3D=20= SearchSysCacheCopy1(RELOID,=20ObjectIdGetDatum(relid));=0A+=09if=20= (HeapTupleIsValid(tuple))=0A+=09{=0A+=09=09Form_pg_class=20form=20=3D=20= (Form_pg_class)=20GETSTRUCT(tuple);=0A+=0A+=09=09if=20= (form->relhasrestrictedslots)=0A+=09=09{=0A+=09=09=09= form->relhasrestrictedslots=20=3D=20false;=0A+=09=09=09= CatalogTupleUpdate(classrel,=20&tuple->t_self,=20tuple);=0A+=09=09}=0A+=09= =09heap_freetuple(tuple);=0A+=09}=0A+=09table_close(classrel,=20= RowExclusiveLock);=0A+}=0A+=0A+/*=0A+=20*=20Remove=20mappings=20= belonging=20to=20obsolete=20restricted-slot=20incarnations.=0A+=20*=0A+=20= *=20Build=20the=20set=20of=20valid=20restricted=20slots=20in=20the=20= current=20database,=20identified=0A+=20*=20by=20name=20and=20= incarnation,=20and=20delete=20mappings=20whose=20owner=20is=20no=20= longer=20in=0A+=20*=20that=20set.=20=20After=20deleting=20mappings,=20= clear=20relhasrestrictedslots=20only=20for=0A+=20*=20relations=20that=20= have=20no=20remaining=20mapping.=0A+=20*=0A+=20*=20Required=20mapping=20= additions=20are=20always=20performed=20synchronously=20by=20the=0A+=20*=20= transaction=20that=20expands=20a=20scope.=20=20Cleanup=20must=20not=20= attempt=20to=20reconstruct=0A+=20*=20missing=20mappings=20after=20= logical=20WAL=20could=20already=20have=20been=20omitted.=0A+=20*=20= Mappings=20made=20obsolete=20by=20publication=20contraction=20may=20= remain=20indefinitely,=0A+=20*=20since=20they=20cause=20only=20= conservative=20extra=20WAL.=0A+=20*/=0A+void=0A= +LogicalSlotScopeCleanup(void)=0A+{=0A+=09List=09=20=20=20*slots=20=3D=20= NIL;=0A+=09List=09=20=20=20*changed_relations=20=3D=20NIL;=0A+=09= Relation=09maprel;=0A+=09TableScanDesc=20scan;=0A+=09HeapTuple=09tuple;=0A= +=0A+=09lock_database_scope();=0A+=09= LWLockAcquire(ReplicationSlotAllocationLock,=20LW_SHARED);=0A+=09for=20= (int=20i=20=3D=200;=20i=20<=20max_replication_slots=20+=20= max_repack_replication_slots;=20i++)=0A+=09{=0A+=09=09ReplicationSlot=20= *slot=20=3D=20&ReplicationSlotCtl->replication_slots[i];=0A+=09=09= ReplicationSlotPersistentData=20data;=0A+=09=09ReconcileSlot=20*item;=0A= +=0A+=09=09if=20(!slot->in_use)=0A+=09=09=09continue;=0A+=09=09= SpinLockAcquire(&slot->mutex);=0A+=09=09data=20=3D=20slot->data;=0A+=09=09= SpinLockRelease(&slot->mutex);=0A+=09=09if=20(data.database=20!=3D=20= MyDatabaseId=20||=20data.unrestricted=20||=0A+=09=09=09data.invalidated=20= !=3D=20RS_INVAL_NONE)=0A+=09=09=09continue;=0A+=09=09item=20=3D=20= palloc(sizeof(*item));=0A+=09=09item->name=20=3D=20data.name;=0A+=09=09= item->incarnation=20=3D=20data.restricted_scope_incarnation;=0A+=09=09= slots=20=3D=20lappend(slots,=20item);=0A+=09}=0A+=09= LWLockRelease(ReplicationSlotAllocationLock);=0A+=0A+=09/*=0A+=09=20*=20= Required=20additions=20are=20synchronous=20in=20their=20originating=20= transaction;=0A+=09=20*=20maintenance=20must=20never=20try=20to=20repair=20= them=20after=20WAL=20could=20be=20lost.=20Keep=0A+=09=20*=20this=20pass=20= bounded=20to=20removing=20mappings=20for=20slots=20that=20no=20longer=20= exist.=0A+=09=20*=20Publication=20contraction=20can=20safely=20leave=20= conservative=20entries.=0A+=09=20*/=0A+=09maprel=20=3D=20= table_open(RestrictedSlotRelationRelationId,=20RowExclusiveLock);=0A+=09= scan=20=3D=20table_beginscan_catalog(maprel,=200,=20NULL);=0A+=09while=20= (HeapTupleIsValid(tuple=20=3D=20heap_getnext(scan,=20= ForwardScanDirection)))=0A+=09{=0A+=09=09= Form_pg_restricted_slot_relation=20form=20=3D=0A+=09=09=09= (Form_pg_restricted_slot_relation)=20GETSTRUCT(tuple);=0A+=0A+=09=09if=20= (!slot_identity_list_contains(slots,=20NameStr(form->rsrslotname),=0A+=09= =09=09=09=09=09=09=09=09=09=20(uint64)=20form->rsrincarnation))=0A+=09=09= {=0A+=09=09=09if=20(OidIsValid(form->rsrrelid))=0A+=09=09=09=09= changed_relations=20=3D=20list_append_unique_oid(changed_relations,=0A+=09= =09=09=09=09=09=09=09=09=09=09=09=09=09=20=20=20form->rsrrelid);=0A+=09=09= =09CatalogTupleDelete(maprel,=20&tuple->t_self);=0A+=09=09}=0A+=09}=0A+=09= table_endscan(scan);=0A+=09table_close(maprel,=20RowExclusiveLock);=0A+=09= CommandCounterIncrement();=0A+=09foreach_oid(relid,=20changed_relations)=0A= +=09=09clear_relation_restricted_flag(relid);=0A+=09= list_free(changed_relations);=0A+=09list_free_deep(slots);=0A+=09= RequestDisableLogicalDecoding();=0A+}=0A+=0A+/*=20Resolve=20publication=20= names=20and=20prepare=20a=20restricted=20slot=20for=20mappings.=20*/=0A= +void=0A+LogicalSlotScopePrepareFromPublications(ReplicationSlot=20= *slot,=20List=20*pubnames)=0A+{=0A+=09List=09=20=20=20*publications=20=3D=20= NIL;=0A+=0A+=09Assert(IsTransactionState());=0A+=09Assert(pubnames=20!=3D=20= NIL);=0A+=09foreach_ptr(char,=20pubname,=20pubnames)=0A+=09{=0A+=09=09= Publication=20*pub=20=3D=20GetPublicationByName(pubname,=20false);=0A+=0A= +=09=09if=20(pub->alltables)=0A+=09=09=09ereport(ERROR,=0A+=09=09=09=09=09= (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),=0A+=09=09=09=09=09=20= errmsg("publication=20\"%s\"=20is=20defined=20FOR=20ALL=20TABLES",=20= pubname),=0A+=09=09=09=09=09=20errhint("Create=20an=20unrestricted=20= logical=20replication=20slot=20instead.")));=0A+=09=09publications=20=3D=20= lappend_oid(publications,=20pub->oid);=0A+=09}=0A+=09= list_sort(publications,=20list_oid_cmp);=0A+=09= list_deduplicate_oid(publications);=0A+=09= LogicalSlotScopePrepareFromPublicationOids(slot,=20publications);=0A+=09= list_free(publications);=0A+}=0A+=0A+/*=0A+=20*=20Prepare=20a=20slot=20= for=20transactional=20restricted-scope=20initialization.=0A+=20*=0A+=20*=20= Assign=20a=20new=20nonzero=20incarnation,=20mark=20the=20slot=20not=20= ready,=20and=20persist=20its=0A+=20*=20publication=20identities.=20=20= Enable=20logical=20decoding=20and=20temporarily=20require=0A+=20*=20full=20= logical=20WAL=20so=20that=20concurrent=20changes=20cannot=20be=20missed=20= before=20the=0A+=20*=20initial=20relation=20mappings=20commit.=20=20Also=20= enable=20restricted=20logical=20WAL=20for=0A+=20*=20the=20steady=20state=20= after=20the=20temporary=20full-WAL=20requirement=20is=20removed.=0A+=20= */=0A+void=0A+LogicalSlotScopePrepareFromPublicationOids(ReplicationSlot=20= *slot,=0A+=09=09=09=09=09=09=09=09=09=09=20=20=20List=20*publications)=0A= +{=0A+=09uint64=09=09incarnation;=0A+=0A+=09= Assert(IsTransactionState());=0A+=09Assert(publications=20!=3D=20NIL);=0A= +=09lock_database_scope();=0A+=09check_no_database_scope_change();=0A+=09= do=0A+=09{=0A+=09=09incarnation=20=3D=20= pg_prng_uint64(&pg_global_prng_state);=0A+=09}=20while=20(incarnation=20= =3D=3D=200);=0A+=0A+=09/*=0A+=09=20*=20Install=20the=20publication=20= file=20before=20making=20the=20restricted=20slot=20visible=0A+=09=20*=20= to=20concurrent=20scope-expansion=20hooks.=20=20Such=20hooks=20may=20= then=20safely=0A+=09=20*=20include=20a=20not-yet-ready=20slot.=0A+=09=20= */=0A+=09write_publications_file(slot,=20publications);=0A+=0A+=09/*=20= Enable=20logical=20decoding=20and=20WAL=20*/=0A+=09= EnsureLogicalDecodingEnabled();=0A+=0A+=09/*=20Temporarily=20raise=20= effective_wal_level=20to=20logical=20*/=0A+=09EnsureFullLogicalWAL();=0A= +=0A+=09/*=0A+=09=20*=20Raise=20restricted_wal_level=20to=20logical,=20= but=20before=0A+=09=20*=20LogicalSlotScopeFinishCreate,=20the=20slot=20= is=20not=20yet=20ready.=0A+=09=20*/=0A+=09EnsureRestrictedLogicalWAL();=0A= +=0A+=09SpinLockAcquire(&slot->mutex);=0A+=09slot->data.unrestricted=20=3D= =20false;=0A+=09slot->data.restricted_scope_ready=20=3D=20false;=0A+=09= slot->data.restricted_scope_incarnation=20=3D=20incarnation;=0A+=09= slot->data.restricted_scope_ready_lsn=20=3D=20InvalidXLogRecPtr;=0A+=09= SpinLockRelease(&slot->mutex);=0A+=09ReplicationSlotMarkDirty();=0A+}=0A= +=0A+/*=0A+=20*=20Install=20the=20initial=20mappings=20for=20a=20= prepared=20restricted=20slot.=0A+=20*=0A+=20*=20Re-read=20and=20expand=20= the=20stored=20publications,=20add=20mappings=20for=20their=20current=0A= +=20*=20physical=20relation=20closure,=20and=20insert=20the=20readiness=20= marker=20in=20the=20same=0A+=20*=20transaction.=20=20Queue=20the=20slot=20= for=20transaction-end=20finalization;=20it=20must=20not=0A+=20*=20be=20= marked=20ready=20until=20that=20transaction=20commits.=0A+=20*=0A+=20*=20= If=20finalize_at_xact_end=20is=20true,=20the=20transaction=20callback=20= also=20releases=0A+=20*=20the=20slot=20after=20commit=20or=20abort.=0A+=20= */=0A+void=0A+LogicalSlotScopeFinishCreate(ReplicationSlot=20*slot,=20= bool=20finalize_at_xact_end)=0A+{=0A+=09List=09=20=20=20*publications;=0A= +=09List=09=20=20=20*relations;=0A+=09MemoryContext=20oldcontext;=0A+=09= PendingReadySlot=20*pending;=0A+=0A+=09Assert(IsTransactionState());=0A+=09= Assert(!slot->data.unrestricted);=0A+=0A+=09lock_database_scope();=0A+=09= check_no_database_scope_change();=0A+=09publications=20=3D=20= read_publications_file(slot);=0A+=09relations=20=3D=20= publication_relation_closure(publications,=20true);=0A+=09= logical_slot_scope_add_relations(NameStr(slot->data.name),=20relations,=20= true);=0A+=09restricted_ready_marker_add(NameStr(slot->data.name),=0A+=09= =09=09=09=09=09=09=09slot->data.restricted_scope_incarnation);=0A+=09= ReplicationSlotMarkDirty();=0A+=09oldcontext=20=3D=20= MemoryContextSwitchTo(TopTransactionContext);=0A+=09pending=20=3D=20= palloc(sizeof(*pending));=0A+=09pending->slot=20=3D=20slot;=0A+=09= pending->name=20=3D=20slot->data.name;=0A+=09= pending->finalize_at_xact_end=20=3D=20finalize_at_xact_end;=0A+=09/*=20= pending_ready_slots=20will=20be=20processed=20by=20the=20transaction=20= callback=20*/=0A+=09pending_ready_slots=20=3D=20= lappend(pending_ready_slots,=20pending);=0A+=09= MemoryContextSwitchTo(oldcontext);=0A+=09list_free(relations);=0A+=09= list_free(publications);=0A+}=0A+=0A+/*=0A+=20*=20Restore=20the=20scope=20= metadata=20of=20a=20synchronized=20restricted=20slot.=0A+=20*=0A+=20*=20= Install=20the=20publication=20identities=20and=20upstream=20slot=20= incarnation,=20mark=20the=0A+=20*=20slot=20ready=20at=20the=20supplied=20= mapping-durability=20boundary,=20and=20enable=0A+=20*=20restricted=20= logical=20WAL.=20=20The=20caller=20must=20ensure=20that=20WAL=20through=20= ready_lsn=0A+=20*=20has=20been=20flushed=20locally,=20so=20the=20= corresponding=20transactional=20catalog=0A+=20*=20mappings=20are=20= available=20before=20the=20synchronized=20slot=20becomes=20usable.=0A+=20= */=0A+void=0A+LogicalSlotScopeRestorePublications(ReplicationSlot=20= *slot,=20List=20*publications,=0A+=09=09=09=09=09=09=09=09=09uint64=20= incarnation,=20XLogRecPtr=20ready_lsn)=0A+{=0A+=09= write_publications_file(slot,=20publications);=0A+=09= SpinLockAcquire(&slot->mutex);=0A+=09slot->data.unrestricted=20=3D=20= false;=0A+=09slot->data.restricted_scope_ready=20=3D=20true;=0A+=09= slot->data.restricted_scope_incarnation=20=3D=20incarnation;=0A+=09= slot->data.restricted_scope_ready_lsn=20=3D=20ready_lsn;=0A+=09= SpinLockRelease(&slot->mutex);=0A+=09EnableLogicalDecoding();=0A+=09= EnsureRestrictedLogicalWAL();=0A+=09ReplicationSlotMarkDirty();=0A+}=0A+=0A= +/*=20Configure=20the=20writer-WAL=20requirement=20for=20an=20= unrestricted=20slot.=20*/=0A+void=0A= +LogicalSlotScopeConfigureUnrestricted(ReplicationSlot=20*slot)=0A+{=0A+=09= SpinLockAcquire(&slot->mutex);=0A+=09slot->data.unrestricted=20=3D=20= true;=0A+=09slot->data.restricted_scope_ready=20=3D=20true;=0A+=09= slot->data.restricted_scope_ready_lsn=20=3D=20InvalidXLogRecPtr;=0A+=09= SpinLockRelease(&slot->mutex);=0A+=09EnsureLogicalDecodingEnabled();=0A= +}=0A+=0A+/*=0A+=20*=20Restore=20writer-side=20WAL=20requirements=20for=20= a=20logical=20slot=20loaded=20from=20disk.=0A+=20*=0A+=20*=20A=20ready=20= restricted=20slot=20requires=20restricted=20logical=20WAL.=20=20An=20= incomplete=0A+=20*=20restricted=20slot=20temporarily=20requires=20full=20= logical=20WAL=20until=20database-level=0A+=20*=20reconciliation=20either=20= completes=20or=20drops=20it.=20=20Unrestricted=20slots=20require=0A+=20*=20= no=20scope-specific=20restoration=20here.=0A+=20*/=0A+void=0A= +LogicalSlotScopeRestore(ReplicationSlot=20*slot)=0A+{=0A+=09if=20= (!SlotIsLogical(slot))=0A+=09=09return;=0A+=09if=20= (!slot->data.unrestricted=20&&=20slot->data.restricted_scope_ready)=0A+=09= {=0A+=09=09EnableLogicalDecoding();=0A+=09=09= EnsureRestrictedLogicalWAL();=0A+=09}=0A+=09else=20if=20= (!slot->data.unrestricted)=0A+=09{=0A+=09=09EnableLogicalDecoding();=0A+=09= =09EnsureFullLogicalWAL();=0A+=09}=0A+}=0A+=0A+/*=0A+=20*=20Propagate=20= restricted-slot=20mappings=20across=20a=20new=20partition=20hierarchy=20= link.=0A+=20*=0A+=20*=20For=20every=20restricted=20slot=20mapped=20to=20= the=20parent,=20add=20mappings=20for=20the=20child,=0A+=20*=20its=20= partition=20descendants,=20and=20their=20TOAST=20relations.=20=20These=20= changes=20occur=0A+=20*=20in=20the=20transaction=20that=20creates=20the=20= hierarchy=20link,=20so=20the=20new=20members=0A+=20*=20require=20logical=20= WAL=20as=20soon=20as=20the=20link=20becomes=20visible.=0A+=20*=0A+=20*=20= Mapping=20removal=20after=20detach=20is=20deliberately=20deferred,=20= since=20a=20stale=0A+=20*=20mapping=20causes=20only=20conservative=20= extra=20WAL.=0A+=20*/=0A+void=0A= +CheckLogicalSlotScopeHierarchyChange(Oid=20childrelid,=20Oid=20= parentrelid)=0A+{=0A+=09List=09=20=20=20*slotnames;=0A+=09List=09=20=20=20= *roots;=0A+=09List=09=20=20=20*closure;=0A+=0A+=09= mark_database_scope_change();=0A+=09lock_database_scope();=0A+=09= slotnames=20=3D=20restricted_slots_for_relation(parentrelid);=0A+=09= unlock_database_scope();=0A+=09if=20(slotnames=20=3D=3D=20NIL)=0A+=09{=0A= +=09=09return;=0A+=09}=0A+=09list_free_deep(slotnames);=0A+=0A+=09roots=20= =3D=20list_make1_oid(childrelid);=0A+=09closure=20=3D=20= relation_physical_closure(roots);=0A+=09lock_database_scope();=0A+=09/*=20= Recheck=20after=20taking=20relation=20locks=20and=20reacquiring=20= serialization.=20*/=0A+=09slotnames=20=3D=20= restricted_slots_for_relation(parentrelid);=0A+=09if=20(slotnames=20=3D=3D= =20NIL)=0A+=09{=0A+=09=09list_free(closure);=0A+=09=09list_free(roots);=0A= +=09=09unlock_database_scope();=0A+=09=09return;=0A+=09}=0A+=09= EnsureRestrictedLogicalWAL();=0A+=09foreach_ptr(char,=20slotname,=20= slotnames)=0A+=09=09logical_slot_scope_add_relations(slotname,=20= closure,=20false);=0A+=09list_free(closure);=0A+=09list_free(roots);=0A+=09= list_free_deep(slotnames);=0A+=09unlock_database_scope();=0A+}=0A+=0A+/*=0A= +=20*=20Return=20whether=20changes=20to=20a=20relation=20require=20= logical=20tuple=20information.=0A+=20*=0A+=20*=20Relations=20that=20= cannot=20participate=20in=20logical=20decoding=20never=20require=20it.=0A= +=20*=20Full=20logical=20WAL=20covers=20every=20eligible=20relation.=20=20= In=20restricted=20mode,=20use=0A+=20*=20the=20relcache=20copy=20of=20= pg_class.relhasrestrictedslots,=20avoiding=20catalog=0A+=20*=20access=20= in=20the=20WAL=20insertion=20path.=0A+=20*/=0A+bool=0A= +RelationNeedsLogicalTupleWAL(Relation=20relation)=0A+{=0A+=09if=20= (!RelationCanBeLogicallyLogged(relation))=0A+=09=09return=20false;=0A+=09= if=20(XLogFullLogicalInfoActive())=0A+=09=09return=20true;=0A+=09if=20= (!XLogRestrictedInfoActive())=0A+=09=09return=20false;=0A+=09return=20= relation->rd_rel->relhasrestrictedslots;=0A+}=0A+=0A+/*=0A+=20*=20Return=20= whether=20a=20slot=20permits=20all=20requested=20publications.=0A+=20*=0A= +=20*=20Unrestricted=20slots=20permit=20any=20publication.=20=20For=20a=20= restricted=20slot,=20resolve=0A+=20*=20each=20requested=20publication=20= name=20to=20its=20current=20OID=20and=20require=20that=20OID=20to=0A+=20= *=20appear=20in=20the=20immutable=20publication=20set=20stored=20by=20= the=20slot.=20=20This=20validates=0A+=20*=20publication=20identity=20= only;=20normal=20pgoutput=20processing=20determines=20current=0A+=20*=20= relation=20membership.=0A+=20*/=0A+bool=0A= +LogicalSlotScopePublicationsContain(ReplicationSlot=20*slot,=20List=20= *pubnames)=0A+{=0A+=09List=09=20=20=20*stored;=0A+=09bool=09=09result=20= =3D=20true;=0A+=0A+=09if=20(slot->data.unrestricted)=0A+=09=09return=20= true;=0A+=09stored=20=3D=20read_publications_file(slot);=0A+=09= foreach_ptr(char,=20pubname,=20pubnames)=0A+=09{=0A+=09=09Publication=20= *pub=20=3D=20GetPublicationByName(pubname,=20false);=0A+=0A+=09=09if=20= (!list_member_oid(stored,=20pub->oid))=0A+=09=09{=0A+=09=09=09result=20=3D= =20false;=0A+=09=09=09break;=0A+=09=09}=0A+=09}=0A+=09list_free(stored);=0A= +=09return=20result;=0A+}=0Adiff=20--git=20= a/src/backend/replication/logical/slotsync.c=20= b/src/backend/replication/logical/slotsync.c=0Aindex=20= c0403893e23..852fb77c846=20100644=0A---=20= a/src/backend/replication/logical/slotsync.c=0A+++=20= b/src/backend/replication/logical/slotsync.c=0A@@=20-68,6=20+68,8=20@@=0A= =20#include=20"postmaster/interrupt.h"=0A=20#include=20= "replication/logical.h"=0A=20#include=20"replication/logicalctl.h"=0A= +#include=20"replication/slot.h"=0A+#include=20"replication/slotscope.h"=0A= =20#include=20"replication/slotsync.h"=0A=20#include=20= "replication/snapbuild.h"=0A=20#include=20"storage/ipc.h"=0A@@=20-76,6=20= +78,7=20@@=0A=20#include=20"storage/procarray.h"=0A=20#include=20= "storage/subsystems.h"=0A=20#include=20"tcop/tcopprot.h"=0A+#include=20= "utils/array.h"=0A=20#include=20"utils/builtins.h"=0A=20#include=20= "utils/memutils.h"=0A=20#include=20"utils/pg_lsn.h"=0A@@=20-170,6=20= +173,11=20@@=20typedef=20struct=20RemoteSlot=0A=20=09char=09=20=20=20= *database;=0A=20=09bool=09=09two_phase;=0A=20=09bool=09=09failover;=0A+=09= bool=09=09unrestricted;=0A+=09bool=09=09restricted_scope_ready;=0A+=09= uint64=09=09restricted_scope_incarnation;=0A+=09XLogRecPtr=09= restricted_scope_ready_lsn;=0A+=09List=09=20=20=20*publications;=0A=20=09= XLogRecPtr=09restart_lsn;=0A=20=09XLogRecPtr=09confirmed_lsn;=0A=20=09= XLogRecPtr=09two_phase_at;=0A@@=20-233,7=20+241,8=20@@=20= update_local_synced_slot(RemoteSlot=20*remote_slot,=20Oid=20remote_dbid)=0A= =20=09=20*=20Make=20sure=20that=20concerned=20WAL=20is=20received=20and=20= flushed=20before=20syncing=0A=20=09=20*=20slot=20to=20target=20lsn=20= received=20from=20the=20primary=20server.=0A=20=09=20*/=0A-=09if=20= (remote_slot->confirmed_lsn=20>=20latestFlushPtr)=0A+=09if=20= (remote_slot->confirmed_lsn=20>=20latestFlushPtr=20||=0A+=09=09= remote_slot->restricted_scope_ready_lsn=20>=20latestFlushPtr)=0A=20=09{=0A= =20=09=09update_slotsync_skip_stats(SS_SKIP_WAL_NOT_FLUSHED);=0A=20=0A@@=20= -786,7=20+795,36=20@@=20synchronize_one_slot(RemoteSlot=20*remote_slot,=20= Oid=20remote_dbid,=0A=20=09bool=09=09slot_updated=20=3D=20false;=0A=20=0A= =20=09/*=20Search=20for=20the=20named=20slot=20*/=0A-=09if=20((slot=20=3D=20= SearchNamedReplicationSlot(remote_slot->name,=20true)))=0A+=09slot=20=3D=20= SearchNamedReplicationSlot(remote_slot->name,=20true);=0A+=09if=20(slot=20= !=3D=20NULL)=0A+=09{=0A+=09=09ReplicationSlotPersistentData=20data;=0A+=0A= +=09=09SpinLockAcquire(&slot->mutex);=0A+=09=09data=20=3D=20slot->data;=0A= +=09=09SpinLockRelease(&slot->mutex);=0A+=09=09if=20(data.synced=20&&=0A= +=09=09=09(data.unrestricted=20!=3D=20remote_slot->unrestricted=20||=0A+=09= =09=09=20(!data.unrestricted=20&&=0A+=09=09=09=20=20= data.restricted_scope_incarnation=20!=3D=0A+=09=09=09=20=20= remote_slot->restricted_scope_incarnation)))=0A+=09=09{=0A+=09=09=09= ReplicationSlotAcquire(remote_slot->name,=20true,=20false);=0A+=09=09=09= ReplicationSlotDropAcquired(false);=0A+=09=09=09slot=20=3D=20NULL;=0A+=09= =09=09slot_updated=20=3D=20true;=0A+=09=09}=0A+=09}=0A+=0A+=09/*=20Its=20= transactional=20relation=20mappings=20may=20not=20have=20committed=20= yet.=20*/=0A+=09if=20(!remote_slot->unrestricted=20&&=20= !remote_slot->restricted_scope_ready)=0A+=09{=0A+=09=09if=20= (slot_persistence_pending)=0A+=09=09=09*slot_persistence_pending=20=3D=20= true;=0A+=09=09return=20slot_updated;=0A+=09}=0A+=0A+=09if=20(slot=20!=3D=20= NULL)=0A=20=09{=0A=20=09=09bool=09=09synced;=0A=20=0A@@=20-912,6=20= +950,14=20@@=20synchronize_one_slot(RemoteSlot=20*remote_slot,=20Oid=20= remote_dbid,=0A=20=09=09slot->data.plugin=20=3D=20plugin_name;=0A=20=09=09= SpinLockRelease(&slot->mutex);=0A=20=0A+=09=09if=20= (remote_slot->unrestricted)=0A+=09=09=09= LogicalSlotScopeConfigureUnrestricted(slot);=0A+=09=09else=0A+=09=09=09= LogicalSlotScopeRestorePublications(slot,=0A+=09=09=09=09=09=09=09=09=09=09= =09=09remote_slot->publications,=0A+=09=09=09=09=09=09=09=09=09=09=09=09= remote_slot->restricted_scope_incarnation,=0A+=09=09=09=09=09=09=09=09=09= =09=09=09remote_slot->restricted_scope_ready_lsn);=0A+=0A=20=09=09= reserve_wal_for_local_slot(remote_slot->restart_lsn);=0A=20=0A=20=09=09= LWLockAcquire(ReplicationSlotControlLock,=20LW_EXCLUSIVE);=0A@@=20-948,9=20= +994,10=20@@=20synchronize_one_slot(RemoteSlot=20*remote_slot,=20Oid=20= remote_dbid,=0A=20static=20List=20*=0A=20= fetch_remote_slots(WalReceiverConn=20*wrconn,=20List=20*slot_names)=0A=20= {=0A-#define=20SLOTSYNC_COLUMN_COUNT=2010=0A+#define=20= SLOTSYNC_COLUMN_COUNT=2015=0A=20=09Oid=09=09=09= slotRow[SLOTSYNC_COLUMN_COUNT]=20=3D=20{TEXTOID,=20TEXTOID,=20LSNOID,=0A= -=09LSNOID,=20XIDOID,=20BOOLOID,=20LSNOID,=20BOOLOID,=20TEXTOID,=20= TEXTOID};=0A+=09=09LSNOID,=20XIDOID,=20BOOLOID,=20LSNOID,=20BOOLOID,=20= TEXTOID,=20TEXTOID,=20BOOLOID,=0A+=09OIDARRAYOID,=20BOOLOID,=20INT8OID,=20= LSNOID};=0A=20=0A=20=09WalRcvExecResult=20*res;=0A=20=09TupleTableSlot=20= *tupslot;=0A@@=20-962,7=20+1009,10=20@@=20= fetch_remote_slots(WalReceiverConn=20*wrconn,=20List=20*slot_names)=0A=20= =09=09=09=09=09=09=20=20=20"SELECT=20slot_name,=20plugin,=20= confirmed_flush_lsn,"=0A=20=09=09=09=09=09=09=20=20=20"=20restart_lsn,=20= catalog_xmin,=20two_phase,"=0A=20=09=09=09=09=09=09=20=20=20"=20= two_phase_at,=20failover,"=0A-=09=09=09=09=09=09=20=20=20"=20database,=20= invalidation_reason"=0A+=09=09=09=09=09=09=20=20=20"=20database,=20= invalidation_reason,=20unrestricted,"=0A+=09=09=09=09=09=09=20=20=20"=20= publication_oids,=20restricted_scope_ready,"=0A+=09=09=09=09=09=09=20=20=20= "=20restricted_scope_incarnation,"=0A+=09=09=09=09=09=09=20=20=20"=20= restricted_scope_ready_lsn"=0A=20=09=09=09=09=09=09=20=20=20"=20FROM=20= pg_catalog.pg_replication_slots"=0A=20=09=09=09=09=09=09=20=20=20"=20= WHERE=20failover=20and=20NOT=20temporary");=0A=20=0A@@=20-1044,6=20= +1094,35=20@@=20fetch_remote_slots(WalReceiverConn=20*wrconn,=20List=20= *slot_names)=0A=20=09=09remote_slot->invalidated=20=3D=20isnull=20?=20= RS_INVAL_NONE=20:=0A=20=09=09=09= GetSlotInvalidationCause(TextDatumGetCString(d));=0A=20=0A+=09=09= remote_slot->unrestricted=20=3D=20DatumGetBool(slot_getattr(tupslot,=20= ++col,=0A+=09=09=09=09=09=09=09=09=09=09=09=09=09=09=09=20=20&isnull));=0A= +=09=09Assert(!isnull);=0A+=0A+=09=09d=20=3D=20slot_getattr(tupslot,=20= ++col,=20&isnull);=0A+=09=09if=20(!isnull)=0A+=09=09{=0A+=09=09=09Datum=09= =20=20=20*elems;=0A+=09=09=09int=09=09=09nelems;=0A+=0A+=09=09=09= deconstruct_array_builtin(DatumGetArrayTypeP(d),=20OIDOID,=0A+=09=09=09=09= =09=09=09=09=09=20=20&elems,=20NULL,=20&nelems);=0A+=09=09=09for=20(int=20= i=20=3D=200;=20i=20<=20nelems;=20i++)=0A+=09=09=09=09= remote_slot->publications=20=3D=20lappend_oid(remote_slot->publications,=0A= +=09=09=09=09=09=09=09=09=09=09=09=09=09=09DatumGetObjectId(elems[i]));=0A= +=09=09}=0A+=0A+=09=09remote_slot->restricted_scope_ready=20=3D=0A+=09=09= =09DatumGetBool(slot_getattr(tupslot,=20++col,=20&isnull));=0A+=09=09= Assert(!isnull);=0A+=0A+=09=09d=20=3D=20slot_getattr(tupslot,=20++col,=20= &isnull);=0A+=09=09remote_slot->restricted_scope_incarnation=20=3D=0A+=09= =09=09isnull=20?=200=20:=20(uint64)=20DatumGetInt64(d);=0A+=0A+=09=09d=20= =3D=20slot_getattr(tupslot,=20++col,=20&isnull);=0A+=09=09= remote_slot->restricted_scope_ready_lsn=20=3D=0A+=09=09=09isnull=20?=20= InvalidXLogRecPtr=20:=20DatumGetLSN(d);=0A+=0A=20=09=09/*=20Sanity=20= check=20*/=0A=20=09=09Assert(col=20=3D=3D=20SLOTSYNC_COLUMN_COUNT);=0A=20= =0A@@=20-1058,9=20+1137,11=20@@=20fetch_remote_slots(WalReceiverConn=20= *wrconn,=20List=20*slot_names)=0A=20=09=09=20*=20pg_replication_slots=20= view,=20then=20we=20can=20avoid=20fetching=20RS_EPHEMERAL=0A=20=09=09=20= *=20slots=20in=20the=20first=20place.=0A=20=09=09=20*/=0A-=09=09if=20= ((!XLogRecPtrIsValid(remote_slot->restart_lsn)=20||=0A-=09=09=09=20= !XLogRecPtrIsValid(remote_slot->confirmed_lsn)=20||=0A-=09=09=09=20= !TransactionIdIsValid(remote_slot->catalog_xmin))=20&&=0A+=09=09if=20= (((!remote_slot->unrestricted=20&&=0A+=09=09=09=20=20= !remote_slot->restricted_scope_ready)=20||=0A+=09=09=09=20= (!XLogRecPtrIsValid(remote_slot->restart_lsn)=20||=0A+=09=09=09=20=20= !XLogRecPtrIsValid(remote_slot->confirmed_lsn)=20||=0A+=09=09=09=20=20= !TransactionIdIsValid(remote_slot->catalog_xmin)))=20&&=0A=20=09=09=09= remote_slot->invalidated=20=3D=3D=20RS_INVAL_NONE)=0A=20=09=09=09= pfree(remote_slot);=0A=20=09=09else=0Adiff=20--git=20= a/src/backend/replication/logical/tablesync.c=20= b/src/backend/replication/logical/tablesync.c=0Aindex=20= e5101997cd3..bc9f9d545f9=20100644=0A---=20= a/src/backend/replication/logical/tablesync.c=0A+++=20= b/src/backend/replication/logical/tablesync.c=0A@@=20-1423,6=20+1423,7=20= @@=20LogicalRepSyncTableStart(XLogRecPtr=20*origin_startpos)=0A=20=09= walrcv_create_slot(LogRepWorkerWalRcvConn,=0A=20=09=09=09=09=09=20=20=20= slotname,=20false=20/*=20permanent=20*/=20,=20false=20/*=20two_phase=20= */=20,=0A=20=09=09=09=09=09=20=20=20MySubscription->failover,=0A+=09=09=09= =09=09=20=20=20MySubscription->unrestricted,=20= MySubscription->publications,=0A=20=09=09=09=09=09=20=20=20= CRS_USE_SNAPSHOT,=20origin_startpos);=0A=20=0A=20=09/*=0Adiff=20--git=20= a/src/backend/replication/pgoutput/pgoutput.c=20= b/src/backend/replication/pgoutput/pgoutput.c=0Aindex=20= 484ffbe2cee..e013f18edd9=20100644=0A---=20= a/src/backend/replication/pgoutput/pgoutput.c=0A+++=20= b/src/backend/replication/pgoutput/pgoutput.c=0A@@=20-27,6=20+27,8=20@@=0A= =20#include=20"replication/logicalproto.h"=0A=20#include=20= "replication/origin.h"=0A=20#include=20"replication/pgoutput.h"=0A= +#include=20"replication/slot.h"=0A+#include=20"replication/slotscope.h"=0A= =20#include=20"rewrite/rewriteHandler.h"=0A=20#include=20= "utils/builtins.h"=0A=20#include=20"utils/inval.h"=0A@@=20-86,6=20+88,7=20= @@=20static=20void=20pgoutput_stream_prepare_txn(LogicalDecodingContext=20= *ctx,=0A=20static=20bool=20publications_valid;=0A=20=0A=20static=20List=20= *LoadPublications(List=20*pubnames);=0A+static=20void=20= ValidateSlotScope(List=20*pubnames);=0A=20static=20void=20= publication_invalidation_cb(Datum=20arg,=20SysCacheIdentifier=20cacheid,=0A= =20=09=09=09=09=09=09=09=09=09=09uint32=20hashvalue);=0A=20static=20void=20= send_repl_origin(LogicalDecodingContext=20*ctx,=0A@@=20-492,6=20+495,20=20= @@=20pgoutput_startup(LogicalDecodingContext=20*ctx,=20= OutputPluginOptions=20*opt,=0A=20=09=09/*=20Parse=20the=20params=20and=20= ERROR=20if=20we=20see=20any=20we=20don't=20recognize=20*/=0A=20=09=09= parse_output_parameters(ctx->output_plugin_options,=20data);=0A=20=0A+=09= =09/*=0A+=09=09=20*=20A=20walsender=20invokes=20output-plugin=20startup=20= outside=20a=20transaction,=0A+=09=09=20*=20whereas=20SQL=20decoding=20= functions=20already=20have=20one.=20=20Publication=20lookup=0A+=09=09=20= *=20needs=20a=20transaction=20environment=20in=20either=20case.=0A+=09=09= =20*/=0A+=09=09if=20(!IsTransactionState())=0A+=09=09{=0A+=09=09=09= StartTransactionCommand();=0A+=09=09=09= ValidateSlotScope(data->publication_names);=0A+=09=09=09= CommitTransactionCommand();=0A+=09=09}=0A+=09=09else=0A+=09=09=09= ValidateSlotScope(data->publication_names);=0A+=0A=20=09=09/*=20Check=20= if=20we=20support=20requested=20protocol=20*/=0A=20=09=09if=20= (data->protocol_version=20>=20LOGICALREP_PROTO_MAX_VERSION_NUM)=0A=20=09=09= =09ereport(ERROR,=0A@@=20-1805,6=20+1822,8=20@@=20LoadPublications(List=20= *pubnames)=0A=20=09List=09=20=20=20*result=20=3D=20NIL;=0A=20=09ListCell=20= =20=20*lc;=0A=20=0A+=09ValidateSlotScope(pubnames);=0A+=0A=20=09= foreach(lc,=20pubnames)=0A=20=09{=0A=20=09=09char=09=20=20=20*pubname=20= =3D=20(char=20*)=20lfirst(lc);=0A@@=20-1823,6=20+1842,24=20@@=20= LoadPublications(List=20*pubnames)=0A=20=09return=20result;=0A=20}=0A=20=0A= +/*=20A=20restricted=20slot=20may=20use=20any=20subset=20of=20its=20= durable=20publication=20set.=20*/=0A+static=20void=0A= +ValidateSlotScope(List=20*pubnames)=0A+{=0A+=09if=20= (MyReplicationSlot->data.unrestricted)=0A+=09=09return;=0A+=09if=20= (!MyReplicationSlot->data.restricted_scope_ready)=0A+=09=09= ereport(ERROR,=0A+=09=09=09=09= (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),=0A+=09=09=09=09=20= errmsg("restricted=20replication=20slot=20\"%s\"=20is=20not=20ready",=0A= +=09=09=09=09=09=09NameStr(MyReplicationSlot->data.name))));=0A+=09if=20= (!LogicalSlotScopePublicationsContain(MyReplicationSlot,=20pubnames))=0A= +=09=09ereport(ERROR,=0A+=09=09=09=09= (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),=0A+=09=09=09=09=20= errmsg("requested=20publications=20are=20not=20contained=20in=20= replication=20slot=20\"%s\"",=0A+=09=09=09=09=09=09= NameStr(MyReplicationSlot->data.name))));=0A+}=0A+=0A=20/*=0A=20=20*=20= Publication=20syscache=20invalidation=20callback.=0A=20=20*=0Adiff=20= --git=20a/src/backend/replication/slot.c=20= b/src/backend/replication/slot.c=0Aindex=2063ce6d27885..a0dd9d2cfe3=20= 100644=0A---=20a/src/backend/replication/slot.c=0A+++=20= b/src/backend/replication/slot.c=0A@@=20-43,13=20+43,15=20@@=0A=20= #include=20"access/xlog_internal.h"=0A=20#include=20= "access/xlogrecovery.h"=0A=20#include=20"common/file_utils.h"=0A= +#include=20"common/pg_prng.h"=0A=20#include=20"common/string.h"=0A=20= #include=20"miscadmin.h"=0A=20#include=20"pgstat.h"=0A=20#include=20= "postmaster/interrupt.h"=0A=20#include=20"replication/logicallauncher.h"=0A= -#include=20"replication/slotsync.h"=0A=20#include=20= "replication/slot.h"=0A+#include=20"replication/slotscope.h"=0A+#include=20= "replication/slotsync.h"=0A=20#include=20= "replication/walsender_private.h"=0A=20#include=20"storage/fd.h"=0A=20= #include=20"storage/ipc.h"=0A@@=20-141,7=20+143,8=20@@=20= StaticAssertDecl(lengthof(SlotInvalidationCauses)=20=3D=3D=20= (RS_INVAL_MAX_CAUSES=20+=201),=0A=20=09sizeof(ReplicationSlotOnDisk)=20-=20= ReplicationSlotOnDiskConstantSize=0A=20=0A=20#define=20SLOT_MAGIC=09=09= 0x1051CA1=09/*=20format=20identifier=20*/=0A-#define=20SLOT_VERSION=095=09= =09/*=20version=20for=20new=20files=20*/=0A+#define=20= SLOT_MIN_COMPATIBLE_VERSION=096=0A+#define=20SLOT_VERSION=09=096=09/*=20= version=20for=20new=20files=20*/=0A=20=0A=20/*=20Control=20array=20for=20= replication=20slot=20management=20*/=0A=20ReplicationSlotCtlData=20= *ReplicationSlotCtl=20=3D=20NULL;=0A@@=20-239,6=20+242,7=20@@=20= ReplicationSlotsShmemInit(void=20*arg)=0A=20void=0A=20= ReplicationSlotInitialize(void)=0A=20{=0A+=09= LogicalSlotScopeInitialize();=0A=20=09= before_shmem_exit(ReplicationSlotShmemExit,=200);=0A=20}=0A=20=0A@@=20= -383,7=20+387,12=20@@=20ReplicationSlotCreate(const=20char=20*name,=20= bool=20db_specific,=0A=20=09int=09=09=09startpoint,=0A=20=09=09=09=09= endpoint;=0A=20=0A-=09Assert(MyReplicationSlot=20=3D=3D=20NULL);=0A+=09= if=20(MyReplicationSlot=20!=3D=20NULL)=0A+=09=09ereport(ERROR,=0A+=09=09=09= =09(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),=0A+=09=09=09=09=20= errmsg("cannot=20create=20another=20replication=20slot=20while=20= replication=20slot=20\"%s\"=20is=20acquired",=0A+=09=09=09=09=09=09= NameStr(MyReplicationSlot->data.name)),=0A+=09=09=09=09=20= errhint("Finish=20the=20transaction=20that=20is=20initializing=20the=20= restricted=20slot=20first.")));=0A=20=0A=20=09/*=0A=20=09=20*=20The=20= logical=20launcher=20or=20pg_upgrade=20may=20create=20or=20migrate=20an=20= internal=0A@@=20-482,6=20+491,8=20@@=20ReplicationSlotCreate(const=20= char=20*name,=20bool=20db_specific,=0A=20=09slot->data.two_phase_at=20=3D=20= InvalidXLogRecPtr;=0A=20=09slot->data.failover=20=3D=20failover;=0A=20=09= slot->data.synced=20=3D=20synced;=0A+=09slot->data.unrestricted=20=3D=20= true;=0A+=09slot->data.restricted_scope_ready=20=3D=20true;=0A=20=0A=20=09= /*=20and=20then=20data=20only=20present=20in=20shared=20memory=20*/=0A=20= =09slot->just_dirtied=20=3D=20false;=0A@@=20-627,14=20+638,20=20@@=20= ReplicationSlotName(int=20index,=20Name=20name)=0A=20=20*=20be=20= invalid.=20It=20should=20always=20be=20set=20to=20true,=20except=20when=20= we=20are=20temporarily=0A=20=20*=20acquiring=20the=20slot=20and=20don't=20= intend=20to=20change=20it.=0A=20=20*/=0A-void=0A= -ReplicationSlotAcquire(const=20char=20*name,=20bool=20nowait,=20bool=20= error_if_invalid)=0A+static=20bool=0A= +ReplicationSlotAcquireInternal(const=20char=20*name,=20bool=20nowait,=0A= +=09=09=09=09=09=09=09=20=20=20bool=20error_if_invalid,=20bool=20= conditional)=0A=20{=0A=20=09ReplicationSlot=20*s;=0A=20=09ProcNumber=09= active_proc;=0A=20=09int=09=09=09active_pid;=0A=20=0A=20=09Assert(name=20= !=3D=20NULL);=0A+=09if=20(MyReplicationSlot=20!=3D=20NULL)=0A+=09=09= ereport(ERROR,=0A+=09=09=09=09= (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),=0A+=09=09=09=09=20= errmsg("cannot=20acquire=20replication=20slot=20\"%s\"=20while=20= replication=20slot=20\"%s\"=20is=20acquired",=0A+=09=09=09=09=09=09name,=20= NameStr(MyReplicationSlot->data.name))));=0A=20=0A=20retry:=0A=20=09= Assert(MyReplicationSlot=20=3D=3D=20NULL);=0A@@=20-646,6=20+663,8=20@@=20= retry:=0A=20=09if=20(s=20=3D=3D=20NULL=20||=20!s->in_use)=0A=20=09{=0A=20= =09=09LWLockRelease(ReplicationSlotControlLock);=0A+=09=09if=20= (conditional)=0A+=09=09=09return=20false;=0A=20=0A=20=09=09= ereport(ERROR,=0A=20=09=09=09=09(errcode(ERRCODE_UNDEFINED_OBJECT),=0A@@=20= -659,10=20+678,17=20@@=20retry:=0A=20=09=20*=20due=20to=20an=20error,=20= and=20a=20backend=20process=20attempts=20to=20reuse=20the=20slot.=0A=20=09= =20*/=0A=20=09if=20(!IsLogicalLauncher()=20&&=20= IsSlotForConflictCheck(name))=0A+=09{=0A+=09=09if=20(conditional)=0A+=09=09= {=0A+=09=09=09LWLockRelease(ReplicationSlotControlLock);=0A+=09=09=09= return=20false;=0A+=09=09}=0A=20=09=09ereport(ERROR,=0A=20=09=09=09=09= errcode(ERRCODE_UNDEFINED_OBJECT),=0A=20=09=09=09=09errmsg("cannot=20= acquire=20replication=20slot=20\"%s\"",=20name),=0A=20=09=09=09=09= errdetail("The=20slot=20is=20reserved=20for=20conflict=20detection=20and=20= can=20only=20be=20acquired=20by=20logical=20replication=20launcher."));=0A= +=09}=0A=20=0A=20=09/*=0A=20=09=20*=20This=20is=20the=20slot=20we=20= want;=20check=20if=20it's=20active=20under=20some=20other=0A@@=20-714,6=20= +740,9=20@@=20retry:=0A=20=09=09=09goto=20retry;=0A=20=09=09}=0A=20=0A+=09= =09if=20(conditional)=0A+=09=09=09return=20false;=0A+=0A=20=09=09= ereport(ERROR,=0A=20=09=09=09=09(errcode(ERRCODE_OBJECT_IN_USE),=0A=20=09= =09=09=09=20errmsg("replication=20slot=20\"%s\"=20is=20active=20for=20= PID=20%d",=0A@@=20-759,6=20+788,19=20@@=20retry:=0A=20=09=09=09=09:=20= errmsg("acquired=20physical=20replication=20slot=20\"%s\"",=0A=20=09=09=09= =09=09=09=20NameStr(s->data.name)));=0A=20=09}=0A+=09return=20true;=0A+}=0A= +=0A+void=0A+ReplicationSlotAcquire(const=20char=20*name,=20bool=20= nowait,=20bool=20error_if_invalid)=0A+{=0A+=09(void)=20= ReplicationSlotAcquireInternal(name,=20nowait,=20error_if_invalid,=20= false);=0A+}=0A+=0A+bool=0A+ReplicationSlotConditionalAcquire(const=20= char=20*name,=20bool=20error_if_invalid)=0A+{=0A+=09return=20= ReplicationSlotAcquireInternal(name,=20true,=20error_if_invalid,=20= true);=0A=20}=0A=20=0A=20/*=0A@@=20-1091,6=20+1133,7=20@@=20= ReplicationSlotDropPtr(ReplicationSlot=20*slot)=0A=20=09=09= fsync_fname(tmppath,=20true);=0A=20=09=09fsync_fname(PG_REPLSLOT_DIR,=20= true);=0A=20=09=09END_CRIT_SECTION();=0A+=0A=20=09}=0A=20=09else=0A=20=09= {=0A@@=20-1516,14=20+1559,12=20@@=20void=0A=20= ReplicationSlotsDropDBSlots(Oid=20dboid)=0A=20{=0A=20=09int=09=09=09i;=0A= -=09bool=09=09found_valid_logicalslot;=0A=20=09bool=09=09dropped=20=3D=20= false;=0A=20=0A=20=09if=20(max_replication_slots=20+=20= max_repack_replication_slots=20<=3D=200)=0A=20=09=09return;=0A=20=0A=20= restart:=0A-=09found_valid_logicalslot=20=3D=20false;=0A=20=09= LWLockAcquire(ReplicationSlotControlLock,=20LW_SHARED);=0A=20=09for=20(i=20= =3D=200;=20i=20<=20max_replication_slots=20+=20= max_repack_replication_slots;=20i++)=0A=20=09{=0A@@=20-1541,14=20+1582,6=20= @@=20restart:=0A=20=09=09if=20(!SlotIsLogical(s))=0A=20=09=09=09= continue;=0A=20=0A-=09=09/*=0A-=09=09=20*=20Check=20logical=20slots=20on=20= other=20databases=20too=20so=20we=20can=20disable=0A-=09=09=20*=20= logical=20decoding=20only=20if=20no=20slots=20in=20the=20cluster.=0A-=09=09= =20*/=0A-=09=09SpinLockAcquire(&s->mutex);=0A-=09=09= found_valid_logicalslot=20|=3D=20(s->data.invalidated=20=3D=3D=20= RS_INVAL_NONE);=0A-=09=09SpinLockRelease(&s->mutex);=0A-=0A=20=09=09/*=20= not=20our=20database,=20skip=20*/=0A=20=09=09if=20(s->data.database=20!=3D= =20dboid)=0A=20=09=09=09continue;=0A@@=20-1610,7=20+1643,7=20@@=20= restart:=0A=20=09}=0A=20=09LWLockRelease(ReplicationSlotControlLock);=0A=20= =0A-=09if=20(dropped=20&&=20!found_valid_logicalslot)=0A+=09if=20= (dropped)=0A=20=09=09RequestDisableLogicalDecoding();=0A=20}=0A=20=0A@@=20= -1655,6=20+1688,47=20@@=20CheckLogicalSlotExists(void)=0A=20=09return=20= found;=0A=20}=0A=20=0A+static=20bool=0A+check_logical_slot_mode(bool=20= restricted)=0A+{=0A+=09bool=09=09found=20=3D=20false;=0A+=0A+=09= LWLockAcquire(ReplicationSlotControlLock,=20LW_SHARED);=0A+=09for=20(int=20= i=20=3D=200;=20i=20<=20max_replication_slots=20+=20= max_repack_replication_slots;=20i++)=0A+=09{=0A+=09=09ReplicationSlot=20= *s=20=3D=20&ReplicationSlotCtl->replication_slots[i];=0A+=09=09bool=09=09= match;=0A+=0A+=09=09if=20(!s->in_use=20||=20SlotIsPhysical(s))=0A+=09=09=09= continue;=0A+=09=09SpinLockAcquire(&s->mutex);=0A+=09=09match=20=3D=20= s->data.invalidated=20=3D=3D=20RS_INVAL_NONE=20&&=0A+=09=09=09= (restricted=20?=20(!s->data.unrestricted=20&&=0A+=09=09=09=09=09=09=20=20= =20s->data.restricted_scope_ready)=20:=0A+=09=09=09=20= (s->data.unrestricted=20||=20!s->data.restricted_scope_ready));=0A+=09=09= SpinLockRelease(&s->mutex);=0A+=09=09if=20(match)=0A+=09=09{=0A+=09=09=09= found=20=3D=20true;=0A+=09=09=09break;=0A+=09=09}=0A+=09}=0A+=09= LWLockRelease(ReplicationSlotControlLock);=0A+=09return=20found;=0A+}=0A= +=0A+bool=0A+CheckRestrictedLogicalSlotExists(void)=0A+{=0A+=09return=20= check_logical_slot_mode(true);=0A+}=0A+=0A+bool=0A= +CheckFullWalLogicalSlotExists(void)=0A+{=0A+=09return=20= check_logical_slot_mode(false);=0A+}=0A+=0A=20/*=0A=20=20*=20Check=20= whether=20the=20server's=20configuration=20supports=20using=20= replication=0A=20=20*=20slots.=0A@@=20-2695,6=20+2769,7=20@@=20= RestoreSlotFromDisk(const=20char=20*name)=0A=20=09TimestampTz=20now=20=3D=20= 0;=0A=20=0A=20=09/*=20no=20need=20to=20lock=20here,=20no=20concurrent=20= access=20allowed=20yet=20*/=0A+=09memset(&cp,=200,=20sizeof(cp));=0A=20=0A= =20=09/*=20delete=20temp=20file=20if=20it=20exists=20*/=0A=20=09= sprintf(slotdir,=20"%s/%s",=20PG_REPLSLOT_DIR,=20name);=0A@@=20-2763,7=20= +2838,8=20@@=20RestoreSlotFromDisk(const=20char=20*name)=0A=20=09=09=09=09= =09=09path,=20cp.magic,=20SLOT_MAGIC)));=0A=20=0A=20=09/*=20verify=20= version=20*/=0A-=09if=20(cp.version=20!=3D=20SLOT_VERSION)=0A+=09if=20= (cp.version=20>=20SLOT_VERSION=20||=0A+=09=09cp.version=20<=20= SLOT_MIN_COMPATIBLE_VERSION)=0A=20=09=09ereport(PANIC,=0A=20=09=09=09=09= (errcode(ERRCODE_DATA_CORRUPTED),=0A=20=09=09=09=09=20= errmsg("replication=20slot=20file=20\"%s\"=20has=20unsupported=20version=20= %u",=0A@@=20-2915,6=20+2991,7=20@@=20RestoreSlotFromDisk(const=20char=20= *name)=0A=20=09=09=09now=20=3D=20GetCurrentTimestamp();=0A=20=0A=20=09=09= ReplicationSlotSetInactiveSince(slot,=20now,=20false);=0A+=09=09= LogicalSlotScopeRestore(slot);=0A=20=0A=20=09=09restored=20=3D=20true;=0A= =20=09=09break;=0Adiff=20--git=20a/src/backend/replication/slotfuncs.c=20= b/src/backend/replication/slotfuncs.c=0Aindex=20fdeb6a23d7b..6c361e60c0b=20= 100644=0A---=20a/src/backend/replication/slotfuncs.c=0A+++=20= b/src/backend/replication/slotfuncs.c=0A@@=20-13,16=20+13,24=20@@=0A=20= #include=20"postgres.h"=0A=20=0A=20#include=20"access/htup_details.h"=0A= +#include=20"access/table.h"=0A=20#include=20"access/xlog_internal.h"=0A=20= #include=20"access/xlogrecovery.h"=0A=20#include=20"access/xlogutils.h"=0A= +#include=20"catalog/pg_class.h"=0A+#include=20= "catalog/pg_publication.h"=0A=20#include=20"funcapi.h"=0A+#include=20= "nodes/pg_list.h"=0A=20#include=20"replication/logical.h"=0A=20#include=20= "replication/slot.h"=0A+#include=20"replication/slotscope.h"=0A=20= #include=20"replication/slotsync.h"=0A=20#include=20"storage/proc.h"=0A= +#include=20"storage/lmgr.h"=0A+#include=20"utils/array.h"=0A=20#include=20= "utils/builtins.h"=0A=20#include=20"utils/guc.h"=0A+#include=20= "utils/lsyscache.h"=0A=20#include=20"utils/pg_lsn.h"=0A=20=0A=20/*=0A@@=20= -48,8=20+56,6=20@@=20static=20void=0A=20= create_physical_replication_slot(char=20*name,=20bool=20= immediately_reserve,=0A=20=09=09=09=09=09=09=09=09=20bool=20temporary,=20= XLogRecPtr=20restart_lsn)=0A=20{=0A-=09Assert(!MyReplicationSlot);=0A-=0A= =20=09/*=20acquire=20replication=20slot,=20this=20will=20check=20for=20= conflicting=20names=20*/=0A=20=09ReplicationSlotCreate(name,=20false,=0A=20= =09=09=09=09=09=09=20=20temporary=20?=20RS_TEMPORARY=20:=20= RS_PERSISTENT,=20false,=0A@@=20-126,28=20+132,11=20@@=20= pg_create_physical_replication_slot(PG_FUNCTION_ARGS)=0A=20=20*=20= caller's=20responsibility=20to=20ensure=20it's=20set=20to=20something=20= sensible.=0A=20=20*/=0A=20static=20void=0A= -create_logical_replication_slot(char=20*name,=20char=20*plugin,=0A-=09=09= =09=09=09=09=09=09bool=20temporary,=20bool=20two_phase,=0A-=09=09=09=09=09= =09=09=09bool=20failover,=0A-=09=09=09=09=09=09=09=09XLogRecPtr=20= restart_lsn,=0A-=09=09=09=09=09=09=09=09bool=20find_startpoint)=0A= +initialize_logical_replication_slot(char=20*plugin,=20XLogRecPtr=20= restart_lsn,=0A+=09=09=09=09=09=09=09=09=09bool=20find_startpoint)=0A=20= {=0A=20=09LogicalDecodingContext=20*ctx=20=3D=20NULL;=0A=20=0A-=09= Assert(!MyReplicationSlot);=0A-=0A-=09/*=0A-=09=20*=20Acquire=20a=20= logical=20decoding=20slot,=20this=20will=20check=20for=20conflicting=20= names.=0A-=09=20*=20Initially=20create=20persistent=20slot=20as=20= ephemeral=20-=20that=20allows=20us=20to=0A-=09=20*=20nicely=20handle=20= errors=20during=20initialization=20because=20it'll=20get=20dropped=20if=0A= -=09=20*=20this=20transaction=20fails.=20We'll=20make=20it=20persistent=20= at=20the=20end.=20Temporary=0A-=09=20*=20slots=20can=20be=20created=20as=20= temporary=20from=20beginning=20as=20they=20get=20dropped=20on=0A-=09=20*=20= error=20as=20well.=0A-=09=20*/=0A-=09ReplicationSlotCreate(name,=20true,=0A= -=09=09=09=09=09=09=20=20temporary=20?=20RS_TEMPORARY=20:=20= RS_EPHEMERAL,=20two_phase,=0A-=09=09=09=09=09=09=20=20false,=20failover,=20= false);=0A-=0A=20=09/*=0A=20=09=20*=20Ensure=20the=20logical=20decoding=20= is=20enabled=20before=20initializing=20the=20logical=0A=20=09=20*=20= decoding=20context.=0A@@=20-191,23=20+180,86=20@@=20= create_logical_replication_slot(char=20*name,=20char=20*plugin,=0A=20=09= FreeDecodingContext(ctx);=0A=20}=0A=20=0A+static=20void=0A= +create_logical_replication_slot(char=20*name,=20char=20*plugin,=0A+=09=09= =09=09=09=09=09=09bool=20temporary,=20bool=20two_phase,=0A+=09=09=09=09=09= =09=09=09bool=20failover,=20bool=20unrestricted,=0A+=09=09=09=09=09=09=09= =09List=20*publications,=0A+=09=09=09=09=09=09=09=09XLogRecPtr=20= restart_lsn,=0A+=09=09=09=09=09=09=09=09bool=20find_startpoint)=0A+{=0A+=09= ReplicationSlotCreate(name,=20true,=0A+=09=09=09=09=09=09=20=20temporary=20= ?=20RS_TEMPORARY=20:=20RS_EPHEMERAL,=20two_phase,=0A+=09=09=09=09=09=09=20= =20false,=20failover,=20false);=0A+=0A+=09if=20(unrestricted)=0A+=09{=0A= +=09=09LogicalSlotScopeConfigureUnrestricted(MyReplicationSlot);=0A+=09=09= initialize_logical_replication_slot(plugin,=20restart_lsn,=20= find_startpoint);=0A+=09}=0A+=09else=0A+=09{=0A+=09=09= LogicalSlotScopePrepareFromPublications(MyReplicationSlot,=20= publications);=0A+=09=09initialize_logical_replication_slot(plugin,=20= restart_lsn,=20find_startpoint);=0A+=09=09= LogicalSlotScopeFinishCreate(MyReplicationSlot,=20true);=0A+=09}=0A+}=0A= +=0A+static=20void=0A= +create_logical_replication_slot_from_publications(char=20*name,=20char=20= *plugin,=0A+=09=09=09=09=09=09=09=09=09=09=09=09=20=20bool=20temporary,=20= bool=20two_phase,=0A+=09=09=09=09=09=09=09=09=09=09=09=09=20=20bool=20= failover,=20bool=20unrestricted,=0A+=09=09=09=09=09=09=09=09=09=09=09=09=20= =20List=20*publications,=0A+=09=09=09=09=09=09=09=09=09=09=09=09=20=20= XLogRecPtr=20restart_lsn,=0A+=09=09=09=09=09=09=09=09=09=09=09=09=20=20= bool=20find_startpoint)=0A+{=0A+=09ReplicationSlotCreate(name,=20true,=0A= +=09=09=09=09=09=09=20=20temporary=20?=20RS_TEMPORARY=20:=20= RS_EPHEMERAL,=20two_phase,=0A+=09=09=09=09=09=09=20=20false,=20failover,=20= false);=0A+=09if=20(unrestricted)=0A+=09{=0A+=09=09= LogicalSlotScopeConfigureUnrestricted(MyReplicationSlot);=0A+=09=09= initialize_logical_replication_slot(plugin,=20restart_lsn,=20= find_startpoint);=0A+=09}=0A+=09else=0A+=09{=0A+=09=09= LogicalSlotScopePrepareFromPublicationOids(MyReplicationSlot,=20= publications);=0A+=09=09initialize_logical_replication_slot(plugin,=20= restart_lsn,=20find_startpoint);=0A+=09=09= LogicalSlotScopeFinishCreate(MyReplicationSlot,=20true);=0A+=09}=0A+}=0A= +=0A=20/*=0A=20=20*=20SQL=20function=20for=20creating=20a=20new=20= logical=20replication=20slot.=0A=20=20*/=0A=20Datum=0A=20= pg_create_logical_replication_slot(PG_FUNCTION_ARGS)=0A=20{=0A-=09Name=09= =09name=20=3D=20PG_GETARG_NAME(0);=0A-=09Name=09=09plugin=20=3D=20= PG_GETARG_NAME(1);=0A-=09bool=09=09temporary=20=3D=20PG_GETARG_BOOL(2);=0A= -=09bool=09=09two_phase=20=3D=20PG_GETARG_BOOL(3);=0A-=09bool=09=09= failover=20=3D=20PG_GETARG_BOOL(4);=0A+=09Name=09=09name;=0A+=09Name=09=09= plugin;=0A+=09bool=09=09temporary;=0A+=09bool=09=09two_phase;=0A+=09bool=09= =09failover;=0A+=09bool=09=09unrestricted;=0A+=09bool=09=09= publications_specified=20=3D=20false;=0A+=09List=09=20=20=20= *publications=20=3D=20NIL;=0A=20=09Datum=09=09result;=0A=20=09TupleDesc=09= tupdesc;=0A=20=09HeapTuple=09tuple;=0A=20=09Datum=09=09values[2];=0A=20=09= bool=09=09nulls[2];=0A=20=0A+=09/*=20Preserve=20the=20former=20strict=20= behavior=20for=20the=20original=20arguments.=20*/=0A+=09for=20(int=20i=20= =3D=200;=20i=20<=205;=20i++)=0A+=09=09if=20(PG_ARGISNULL(i))=0A+=09=09=09= PG_RETURN_NULL();=0A+=0A+=09name=20=3D=20PG_GETARG_NAME(0);=0A+=09plugin=20= =3D=20PG_GETARG_NAME(1);=0A+=09temporary=20=3D=20PG_GETARG_BOOL(2);=0A+=09= two_phase=20=3D=20PG_GETARG_BOOL(3);=0A+=09failover=20=3D=20= PG_GETARG_BOOL(4);=0A+=0A=20=09if=20(get_call_result_type(fcinfo,=20= NULL,=20&tupdesc)=20!=3D=20TYPEFUNC_COMPOSITE)=0A=20=09=09elog(ERROR,=20= "return=20type=20must=20be=20a=20row=20type");=0A=20=0A@@=20-215,13=20= +267,48=20@@=20pg_create_logical_replication_slot(PG_FUNCTION_ARGS)=0A=20= =0A=20=09CheckLogicalDecodingRequirements(false);=0A=20=0A+=09if=20= (PG_NARGS()=20>=205=20&&=20!PG_ARGISNULL(5))=0A+=09{=0A+=09=09ArrayType=20= =20*array=20=3D=20PG_GETARG_ARRAYTYPE_P(5);=0A+=09=09Datum=09=20=20=20= *elems;=0A+=09=09bool=09=20=20=20*nulls;=0A+=09=09int=09=09=09nelems;=0A= +=0A+=09=09publications_specified=20=3D=20true;=0A+=0A+=09=09= deconstruct_array_builtin(array,=20TEXTOID,=20&elems,=20&nulls,=20= &nelems);=0A+=09=09for=20(int=20i=20=3D=200;=20i=20<=20nelems;=20i++)=0A= +=09=09{=0A+=09=09=09if=20(nulls[i])=0A+=09=09=09=09ereport(ERROR,=0A+=09= =09=09=09=09=09(errcode(ERRCODE_INVALID_PARAMETER_VALUE),=0A+=09=09=09=09= =09=09=20errmsg("publications=20must=20not=20contain=20null=20= values")));=0A+=09=09=09publications=20=3D=20lappend(publications,=20= TextDatumGetCString(elems[i]));=0A+=09=09}=0A+=09=09if=20(publications=20= =3D=3D=20NIL)=0A+=09=09=09ereport(ERROR,=0A+=09=09=09=09=09= (errcode(ERRCODE_INVALID_PARAMETER_VALUE),=0A+=09=09=09=09=09=20= errmsg("publications=20must=20not=20be=20empty")));=0A+=09}=0A+=09= unrestricted=20=3D=20!publications_specified;=0A+=09if=20(!unrestricted=20= &&=20strcmp(NameStr(*plugin),=20"pgoutput")=20!=3D=200)=0A+=09=09= ereport(ERROR,=0A+=09=09=09=09(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),=0A= +=09=09=09=09=20errmsg("restricted=20logical=20replication=20slots=20= currently=20require=20pgoutput")));=0A+=09if=20(!unrestricted=20&&=20= IsTransactionBlock())=0A+=09=09ereport(ERROR,=0A+=09=09=09=09= (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),=0A+=09=09=09=09=20= errmsg("restricted=20logical=20replication=20slots=20cannot=20be=20= created=20inside=20a=20transaction=20block")));=0A+=0A=20=09= create_logical_replication_slot(NameStr(*name),=0A=20=09=09=09=09=09=09=09= =09=09NameStr(*plugin),=0A=20=09=09=09=09=09=09=09=09=09temporary,=0A=20=09= =09=09=09=09=09=09=09=09two_phase,=0A=20=09=09=09=09=09=09=09=09=09= failover,=0A+=09=09=09=09=09=09=09=09=09unrestricted,=20publications,=0A=20= =09=09=09=09=09=09=09=09=09InvalidXLogRecPtr,=0A=20=09=09=09=09=09=09=09=09= =09true);=0A+=09list_free(publications);=0A=20=0A=20=09values[0]=20=3D=20= NameGetDatum(&MyReplicationSlot->data.name);=0A=20=09values[1]=20=3D=20= LSNGetDatum(MyReplicationSlot->data.confirmed_flush);=0A@@=20-231,10=20= +318,15=20@@=20pg_create_logical_replication_slot(PG_FUNCTION_ARGS)=0A=20= =09tuple=20=3D=20heap_form_tuple(tupdesc,=20values,=20nulls);=0A=20=09= result=20=3D=20HeapTupleGetDatum(tuple);=0A=20=0A-=09/*=20ok,=20slot=20= is=20now=20fully=20created,=20mark=20it=20as=20persistent=20if=20needed=20= */=0A+=09/*=0A+=09=20*=20Restricted=20creation=20is=20finalized=20by=20= its=20transaction=20callback,=20after=20the=0A+=09=20*=20mapping=20= catalog=20changes=20commit.=20=20Keep=20that=20slot=20acquired=20and=20= ephemeral=0A+=09=20*=20until=20then=20so=20abort=20can=20remove=20it=20= safely.=0A+=09=20*/=0A=20=09if=20(!temporary)=0A=20=09=09= ReplicationSlotPersist();=0A-=09ReplicationSlotRelease();=0A+=09if=20= (unrestricted)=0A+=09=09ReplicationSlotRelease();=0A=20=0A=20=09= PG_RETURN_DATUM(result);=0A=20}=0A@@=20-264,7=20+356,7=20@@=20= pg_drop_replication_slot(PG_FUNCTION_ARGS)=0A=20Datum=0A=20= pg_get_replication_slots(PG_FUNCTION_ARGS)=0A=20{=0A-#define=20= PG_GET_REPLICATION_SLOTS_COLS=2021=0A+#define=20= PG_GET_REPLICATION_SLOTS_COLS=2026=0A=20=09ReturnSetInfo=20*rsinfo=20=3D=20= (ReturnSetInfo=20*)=20fcinfo->resultinfo;=0A=20=09XLogRecPtr=09currlsn;=0A= =20=09int=09=09=09slotno;=0A@@=20-279,6=20+371,7=20@@=20= pg_get_replication_slots(PG_FUNCTION_ARGS)=0A=20=0A=20=09currlsn=20=3D=20= GetXLogWriteRecPtr();=0A=20=0A+=09= LWLockAcquire(ReplicationSlotAllocationLock,=20LW_SHARED);=0A=20=09= LWLockAcquire(ReplicationSlotControlLock,=20LW_SHARED);=0A=20=09for=20= (slotno=20=3D=200;=20slotno=20<=20max_replication_slots=20+=20= max_repack_replication_slots;=20slotno++)=0A=20=09{=0A@@=20-477,6=20= +570,52=20@@=20pg_get_replication_slots(PG_FUNCTION_ARGS)=0A=20=09=09= else=0A=20=09=09=09values[i++]=20=3D=20= CStringGetTextDatum(SlotSyncSkipReasonNames[slot_contents.slotsync_skip_re= ason]);=0A=20=0A+=09=09if=20(SlotIsPhysical(&slot_contents))=0A+=09=09=09= nulls[i++]=20=3D=20true;=0A+=09=09else=0A+=09=09=09values[i++]=20=3D=20= BoolGetDatum(slot_contents.data.unrestricted);=0A+=0A+=09=09if=20= (SlotIsPhysical(&slot_contents)=20||=20slot_contents.data.unrestricted)=0A= +=09=09=09nulls[i++]=20=3D=20true;=0A+=09=09else=0A+=09=09{=0A+=09=09=09= List=09=20=20=20*publications;=0A+=09=09=09Datum=09=20=20=20*oids;=0A+=09= =09=09int=09=09=09j=20=3D=200;=0A+=0A+=09=09=09/*=0A+=09=09=09=20*=20= ReplicationSlotAllocationLock=20keeps=20the=20slot=20directory=20stable.=0A= +=09=09=09=20*=20Do=20not=20hold=20ReplicationSlotControlLock=20across=20= sidecar=20file=20I/O.=0A+=09=09=09=20*/=0A+=09=09=09= LWLockRelease(ReplicationSlotControlLock);=0A+=09=09=09publications=20=3D=20= LogicalSlotScopeGetPublications(slot);=0A+=09=09=09= LWLockAcquire(ReplicationSlotControlLock,=20LW_SHARED);=0A+=09=09=09oids=20= =3D=20palloc_array(Datum,=20list_length(publications));=0A+=0A+=09=09=09= foreach_oid(pubid,=20publications)=0A+=09=09=09=09oids[j++]=20=3D=20= ObjectIdGetDatum(pubid);=0A+=09=09=09values[i++]=20=3D=20= PointerGetDatum(construct_array_builtin(oids,=20j,=20OIDOID));=0A+=09=09=09= list_free(publications);=0A+=09=09=09pfree(oids);=0A+=09=09}=0A+=0A+=09=09= if=20(SlotIsPhysical(&slot_contents))=0A+=09=09=09nulls[i++]=20=3D=20= true;=0A+=09=09else=0A+=09=09=09values[i++]=20=3D=20= BoolGetDatum(slot_contents.data.restricted_scope_ready);=0A+=0A+=09=09if=20= (SlotIsPhysical(&slot_contents)=20||=20slot_contents.data.unrestricted)=0A= +=09=09=09nulls[i++]=20=3D=20true;=0A+=09=09else=0A+=09=09=09values[i++]=20= =3D=20Int64GetDatum(=0A+=09=09=09=09=09=09=09=09=09=09(int64)=20= slot_contents.data.restricted_scope_incarnation);=0A+=0A+=09=09if=20= (SlotIsPhysical(&slot_contents)=20||=20slot_contents.data.unrestricted=20= ||=0A+=09=09=09!slot_contents.data.restricted_scope_ready)=0A+=09=09=09= nulls[i++]=20=3D=20true;=0A+=09=09else=0A+=09=09=09values[i++]=20=3D=20= LSNGetDatum(slot_contents.data.restricted_scope_ready_lsn);=0A+=0A=20=09=09= Assert(i=20=3D=3D=20PG_GET_REPLICATION_SLOTS_COLS);=0A=20=0A=20=09=09= tuplestore_putvalues(rsinfo->setResult,=20rsinfo->setDesc,=0A@@=20-484,6=20= +623,7=20@@=20pg_get_replication_slots(PG_FUNCTION_ARGS)=0A=20=09}=0A=20=0A= =20=09LWLockRelease(ReplicationSlotControlLock);=0A+=09= LWLockRelease(ReplicationSlotAllocationLock);=0A=20=0A=20=09return=20= (Datum)=200;=0A=20}=0A@@=20-553,8=20+693,6=20@@=20= pg_replication_slot_advance(PG_FUNCTION_ARGS)=0A=20=09HeapTuple=09tuple;=0A= =20=09Datum=09=09result;=0A=20=0A-=09Assert(!MyReplicationSlot);=0A-=0A=20= =09CheckSlotPermissions();=0A=20=0A=20=09if=20= (!XLogRecPtrIsValid(moveto))=0A@@=20-651,6=20+789,7=20@@=20= copy_replication_slot(FunctionCallInfo=20fcinfo,=20bool=20logical_slot)=0A= =20=09Datum=09=09result;=0A=20=09TupleDesc=09tupdesc;=0A=20=09HeapTuple=09= tuple;=0A+=09List=09=20=20=20*copy_publications=20=3D=20NIL;=0A=20=0A=20=09= if=20(get_call_result_type(fcinfo,=20NULL,=20&tupdesc)=20!=3D=20= TYPEFUNC_COMPOSITE)=0A=20=09=09elog(ERROR,=20"return=20type=20must=20be=20= a=20row=20type");=0A@@=20-662,6=20+801,7=20@@=20= copy_replication_slot(FunctionCallInfo=20fcinfo,=20bool=20logical_slot)=0A= =20=09else=0A=20=09=09CheckSlotRequirements(false);=0A=20=0A+=09= LWLockAcquire(ReplicationSlotAllocationLock,=20LW_SHARED);=0A=20=09= LWLockAcquire(ReplicationSlotControlLock,=20LW_SHARED);=0A=20=0A=20=09/*=0A= @@=20-691,6=20+831,12=20@@=20copy_replication_slot(FunctionCallInfo=20= fcinfo,=20bool=20logical_slot)=0A=20=09}=0A=20=0A=20=09= LWLockRelease(ReplicationSlotControlLock);=0A+=09if=20(src=20!=3D=20NULL=20= &&=20SlotIsLogical(&first_slot_contents)=20&&=0A+=09=09= !first_slot_contents.data.unrestricted)=0A+=09{=0A+=09=09= copy_publications=20=3D=20LogicalSlotScopeGetPublications(src);=0A+=09}=0A= +=09LWLockRelease(ReplicationSlotAllocationLock);=0A=20=0A=20=09if=20= (src=20=3D=3D=20NULL)=0A=20=09=09ereport(ERROR,=0A@@=20-725,6=20+871,12=20= @@=20copy_replication_slot(FunctionCallInfo=20fcinfo,=20bool=20= logical_slot)=0A=20=09=09=09=09errmsg("cannot=20copy=20invalidated=20= replication=20slot=20\"%s\"",=0A=20=09=09=09=09=09=20=20=20= NameStr(*src_name)));=0A=20=0A+=09if=20(logical_slot=20&&=20= !first_slot_contents.data.unrestricted=20&&=0A+=09=09= IsTransactionBlock())=0A+=09=09ereport(ERROR,=0A+=09=09=09=09= (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),=0A+=09=09=09=09=20= errmsg("restricted=20logical=20replication=20slots=20cannot=20be=20= copied=20inside=20a=20transaction=20block")));=0A+=0A=20=09/*=20= Overwrite=20params=20from=20optional=20arguments=20*/=0A=20=09if=20= (PG_NARGS()=20>=3D=203)=0A=20=09=09temporary=20=3D=20PG_GETARG_BOOL(2);=0A= @@=20-755,13=20+907,15=20@@=20copy_replication_slot(FunctionCallInfo=20= fcinfo,=20bool=20logical_slot)=0A=20=09=09=20*=20on=20the=20promoted=20= standby=20because=20the=20slot=20retains=20the=20restart_lsn=0A=20=09=09=20= *=20and=20confirmed_flush_lsn=20that=20are=20much=20later=20than=20= expected.=0A=20=09=09=20*/=0A-=09=09= create_logical_replication_slot(NameStr(*dst_name),=0A-=09=09=09=09=09=09= =09=09=09=09plugin,=0A-=09=09=09=09=09=09=09=09=09=09temporary,=0A-=09=09= =09=09=09=09=09=09=09=09false,=0A-=09=09=09=09=09=09=09=09=09=09false,=0A= -=09=09=09=09=09=09=09=09=09=09src_restart_lsn,=0A-=09=09=09=09=09=09=09=09= =09=09false);=0A+=09=09= create_logical_replication_slot_from_publications(NameStr(*dst_name),=0A= +=09=09=09=09=09=09=09=09=09=09=09=09=09=09=20=20plugin,=0A+=09=09=09=09=09= =09=09=09=09=09=09=09=09=09=20=20temporary,=0A+=09=09=09=09=09=09=09=09=09= =09=09=09=09=09=20=20false,=0A+=09=09=09=09=09=09=09=09=09=09=09=09=09=09= =20=20false,=0A+=09=09=09=09=09=09=09=09=09=09=09=09=09=09=20=20= first_slot_contents.data.unrestricted,=0A+=09=09=09=09=09=09=09=09=09=09=09= =09=09=09=20=20copy_publications,=0A+=09=09=09=09=09=09=09=09=09=09=09=09= =09=09=20=20src_restart_lsn,=0A+=09=09=09=09=09=09=09=09=09=09=09=09=09=09= =20=20false);=0A=20=09}=0A=20=09else=0A=20=09=09= create_physical_replication_slot(NameStr(*dst_name),=0A@@=20-885,7=20= +1039,9=20@@=20copy_replication_slot(FunctionCallInfo=20fcinfo,=20bool=20= logical_slot)=0A=20=09tuple=20=3D=20heap_form_tuple(tupdesc,=20values,=20= nulls);=0A=20=09result=20=3D=20HeapTupleGetDatum(tuple);=0A=20=0A-=09= ReplicationSlotRelease();=0A+=09if=20(!logical_slot=20||=20= first_slot_contents.data.unrestricted)=0A+=09=09= ReplicationSlotRelease();=0A+=09list_free(copy_publications);=0A=20=0A=20= =09PG_RETURN_DATUM(result);=0A=20}=0Adiff=20--git=20= a/src/backend/replication/walreceiver.c=20= b/src/backend/replication/walreceiver.c=0Aindex=20= 61dc6a5588b..07120cf0ee2=20100644=0A---=20= a/src/backend/replication/walreceiver.c=0A+++=20= b/src/backend/replication/walreceiver.c=0A@@=20-441,7=20+441,8=20@@=20= WalReceiverMain(const=20void=20*startup_data,=20size_t=20= startup_data_len)=0A=20=09=09=09=09=09=20"pg_walreceiver_%lld",=0A=20=09=09= =09=09=09=20(long=20long=20int)=20walrcv_get_backend_pid(wrconn));=0A=20=0A= -=09=09=09walrcv_create_slot(wrconn,=20slotname,=20true,=20false,=20= false,=200,=20NULL);=0A+=09=09=09walrcv_create_slot(wrconn,=20slotname,=20= true,=20false,=20false,=20true,=20NIL,=0A+=09=09=09=09=09=09=09=20=20=20= 0,=20NULL);=0A=20=0A=20=09=09=09SpinLockAcquire(&walrcv->mutex);=0A=20=09= =09=09strlcpy(walrcv->slotname,=20slotname,=20NAMEDATALEN);=0Adiff=20= --git=20a/src/backend/replication/walsender.c=20= b/src/backend/replication/walsender.c=0Aindex=20c65dd324325..15a259c748a=20= 100644=0A---=20a/src/backend/replication/walsender.c=0A+++=20= b/src/backend/replication/walsender.c=0A@@=20-54,6=20+54,7=20@@=0A=20= #include=20"access/timeline.h"=0A=20#include=20"access/transam.h"=0A=20= #include=20"access/twophase.h"=0A+#include=20"access/table.h"=0A=20= #include=20"access/xact.h"=0A=20#include=20"access/xlog_internal.h"=0A=20= #include=20"access/xlogreader.h"=0A@@=20-62,6=20+63,7=20@@=0A=20#include=20= "backup/basebackup.h"=0A=20#include=20"backup/basebackup_incremental.h"=0A= =20#include=20"catalog/pg_authid.h"=0A+#include=20= "catalog/pg_publication.h"=0A=20#include=20"catalog/pg_type.h"=0A=20= #include=20"commands/defrem.h"=0A=20#include=20"funcapi.h"=0A@@=20-76,6=20= +78,7=20@@=0A=20#include=20"replication/logical.h"=0A=20#include=20= "replication/slotsync.h"=0A=20#include=20"replication/slot.h"=0A= +#include=20"replication/slotscope.h"=0A=20#include=20= "replication/snapbuild.h"=0A=20#include=20"replication/syncrep.h"=0A=20= #include=20"replication/walreceiver.h"=0A@@=20-85,6=20+88,7=20@@=0A=20= #include=20"storage/aio_subsys.h"=0A=20#include=20"storage/fd.h"=0A=20= #include=20"storage/ipc.h"=0A+#include=20"storage/lmgr.h"=0A=20#include=20= "storage/pmsignal.h"=0A=20#include=20"storage/proc.h"=0A=20#include=20= "storage/procarray.h"=0A@@=20-101,6=20+105,7=20@@=0A=20#include=20= "utils/ps_status.h"=0A=20#include=20"utils/timeout.h"=0A=20#include=20= "utils/timestamp.h"=0A+#include=20"utils/varlena.h"=0A=20#include=20= "utils/wait_event.h"=0A=20=0A=20/*=20Minimum=20interval=20used=20by=20= walsender=20for=20stats=20flushes,=20in=20ms=20*/=0A@@=20-1184,17=20= +1189,20=20@@=20logical_read_xlog_page(XLogReaderState=20*state,=20= XLogRecPtr=20targetPagePtr,=20int=20req=0A=20/*=0A=20=20*=20Process=20= extra=20options=20given=20to=20CREATE_REPLICATION_SLOT.=0A=20=20*/=0A+=0A= =20static=20void=0A=20= parseCreateReplSlotOptions(CreateReplicationSlotCmd=20*cmd,=0A=20=09=09=09= =09=09=09=20=20=20bool=20*reserve_wal,=0A=20=09=09=09=09=09=09=20=20=20= CRSSnapshotAction=20*snapshot_action,=0A-=09=09=09=09=09=09=20=20=20bool=20= *two_phase,=20bool=20*failover)=0A+=09=09=09=09=09=09=20=20=20bool=20= *two_phase,=20bool=20*failover,=0A+=09=09=09=09=09=09=20=20=20bool=20= *unrestricted,=20List=20**publications)=0A=20{=0A=20=09ListCell=20=20=20= *lc;=0A=20=09bool=09=09snapshot_action_given=20=3D=20false;=0A=20=09bool=09= =09reserve_wal_given=20=3D=20false;=0A=20=09bool=09=09two_phase_given=20= =3D=20false;=0A=20=09bool=09=09failover_given=20=3D=20false;=0A+=09bool=09= =09publication_names_given=20=3D=20false;=0A=20=0A=20=09/*=20Parse=20= options=20*/=0A=20=09foreach(lc,=20cmd->options)=0A@@=20-1253,9=20= +1261,28=20@@=20parseCreateReplSlotOptions(CreateReplicationSlotCmd=20= *cmd,=0A=20=09=09=09failover_given=20=3D=20true;=0A=20=09=09=09*failover=20= =3D=20defGetBoolean(defel);=0A=20=09=09}=0A+=09=09else=20if=20= (strcmp(defel->defname,=20"publication_names")=20=3D=3D=200)=0A+=09=09{=0A= +=09=09=09if=20(publication_names_given=20||=20cmd->kind=20!=3D=20= REPLICATION_KIND_LOGICAL)=0A+=09=09=09=09ereport(ERROR,=0A+=09=09=09=09=09= =09(errcode(ERRCODE_SYNTAX_ERROR),=0A+=09=09=09=09=09=09=20= errmsg("conflicting=20or=20redundant=20options")));=0A+=09=09=09= publication_names_given=20=3D=20true;=0A+=09=09=09if=20= (!SplitIdentifierString(pstrdup(defGetString(defel)),=20',',=0A+=09=09=09= =09=09=09=09=09=09=20=20=20publications))=0A+=09=09=09=09ereport(ERROR,=0A= +=09=09=09=09=09=09(errcode(ERRCODE_SYNTAX_ERROR),=0A+=09=09=09=09=09=09=20= errmsg("invalid=20publication=20name=20list")));=0A+=09=09}=0A=20=09=09= else=0A=20=09=09=09elog(ERROR,=20"unrecognized=20option:=20%s",=20= defel->defname);=0A=20=09}=0A+=0A+=09if=20(publication_names_given=20&&=20= *publications=20=3D=3D=20NIL)=0A+=09=09ereport(ERROR,=0A+=09=09=09=09= (errcode(ERRCODE_SYNTAX_ERROR),=0A+=09=09=09=09=20= errmsg("publication_names=20must=20not=20be=20empty")));=0A+=09= *unrestricted=20=3D=20!publication_names_given;=0A=20}=0A=20=0A=20/*=0A= @@=20-1270,6=20+1297,8=20@@=20= CreateReplicationSlot(CreateReplicationSlotCmd=20*cmd)=0A=20=09bool=09=09= reserve_wal=20=3D=20false;=0A=20=09bool=09=09two_phase=20=3D=20false;=0A=20= =09bool=09=09failover=20=3D=20false;=0A+=09bool=09=09unrestricted=20=3D=20= true;=0A+=09List=09=20=20=20*publications=20=3D=20NIL;=0A=20=09= CRSSnapshotAction=20snapshot_action=20=3D=20CRS_EXPORT_SNAPSHOT;=0A=20=09= DestReceiver=20*dest;=0A=20=09TupOutputState=20*tstate;=0A@@=20-1277,10=20= +1306,8=20@@=20CreateReplicationSlot(CreateReplicationSlotCmd=20*cmd)=0A=20= =09Datum=09=09values[4];=0A=20=09bool=09=09nulls[4]=20=3D=20{0};=0A=20=0A= -=09Assert(!MyReplicationSlot);=0A-=0A=20=09= parseCreateReplSlotOptions(cmd,=20&reserve_wal,=20&snapshot_action,=20= &two_phase,=0A-=09=09=09=09=09=09=09=20=20=20&failover);=0A+=09=09=09=09=09= =09=09=20=20=20&failover,=20&unrestricted,=20&publications);=0A=20=0A=20=09= if=20(cmd->kind=20=3D=3D=20REPLICATION_KIND_PHYSICAL)=0A=20=09{=0A@@=20= -1305,6=20+1332,10=20@@=20CreateReplicationSlot(CreateReplicationSlotCmd=20= *cmd)=0A=20=09=09bool=09=09need_full_snapshot=20=3D=20false;=0A=20=0A=20=09= =09Assert(cmd->kind=20=3D=3D=20REPLICATION_KIND_LOGICAL);=0A+=09=09if=20= (!unrestricted=20&&=20strcmp(cmd->plugin,=20"pgoutput")=20!=3D=200)=0A+=09= =09=09ereport(ERROR,=0A+=09=09=09=09=09= (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),=0A+=09=09=09=09=09=20= errmsg("restricted=20logical=20replication=20slots=20currently=20require=20= pgoutput")));=0A=20=0A=20=09=09CheckLogicalDecodingRequirements(false);=0A= =20=0A@@=20-1371,6=20+1402,19=20@@=20= CreateReplicationSlot(CreateReplicationSlotCmd=20*cmd)=0A=20=09=09=20*=20= Ensure=20the=20logical=20decoding=20is=20enabled=20before=20initializing=20= the=0A=20=09=09=20*=20logical=20decoding=20context.=0A=20=09=09=20*/=0A+=09= =09if=20(!unrestricted)=0A+=09=09{=0A+=09=09=09/*=20Publication=20= catalog=20access=20needs=20a=20transaction.=20*/=0A+=09=09=09if=20= (snapshot_action=20!=3D=20CRS_USE_SNAPSHOT)=0A+=09=09=09=09= StartTransactionCommand();=0A+=09=09=09= LogicalSlotScopePrepareFromPublications(MyReplicationSlot,=0A+=09=09=09=09= =09=09=09=09=09=09=09=09=09publications);=0A+=09=09=09if=20= (snapshot_action=20!=3D=20CRS_USE_SNAPSHOT)=0A+=09=09=09=09= CommitTransactionCommand();=0A+=09=09}=0A+=09=09else=0A+=09=09=09= LogicalSlotScopeConfigureUnrestricted(MyReplicationSlot);=0A+=09=09= list_free(publications);=0A=20=09=09EnsureLogicalDecodingEnabled();=0A=20= =0A=20=09=09/*=20See=20the=20comment=20in=20= create_logical_replication_slot()=20*/=0A@@=20-1397,6=20+1441,20=20@@=20= CreateReplicationSlot(CreateReplicationSlotCmd=20*cmd)=0A=20=09=09/*=20= build=20initial=20snapshot,=20might=20take=20a=20while=20*/=0A=20=09=09= DecodingContextFindStartpoint(ctx);=0A=20=0A+=09=09/*=0A+=09=09=20*=20= Install=20transactional=20relation=20mappings=20only=20after=20logical=20= decoding=0A+=09=09=20*=20has=20established=20the=20slot's=20start=20= point.=20CreateInitDecodingContext()=0A+=09=09=20*=20rejects=20a=20= transaction=20that=20has=20already=20performed=20catalog=20writes.=0A+=09= =09=20*/=0A+=09=09if=20(!unrestricted)=0A+=09=09{=0A+=09=09=09if=20= (snapshot_action=20!=3D=20CRS_USE_SNAPSHOT)=0A+=09=09=09=09= StartTransactionCommand();=0A+=09=09=09= LogicalSlotScopeFinishCreate(MyReplicationSlot,=20false);=0A+=09=09=09if=20= (snapshot_action=20!=3D=20CRS_USE_SNAPSHOT)=0A+=09=09=09=09= CommitTransactionCommand();=0A+=09=09}=0A+=0A=20=09=09/*=0A=20=09=09=20*=20= Export=20or=20use=20the=20snapshot=20if=20we've=20been=20asked=20to=20do=20= so.=0A=20=09=09=20*=0A@@=20-1537,8=20+1595,6=20@@=20= StartLogicalReplication(StartReplicationCmd=20*cmd)=0A=20=09/*=20make=20= sure=20that=20our=20requirements=20are=20still=20fulfilled=20*/=0A=20=09= CheckLogicalDecodingRequirements(false);=0A=20=0A-=09= Assert(!MyReplicationSlot);=0A-=0A=20=09= ReplicationSlotAcquire(cmd->slotname,=20true,=20true);=0A=20=0A=20=09/*=0A= diff=20--git=20a/src/backend/utils/init/postinit.c=20= b/src/backend/utils/init/postinit.c=0Aindex=203d8c9bdebd5..8695829900d=20= 100644=0A---=20a/src/backend/utils/init/postinit.c=0A+++=20= b/src/backend/utils/init/postinit.c=0A@@=20-42,6=20+42,7=20@@=0A=20= #include=20"postmaster/autovacuum.h"=0A=20#include=20= "postmaster/postmaster.h"=0A=20#include=20"replication/slot.h"=0A= +#include=20"replication/slotscope.h"=0A=20#include=20= "replication/slotsync.h"=0A=20#include=20"replication/walsender.h"=0A=20= #include=20"storage/aio_subsys.h"=0A@@=20-1239,6=20+1240,9=20@@=20= InitPostgres(const=20char=20*in_dbname,=20Oid=20dboid,=0A=20=09=09= CheckMyDatabase(dbname,=20am_superuser,=0A=20=09=09=09=09=09=09(flags=20= &=20INIT_PG_OVERRIDE_ALLOW_CONNS)=20!=3D=200);=0A=20=0A+=09if=20= (!bootstrap)=0A+=09=09LogicalSlotScopeReconcileDatabase();=0A+=0A=20=09= /*=0A=20=09=20*=20Now=20process=20any=20command-line=20switches=20and=20= any=20additional=20GUC=20variable=0A=20=09=20*=20settings=20passed=20in=20= the=20startup=20packet.=20=20=20We=20couldn't=20do=20this=20before=0A= diff=20--git=20a/src/backend/utils/misc/guc_parameters.dat=20= b/src/backend/utils/misc/guc_parameters.dat=0Aindex=20= 3c5e16ad1e7..d20a6452669=20100644=0A---=20= a/src/backend/utils/misc/guc_parameters.dat=0A+++=20= b/src/backend/utils/misc/guc_parameters.dat=0A@@=20-2579,6=20+2579,15=20= @@=0A=20=20=20assign_hook=20=3D>=20= 'assign_restrict_nonsystem_relation_kind',=0A=20},=0A=20=0A+{=20name=20= =3D>=20'restricted_wal_level',=20type=20=3D>=20'enum',=20context=20=3D>=20= 'PGC_INTERNAL',=20group=20=3D>=20'PRESET_OPTIONS',=0A+=20=20short_desc=20= =3D>=20'Shows=20the=20WAL=20level=20required=20by=20restricted=20logical=20= slots.',=0A+=20=20flags=20=3D>=20'GUC_NOT_IN_SAMPLE=20|=20= GUC_DISALLOW_IN_FILE',=0A+=20=20variable=20=3D>=20= 'restricted_wal_level',=0A+=20=20boot_val=20=3D>=20'WAL_LEVEL_REPLICA',=0A= +=20=20options=20=3D>=20'wal_level_options',=0A+=20=20show_hook=20=3D>=20= 'show_restricted_wal_level',=0A+},=0A+=0A=20#=20Not=20for=20general=20= use=20---=20used=20by=20SET=20ROLE=0A=20{=20name=20=3D>=20'role',=20type=20= =3D>=20'string',=20context=20=3D>=20'PGC_USERSET',=20group=20=3D>=20= 'UNGROUPED',=0A=20=20=20short_desc=20=3D>=20'Sets=20the=20current=20= role.',=0Adiff=20--git=20a/src/backend/utils/misc/guc_tables.c=20= b/src/backend/utils/misc/guc_tables.c=0Aindex=20c6d9b2a6f89..6f6e2a879f5=20= 100644=0A---=20a/src/backend/utils/misc/guc_tables.c=0A+++=20= b/src/backend/utils/misc/guc_tables.c=0A@@=20-649,6=20+649,7=20@@=20= static=20int=09shared_memory_size_in_huge_pages;=0A=20static=20int=09= wal_block_size;=0A=20static=20int=09num_os_semaphores;=0A=20static=20int=09= effective_wal_level=20=3D=20WAL_LEVEL_REPLICA;=0A+static=20int=09= restricted_wal_level=20=3D=20WAL_LEVEL_REPLICA;=0A=20static=20bool=20= integer_datetimes;=0A=20=0A=20#ifdef=20USE_ASSERT_CHECKING=0Adiff=20= --git=20a/src/bin/pg_dump/pg_dump.c=20b/src/bin/pg_dump/pg_dump.c=0A= index=20feed88f9854..18beaf963b3=20100644=0A---=20= a/src/bin/pg_dump/pg_dump.c=0A+++=20b/src/bin/pg_dump/pg_dump.c=0A@@=20= -5136,6=20+5136,7=20@@=20getSubscriptions(Archive=20*fout)=0A=20=09int=09= =09=09i_suboriginremotelsn;=0A=20=09int=09=09=09i_subenabled;=0A=20=09= int=09=09=09i_subfailover;=0A+=09int=09=09=09i_subunrestricted;=0A=20=09= int=09=09=09i_subretaindeadtuples;=0A=20=09int=09=09=09= i_submaxretention;=0A=20=09int=09=09=09i,=0A@@=20-5215,6=20+5216,13=20@@=20= getSubscriptions(Archive=20*fout)=0A=20=09=09appendPQExpBufferStr(query,=0A= =20=09=09=09=09=09=09=09=20"=20false=20AS=20subfailover,\n");=0A=20=0A+=09= if=20(fout->remoteVersion=20>=3D=20200000)=0A+=09=09= appendPQExpBufferStr(query,=0A+=09=09=09=09=09=09=09=20"=20= s.subunrestricted,\n");=0A+=09else=0A+=09=09appendPQExpBufferStr(query,=0A= +=09=09=09=09=09=09=09=20"=20true=20AS=20subunrestricted,\n");=0A+=0A=20=09= if=20(fout->remoteVersion=20>=3D=20190000)=0A=20=09=09= appendPQExpBufferStr(query,=0A=20=09=09=09=09=09=09=09=20"=20= s.subretaindeadtuples,\n");=0A@@=20-5277,6=20+5285,7=20@@=20= getSubscriptions(Archive=20*fout)=0A=20=09i_subpasswordrequired=20=3D=20= PQfnumber(res,=20"subpasswordrequired");=0A=20=09i_subrunasowner=20=3D=20= PQfnumber(res,=20"subrunasowner");=0A=20=09i_subfailover=20=3D=20= PQfnumber(res,=20"subfailover");=0A+=09i_subunrestricted=20=3D=20= PQfnumber(res,=20"subunrestricted");=0A=20=09i_subretaindeadtuples=20=3D=20= PQfnumber(res,=20"subretaindeadtuples");=0A=20=09i_submaxretention=20=3D=20= PQfnumber(res,=20"submaxretention");=0A=20=09i_subservername=20=3D=20= PQfnumber(res,=20"subservername");=0A@@=20-5318,6=20+5327,8=20@@=20= getSubscriptions(Archive=20*fout)=0A=20=09=09=09(strcmp(PQgetvalue(res,=20= i,=20i_subrunasowner),=20"t")=20=3D=3D=200);=0A=20=09=09= subinfo[i].subfailover=20=3D=0A=20=09=09=09(strcmp(PQgetvalue(res,=20i,=20= i_subfailover),=20"t")=20=3D=3D=200);=0A+=09=09= subinfo[i].subunrestricted=20=3D=0A+=09=09=09(strcmp(PQgetvalue(res,=20= i,=20i_subunrestricted),=20"t")=20=3D=3D=200);=0A=20=09=09= subinfo[i].subretaindeadtuples=20=3D=0A=20=09=09=09= (strcmp(PQgetvalue(res,=20i,=20i_subretaindeadtuples),=20"t")=20=3D=3D=20= 0);=0A=20=09=09subinfo[i].submaxretention=20=3D=0A@@=20-5593,6=20+5604,9=20= @@=20dumpSubscription(Archive=20*fout,=20const=20SubscriptionInfo=20= *subinfo)=0A=20=09if=20(subinfo->subfailover)=0A=20=09=09= appendPQExpBufferStr(query,=20",=20failover=20=3D=20true");=0A=20=0A+=09= if=20(!subinfo->subunrestricted)=0A+=09=09appendPQExpBufferStr(query,=20= ",=20unrestricted_slot=20=3D=20false");=0A+=0A=20=09if=20= (subinfo->subretaindeadtuples)=0A=20=09=09appendPQExpBufferStr(query,=20= ",=20retain_dead_tuples=20=3D=20true");=0A=20=0Adiff=20--git=20= a/src/bin/pg_dump/pg_dump.h=20b/src/bin/pg_dump/pg_dump.h=0Aindex=20= e6eefa98460..7b01224b8ab=20100644=0A---=20a/src/bin/pg_dump/pg_dump.h=0A= +++=20b/src/bin/pg_dump/pg_dump.h=0A@@=20-720,6=20+720,7=20@@=20typedef=20= struct=20_SubscriptionInfo=0A=20=09bool=09=09subpasswordrequired;=0A=20=09= bool=09=09subrunasowner;=0A=20=09bool=09=09subfailover;=0A+=09bool=09=09= subunrestricted;=0A=20=09bool=09=09subretaindeadtuples;=0A=20=09int=09=09= =09submaxretention;=0A=20=09char=09=20=20=20*subservername;=0Adiff=20= --git=20a/src/bin/pg_dump/t/002_pg_dump.pl=20= b/src/bin/pg_dump/t/002_pg_dump.pl=0Aindex=209258948b583..78adb4d2afa=20= 100644=0A---=20a/src/bin/pg_dump/t/002_pg_dump.pl=0A+++=20= b/src/bin/pg_dump/t/002_pg_dump.pl=0A@@=20-3317,9=20+3317,10=20@@=20my=20= %tests=20=3D=20(=0A=20=09=09create_order=20=3D>=2050,=0A=20=09=09= create_sql=20=3D>=20'CREATE=20SUBSCRIPTION=20sub3=0A=20=09=09=09=09=09=09= =20CONNECTION=20\'dbname=3Ddoesnotexist\'=20PUBLICATION=20pub1=0A-=09=09=09= =09=09=09=20WITH=20(connect=20=3D=20false,=20origin=20=3D=20any,=20= streaming=20=3D=20on);',=0A+=09=09=09=09=09=09=20WITH=20(connect=20=3D=20= false,=20origin=20=3D=20any,=20streaming=20=3D=20on,=0A+=09=09=09=09=09=09= =20=20=20=20=20=20=20unrestricted_slot=20=3D=20false);',=0A=20=09=09= regexp=20=3D>=20qr/^=0A-=09=09=09\QCREATE=20SUBSCRIPTION=20sub3=20= CONNECTION=20'dbname=3Ddoesnotexist'=20PUBLICATION=20pub1=20WITH=20= (connect=20=3D=20false,=20slot_name=20=3D=20'sub3',=20streaming=20=3D=20= on);\E=0A+=09=09=09\QCREATE=20SUBSCRIPTION=20sub3=20CONNECTION=20= 'dbname=3Ddoesnotexist'=20PUBLICATION=20pub1=20WITH=20(connect=20=3D=20= false,=20slot_name=20=3D=20'sub3',=20streaming=20=3D=20on,=20= unrestricted_slot=20=3D=20false);\E=0A=20=09=09=09/xm,=0A=20=09=09like=20= =3D>=20{=20%full_runs,=20section_post_data=20=3D>=201,=20},=0A=20=09=09= unlike=20=3D>=20{=0Adiff=20--git=20a/src/include/access/xlog.h=20= b/src/include/access/xlog.h=0Aindex=20338d68d7424..8141ea52506=20100644=0A= ---=20a/src/include/access/xlog.h=0A+++=20b/src/include/access/xlog.h=0A= @@=20-110,6=20+110,7=20@@=20typedef=20enum=20RecoveryState=0A=20=0A=20= extern=20PGDLLIMPORT=20int=20wal_level;=0A=20extern=20PGDLLIMPORT=20bool=20= XLogLogicalInfo;=0A+extern=20PGDLLIMPORT=20bool=20XLogRestrictedInfo;=0A=20= =0A=20/*=20Is=20WAL=20archiving=20enabled=20(always=20or=20only=20while=20= server=20is=20running=20normally)?=20*/=0A=20#define=20= XLogArchivingActive()=20\=0A@@=20-147,8=20+148,12=20@@=20extern=20= PGDLLIMPORT=20bool=20XLogLogicalInfo;=0A=20=20*=20change=20until=20an=20= XID=20is=20assigned=20to=20the=20transaction.=20In=20other=20words,=20it=0A= =20=20*=20ensures=20that=20the=20same=20result=20is=20returned=20within=20= an=20XID-assigned=20transaction.=0A=20=20*/=0A-#define=20= XLogLogicalInfoActive()=20\=0A+#define=20XLogFullLogicalInfoActive()=20\=0A= =20=09=20(wal_level=20>=3D=20WAL_LEVEL_LOGICAL=20||=20XLogLogicalInfo)=0A= +#define=20XLogRestrictedInfoActive()=20\=0A+=09=20(wal_level=20>=3D=20= WAL_LEVEL_LOGICAL=20||=20XLogRestrictedInfo)=0A+#define=20= XLogLogicalInfoActive()=20\=0A+=09=20(XLogFullLogicalInfoActive()=20||=20= XLogRestrictedInfoActive())=0A=20=0A=20#ifdef=20WAL_DEBUG=0A=20extern=20= PGDLLIMPORT=20bool=20XLOG_DEBUG;=0Adiff=20--git=20= a/src/include/catalog/Makefile=20b/src/include/catalog/Makefile=0Aindex=20= bab57372b88..41c613aa5b2=20100644=0A---=20a/src/include/catalog/Makefile=0A= +++=20b/src/include/catalog/Makefile=0A@@=20-80,6=20+80,7=20@@=20= CATALOG_HEADERS=20:=3D=20\=0A=20=09pg_publication.h=20\=0A=20=09= pg_publication_namespace.h=20\=0A=20=09pg_publication_rel.h=20\=0A+=09= pg_restricted_slot_relation.h=20\=0A=20=09pg_subscription.h=20\=0A=20=09= pg_subscription_rel.h=20\=0A=20=09pg_propgraph_element.h=20\=0Adiff=20= --git=20a/src/include/catalog/meson.build=20= b/src/include/catalog/meson.build=0Aindex=20fa836e4ee25..7dd2e5a7af8=20= 100644=0A---=20a/src/include/catalog/meson.build=0A+++=20= b/src/include/catalog/meson.build=0A@@=20-67,6=20+67,7=20@@=20= catalog_headers=20=3D=20[=0A=20=20=20'pg_publication.h',=0A=20=20=20= 'pg_publication_namespace.h',=0A=20=20=20'pg_publication_rel.h',=0A+=20=20= 'pg_restricted_slot_relation.h',=0A=20=20=20'pg_subscription.h',=0A=20=20= =20'pg_subscription_rel.h',=0A=20=20=20'pg_propgraph_element.h',=0Adiff=20= --git=20a/src/include/catalog/pg_class.h=20= b/src/include/catalog/pg_class.h=0Aindex=20c4af599dc90..1484221a278=20= 100644=0A---=20a/src/include/catalog/pg_class.h=0A+++=20= b/src/include/catalog/pg_class.h=0A@@=20-124,6=20+124,9=20@@=20= CATALOG(pg_class,1259,RelationRelationId)=20BKI_BOOTSTRAP=20= BKI_ROWTYPE_OID(83,Relat=0A=20=09/*=20is=20relation=20a=20partition?=20= */=0A=20=09bool=09=09relispartition=20BKI_DEFAULT(f);=0A=20=0A+=09/*=20= has=20conservative=20mappings=20for=20restricted=20logical=20slots=20*/=0A= +=09bool=09=09relhasrestrictedslots=20BKI_DEFAULT(f);=0A+=0A=20=09/*=20= link=20to=20original=20rel=20during=20table=20rewrite;=20otherwise=200=20= */=0A=20=09Oid=09=09=09relrewrite=20BKI_DEFAULT(0)=20= BKI_LOOKUP_OPT(pg_class);=0A=20=0Adiff=20--git=20= a/src/include/catalog/pg_proc.dat=20b/src/include/catalog/pg_proc.dat=0A= index=2066c3c9a04cf..1d5252fb101=20100644=0A---=20= a/src/include/catalog/pg_proc.dat=0A+++=20= b/src/include/catalog/pg_proc.dat=0A@@=20-11733,18=20+11733,18=20@@=0A=20= =20=20proname=20=3D>=20'pg_get_replication_slots',=20prorows=20=3D>=20= '10',=20proisstrict=20=3D>=20'f',=0A=20=20=20proretset=20=3D>=20't',=20= provolatile=20=3D>=20's',=20prorettype=20=3D>=20'record',=0A=20=20=20= proargtypes=20=3D>=20'',=0A-=20=20proallargtypes=20=3D>=20= '{name,name,text,oid,bool,bool,int4,xid,xid,pg_lsn,pg_lsn,text,int8,bool,p= g_lsn,timestamptz,bool,text,bool,bool,text}',=0A-=20=20proargmodes=20=3D>=20= '{o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o}',=0A-=20=20proargnames=20=3D>= =20= '{slot_name,plugin,slot_type,datoid,temporary,active,active_pid,xmin,catal= og_xmin,restart_lsn,confirmed_flush_lsn,wal_status,safe_wal_size,two_phase= ,two_phase_at,inactive_since,conflicting,invalidation_reason,failover,sync= ed,slotsync_skip_reason}',=0A+=20=20proallargtypes=20=3D>=20= '{name,name,text,oid,bool,bool,int4,xid,xid,pg_lsn,pg_lsn,text,int8,bool,p= g_lsn,timestamptz,bool,text,bool,bool,text,bool,_oid,bool,int8,pg_lsn}',=0A= +=20=20proargmodes=20=3D>=20= '{o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o}',=0A+=20=20= proargnames=20=3D>=20= '{slot_name,plugin,slot_type,datoid,temporary,active,active_pid,xmin,catal= og_xmin,restart_lsn,confirmed_flush_lsn,wal_status,safe_wal_size,two_phase= ,two_phase_at,inactive_since,conflicting,invalidation_reason,failover,sync= ed,slotsync_skip_reason,unrestricted,publication_oids,restricted_scope_rea= dy,restricted_scope_incarnation,restricted_scope_ready_lsn}',=0A=20=20=20= prosrc=20=3D>=20'pg_get_replication_slots'=20},=0A=20{=20oid=20=3D>=20= '3786',=20descr=20=3D>=20'set=20up=20a=20logical=20replication=20slot',=0A= =20=20=20proname=20=3D>=20'pg_create_logical_replication_slot',=20= provolatile=20=3D>=20'v',=0A-=20=20proparallel=20=3D>=20'u',=20= prorettype=20=3D>=20'record',=0A-=20=20proargtypes=20=3D>=20'name=20name=20= bool=20bool=20bool',=0A-=20=20proallargtypes=20=3D>=20= '{name,name,bool,bool,bool,name,pg_lsn}',=0A-=20=20proargmodes=20=3D>=20= '{i,i,i,i,i,o,o}',=0A-=20=20proargnames=20=3D>=20= '{slot_name,plugin,temporary,twophase,failover,slot_name,lsn}',=0A-=20=20= proargdefaults=20=3D>=20'{false,false,false}',=0A+=20=20proparallel=20=3D>= =20'u',=20proisstrict=20=3D>=20'f',=20prorettype=20=3D>=20'record',=0A+=20= =20proargtypes=20=3D>=20'name=20name=20bool=20bool=20bool=20_text',=0A+=20= =20proallargtypes=20=3D>=20= '{name,name,bool,bool,bool,_text,name,pg_lsn}',=0A+=20=20proargmodes=20= =3D>=20'{i,i,i,i,i,i,o,o}',=0A+=20=20proargnames=20=3D>=20= '{slot_name,plugin,temporary,twophase,failover,publications,slot_name,lsn}= ',=0A+=20=20proargdefaults=20=3D>=20'{false,false,false,NULL}',=0A=20=20=20= prosrc=20=3D>=20'pg_create_logical_replication_slot'=20},=0A=20{=20oid=20= =3D>=20'4222',=0A=20=20=20descr=20=3D>=20'copy=20a=20logical=20= replication=20slot,=20changing=20temporality=20and=20plugin',=0Adiff=20= --git=20a/src/include/catalog/pg_restricted_slot_relation.h=20= b/src/include/catalog/pg_restricted_slot_relation.h=0Anew=20file=20mode=20= 100644=0Aindex=2000000000000..9e0bd547251=0A---=20/dev/null=0A+++=20= b/src/include/catalog/pg_restricted_slot_relation.h=0A@@=20-0,0=20+1,36=20= @@=0A= +/*-----------------------------------------------------------------------= --=0A+=20*=0A+=20*=20pg_restricted_slot_relation.h=0A+=20*=09=20=20= conservative=20relation=20mappings=20for=20restricted=20logical=20slots=0A= +=20*=0A+=20*=20Portions=20Copyright=20(c)=201996-2026,=20PostgreSQL=20= Global=20Development=20Group=0A+=20*=20Portions=20Copyright=20(c)=20= 1994,=20Regents=20of=20the=20University=20of=20California=0A+=20*=0A+=20= *=20src/include/catalog/pg_restricted_slot_relation.h=0A+=20*=0A+=20= *-------------------------------------------------------------------------= =0A+=20*/=0A+#ifndef=20PG_RESTRICTED_SLOT_RELATION_H=0A+#define=20= PG_RESTRICTED_SLOT_RELATION_H=0A+=0A+#include=20"catalog/genbki.h"=0A= +#include=20"catalog/pg_restricted_slot_relation_d.h"=20/*=20IWYU=20= pragma:=20export=20*/=0A+=0A+BEGIN_CATALOG_STRUCT=0A+=0A= +CATALOG(pg_restricted_slot_relation,8050,RestrictedSlotRelationRelationId= )=0A+{=0A+=09NameData=09rsrslotname;=0A+=09Oid=09=09=09rsrrelid=20= BKI_LOOKUP_OPT(pg_class);=0A+=09int64=09=09rsrincarnation;=0A+}=20= FormData_pg_restricted_slot_relation;=0A+=0A+END_CATALOG_STRUCT=0A+=0A= +typedef=20FormData_pg_restricted_slot_relation=20= *Form_pg_restricted_slot_relation;=0A+=0A= +DECLARE_UNIQUE_INDEX_PKEY(pg_restricted_slot_relation_slot_rel_index,=20= 8051,=20RestrictedSlotRelationSlotRelIndexId,=20= pg_restricted_slot_relation,=20btree(rsrslotname=20name_ops,=20rsrrelid=20= oid_ops,=20rsrincarnation=20int8_ops));=0A= +DECLARE_INDEX(pg_restricted_slot_relation_rel_index,=208052,=20= RestrictedSlotRelationRelIndexId,=20pg_restricted_slot_relation,=20= btree(rsrrelid=20oid_ops));=0A= +DECLARE_INDEX(pg_restricted_slot_relation_slot_index,=208053,=20= RestrictedSlotRelationSlotIndexId,=20pg_restricted_slot_relation,=20= btree(rsrslotname=20name_ops));=0A+=0A+#endif=09=09=09=09=09=09=09/*=20= PG_RESTRICTED_SLOT_RELATION_H=20*/=0Adiff=20--git=20= a/src/include/catalog/pg_subscription.h=20= b/src/include/catalog/pg_subscription.h=0Aindex=20= d2781a0b837..fde1b81eaf8=20100644=0A---=20= a/src/include/catalog/pg_subscription.h=0A+++=20= b/src/include/catalog/pg_subscription.h=0A@@=20-80,6=20+80,9=20@@=20= CATALOG(pg_subscription,6100,SubscriptionRelationId)=20= BKI_SHARED_RELATION=20BKI_ROW=0A=20=09=09=09=09=09=09=09=09=20*=20slots)=20= in=20the=20upstream=20database=20are=20enabled=0A=20=09=09=09=09=09=09=09= =09=20*=20to=20be=20synchronized=20to=20the=20standbys.=20*/=0A=20=0A+=09= bool=09=09subunrestricted=20BKI_DEFAULT(t);=20/*=20True=20if=20the=20= associated=0A+=09=09=09=09=09=09=09=09=09=09=09=20*=20replication=20= slots=20are=20unrestricted.=20*/=0A+=0A=20=09bool=09=09= subretaindeadtuples;=09/*=20True=20if=20dead=20tuples=20useful=20for=0A=20= =09=09=09=09=09=09=09=09=09=09=20*=20conflict=20detection=20are=20= retained=20*/=0A=20=0A@@=20-163,6=20+166,8=20@@=20typedef=20struct=20= Subscription=0A=20=09=09=09=09=09=09=09=09=20*=20(i.e.=20the=20main=20= slot=20and=20the=20table=20sync=0A=20=09=09=09=09=09=09=09=09=20*=20= slots)=20in=20the=20upstream=20database=20are=20enabled=0A=20=09=09=09=09= =09=09=09=09=20*=20to=20be=20synchronized=20to=20the=20standbys.=20*/=0A= +=09bool=09=09unrestricted;=09/*=20True=20if=20the=20associated=20= replication=20slots=20are=0A+=09=09=09=09=09=09=09=09=20*=20= unrestricted.=20*/=0A=20=09bool=09=09retaindeadtuples;=09/*=20True=20if=20= dead=20tuples=20useful=20for=20conflict=0A=20=09=09=09=09=09=09=09=09=09=20= *=20detection=20are=20retained=20*/=0A=20=09int32=09=09maxretention;=09= /*=20The=20maximum=20duration=20(in=20milliseconds)=20for=0Adiff=20--git=20= a/src/include/replication/logicalctl.h=20= b/src/include/replication/logicalctl.h=0Aindex=20= 0bc1302f130..23fdfe78cfb=20100644=0A---=20= a/src/include/replication/logicalctl.h=0A+++=20= b/src/include/replication/logicalctl.h=0A@@=20-19,8=20+19,11=20@@=20= extern=20void=20InitializeProcessXLogLogicalInfo(void);=0A=20extern=20= bool=20ProcessBarrierUpdateXLogLogicalInfo(void);=0A=20extern=20bool=20= IsLogicalDecodingEnabled(void);=0A=20extern=20bool=20= IsXLogLogicalInfoEnabled(void);=0A+extern=20bool=20= IsXLogRestrictedInfoEnabled(void);=0A=20extern=20void=20= AtEOXact_LogicalCtl(void);=0A=20extern=20void=20= EnsureLogicalDecodingEnabled(void);=0A+extern=20void=20= EnsureFullLogicalWAL(void);=0A+extern=20void=20= EnsureRestrictedLogicalWAL(void);=0A=20extern=20void=20= EnableLogicalDecoding(void);=0A=20extern=20void=20= RequestDisableLogicalDecoding(void);=0A=20extern=20void=20= DisableLogicalDecodingIfNecessary(void);=0Adiff=20--git=20= a/src/include/replication/slot.h=20b/src/include/replication/slot.h=0A= index=209b29444cbca..49bf1ee6a1b=20100644=0A---=20= a/src/include/replication/slot.h=0A+++=20= b/src/include/replication/slot.h=0A@@=20-159,6=20+159,18=20@@=20typedef=20= struct=20ReplicationSlotPersistentData=0A=20=09=20*=20for=20logical=20= slots=20on=20the=20primary=20server.=0A=20=09=20*/=0A=20=09bool=09=09= failover;=0A+=0A+=09/*=20May=20this=20logical=20slot=20decode=20every=20= eligible=20relation?=20*/=0A+=09bool=09=09unrestricted;=0A+=0A+=09/*=20= Are=20the=20transactional=20writer=20mappings=20for=20this=20restricted=20= slot=20ready?=20*/=0A+=09bool=09=09restricted_scope_ready;=0A+=0A+=09/*=20= Identifies=20this=20restricted=20slot=20creation=20across=20name=20= reuse.=20*/=0A+=09uint64=09=09restricted_scope_incarnation;=0A+=0A+=09/*=20= WAL=20boundary=20after=20the=20transactional=20scope=20mappings=20= committed.=20*/=0A+=09XLogRecPtr=09restricted_scope_ready_lsn;=0A=20}=20= ReplicationSlotPersistentData;=0A=20=0A=20/*=0A@@=20-341,6=20+353,8=20@@=20= extern=20void=20ReplicationSlotAlter(const=20char=20*name,=20const=20= bool=20*failover,=0A=20=0A=20extern=20void=20= ReplicationSlotAcquire(const=20char=20*name,=20bool=20nowait,=0A=20=09=09= =09=09=09=09=09=09=20=20=20bool=20error_if_invalid);=0A+extern=20bool=20= ReplicationSlotConditionalAcquire(const=20char=20*name,=0A+=09=09=09=09=09= =09=09=09=09=09=09=20=20bool=20error_if_invalid);=0A=20extern=20void=20= ReplicationSlotRelease(void);=0A=20extern=20void=20= ReplicationSlotCleanup(bool=20synced_only);=0A=20extern=20void=20= ReplicationSlotSave(void);=0A@@=20-360,6=20+374,8=20@@=20extern=20void=20= ReplicationSlotsComputeRequiredLSN(void);=0A=20extern=20XLogRecPtr=20= ReplicationSlotsComputeLogicalRestartLSN(void);=0A=20extern=20bool=20= ReplicationSlotsCountDBSlots(Oid=20dboid,=20int=20*nslots,=20int=20= *nactive);=0A=20extern=20bool=20CheckLogicalSlotExists(void);=0A+extern=20= bool=20CheckRestrictedLogicalSlotExists(void);=0A+extern=20bool=20= CheckFullWalLogicalSlotExists(void);=0A=20extern=20void=20= ReplicationSlotsDropDBSlots(Oid=20dboid);=0A=20extern=20bool=20= InvalidateObsoleteReplicationSlots(uint32=20possible_causes,=0A=20=09=09=09= =09=09=09=09=09=09=09=09=20=20=20XLogSegNo=20oldestSegno,=0Adiff=20--git=20= a/src/include/replication/slotscope.h=20= b/src/include/replication/slotscope.h=0Anew=20file=20mode=20100644=0A= index=2000000000000..eae5531844b=0A---=20/dev/null=0A+++=20= b/src/include/replication/slotscope.h=0A@@=20-0,0=20+1,49=20@@=0A= +/*-----------------------------------------------------------------------= --=0A+=20*=0A+=20*=20slotscope.h=0A+=20*=09=20=20Logical=20replication=20= slot=20scope=20management.=0A+=20*=0A+=20*=0A+=20*=20Portions=20= Copyright=20(c)=202026,=20PostgreSQL=20Global=20Development=20Group=0A+=20= *=0A+=20*=09=20=20src/include/replication/slotscope.h=0A+=20*=0A+=20= *-------------------------------------------------------------------------= =0A+=20*/=0A+#ifndef=20SLOTSCOPE_H=0A+#define=20SLOTSCOPE_H=0A+=0A= +#include=20"nodes/pg_list.h"=0A+=0A+struct=20RelationData;=0A+struct=20= ReplicationSlot;=0A+=0A+extern=20void=20= LogicalSlotScopeInitialize(void);=0A+extern=20void=20= LogicalSlotScopeReconcileDatabase(void);=0A+extern=20void=20= LogicalSlotScopeConfigureUnrestricted(struct=20ReplicationSlot=20*slot);=0A= +=0A+/*=20Caller=20must=20provide=20a=20transaction=20environment.=20*/=0A= +extern=20void=20LogicalSlotScopePrepareFromPublications(=0A+=09=09=09=09= =09=09=09=09=09=09=09=09=09struct=20ReplicationSlot=20*slot,=20List=20= *pubnames);=0A+extern=20void=20= LogicalSlotScopePrepareFromPublicationOids(=0A+=09=09=09=09=09=09=09=09=09= =09=09=09=09=20=20=20struct=20ReplicationSlot=20*slot,=20List=20= *publications);=0A+extern=20void=20LogicalSlotScopeFinishCreate(struct=20= ReplicationSlot=20*slot,=0A+=09=09=09=09=09=09=09=09=09=09=20bool=20= finalize_at_xact_end);=0A+extern=20void=20= LogicalSlotScopeRestorePublications(struct=20ReplicationSlot=20*slot,=0A= +=09=09=09=09=09=09=09=09=09=09=09=09List=20*publications,=0A+=09=09=09=09= =09=09=09=09=09=09=09=09uint64=20incarnation,=0A+=09=09=09=09=09=09=09=09= =09=09=09=09XLogRecPtr=20ready_lsn);=0A+extern=20void=20= LogicalSlotScopeRestore(struct=20ReplicationSlot=20*slot);=0A+extern=20= List=20*LogicalSlotScopeGetPublications(struct=20ReplicationSlot=20= *slot);=0A+extern=20void=20LogicalSlotScopePublicationAddRelations(Oid=20= pubid,=20List=20*relations);=0A+extern=20void=20= LogicalSlotScopeNoteToastCreation(Oid=20owner,=20Oid=20toastrelid);=0A= +extern=20void=20LogicalSlotScopeRelationDrop(Oid=20relid);=0A+extern=20= void=20LogicalSlotScopeCleanup(void);=0A+extern=20bool=20= LogicalSlotScopePublicationsContain(struct=20ReplicationSlot=20*slot,=0A= +=09=09=09=09=09=09=09=09=09=09=09=09List=20*pubnames);=0A+=0A+extern=20= void=20CheckLogicalSlotScopeHierarchyChange(Oid=20childrelid,=0A+=09=09=09= =09=09=09=09=09=09=09=09=09=20Oid=20parentrelid);=0A+extern=20bool=20= RelationNeedsLogicalTupleWAL(struct=20RelationData=20*relation);=0A+=0A= +#endif=09=09=09=09=09=09=09/*=20SLOTSCOPE_H=20*/=0Adiff=20--git=20= a/src/include/replication/walreceiver.h=20= b/src/include/replication/walreceiver.h=0Aindex=20= 5cfe90f9989..515a9df9a6d=20100644=0A---=20= a/src/include/replication/walreceiver.h=0A+++=20= b/src/include/replication/walreceiver.h=0A@@=20-367,12=20+367,14=20@@=20= typedef=20void=20(*walrcv_send_fn)=20(WalReceiverConn=20*conn,=0A=20=20*=20= slot,=20or=20NULL=20for=20a=20physical=20slot.=0A=20=20*/=0A=20typedef=20= char=20*(*walrcv_create_slot_fn)=20(WalReceiverConn=20*conn,=0A-=09=09=09= =09=09=09=09=09=09=09const=20char=20*slotname,=0A-=09=09=09=09=09=09=09=09= =09=09bool=20temporary,=0A-=09=09=09=09=09=09=09=09=09=09bool=20= two_phase,=0A-=09=09=09=09=09=09=09=09=09=09bool=20failover,=0A-=09=09=09= =09=09=09=09=09=09=09CRSSnapshotAction=20snapshot_action,=0A-=09=09=09=09= =09=09=09=09=09=09XLogRecPtr=20*lsn);=0A+=09=09=09=09=09=09=09=09=09=09=09= const=20char=20*slotname,=0A+=09=09=09=09=09=09=09=09=09=09=09bool=20= temporary,=0A+=09=09=09=09=09=09=09=09=09=09=09bool=20two_phase,=0A+=09=09= =09=09=09=09=09=09=09=09=09bool=20failover,=0A+=09=09=09=09=09=09=09=09=09= =09=09bool=20unrestricted,=0A+=09=09=09=09=09=09=09=09=09=09=09List=20= *publications,=0A+=09=09=09=09=09=09=09=09=09=09=09CRSSnapshotAction=20= snapshot_action,=0A+=09=09=09=09=09=09=09=09=09=09=09XLogRecPtr=20*lsn);=0A= =20=0A=20/*=0A=20=20*=20walrcv_alter_slot_fn=0A@@=20-460,8=20+462,8=20@@=20= extern=20PGDLLIMPORT=20WalReceiverFunctionsType=20*WalReceiverFunctions;=0A= =20=09WalReceiverFunctions->walrcv_receive(conn,=20buffer,=20wait_fd)=0A=20= #define=20walrcv_send(conn,=20buffer,=20nbytes)=20\=0A=20=09= WalReceiverFunctions->walrcv_send(conn,=20buffer,=20nbytes)=0A-#define=20= walrcv_create_slot(conn,=20slotname,=20temporary,=20two_phase,=20= failover,=20snapshot_action,=20lsn)=20\=0A-=09= WalReceiverFunctions->walrcv_create_slot(conn,=20slotname,=20temporary,=20= two_phase,=20failover,=20snapshot_action,=20lsn)=0A+#define=20= walrcv_create_slot(conn,=20slotname,=20temporary,=20two_phase,=20= failover,=20unrestricted,=20publications,=20snapshot_action,=20lsn)=20\=0A= +=09WalReceiverFunctions->walrcv_create_slot(conn,=20slotname,=20= temporary,=20two_phase,=20failover,=20unrestricted,=20publications,=20= snapshot_action,=20lsn)=0A=20#define=20walrcv_alter_slot(conn,=20= slotname,=20failover,=20two_phase)=20\=0A=20=09= WalReceiverFunctions->walrcv_alter_slot(conn,=20slotname,=20failover,=20= two_phase)=0A=20#define=20walrcv_get_backend_pid(conn)=20\=0Adiff=20= --git=20a/src/include/utils/guc_hooks.h=20= b/src/include/utils/guc_hooks.h=0Aindex=206a76f8d5ed6..8d26a0e7e51=20= 100644=0A---=20a/src/include/utils/guc_hooks.h=0A+++=20= b/src/include/utils/guc_hooks.h=0A@@=20-62,6=20+62,7=20@@=20extern=20= void=20assign_default_text_search_config(const=20char=20*newval,=20void=20= *extra);=0A=20extern=20bool=20check_default_with_oids(bool=20*newval,=20= void=20**extra,=0A=20=09=09=09=09=09=09=09=09=09GucSource=20source);=0A=20= extern=20const=20char=20*show_effective_wal_level(void);=0A+extern=20= const=20char=20*show_restricted_wal_level(void);=0A=20extern=20bool=20= check_huge_page_size(int=20*newval,=20void=20**extra,=20GucSource=20= source);=0A=20extern=20void=20assign_io_method(int=20newval,=20void=20= *extra);=0A=20extern=20bool=20check_io_max_concurrency(int=20*newval,=20= void=20**extra,=20GucSource=20source);=0Adiff=20--git=20= a/src/include/utils/rel.h=20b/src/include/utils/rel.h=0Aindex=20= 41ab4586c6b..1834ef448fd=20100644=0A---=20a/src/include/utils/rel.h=0A= +++=20b/src/include/utils/rel.h=0A@@=20-709,8=20+709,9=20@@=20= RelationCloseSmgr(Relation=20relation)=0A=20=0A=20/*=0A=20=20*=20= RelationIsLogicallyLogged=0A-=20*=09=09True=20if=20we=20need=20to=20log=20= enough=20information=20to=20extract=20the=20data=20from=20the=0A-=20*=09=09= WAL=20stream.=0A+=20*=20=09=09True=20if=20the=20relation=20is=20eligible=20= for=20logical=20logging.=20=20This=20does=20not=0A+=20*=20=09=09consider=20= whether=20logical=20WAL=20is=20active=20or=20whether=20any=20replication=20= slot=0A+=20*=20=09=09requires=20changes=20for=20this=20particular=20= relation.=0A=20=20*=0A=20=20*=20We=20don't=20log=20information=20for=20= unlogged=20tables=20(since=20they=20don't=20WAL=20log=0A=20=20*=20= anyway),=20for=20foreign=20tables=20(since=20they=20don't=20WAL=20log,=20= either),=0A@@=20-719,12=20+720,24=20@@=20RelationCloseSmgr(Relation=20= relation)=0A=20=20*=20log=20information=20for=20user=20defined=20catalog=20= tables=20since=20they=20presumably=20are=0A=20=20*=20interesting=20to=20= the=20user...=0A=20=20*/=0A-#define=20= RelationIsLogicallyLogged(relation)=20\=0A-=09(XLogLogicalInfoActive()=20= &&=20\=0A-=09=20RelationNeedsWAL(relation)=20&&=20\=0A-=09=20= (relation)->rd_rel->relkind=20!=3D=20RELKIND_FOREIGN_TABLE=20&&=09\=0A= +#define=20RelationCanBeLogicallyLogged(relation)=20\=0A+=09= (RelationNeedsWAL(relation)=20&&=20\=0A+=09=20= (relation)->rd_rel->relkind=20!=3D=20RELKIND_FOREIGN_TABLE=20&&=20\=0A=20= =09=20!IsCatalogRelation(relation))=0A=20=0A+/*=0A+=20*=20= RelationIsLogicallyLogged=0A+=20*=09=09True=20if=20we=20need=20to=20log=20= enough=20information=20to=20extract=20the=20data=20from=20the=0A+=20*=09=09= WAL=20stream.=0A+=20*=0A+=20*=20This=20has=20included=20the=20check=20= for=20RelationCanBeLogicallyLogged(),=20and=20also=0A+=20*=20checks=20= whether=20logical=20WAL=20is=20required=20for=20this=20relation.=0A+=20= */=0A+#define=20RelationIsLogicallyLogged(relation)=20\=0A+=09= RelationNeedsLogicalTupleWAL(relation)=0A+=0A+extern=20bool=20= RelationNeedsLogicalTupleWAL(Relation=20relation);=0A+=0A=20/*=20= routines=20in=20utils/cache/relcache.c=20*/=0A=20extern=20void=20= RelationIncrementReferenceCount(Relation=20rel);=0A=20extern=20void=20= RelationDecrementReferenceCount(Relation=20rel);=0Adiff=20--git=20= a/src/test/recovery/meson.build=20b/src/test/recovery/meson.build=0A= index=2039ec8c4946d..5162561bd27=20100644=0A---=20= a/src/test/recovery/meson.build=0A+++=20b/src/test/recovery/meson.build=0A= @@=20-64,6=20+64,7=20@@=20tests=20+=3D=20{=0A=20=20=20=20=20=20=20= 't/053_standby_login_event_trigger.pl',=0A=20=20=20=20=20=20=20= 't/054_unlogged_sequence_promotion.pl',=0A=20=20=20=20=20=20=20= 't/055_cascade_reconnect.pl',=0A+=20=20=20=20=20=20= 't/056_logical_slot_scope.pl',=0A=20=20=20=20=20],=0A=20=20=20},=0A=20}=0A= diff=20--git=20a/src/test/recovery/t/040_standby_failover_slots_sync.pl=20= b/src/test/recovery/t/040_standby_failover_slots_sync.pl=0Aindex=20= f8922aaa1a2..90afd993b40=20100644=0A---=20= a/src/test/recovery/t/040_standby_failover_slots_sync.pl=0A+++=20= b/src/test/recovery/t/040_standby_failover_slots_sync.pl=0A@@=20-210,6=20= +210,153=20@@=20is(=20$standby1->safe_psql(=0A=20=09"t",=0A=20=09= 'synchronized=20slot=20has=20got=20its=20own=20inactive_since');=0A=20=0A= +##################################################=0A+#=20Test=20= synchronization=20and=20replacement=20of=20a=20restricted=20failover=20= slot.=0A+##################################################=0A+=0A= +$primary->safe_psql(=0A+=09'postgres',=20q{=0A+=09CREATE=20TABLE=20= restricted_sync_tab1=20(a=20int);=0A+=09CREATE=20TABLE=20= restricted_sync_tab2=20(a=20int);=0A+=09CREATE=20PUBLICATION=20= restricted_sync_pub1=20FOR=20TABLE=20restricted_sync_tab1;=0A+=09CREATE=20= PUBLICATION=20restricted_sync_pub2=20FOR=20TABLE=20restricted_sync_tab2;=0A= +});=0A+$primary->safe_psql(=0A+=09'postgres',=20q{=0A+=09SELECT=20= pg_create_logical_replication_slot(=0A+=09=09'restricted_sync_slot',=20= 'pgoutput',=20failover=20=3D>=20true,=0A+=09=09publications=20=3D>=20= ARRAY['restricted_sync_pub1']);=0A+});=0A+=0A+my=20= $restricted_incarnation1=20=3D=20$primary->safe_psql(=0A+=09'postgres',=20= q{=0A+=09SELECT=20restricted_scope_incarnation=0A+=09FROM=20= pg_replication_slots=0A+=09WHERE=20slot_name=20=3D=20= 'restricted_sync_slot';=0A+});=0A+my=20$restricted_ready_lsn1=20=3D=20= $primary->safe_psql(=0A+=09'postgres',=20q{=0A+=09SELECT=20= restricted_scope_ready_lsn=0A+=09FROM=20pg_replication_slots=0A+=09WHERE=20= slot_name=20=3D=20'restricted_sync_slot';=0A+});=0A+=0A= +$primary->wait_for_replay_catchup($standby1);=0A= +$standby1->safe_psql('postgres',=20"SELECT=20= pg_sync_replication_slots();");=0A+=0A+#=20A=20ready=20restricted=20= failover=20slot=20should=20retain=20its=20scope=20metadata=20on=20= standby.=0A+is(=20$standby1->safe_psql(=0A+=09=09'postgres',=20qq{=0A+=09= =09SELECT=20synced=20AND=20NOT=20temporary=20AND=20NOT=20unrestricted=20= AND=0A+=09=09=20=20=20=20=20=20=20restricted_scope_ready=20AND=0A+=09=09=20= =20=20=20=20=20=20restricted_scope_incarnation=20=3D=20= $restricted_incarnation1=20AND=0A+=09=09=20=20=20=20=20=20=20= restricted_scope_ready_lsn=20=3D=20'$restricted_ready_lsn1'=20AND=0A+=09=09= =20=20=20=20=20=20=20publication_oids=20=3D=20ARRAY[(SELECT=20oid=20FROM=20= pg_publication=0A+=09=09=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20WHERE=20= pubname=20=3D=20'restricted_sync_pub1')]=0A+=09=09FROM=20= pg_replication_slots=0A+=09=09WHERE=20slot_name=20=3D=20= 'restricted_sync_slot';=0A+=09}),=0A+=09"t",=0A+=09'restricted=20= failover=20slot=20scope=20metadata=20is=20synchronized');=0A+=0A+#=20= Replayed=20mappings=20should=20enable=20selective=20WAL=20for=20the=20= restricted=20relation.=0A+is(=20$standby1->safe_psql(=0A+=09=09= 'postgres',=20q{=0A+=09=09SELECT=20c.relhasrestrictedslots=20AND=20= EXISTS=20(=0A+=09=09=09SELECT=201=20FROM=20pg_restricted_slot_relation=20= AS=20m=0A+=09=09=09WHERE=20m.rsrslotname=20=3D=20'restricted_sync_slot'=0A= +=09=09=09=20=20AND=20m.rsrrelid=20=3D=20c.oid)=0A+=09=09FROM=20pg_class=20= AS=20c=0A+=09=09WHERE=20c.oid=20=3D=20'restricted_sync_tab1'::regclass;=0A= +=09}),=0A+=09"t",=0A+=09'restricted=20failover=20slot=20relation=20= mapping=20is=20available=20on=20standby');=0A+=0A= +$primary->safe_psql('postgres',=0A+=09"SELECT=20= pg_drop_replication_slot('restricted_sync_slot');");=0A= +$primary->safe_psql(=0A+=09'postgres',=20q{=0A+=09SELECT=20= pg_create_logical_replication_slot(=0A+=09=09'restricted_sync_slot',=20= 'pgoutput',=20failover=20=3D>=20true,=0A+=09=09publications=20=3D>=20= ARRAY['restricted_sync_pub2']);=0A+});=0A+=0A+my=20= $restricted_incarnation2=20=3D=20$primary->safe_psql(=0A+=09'postgres',=20= q{=0A+=09SELECT=20restricted_scope_incarnation=0A+=09FROM=20= pg_replication_slots=0A+=09WHERE=20slot_name=20=3D=20= 'restricted_sync_slot';=0A+});=0A+my=20$restricted_ready_lsn2=20=3D=20= $primary->safe_psql(=0A+=09'postgres',=20q{=0A+=09SELECT=20= restricted_scope_ready_lsn=0A+=09FROM=20pg_replication_slots=0A+=09WHERE=20= slot_name=20=3D=20'restricted_sync_slot';=0A+});=0A+=0A= +$primary->wait_for_replay_catchup($standby1);=0A= +$standby1->safe_psql('postgres',=20"SELECT=20= pg_sync_replication_slots();");=0A+=0A+#=20A=20changed=20incarnation=20= should=20replace=20the=20existing=20synchronized=20slot.=0A+is(=20= $standby1->safe_psql(=0A+=09=09'postgres',=20qq{=0A+=09=09SELECT=20= restricted_scope_incarnation=20=3D=20$restricted_incarnation2=20AND=0A+=09= =09=20=20=20=20=20=20=20restricted_scope_incarnation=20<>=20= $restricted_incarnation1=20AND=0A+=09=09=20=20=20=20=20=20=20= restricted_scope_ready_lsn=20=3D=20'$restricted_ready_lsn2'=20AND=0A+=09=09= =20=20=20=20=20=20=20publication_oids=20=3D=20ARRAY[(SELECT=20oid=20FROM=20= pg_publication=0A+=09=09=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20WHERE=20= pubname=20=3D=20'restricted_sync_pub2')]=0A+=09=09FROM=20= pg_replication_slots=0A+=09=09WHERE=20slot_name=20=3D=20= 'restricted_sync_slot';=0A+=09}),=0A+=09"t",=0A+=09'restricted=20= synchronized=20slot=20is=20replaced=20after=20incarnation=20changes');=0A= +=0A+#=20Replacement=20should=20install=20mappings=20only=20for=20the=20= new=20incarnation's=20scope.=0A+is(=20$standby1->safe_psql(=0A+=09=09= 'postgres',=20qq{=0A+=09=09SELECT=20newrel.relhasrestrictedslots=20AND=0A= +=09=09=20=20=20=20=20=20=20NOT=20EXISTS=20(=0A+=09=09=09=20=20=20SELECT=20= 1=20FROM=20pg_restricted_slot_relation=0A+=09=09=09=20=20=20WHERE=20= rsrslotname=20=3D=20'restricted_sync_slot'=0A+=09=09=09=20=20=20=20=20= AND=20rsrrelid=20=3D=20oldrel.oid=0A+=09=09=09=20=20=20=20=20AND=20= rsrincarnation=20=3D=20$restricted_incarnation2)=20AND=0A+=09=09=20=20=20= =20=20=20=20EXISTS=20(=0A+=09=09=09=20=20=20SELECT=201=20FROM=20= pg_restricted_slot_relation=0A+=09=09=09=20=20=20WHERE=20rsrslotname=20=3D= =20'restricted_sync_slot'=0A+=09=09=09=20=20=20=20=20AND=20rsrrelid=20=3D=20= newrel.oid=0A+=09=09=09=20=20=20=20=20AND=20rsrincarnation=20=3D=20= $restricted_incarnation2)=0A+=09=09FROM=20pg_class=20AS=20oldrel,=20= pg_class=20AS=20newrel=0A+=09=09WHERE=20oldrel.oid=20=3D=20= 'restricted_sync_tab1'::regclass=0A+=09=09=20=20AND=20newrel.oid=20=3D=20= 'restricted_sync_tab2'::regclass;=0A+=09}),=0A+=09"t",=0A+=09'restricted=20= synchronized=20slot=20replacement=20installs=20its=20new=20mappings');=0A= +=0A+$primary->safe_psql('postgres',=0A+=09"SELECT=20= pg_drop_replication_slot('restricted_sync_slot');");=0A= +$primary->wait_for_replay_catchup($standby1);=0A= +$standby1->safe_psql('postgres',=20"SELECT=20= pg_sync_replication_slots();");=0A+=0A+#=20Dropping=20the=20remote=20= restricted=20slot=20should=20remove=20its=20synchronized=20copy.=0A+is(=20= $standby1->safe_psql(=0A+=09=09'postgres',=20q{=0A+=09=09SELECT=20= count(*)=20=3D=200=20FROM=20pg_replication_slots=0A+=09=09WHERE=20= slot_name=20=3D=20'restricted_sync_slot';=0A+=09}),=0A+=09"t",=0A+=09= 'dropped=20restricted=20failover=20slot=20is=20removed=20from=20= standby');=0A+=0A+#=20Temporary=20restricted-slot=20relations=20should=20= not=20remain=20in=20the=20FOR=20ALL=20TABLES=0A+#=20publication=20used=20= by=20later=20subscription=20tests.=0A+$primary->safe_psql(=0A+=09= 'postgres',=20q{=0A+=09DROP=20PUBLICATION=20restricted_sync_pub1;=0A+=09= DROP=20PUBLICATION=20restricted_sync_pub2;=0A+=09DROP=20TABLE=20= restricted_sync_tab1;=0A+=09DROP=20TABLE=20restricted_sync_tab2;=0A+});=0A= +$primary->wait_for_replay_catchup($standby1);=0A+=0A=20= ##################################################=0A=20#=20Test=20that=20= the=20synchronized=20slot=20will=20be=20dropped=20if=20the=20= corresponding=20remote=0A=20#=20slot=20on=20the=20primary=20server=20has=20= been=20dropped.=0Adiff=20--git=20= a/src/test/recovery/t/056_logical_slot_scope.pl=20= b/src/test/recovery/t/056_logical_slot_scope.pl=0Anew=20file=20mode=20= 100644=0Aindex=2000000000000..8583df9cbdb=0A---=20/dev/null=0A+++=20= b/src/test/recovery/t/056_logical_slot_scope.pl=0A@@=20-0,0=20+1,647=20= @@=0A+#=20Copyright=20(c)=202026,=20PostgreSQL=20Global=20Development=20= Group=0A+=0A+use=20strict;=0A+use=20warnings=20FATAL=20=3D>=20'all';=0A= +use=20PostgreSQL::Test::Cluster;=0A+use=20PostgreSQL::Test::Utils;=0A= +use=20Test::More;=0A+=0A+my=20$node=20=3D=20= PostgreSQL::Test::Cluster->new('node');=0A+my=20($ret,=20$stdout,=20= $stderr);=0A+$node->init(allows_streaming=20=3D>=20'logical');=0A= +$node->append_conf('postgresql.conf',=20'wal_level=20=3D=20replica');=0A= +$node->append_conf('postgresql.conf',=20'max_prepared_transactions=20=3D=20= 10');=0A+$node->start;=0A+=0A+$node->safe_psql('postgres',=20q{=0A+=09= CREATE=20TABLE=20orders=20(id=20integer=20PRIMARY=20KEY,=20payload=20= text);=0A+=09CREATE=20TABLE=20customers=20(id=20integer=20PRIMARY=20KEY,=20= payload=20text);=0A+=09CREATE=20PUBLICATION=20orders_pub=20FOR=20TABLE=20= orders;=0A+=09CREATE=20PUBLICATION=20customers_pub=20FOR=20TABLE=20= customers;=0A+});=0A+=0A+#=20Specifying=20publications=20should=20create=20= a=20restricted=20logical=20slot.=0A+like($node->safe_psql('postgres',=20= q{=0A+=09SELECT=20slot_name=20FROM=20pg_create_logical_replication_slot(=0A= +=09=09'orders_slot',=20'pgoutput',=20publications=20=3D>=20= ARRAY['orders_pub']);=0A+}),=20qr/orders_slot/,=20'a=20publication=20= list=20creates=20a=20restricted=20slot');=0A+=0A+#=20= pg_replication_slots=20should=20expose=20the=20durable=20= restricted-scope=20state.=0A+is($node->safe_psql('postgres',=20q{=0A+=09= SELECT=20NOT=20unrestricted=20AND=20restricted_scope_ready=20AND=0A+=09=20= =20=20=20=20=20=20(SELECT=20oid=20FROM=20pg_publication=20WHERE=20= pubname=20=3D=20'orders_pub')=20=3D=0A+=09=20=20=20=20=20=20=20ANY=20= (publication_oids)=20AND=20restricted_scope_incarnation=20<>=200=20AND=0A= +=09=20=20=20=20=20=20=20restricted_scope_ready_lsn=20IS=20NOT=20NULL=0A= +=09FROM=20pg_replication_slots=20WHERE=20slot_name=20=3D=20= 'orders_slot';=0A+}),=20't',=20'restricted=20slot=20properties=20are=20= exposed');=0A+=0A+#=20Slot=20creation=20should=20install=20the=20initial=20= relation=20mapping=20and=20flag.=0A+is($node->safe_psql('postgres',=20q{=0A= +=09SELECT=20relhasrestrictedslots=20AND=20EXISTS=20(=0A+=09=09SELECT=20= 1=20FROM=20pg_restricted_slot_relation=0A+=09=09WHERE=20rsrslotname=20=3D=20= 'orders_slot'=20AND=20rsrrelid=20=3D=20'orders'::regclass)=0A+=09FROM=20= pg_class=20WHERE=20oid=20=3D=20'orders'::regclass;=0A+}),=20't',=20= 'initial=20publication=20membership=20is=20installed');=0A+=0A+#=20A=20= restricted=20slot=20should=20enable=20selective,=20rather=20than=20full,=20= WAL.=0A+is($node->safe_psql('postgres',=20q{=0A+=09SELECT=20= current_setting('effective_wal_level'),=0A+=09=20=20=20=20=20=20=20= current_setting('restricted_wal_level');=0A+}),=20"replica|logical",=20= 'restricted=20slots=20enable=20only=20restricted=20logical=20WAL');=0A+=0A= +$node->stop('immediate');=0A+$node->start;=0A+#=20Scope=20readiness=20= and=20selective=20WAL=20should=20survive=20an=20immediate=20restart.=0A= +is($node->safe_psql('postgres',=20q{=0A+=09SELECT=20= restricted_scope_ready=20AND=0A+=09=20=20=20=20=20=20=20= current_setting('restricted_wal_level')=20=3D=20'logical'=0A+=09FROM=20= pg_replication_slots=20WHERE=20slot_name=20=3D=20'orders_slot';=0A+}),=20= 't',=20'scope=20readiness=20is=20durable=20across=20an=20immediate=20= restart');=0A+=0A+#=20NULL=20original=20arguments=20should=20preserve=20= the=20function's=20former=20strictness.=0A= +is($node->safe_psql('postgres',=20q{=0A+=09SELECT=20= pg_create_logical_replication_slot(NULL,=20'pgoutput')=20IS=20NULL=20AND=0A= +=09=20=20=20=20=20=20=20= pg_create_logical_replication_slot('null_plugin',=20NULL)=20IS=20NULL=20= AND=0A+=09=20=20=20=20=20=20=20pg_create_logical_replication_slot(=0A+=09= =09=20=20=20'null_temporary',=20'pgoutput',=20NULL)=20IS=20NULL=20AND=0A= +=09=20=20=20=20=20=20=20pg_create_logical_replication_slot(=0A+=09=09=20= =20=20'null_twophase',=20'pgoutput',=20false,=20NULL)=20IS=20NULL=20AND=0A= +=09=20=20=20=20=20=20=20pg_create_logical_replication_slot(=0A+=09=09=20= =20=20'null_failover',=20'pgoutput',=20false,=20false,=20NULL)=20IS=20= NULL;=0A+}),=20't',=20'NULL=20original=20arguments=20retain=20strict=20= behavior');=0A+=0A+($ret,=20$stdout,=20$stderr)=20=3D=20= $node->psql('postgres',=20q{=0A+=09SELECT=20= pg_create_logical_replication_slot(=0A+=09=09'bad_plugin',=20= 'test_decoding',=20publications=20=3D>=20ARRAY['orders_pub']);=0A+});=0A= +#=20Restricted=20slots=20should=20reject=20output=20plugins=20other=20= than=20pgoutput.=0A+isnt($ret,=200,=20'a=20restricted=20slot=20requires=20= pgoutput');=0A+#=20The=20plugin=20restriction=20should=20produce=20the=20= expected=20error=20message.=0A+like($stderr,=20qr/restricted=20logical=20= replication=20slots=20currently=20require=20pgoutput/,=0A+=09'wrong=20= output=20plugin=20reports=20the=20restriction');=0A+=0A+($ret,=20= $stdout,=20$stderr)=20=3D=20$node->psql('postgres',=20q{=0A+=09BEGIN;=0A= +=09SELECT=20pg_create_logical_replication_slot(=0A+=09=09= 'in_xact_slot',=20'pgoutput',=20publications=20=3D>=20= ARRAY['orders_pub']);=0A+});=0A+#=20Restricted=20slot=20creation=20= should=20be=20rejected=20in=20an=20explicit=20transaction.=0A+isnt($ret,=20= 0,=20'restricted=20slot=20creation=20is=20rejected=20in=20a=20= transaction=20block');=0A+#=20Explicit-transaction=20rejection=20should=20= report=20the=20expected=20error.=0A+like($stderr,=20qr/restricted=20= logical=20replication=20slots=20cannot=20be=20created=20inside=20a=20= transaction=20block/,=0A+=09'explicit=20transaction=20reports=20the=20= creation=20restriction');=0A+=0A+($ret,=20$stdout,=20$stderr)=20=3D=20= $node->psql('postgres',=20q{=0A+=09BEGIN;=0A+=09SAVEPOINT=20s;=0A+=09= SELECT=20pg_create_logical_replication_slot(=0A+=09=09'in_subxact_slot',=20= 'pgoutput',=20publications=20=3D>=20ARRAY['orders_pub']);=0A+});=0A+#=20= Restricted=20slot=20creation=20should=20also=20be=20rejected=20in=20a=20= subtransaction.=0A+isnt($ret,=200,=20'restricted=20slot=20creation=20is=20= rejected=20in=20a=20subtransaction');=0A+#=20Subtransaction=20rejection=20= should=20report=20the=20expected=20error.=0A+like($stderr,=20= qr/restricted=20logical=20replication=20slots=20cannot=20be=20created=20= inside=20a=20transaction=20block/,=0A+=09'subtransaction=20reports=20the=20= creation=20restriction');=0A+=0A+($ret,=20$stdout,=20$stderr)=20=3D=20= $node->psql('postgres',=20q{=0A+=09BEGIN;=0A+=09SELECT=20= pg_copy_logical_replication_slot('orders_slot',=20'copy_in_xact');=0A= +});=0A+#=20Copying=20a=20restricted=20slot=20should=20be=20rejected=20= in=20an=20explicit=20transaction.=0A+isnt($ret,=200,=20'restricted=20= slot=20copying=20is=20rejected=20in=20a=20transaction=20block');=0A+#=20= Restricted-slot=20copy=20rejection=20should=20report=20the=20expected=20= error.=0A+like($stderr,=20qr/restricted=20logical=20replication=20slots=20= cannot=20be=20copied=20inside=20a=20transaction=20block/,=0A+=09= 'explicit=20transaction=20reports=20the=20copy=20restriction');=0A+=0A= +($ret,=20$stdout,=20$stderr)=20=3D=20$node->psql('postgres',=20q{=0A+=09= SELECT=20count(*)=20FROM=20pg_logical_slot_peek_binary_changes(=0A+=09=09= 'orders_slot',=20NULL,=20NULL,=0A+=09=09'proto_version',=20'1',=20= 'publication_names',=20'customers_pub');=0A+});=0A+#=20Decoding=20should=20= reject=20a=20publication=20absent=20from=20the=20fixed=20set.=0A= +isnt($ret,=200,=20'pgoutput=20rejects=20an=20unstored=20publication');=0A= +#=20Publication-set=20validation=20should=20produce=20the=20expected=20= error.=0A+like($stderr,=20qr/requested=20publications=20are=20not=20= contained=20in=20replication=20slot/,=0A+=09'pgoutput=20reports=20= publication-set=20containment=20failure');=0A+=0A+($ret,=20$stdout,=20= $stderr)=20=3D=20$node->psql('postgres',=20q{=0A+=09SELECT=20= pg_create_logical_replication_slot(=0A+=09=09'null_publication_slot',=20= 'pgoutput',=0A+=09=09publications=20=3D>=20ARRAY['orders_pub',=20NULL]);=0A= +});=0A+#=20A=20NULL=20publication=20name=20should=20be=20rejected=20as=20= an=20invalid=20parameter.=0A+isnt($ret,=200,=20'NULL=20publication=20= array=20element=20is=20rejected');=0A+like($stderr,=20qr/publications=20= must=20not=20contain=20null=20values/,=0A+=09'NULL=20publication=20array=20= element=20reports=20the=20expected=20error');=0A+=0A= +$node->safe_psql('postgres',=20q{=0A+=09CREATE=20SUBSCRIPTION=20= restricted_test_sub=0A+=09CONNECTION=20'dbname=3Ddoesnotexist'=20= PUBLICATION=20orders_pub,=20customers_pub=0A+=09WITH=20(connect=20=3D=20= false,=20slot_name=20=3D=20NONE,=20unrestricted_slot=20=3D=20false);=0A= +});=0A+($ret,=20$stdout,=20$stderr)=20=3D=20$node->psql('postgres',=20= q{=0A+=09ALTER=20SUBSCRIPTION=20restricted_test_sub=0A+=09SET=20= PUBLICATION=20outside_pub=20WITH=20(refresh=20=3D=20false);=0A+});=0A+#=20= A=20restricted=20subscription=20should=20reject=20publication=20identity=20= expansion.=0A+isnt($ret,=200,=20'restricted=20subscription=20publication=20= expansion=20is=20rejected');=0A+like($stderr,=0A+=09qr/cannot=20add=20= publication=20"outside_pub"=20to=20restricted=20subscription=20= "restricted_test_sub"/,=0A+=09'restricted=20subscription=20expansion=20= reports=20the=20expected=20error');=0A+$node->safe_psql('postgres',=20q{=0A= +=09ALTER=20SUBSCRIPTION=20restricted_test_sub=0A+=09DROP=20PUBLICATION=20= customers_pub=20WITH=20(refresh=20=3D=20false);=0A+});=0A+($ret,=20= $stdout,=20$stderr)=20=3D=20$node->psql('postgres',=20q{=0A+=09ALTER=20= SUBSCRIPTION=20restricted_test_sub=0A+=09ADD=20PUBLICATION=20= customers_pub=20WITH=20(refresh=20=3D=20false);=0A+});=0A+#=20A=20= removed=20publication=20should=20not=20be=20added=20back=20to=20a=20= restricted=20subscription.=0A+isnt($ret,=200,=20'restricted=20= subscription=20ADD=20PUBLICATION=20is=20rejected');=0A+like($stderr,=0A+=09= qr/cannot=20add=20publications=20to=20restricted=20subscription=20= "restricted_test_sub"/,=0A+=09'restricted=20subscription=20ADD=20= PUBLICATION=20reports=20the=20expected=20error');=0A= +$node->safe_psql('postgres',=20'DROP=20SUBSCRIPTION=20= restricted_test_sub');=0A+=0A+$node->safe_psql('postgres',=20'CREATE=20= TABLE=20lock_target=20(id=20integer)');=0A+my=20$writer=20=3D=20= $node->background_psql('postgres');=0A+$writer->query_safe('BEGIN');=0A= +$writer->query_safe('INSERT=20INTO=20lock_target=20VALUES=20(1)');=0A= +my=20$expander=20=3D=20$node->background_psql('postgres');=0A= +$expander->query_until(qr/alter-start/,=20q{=0A+=09\echo=20alter-start=0A= +=09ALTER=20PUBLICATION=20orders_pub=20ADD=20TABLE=20lock_target;=0A+=09= \echo=20alter-done=0A+});=0A+$node->poll_query_until('postgres',=20q{=0A= +=09SELECT=20count(*)=20>=200=20FROM=20pg_stat_activity=0A+=09WHERE=20= query=20LIKE=20'ALTER=20PUBLICATION=20orders_pub=20ADD=20TABLE=20= lock_target%'=0A+=09=20=20AND=20wait_event_type=20=3D=20'Lock';=0A+},=20= 't');=0A+#=20Publication=20expansion=20should=20wait=20for=20an=20= existing=20relation=20writer.=0A+pass('publication=20expansion=20waits=20= for=20concurrent=20relation=20writers');=0A= +$writer->query_safe('COMMIT');=0A+$writer->quit;=0A+$expander->quit;=0A= +#=20The=20relation=20flag=20should=20be=20installed=20after=20the=20= writer=20releases=20its=20lock.=0A+is($node->safe_psql('postgres',=20q{=0A= +=09SELECT=20relhasrestrictedslots=20FROM=20pg_class=0A+=09WHERE=20oid=20= =3D=20'lock_target'::regclass;=0A+}),=20't',=20'relation=20flag=20is=20= visible=20after=20the=20writer=20interlock=20is=20released');=0A+=0A= +$node->safe_psql('postgres',=20'ALTER=20PUBLICATION=20orders_pub=20ADD=20= TABLE=20customers');=0A+#=20Publication=20expansion=20should=20install=20= mapping=20state=20synchronously.=0A+is($node->safe_psql('postgres',=20q{=0A= +=09SELECT=20relhasrestrictedslots=20AND=20EXISTS=20(=0A+=09=09SELECT=20= 1=20FROM=20pg_restricted_slot_relation=0A+=09=09WHERE=20rsrslotname=20=3D=20= 'orders_slot'=20AND=20rsrrelid=20=3D=20'customers'::regclass)=0A+=09FROM=20= pg_class=20WHERE=20oid=20=3D=20'customers'::regclass;=0A+}),=20't',=20= 'publication=20expansion=20synchronously=20installs=20membership');=0A+=0A= +$node->safe_psql('postgres',=20q{=0A+=09CREATE=20TABLE=20= unused_scope_table=20(id=20integer);=0A+=09CREATE=20PUBLICATION=20= unused_scope_pub;=0A+});=0A+my=20$unused_scope_writer=20=3D=20= $node->background_psql('postgres');=0A= +$unused_scope_writer->query_safe('BEGIN');=0A= +$unused_scope_writer->query_safe(=0A+=09'INSERT=20INTO=20= unused_scope_table=20VALUES=20(1)');=0A+#=20An=20unmatched=20publication=20= should=20not=20take=20a=20stronger=20table=20lock.=0A= +is($node->safe_psql('postgres',=20q{=0A+=09SET=20statement_timeout=20=3D=20= '1s';=0A+=09ALTER=20PUBLICATION=20unused_scope_pub=20ADD=20TABLE=20= unused_scope_table;=0A+=09SELECT=201;=0A+}),=20'1',=20'unmatched=20= publication=20expansion=20avoids=20closure=20locking');=0A= +$unused_scope_writer->query_safe('ROLLBACK');=0A= +$unused_scope_writer->quit;=0A+=0A+$node->safe_psql('postgres',=20q{=0A= +=09CREATE=20TABLE=20initializing_member=20(id=20integer);=0A+=09CREATE=20= TABLE=20concurrent_member=20(id=20integer);=0A+=09CREATE=20PUBLICATION=20= initializing_pub=20FOR=20TABLE=20initializing_member;=0A+});=0A= +$node->safe_psql('postgres',=20q{=0A+=09SELECT=20= pg_create_logical_replication_slot(=0A+=09=09'initializing_source',=20= 'pgoutput',=0A+=09=09publications=20=3D>=20ARRAY['initializing_pub']);=0A= +});=0A+my=20$initialization_expander=20=3D=20= $node->background_psql('postgres');=0A= +$initialization_expander->query_safe('BEGIN');=0A= +$initialization_expander->query_safe(=0A+=09'ALTER=20PUBLICATION=20= initializing_pub=20ADD=20TABLE=20concurrent_member');=0A+($ret,=20= $stdout,=20$stderr)=20=3D=20$node->psql('postgres',=20q{=0A+=09SELECT=20= pg_copy_logical_replication_slot(=0A+=09=09'initializing_source',=20= 'initializing_slot');=0A+});=0A+#=20Copy=20initialization=20should=20= fail=20instead=20of=20using=20a=20stale=20publication=20snapshot.=0A= +isnt($ret,=200,=20'concurrent=20publication=20expansion=20rejects=20= slot=20copying');=0A+like($stderr,=0A+=09qr/could=20not=20initialize=20= restricted=20logical=20replication=20slot=20due=20to=20concurrent=20= activity/,=0A+=09'concurrent=20publication=20expansion=20reports=20the=20= expected=20error');=0A+=0A= +$initialization_expander->query_safe('COMMIT');=0A= +$initialization_expander->quit;=0A+$node->safe_psql('postgres',=20q{=0A= +=09SELECT=20pg_copy_logical_replication_slot(=0A+=09=09= 'initializing_source',=20'initializing_slot');=0A+});=0A+=0A+#=20Slot=20= initialization=20should=20finish=20with=20the=20concurrent=20relation=20= mapped.=0A+is($node->safe_psql('postgres',=20q{=0A+=09SELECT=20= s.restricted_scope_ready=20AND=20c.relhasrestrictedslots=20AND=20EXISTS=20= (=0A+=09=09SELECT=201=20FROM=20pg_restricted_slot_relation=0A+=09=09= WHERE=20rsrslotname=20=3D=20'initializing_slot'=0A+=09=09=20=20AND=20= rsrrelid=20=3D=20c.oid)=0A+=09FROM=20pg_replication_slots=20AS=20s,=20= pg_class=20AS=20c=0A+=09WHERE=20s.slot_name=20=3D=20'initializing_slot'=0A= +=09=20=20AND=20c.oid=20=3D=20'concurrent_member'::regclass;=0A+}),=20= 't',=20'initial=20scope=20retains=20a=20concurrent=20publication=20= expansion');=0A+$node->safe_psql('postgres',=20q{=0A+=09SELECT=20= pg_drop_replication_slot('initializing_slot');=0A+=09SELECT=20= pg_drop_replication_slot('initializing_source');=0A+});=0A+=0A= +$node->safe_psql('postgres',=20q{=0A+=09CREATE=20TABLE=20= lock_order_root=20(id=20integer)=20PARTITION=20BY=20RANGE=20(id);=0A+=09= CREATE=20TABLE=20lock_order_child=20(LIKE=20lock_order_root);=0A+=09= CREATE=20PUBLICATION=20lock_order_pub=20FOR=20TABLE=20lock_order_root;=0A= +});=0A+$node->safe_psql('postgres',=20q{=0A+=09SELECT=20= pg_create_logical_replication_slot(=0A+=09=09'lock_order_source',=20= 'pgoutput',=0A+=09=09publications=20=3D>=20ARRAY['lock_order_pub']);=0A= +});=0A+$node->safe_psql('postgres',=20q{=0A+=09SELECT=20= pg_create_logical_replication_slot(=0A+=09=09'lock_order_full_wal',=20= 'pgoutput');=0A+});=0A+my=20$hierarchy_ddl=20=3D=0A+=20=20= $node->background_psql('postgres',=20on_error_stop=20=3D>=200);=0A= +$hierarchy_ddl->query_safe('BEGIN');=0A+$hierarchy_ddl->query_safe(=0A+=09= 'LOCK=20TABLE=20lock_order_root=20IN=20ACCESS=20EXCLUSIVE=20MODE');=0A+=0A= +($ret,=20$stdout,=20$stderr)=20=3D=20$node->psql('postgres',=20q{=0A+=09= SELECT=20pg_copy_logical_replication_slot(=0A+=09=09'lock_order_source',=20= 'lock_order_slot');=0A+});=0A+#=20Slot=20copying=20should=20fail=20= instead=20of=20waiting=20while=20holding=20the=20scope=20lock.=0A= +isnt($ret,=200,=0A+=09'restricted=20slot=20copying=20does=20not=20= deadlock=20on=20a=20relation=20lock');=0A+#=20The=20lock-order=20failure=20= should=20identify=20concurrent=20scope=20initialization=20activity.=0A= +like($stderr,=0A+=09qr/could=20not=20initialize=20restricted=20logical=20= replication=20slot=20due=20to=20concurrent=20activity/,=0A+=09= 'lock-order=20conflict=20reports=20the=20expected=20error');=0A= +$hierarchy_ddl->query_safe('ROLLBACK');=0A+$hierarchy_ddl->quit;=0A+=0A= +#=20Retrying=20the=20slot=20copy=20should=20succeed=20after=20the=20= relation=20lock=20is=20released.=0A+$node->safe_psql('postgres',=20q{=0A= +=09SELECT=20pg_copy_logical_replication_slot(=0A+=09=09= 'lock_order_source',=20'lock_order_slot');=0A+});=0A= +$node->safe_psql('postgres',=0A+=09"SELECT=20= pg_drop_replication_slot('lock_order_full_wal')");=0A= +$node->safe_psql('postgres',=20q{=0A+=09SELECT=20= pg_drop_replication_slot('lock_order_slot');=0A+=09SELECT=20= pg_drop_replication_slot('lock_order_source');=0A+});=0A+=0A= +$node->safe_psql('postgres',=20q{=0A+=09CREATE=20TABLE=20= command_scope_a=20(id=20integer);=0A+=09CREATE=20TABLE=20command_scope_b=20= (id=20integer);=0A+=09CREATE=20PUBLICATION=20command_scope_pub_a;=0A+=09= CREATE=20PUBLICATION=20command_scope_pub_b;=0A+});=0A+my=20$scope_xact_a=20= =3D=20$node->background_psql('postgres');=0A+my=20$scope_xact_b=20=3D=20= $node->background_psql('postgres');=0A= +$scope_xact_a->query_safe('BEGIN');=0A+$scope_xact_a->query_safe(=0A+=09= 'ALTER=20PUBLICATION=20command_scope_pub_a=20ADD=20TABLE=20= command_scope_a');=0A+$scope_xact_b->query_safe('BEGIN');=0A= +$scope_xact_b->query_safe(=0A+=09'ALTER=20PUBLICATION=20= command_scope_pub_b=20ADD=20TABLE=20command_scope_b');=0A+=0A+#=20A=20= completed=20scope=20command=20should=20not=20retain=20the=20database=20= scope=20lock.=0A+pass('separate=20transactions=20can=20complete=20= successive=20scope=20commands');=0A= +$scope_xact_a->query_safe('ROLLBACK');=0A= +$scope_xact_b->query_safe('ROLLBACK');=0A+$scope_xact_a->quit;=0A= +$scope_xact_b->quit;=0A+=0A+$node->safe_psql('postgres',=20q{=0A+=09= CREATE=20SCHEMA=20published_schema;=0A+=09CREATE=20SCHEMA=20= unpublished_schema;=0A+=09CREATE=20TABLE=20= unpublished_schema.schema_move=20(id=20integer);=0A+=09CREATE=20= PUBLICATION=20schema_move_pub=0A+=09=09FOR=20TABLES=20IN=20SCHEMA=20= published_schema;=0A+});=0A+$node->safe_psql('postgres',=20q{=0A+=09= SELECT=20pg_create_logical_replication_slot(=0A+=09=09= 'schema_move_slot',=20'pgoutput',=0A+=09=09publications=20=3D>=20= ARRAY['schema_move_pub']);=0A+});=0A+$node->safe_psql('postgres',=20q{=0A= +=09ALTER=20TABLE=20unpublished_schema.schema_move=20SET=20SCHEMA=20= published_schema;=0A+});=0A+=0A+#=20SET=20SCHEMA=20should=20map=20a=20= table=20moved=20into=20a=20published=20schema.=0A= +is($node->safe_psql('postgres',=20q{=0A+=09SELECT=20= c.relhasrestrictedslots=20AND=20EXISTS=20(=0A+=09=09SELECT=201=20FROM=20= pg_restricted_slot_relation=0A+=09=09WHERE=20rsrslotname=20=3D=20= 'schema_move_slot'=0A+=09=09=20=20AND=20rsrrelid=20=3D=20c.oid)=0A+=09= FROM=20pg_class=20AS=20c=0A+=09WHERE=20c.oid=20=3D=20= 'published_schema.schema_move'::regclass;=0A+}),=20't',=20'moving=20a=20= table=20into=20a=20published=20schema=20installs=20its=20mapping');=0A+=0A= +$node->safe_psql('postgres',=20q{=0A+=09CREATE=20TABLE=20measurement=20= (id=20integer,=20payload=20text)=20PARTITION=20BY=20RANGE=20(id);=0A+=09= CREATE=20TABLE=20measurement_old=20PARTITION=20OF=20measurement=20FOR=20= VALUES=20FROM=20(0)=20TO=20(10);=0A+=09ALTER=20PUBLICATION=20orders_pub=20= ADD=20TABLE=20measurement;=0A+=09CREATE=20TABLE=20measurement_new=20= (LIKE=20measurement);=0A+=09ALTER=20TABLE=20measurement=20ATTACH=20= PARTITION=20measurement_new=20FOR=20VALUES=20FROM=20(10)=20TO=20(20);=0A= +});=0A+#=20Attaching=20a=20partition=20should=20propagate=20its=20= parent's=20restricted=20mapping.=0A+is($node->safe_psql('postgres',=20q{=0A= +=09SELECT=20relhasrestrictedslots=20AND=20EXISTS=20(=0A+=09=09SELECT=20= 1=20FROM=20pg_restricted_slot_relation=0A+=09=09WHERE=20rsrslotname=20=3D=20= 'orders_slot'=20AND=20rsrrelid=20=3D=20'measurement_new'::regclass)=0A+=09= FROM=20pg_class=20WHERE=20oid=20=3D=20'measurement_new'::regclass;=0A= +}),=20't',=20'partition=20attachment=20propagates=20restricted=20= membership');=0A+=0A+$node->safe_psql('postgres',=20q{=0A+=09CREATE=20= TABLE=20root_mode=20(id=20integer,=20payload=20text)=20PARTITION=20BY=20= RANGE=20(id);=0A+=09CREATE=20TABLE=20root_mode_old=20PARTITION=20OF=20= root_mode=0A+=09=09FOR=20VALUES=20FROM=20(0)=20TO=20(10);=0A+=09CREATE=20= PUBLICATION=20root_mode_pub=20FOR=20TABLE=20root_mode=0A+=09=09WITH=20= (publish_via_partition_root=20=3D=20true);=0A+});=0A= +$node->safe_psql('postgres',=20q{=0A+=09SELECT=20= pg_create_logical_replication_slot(=0A+=09=09'root_mode_slot',=20= 'pgoutput',=20publications=20=3D>=20ARRAY['root_mode_pub']);=0A+});=0A= +$node->safe_psql('postgres',=20q{=0A+=09CREATE=20TABLE=20root_mode_new=20= (LIKE=20root_mode);=0A+=09BEGIN;=0A+=09ALTER=20TABLE=20root_mode=20= ATTACH=20PARTITION=20root_mode_new=0A+=09=09FOR=20VALUES=20FROM=20(10)=20= TO=20(20);=0A+=09INSERT=20INTO=20root_mode=20VALUES=20(10,=20'new=20= partition');=0A+=09COMMIT;=0A+});=0A+#=20The=20root,=20old=20leaf,=20and=20= newly=20attached=20leaf=20should=20all=20be=20mapped.=0A= +is($node->safe_psql('postgres',=20q{=0A+=09SELECT=20= bool_and(c.relhasrestrictedslots)=20AND=20count(*)=20=3D=203=0A+=09FROM=20= pg_class=20AS=20c=0A+=09JOIN=20pg_restricted_slot_relation=20AS=20m=20ON=20= m.rsrrelid=20=3D=20c.oid=0A+=09WHERE=20m.rsrslotname=20=3D=20= 'root_mode_slot'=0A+=09=20=20AND=20c.oid=20IN=20('root_mode'::regclass,=0A= +=09=09=09=09=20=20'root_mode_old'::regclass,=0A+=09=09=09=09=20=20= 'root_mode_new'::regclass);=0A+}),=20't',=20'partition-root=20= publication=20maps=20a=20newly=20attached=20partition');=0A+=0A+#=20= Table=20synchronization=20should=20handle=20an=20attached=20partition=20= with=20both=20settings=0A+#=20of=20publish_via_partition_root.=20=20In=20= leaf=20mode,=20REFRESH=20PUBLICATION=20discovers=0A+#=20a=20new=20= subscription=20relation=20and=20copies=20its=20existing=20data.=20=20In=20= root=20mode,=20the=0A+#=20root=20is=20already=20a=20subscription=20= relation,=20so=20refresh=20does=20not=20initiate=20another=0A+#=20table=20= synchronization=20for=20the=20newly=20attached=20leaf.=0A+my=20= $subscriber=20=3D=20PostgreSQL::Test::Cluster->new('subscriber');=0A= +$subscriber->init(allows_streaming=20=3D>=20'logical');=0A= +$subscriber->start;=0A+my=20$publisher_connstr=20=3D=20$node->connstr=20= .=20'=20dbname=3Dpostgres';=0A+=0A+$node->safe_psql('postgres',=20q{=0A+=09= CREATE=20TABLE=20leaf_copy_root=20(id=20integer,=20payload=20text)=0A+=09= =09PARTITION=20BY=20RANGE=20(id);=0A+=09CREATE=20TABLE=20leaf_copy_old=20= PARTITION=20OF=20leaf_copy_root=0A+=09=09FOR=20VALUES=20FROM=20(0)=20TO=20= (10);=0A+=09CREATE=20TABLE=20leaf_copy_new=20(LIKE=20leaf_copy_root);=0A= +=09INSERT=20INTO=20leaf_copy_new=20VALUES=20(10,=20'before=20attach');=0A= +=09CREATE=20PUBLICATION=20leaf_copy_pub=20FOR=20TABLE=20leaf_copy_root=0A= +=09=09WITH=20(publish_via_partition_root=20=3D=20false);=0A+=0A+=09= CREATE=20TABLE=20root_copy_root=20(id=20integer,=20payload=20text)=0A+=09= =09PARTITION=20BY=20RANGE=20(id);=0A+=09CREATE=20TABLE=20root_copy_old=20= PARTITION=20OF=20root_copy_root=0A+=09=09FOR=20VALUES=20FROM=20(0)=20TO=20= (10);=0A+=09CREATE=20TABLE=20root_copy_new=20(LIKE=20root_copy_root);=0A= +=09INSERT=20INTO=20root_copy_new=20VALUES=20(10,=20'before=20attach');=0A= +=09CREATE=20PUBLICATION=20root_copy_pub=20FOR=20TABLE=20root_copy_root=0A= +=09=09WITH=20(publish_via_partition_root=20=3D=20true);=0A+});=0A= +$subscriber->safe_psql('postgres',=20q{=0A+=09CREATE=20TABLE=20= leaf_copy_root=20(id=20integer,=20payload=20text)=0A+=09=09PARTITION=20= BY=20RANGE=20(id);=0A+=09CREATE=20TABLE=20leaf_copy_old=20PARTITION=20OF=20= leaf_copy_root=0A+=09=09FOR=20VALUES=20FROM=20(0)=20TO=20(10);=0A+=09= CREATE=20TABLE=20leaf_copy_new=20PARTITION=20OF=20leaf_copy_root=0A+=09=09= FOR=20VALUES=20FROM=20(10)=20TO=20(20);=0A+=0A+=09CREATE=20TABLE=20= root_copy_root=20(id=20integer,=20payload=20text)=0A+=09=09PARTITION=20= BY=20RANGE=20(id);=0A+=09CREATE=20TABLE=20root_copy_old=20PARTITION=20OF=20= root_copy_root=0A+=09=09FOR=20VALUES=20FROM=20(0)=20TO=20(10);=0A+=09= CREATE=20TABLE=20root_copy_new=20PARTITION=20OF=20root_copy_root=0A+=09=09= FOR=20VALUES=20FROM=20(10)=20TO=20(20);=0A+});=0A= +$subscriber->safe_psql('postgres',=20qq{=0A+=09CREATE=20SUBSCRIPTION=20= leaf_copy_sub=20CONNECTION=20'$publisher_connstr'=0A+=09=09PUBLICATION=20= leaf_copy_pub=0A+=09=09WITH=20(copy_data=20=3D=20true,=20= unrestricted_slot=20=3D=20false);=0A+=09CREATE=20SUBSCRIPTION=20= root_copy_sub=20CONNECTION=20'$publisher_connstr'=0A+=09=09PUBLICATION=20= root_copy_pub=0A+=09=09WITH=20(copy_data=20=3D=20true,=20= unrestricted_slot=20=3D=20false);=0A+});=0A= +$subscriber->wait_for_subscription_sync($node,=20'leaf_copy_sub');=0A= +$subscriber->wait_for_subscription_sync($node,=20'root_copy_sub');=0A+=0A= +$node->safe_psql('postgres',=20q{=0A+=09ALTER=20TABLE=20leaf_copy_root=20= ATTACH=20PARTITION=20leaf_copy_new=0A+=09=09FOR=20VALUES=20FROM=20(10)=20= TO=20(20);=0A+=09ALTER=20TABLE=20root_copy_root=20ATTACH=20PARTITION=20= root_copy_new=0A+=09=09FOR=20VALUES=20FROM=20(10)=20TO=20(20);=0A+});=0A= +$subscriber->safe_psql('postgres',=20q{=0A+=09ALTER=20SUBSCRIPTION=20= leaf_copy_sub=20REFRESH=20PUBLICATION=0A+=09=09WITH=20(copy_data=20=3D=20= true);=0A+=09ALTER=20SUBSCRIPTION=20root_copy_sub=20REFRESH=20= PUBLICATION=0A+=09=09WITH=20(copy_data=20=3D=20true);=0A+});=0A= +$subscriber->wait_for_subscription_sync($node,=20'leaf_copy_sub');=0A= +$subscriber->wait_for_subscription_sync($node,=20'root_copy_sub');=0A+=0A= +#=20Leaf-mode=20refresh=20should=20copy=20data=20that=20predates=20= partition=20attachment.=0A+is($subscriber->safe_psql('postgres',=20q{=0A= +=09SELECT=20string_agg(payload,=20','=20ORDER=20BY=20id)=20FROM=20= leaf_copy_root;=0A+}),=20'before=20attach',=0A+=09'leaf=20publication=20= copies=20a=20row=20that=20existed=20before=20partition=20attachment');=0A= +#=20Root-mode=20refresh=20should=20not=20resynchronize=20an=20already=20= known=20root=20table.=0A+is($subscriber->safe_psql('postgres',=20q{=0A+=09= SELECT=20count(*)=20FROM=20root_copy_root;=0A+}),=20'0',=0A+=09= 'partition-root=20publication=20does=20not=20recopy=20its=20existing=20= root=20relation');=0A+=0A+$node->safe_psql('postgres',=20q{=0A+=09INSERT=20= INTO=20leaf_copy_root=20VALUES=20(11,=20'after=20attach');=0A+=09INSERT=20= INTO=20root_copy_root=20VALUES=20(11,=20'after=20attach');=0A+});=0A= +$node->wait_for_catchup('leaf_copy_sub');=0A= +$node->wait_for_catchup('root_copy_sub');=0A+=0A+#=20Leaf=20mode=20= should=20stream=20changes=20made=20after=20partition=20attachment.=0A= +is($subscriber->safe_psql('postgres',=20q{=0A+=09SELECT=20= string_agg(payload,=20','=20ORDER=20BY=20id)=20FROM=20leaf_copy_root;=0A= +}),=20'before=20attach,after=20attach',=0A+=09'leaf=20publication=20= streams=20rows=20inserted=20after=20partition=20attachment');=0A+#=20= Root=20mode=20should=20stream=20changes=20made=20after=20partition=20= attachment.=0A+is($subscriber->safe_psql('postgres',=20q{=0A+=09SELECT=20= string_agg(payload,=20','=20ORDER=20BY=20id)=20FROM=20root_copy_root;=0A= +}),=20'after=20attach',=0A+=09'partition-root=20publication=20streams=20= rows=20inserted=20after=20attachment');=0A+=0A= +$subscriber->safe_psql('postgres',=0A+=09'ALTER=20SUBSCRIPTION=20= root_copy_sub=20DISABLE');=0A+$node->poll_query_until('postgres',=20q{=0A= +=09SELECT=20NOT=20active=20FROM=20pg_replication_slots=0A+=09WHERE=20= slot_name=20=3D=20'root_copy_sub';=0A+},=20't');=0A= +$subscriber->safe_psql('postgres',=20q{=0A+=09CREATE=20TABLE=20= root_history_part=20PARTITION=20OF=20root_copy_root=0A+=09=09FOR=20= VALUES=20FROM=20(20)=20TO=20(30);=0A+});=0A+$node->safe_psql('postgres',=20= q{=0A+=09CREATE=20TABLE=20root_history_part=20(LIKE=20root_copy_root);=0A= +=09SELECT=20pg_create_logical_replication_slot(=0A+=09=09= 'history_cover_slot',=20'test_decoding');=0A+=09INSERT=20INTO=20= root_history_part=20VALUES=20(20,=20'existing=20before=20attach');=0A+=09= INSERT=20INTO=20root_history_part=20VALUES=20(21,=20'before=20attach');=0A= +=09ALTER=20TABLE=20root_copy_root=20ATTACH=20PARTITION=20= root_history_part=0A+=09=09FOR=20VALUES=20FROM=20(20)=20TO=20(30);=0A+=09= INSERT=20INTO=20root_history_part=20VALUES=20(22,=20'after=20attach');=0A= +=09ALTER=20TABLE=20root_copy_root=20DETACH=20PARTITION=20= root_history_part;=0A+=09INSERT=20INTO=20root_history_part=20VALUES=20= (23,=20'after=20detach');=0A+=09ALTER=20TABLE=20root_copy_root=20ATTACH=20= PARTITION=20root_history_part=0A+=09=09FOR=20VALUES=20FROM=20(20)=20TO=20= (30);=0A+=09INSERT=20INTO=20root_history_part=20VALUES=20(24,=20'after=20= reattach');=0A+});=0A+$subscriber->safe_psql('postgres',=0A+=09'ALTER=20= SUBSCRIPTION=20root_copy_sub=20ENABLE');=0A= +$node->wait_for_catchup('root_copy_sub');=0A+=0A+#=20Delayed=20decoding=20= should=20use=20partition=20membership=20at=20each=20change's=20WAL=20= point.=0A+is($subscriber->safe_psql('postgres',=20q{=0A+=09SELECT=20= string_agg(id=20||=20':'=20||=20payload,=20','=20ORDER=20BY=20id)=0A+=09= FROM=20root_copy_root=20WHERE=20id=20>=3D=2020;=0A+}),=20'22:after=20= attach,24:after=20reattach',=0A+=09'delayed=20decoding=20follows=20= attach=20and=20detach=20boundaries');=0A+=0A= +$node->safe_psql('postgres',=0A+=09"SELECT=20= pg_drop_replication_slot('history_cover_slot')");=0A+=0A= +$subscriber->safe_psql('postgres',=0A+=09'ALTER=20SUBSCRIPTION=20= leaf_copy_sub=20DISABLE');=0A+$node->safe_psql('postgres',=0A+=09= q{INSERT=20INTO=20leaf_copy_root=20VALUES=20(12,=20'while=20= disabled')});=0A+=0A+#=20A=20disabled=20subscription=20should=20not=20= apply=20newly=20generated=20changes.=0A= +is($subscriber->safe_psql('postgres',=20q{=0A+=09SELECT=20= string_agg(payload,=20','=20ORDER=20BY=20id)=20FROM=20leaf_copy_root;=0A= +}),=20'before=20attach,after=20attach',=0A+=09'a=20disabled=20= subscription=20does=20not=20apply=20new=20changes');=0A+=0A= +$subscriber->safe_psql('postgres',=0A+=09'ALTER=20SUBSCRIPTION=20= leaf_copy_sub=20ENABLE');=0A+$node->wait_for_catchup('leaf_copy_sub');=0A= +=0A+#=20Enabling=20a=20subscription=20should=20replay=20scoped=20WAL=20= written=20while=20disabled.=0A+is($subscriber->safe_psql('postgres',=20= q{=0A+=09SELECT=20string_agg(payload,=20','=20ORDER=20BY=20id)=20FROM=20= leaf_copy_root;=0A+}),=20'before=20attach,after=20attach,while=20= disabled',=0A+=09'enabling=20a=20subscription=20replays=20changes=20= generated=20while=20disabled');=0A+=0A+$node->safe_psql('postgres',=20q{=0A= +=09CREATE=20TABLE=20speculative_unscoped=20(=0A+=09=09id=20integer=20= PRIMARY=20KEY,=20payload=20text);=0A+=09BEGIN;=0A+=09INSERT=20INTO=20= speculative_unscoped=20VALUES=20(1,=20repeat('x',=20100000))=0A+=09=09ON=20= CONFLICT=20DO=20NOTHING;=0A+=09INSERT=20INTO=20leaf_copy_root=20VALUES=20= (13,=20'after=20speculative=20insert');=0A+=09COMMIT;=0A+});=0A= +$node->wait_for_catchup('leaf_copy_sub');=0A+=0A+#=20An=20unscoped=20= speculative=20insert=20should=20not=20prevent=20decoding=20a=20scoped=20= change.=0A+is($subscriber->safe_psql('postgres',=20q{=0A+=09SELECT=20= payload=20FROM=20leaf_copy_root=20WHERE=20id=20=3D=2013;=0A+}),=20'after=20= speculative=20insert',=0A+=09'decoding=20preserves=20unscoped=20= speculative=20insertion=20ordering');=0A+=0A= +$node->safe_psql('postgres',=20q{=0A+=09ALTER=20TABLE=20measurement=20= DROP=20COLUMN=20payload;=0A+=09CREATE=20TABLE=20measurement_after_drop=20= (LIKE=20measurement);=0A+=09ALTER=20TABLE=20measurement=20ATTACH=20= PARTITION=20measurement_after_drop=20DEFAULT;=0A+});=0A+#=20Dropped=20= columns=20should=20not=20lose=20mappings=20needed=20by=20later=20= partitions.=0A+is($node->safe_psql('postgres',=20q{=0A+=09SELECT=20= EXISTS=20(=0A+=09=09SELECT=201=20FROM=20pg_restricted_slot_relation=0A+=09= =09WHERE=20rsrslotname=20=3D=20'orders_slot'=0A+=09=09=20=20AND=20= rsrrelid=20=3D=20'measurement_after_drop'::regclass);=0A+}),=20't',=20= 'dropping=20a=20column=20preserves=20mappings=20used=20by=20later=20= attachment');=0A+=0A+$node->safe_psql('postgres',=20q{=0A+=09CREATE=20= TABLE=20toast_later=20(id=20integer);=0A+=09ALTER=20PUBLICATION=20= orders_pub=20ADD=20TABLE=20toast_later;=0A+=09BEGIN;=0A+=09ALTER=20TABLE=20= toast_later=20ADD=20COLUMN=20payload=20text;=0A+=09INSERT=20INTO=20= toast_later=20VALUES=20(1,=20repeat('x',=20100000));=0A+=09COMMIT;=0A= +});=0A+#=20TOAST=20creation=20should=20propagate=20mapping=20before=20= same-transaction=20DML.=0A+is($node->safe_psql('postgres',=20q{=0A+=09= SELECT=20toast.relhasrestrictedslots=20AND=20EXISTS=20(=0A+=09=09SELECT=20= 1=20FROM=20pg_restricted_slot_relation=0A+=09=09WHERE=20rsrslotname=20=3D=20= 'orders_slot'=20AND=20rsrrelid=20=3D=20owner.reltoastrelid)=0A+=09FROM=20= pg_class=20owner=20JOIN=20pg_class=20toast=20ON=20toast.oid=20=3D=20= owner.reltoastrelid=0A+=09WHERE=20owner.oid=20=3D=20= 'toast_later'::regclass;=0A+}),=20't',=20'TOAST=20creation=20propagates=20= membership=20before=20same-transaction=20DML');=0A+=0A+$node->restart;=0A= +#=20Restricted=20metadata=20and=20selective=20WAL=20should=20remain=20= active=20after=20restart.=0A+is($node->safe_psql('postgres',=20q{=0A+=09= SELECT=20NOT=20unrestricted=20AND=20restricted_scope_ready=20AND=0A+=09=20= =20=20=20=20=20=20current_setting('restricted_wal_level')=20=3D=20= 'logical'=0A+=09FROM=20pg_replication_slots=20WHERE=20slot_name=20=3D=20= 'orders_slot';=0A+}),=20't',=20'restricted=20slot=20metadata=20and=20WAL=20= level=20survive=20restart');=0A+=0A+$node->safe_psql('postgres',=20q{=0A= +=09SELECT=20pg_create_logical_replication_slot('general_slot',=20= 'test_decoding');=0A+});=0A+#=20Omitting=20publications=20should=20= retain=20ordinary=20unrestricted-slot=20behavior.=0A= +is($node->safe_psql('postgres',=20q{=0A+=09SELECT=20unrestricted=20AND=20= publication_oids=20IS=20NULL=0A+=09FROM=20pg_replication_slots=20WHERE=20= slot_name=20=3D=20'general_slot';=0A+}),=20't',=20'an=20omitted=20= publication=20list=20creates=20an=20unrestricted=20slot');=0A+=0A= +$node->safe_psql('postgres',=20q{=0A+=09SELECT=20= pg_create_logical_replication_slot(=0A+=09=09'reuse_slot',=20'pgoutput',=20= publications=20=3D>=20ARRAY['orders_pub']);=0A+});=0A+my=20= $old_incarnation=20=3D=20$node->safe_psql('postgres',=20q{=0A+=09SELECT=20= restricted_scope_incarnation=20FROM=20pg_replication_slots=0A+=09WHERE=20= slot_name=20=3D=20'reuse_slot';=0A+});=0A+$node->safe_psql('postgres',=20= q{=0A+=09SELECT=20pg_drop_replication_slot('reuse_slot');=0A+=09SELECT=20= pg_create_logical_replication_slot(=0A+=09=09'reuse_slot',=20'pgoutput',=20= publications=20=3D>=20ARRAY['orders_pub']);=0A+});=0A+my=20= $new_incarnation=20=3D=20$node->safe_psql('postgres',=20q{=0A+=09SELECT=20= restricted_scope_incarnation=20FROM=20pg_replication_slots=0A+=09WHERE=20= slot_name=20=3D=20'reuse_slot';=0A+});=0A+#=20A=20recreated=20slot=20= name=20should=20not=20reuse=20the=20old=20scope=20incarnation.=0A= +isnt($new_incarnation,=20$old_incarnation,=0A+=09'slot=20name=20reuse=20= receives=20a=20distinct=20durable=20incarnation');=0A+=0A= +done_testing();=0Adiff=20--git=20= a/src/test/regress/expected/oidjoins.out=20= b/src/test/regress/expected/oidjoins.out=0Aindex=20= d64169b7bf0..b971651c8ec=20100644=0A---=20= a/src/test/regress/expected/oidjoins.out=0A+++=20= b/src/test/regress/expected/oidjoins.out=0A@@=20-268,6=20+268,7=20@@=20= NOTICE:=20=20checking=20pg_publication_namespace=20{pnpubid}=20=3D>=20= pg_publication=20{oid}=0A=20NOTICE:=20=20checking=20= pg_publication_namespace=20{pnnspid}=20=3D>=20pg_namespace=20{oid}=0A=20= NOTICE:=20=20checking=20pg_publication_rel=20{prpubid}=20=3D>=20= pg_publication=20{oid}=0A=20NOTICE:=20=20checking=20pg_publication_rel=20= {prrelid}=20=3D>=20pg_class=20{oid}=0A+NOTICE:=20=20checking=20= pg_restricted_slot_relation=20{rsrrelid}=20=3D>=20pg_class=20{oid}=0A=20= NOTICE:=20=20checking=20pg_subscription=20{subdbid}=20=3D>=20pg_database=20= {oid}=0A=20NOTICE:=20=20checking=20pg_subscription=20{subowner}=20=3D>=20= pg_authid=20{oid}=0A=20NOTICE:=20=20checking=20pg_subscription=20= {subserver}=20=3D>=20pg_foreign_server=20{oid}=0Adiff=20--git=20= a/src/test/regress/expected/rules.out=20= b/src/test/regress/expected/rules.out=0Aindex=201a29d46213e..f9e9d66ca10=20= 100644=0A---=20a/src/test/regress/expected/rules.out=0A+++=20= b/src/test/regress/expected/rules.out=0A@@=20-1510,8=20+1510,13=20@@=20= pg_replication_slots|=20SELECT=20l.slot_name,=0A=20=20=20=20=20= l.invalidation_reason,=0A=20=20=20=20=20l.failover,=0A=20=20=20=20=20= l.synced,=0A-=20=20=20=20l.slotsync_skip_reason=0A-=20=20=20FROM=20= (pg_get_replication_slots()=20l(slot_name,=20plugin,=20slot_type,=20= datoid,=20temporary,=20active,=20active_pid,=20xmin,=20catalog_xmin,=20= restart_lsn,=20confirmed_flush_lsn,=20wal_status,=20safe_wal_size,=20= two_phase,=20two_phase_at,=20inactive_since,=20conflicting,=20= invalidation_reason,=20failover,=20synced,=20slotsync_skip_reason)=0A+=20= =20=20=20l.slotsync_skip_reason,=0A+=20=20=20=20l.unrestricted,=0A+=20=20= =20=20l.publication_oids,=0A+=20=20=20=20l.restricted_scope_ready,=0A+=20= =20=20=20l.restricted_scope_incarnation,=0A+=20=20=20=20= l.restricted_scope_ready_lsn=0A+=20=20=20FROM=20= (pg_get_replication_slots()=20l(slot_name,=20plugin,=20slot_type,=20= datoid,=20temporary,=20active,=20active_pid,=20xmin,=20catalog_xmin,=20= restart_lsn,=20confirmed_flush_lsn,=20wal_status,=20safe_wal_size,=20= two_phase,=20two_phase_at,=20inactive_since,=20conflicting,=20= invalidation_reason,=20failover,=20synced,=20slotsync_skip_reason,=20= unrestricted,=20publication_oids,=20restricted_scope_ready,=20= restricted_scope_incarnation,=20restricted_scope_ready_lsn)=0A=20=20=20=20= =20=20LEFT=20JOIN=20pg_database=20d=20ON=20((l.datoid=20=3D=20d.oid)));=0A= =20pg_roles|=20SELECT=20pg_authid.rolname,=0A=20=20=20=20=20= pg_authid.rolsuper,=0Adiff=20--git=20a/src/tools/pgindent/typedefs.list=20= b/src/tools/pgindent/typedefs.list=0Aindex=20298a3d586e7..b2fd3c4fa4c=20= 100644=0A---=20a/src/tools/pgindent/typedefs.list=0A+++=20= b/src/tools/pgindent/typedefs.list=0A@@=20-1698,6=20+1698,10=20@@=20= LogicalDecodeStreamTruncateCB=0A=20LogicalDecodeTruncateCB=0A=20= LogicalDecodingContext=0A=20LogicalDecodingCtlData=0A= +LogicalSlotScopeCtlData=0A+LogicalSlotScopeEntry=0A+LogicalSlotScopeKey=0A= +LogicalSlotScopeOnDisk=0A=20LogicalErrorCallbackState=0A=20= LogicalOutputPluginInit=0A=20LogicalOutputPluginWriterPrepareWrite=0A--=20= =0A2.50.1=20(Apple=20Git-155)=0A=0A= --Apple-Mail=_C08FF66D-40CC-4A7F-8D7F-AB3E6CF59BE1--