Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4ad2a54

Browse files
committed
Add GUC to enable logging of replication commands.
Previously replication commands like IDENTIFY_COMMAND were not logged even when log_statements is set to all. Some users who want to audit all types of statements were not satisfied with this situation. To address the problem, this commit adds new GUC log_replication_commands. If it's enabled, all replication commands are logged in the server log. There are many ways to allow us to enable that logging. For example, we can extend log_statement so that replication commands are logged when it's set to all. But per discussion in the community, we reached the consensus to add separate GUC for that. Reviewed by Ian Barwick, Robert Haas and Heikki Linnakangas.
1 parent a2dabf0 commit 4ad2a54

File tree

6 files changed

+38
-2
lines changed

6 files changed

+38
-2
lines changed

doc/src/sgml/config.sgml

+16
Original file line numberDiff line numberDiff line change
@@ -4672,6 +4672,22 @@ FROM pg_stat_activity;
46724672
</listitem>
46734673
</varlistentry>
46744674

4675+
<varlistentry id="guc-log-replication-commands" xreflabel="log_replication_commands">
4676+
<term><varname>log_replication_commands</varname> (<type>boolean</type>)
4677+
<indexterm>
4678+
<primary><varname>log_replication_commands</> configuration parameter</primary>
4679+
</indexterm>
4680+
</term>
4681+
<listitem>
4682+
<para>
4683+
Causes each replication command to be logged in the server log.
4684+
See <xref linkend="protocol-replication"> for more information about
4685+
replication command. The default value is <literal>off</>.
4686+
Only superusers can change this setting.
4687+
</para>
4688+
</listitem>
4689+
</varlistentry>
4690+
46754691
<varlistentry id="guc-log-temp-files" xreflabel="log_temp_files">
46764692
<term><varname>log_temp_files</varname> (<type>integer</type>)
46774693
<indexterm>

doc/src/sgml/protocol.sgml

+2
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,8 @@ To initiate streaming replication, the frontend sends the
13061306
of <literal>true</> tells the backend to go into walsender mode, wherein a
13071307
small set of replication commands can be issued instead of SQL statements. Only
13081308
the simple query protocol can be used in walsender mode.
1309+
Replication commands are logged in the server log when
1310+
<xref linkend="guc-log-replication-commands"> is enabled.
13091311
Passing <literal>database</> as the value instructs walsender to connect to
13101312
the database specified in the <literal>dbname</> parameter, which will allow
13111313
the connection to be used for logical replication from that database.

src/backend/replication/walsender.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ bool am_db_walsender = false; /* Connected to a database? */
108108
int max_wal_senders = 0; /* the maximum number of concurrent walsenders */
109109
int wal_sender_timeout = 60 * 1000; /* maximum time to send one
110110
* WAL data message */
111+
bool log_replication_commands = false;
111112

112113
/*
113114
* State for WalSndWakeupRequest
@@ -1267,14 +1268,20 @@ exec_replication_command(const char *cmd_string)
12671268
MemoryContext cmd_context;
12681269
MemoryContext old_context;
12691270

1271+
/*
1272+
* Log replication command if log_replication_commands is enabled.
1273+
* Even when it's disabled, log the command with DEBUG1 level for
1274+
* backward compatibility.
1275+
*/
1276+
ereport(log_replication_commands ? LOG : DEBUG1,
1277+
(errmsg("received replication command: %s", cmd_string)));
1278+
12701279
/*
12711280
* CREATE_REPLICATION_SLOT ... LOGICAL exports a snapshot until the next
12721281
* command arrives. Clean up the old stuff if there's anything.
12731282
*/
12741283
SnapBuildClearExportedSnapshot();
12751284

1276-
elog(DEBUG1, "received replication command: %s", cmd_string);
1277-
12781285
CHECK_FOR_INTERRUPTS();
12791286

12801287
cmd_context = AllocSetContextCreate(CurrentMemoryContext,

src/backend/utils/misc/guc.c

+9
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,15 @@ static struct config_bool ConfigureNamesBool[] =
924924
false,
925925
NULL, NULL, NULL
926926
},
927+
{
928+
{"log_replication_commands", PGC_SUSET, LOGGING_WHAT,
929+
gettext_noop("Logs each replication command."),
930+
NULL
931+
},
932+
&log_replication_commands,
933+
false,
934+
NULL, NULL, NULL
935+
},
927936
{
928937
{"debug_assertions", PGC_INTERNAL, PRESET_OPTIONS,
929938
gettext_noop("Shows whether the running server has assertion checks enabled."),

src/backend/utils/misc/postgresql.conf.sample

+1
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@
431431
# e.g. '<%u%%%d> '
432432
#log_lock_waits = off # log lock waits >= deadlock_timeout
433433
#log_statement = 'none' # none, ddl, mod, all
434+
#log_replication_commands = off
434435
#log_temp_files = -1 # log temporary files equal or larger
435436
# than the specified size in kilobytes;
436437
# -1 disables, 0 logs all temp files

src/include/replication/walsender.h

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ extern bool wake_wal_senders;
2525
/* user-settable parameters */
2626
extern int max_wal_senders;
2727
extern int wal_sender_timeout;
28+
extern bool log_replication_commands;
2829

2930
extern void InitWalSender(void);
3031
extern void exec_replication_command(const char *query_string);

0 commit comments

Comments
 (0)