Skip to content

Commit c314ead

Browse files
committed
Add ability to reserve WAL upon slot creation via replication protocol.
Since 6fcd885 it is possible to immediately reserve WAL when creating a slot via pg_create_physical_replication_slot(). Extend the replication protocol to allow that as well. Although, in contrast to the SQL interface, it is possible to update the reserved location via the replication interface, it is still useful being able to reserve upon creation there. Otherwise the logic in ReplicationSlotReserveWal() has to be repeated in slot employing clients. Author: Michael Paquier Discussion: CAB7nPqT0Wc1W5mdYGeJ_wbutbwNN+3qgrFR64avXaQCiJMGaYA@mail.gmail.com
1 parent 258ee1b commit c314ead

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

doc/src/sgml/protocol.sgml

+12-1
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,7 @@ The commands accepted in walsender mode are:
14341434
</varlistentry>
14351435

14361436
<varlistentry>
1437-
<term>CREATE_REPLICATION_SLOT <replaceable class="parameter">slot_name</> { <literal>PHYSICAL</> | <literal>LOGICAL</> <replaceable class="parameter">output_plugin</> }
1437+
<term>CREATE_REPLICATION_SLOT <replaceable class="parameter">slot_name</> { <literal>PHYSICAL</> [ RESERVE_WAL ] | <literal>LOGICAL</> <replaceable class="parameter">output_plugin</> }
14381438
<indexterm><primary>CREATE_REPLICATION_SLOT</primary></indexterm>
14391439
</term>
14401440
<listitem>
@@ -1463,6 +1463,17 @@ The commands accepted in walsender mode are:
14631463
</para>
14641464
</listitem>
14651465
</varlistentry>
1466+
1467+
<varlistentry>
1468+
<term><literal>RESERVE_WAL</></term>
1469+
<listitem>
1470+
<para>
1471+
Specify that this physical replication reserves <acronym>WAL</>
1472+
immediately; otherwise <acronym>WAL</> is only reserved upon
1473+
connection from a streaming replication client.
1474+
</para>
1475+
</listitem>
1476+
</varlistentry>
14661477
</variablelist>
14671478
</listitem>
14681479
</varlistentry>

src/backend/replication/repl_gram.y

+10-2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Node *replication_parse_result;
7676
%token K_PHYSICAL
7777
%token K_LOGICAL
7878
%token K_SLOT
79+
%token K_RESERVE_WAL
7980

8081
%type <node> command
8182
%type <node> base_backup start_replication start_logical_replication
@@ -88,6 +89,7 @@ Node *replication_parse_result;
8889
%type <defelt> plugin_opt_elem
8990
%type <node> plugin_opt_arg
9091
%type <str> opt_slot
92+
%type <boolval> opt_reserve_wal
9193

9294
%%
9395

@@ -181,13 +183,14 @@ base_backup_opt:
181183
;
182184

183185
create_replication_slot:
184-
/* CREATE_REPLICATION_SLOT slot PHYSICAL */
185-
K_CREATE_REPLICATION_SLOT IDENT K_PHYSICAL
186+
/* CREATE_REPLICATION_SLOT slot PHYSICAL RESERVE_WAL */
187+
K_CREATE_REPLICATION_SLOT IDENT K_PHYSICAL opt_reserve_wal
186188
{
187189
CreateReplicationSlotCmd *cmd;
188190
cmd = makeNode(CreateReplicationSlotCmd);
189191
cmd->kind = REPLICATION_KIND_PHYSICAL;
190192
cmd->slotname = $2;
193+
cmd->reserve_wal = $4;
191194
$$ = (Node *) cmd;
192195
}
193196
/* CREATE_REPLICATION_SLOT slot LOGICAL plugin */
@@ -268,6 +271,11 @@ opt_physical:
268271
| /* EMPTY */
269272
;
270273

274+
opt_reserve_wal:
275+
K_RESERVE_WAL { $$ = true; }
276+
| /* EMPTY */ { $$ = false; }
277+
;
278+
271279
opt_slot:
272280
K_SLOT IDENT
273281
{ $$ = $2; }

src/backend/replication/repl_scanner.l

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ CREATE_REPLICATION_SLOT { return K_CREATE_REPLICATION_SLOT; }
9595
DROP_REPLICATION_SLOT { return K_DROP_REPLICATION_SLOT; }
9696
TIMELINE_HISTORY { return K_TIMELINE_HISTORY; }
9797
PHYSICAL { return K_PHYSICAL; }
98+
RESERVE_WAL { return K_RESERVE_WAL; }
9899
LOGICAL { return K_LOGICAL; }
99100
SLOT { return K_SLOT; }
100101

src/backend/replication/walsender.c

+8
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,14 @@ CreateReplicationSlot(CreateReplicationSlotCmd *cmd)
826826

827827
ReplicationSlotPersist();
828828
}
829+
else if (cmd->kind == REPLICATION_KIND_PHYSICAL && cmd->reserve_wal)
830+
{
831+
ReplicationSlotReserveWal();
832+
833+
/* Write this slot to disk */
834+
ReplicationSlotMarkDirty();
835+
ReplicationSlotSave();
836+
}
829837

830838
slot_name = NameStr(MyReplicationSlot->data.name);
831839
snprintf(xpos, sizeof(xpos), "%X/%X",

src/include/nodes/replnodes.h

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ typedef struct CreateReplicationSlotCmd
5555
char *slotname;
5656
ReplicationKind kind;
5757
char *plugin;
58+
bool reserve_wal;
5859
} CreateReplicationSlotCmd;
5960

6061

0 commit comments

Comments
 (0)