Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc399836

Browse files
committed
Make walsenders show their replication commands in pg_stat_activity.
A walsender process that has executed a SQL command left the text ofthat command in pg_stat_activity.query indefinitely, which is quiteconfusing if it's in RUNNING state but not doing that query. An easyand useful fix is to treat replication commands as if they were SQLqueries, and show them in pg_stat_activity according to the same rulesas for regular queries. While we're at it, it seems also sensible toset debug_query_string, allowing error logging and debugging to seethe replication command.While here, clean up assorted silliness in exec_replication_command:* Clean up SQLCmd code path, and fix its only-accidentally-not-buggy memory management.* Remove useless duplicate call of SnapBuildClearExportedSnapshot().* replication_scanner_finish() was never called.Back-patch of commitf560209 into v10-v13. I'd originally feltthat this didn't merit back-patching, but subsequent confusionwhile debugging walsender problems suggests that it'll be useful.Also, the original commit has now aged long enough to provide somecomfort that it won't cause problems.Discussion:https://postgr.es/m/2673480.1624557299@sss.pgh.pa.usDiscussion:https://postgr.es/m/880181.1600026471@sss.pgh.pa.us
1 parentc4c9c77 commitc399836

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

‎src/backend/replication/walsender.c

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,9 @@ exec_replication_command(const char *cmd_string)
14571457

14581458
CHECK_FOR_INTERRUPTS();
14591459

1460+
/*
1461+
* Parse the command.
1462+
*/
14601463
cmd_context=AllocSetContextCreate(CurrentMemoryContext,
14611464
"Replication command context",
14621465
ALLOCSET_DEFAULT_SIZES);
@@ -1467,33 +1470,49 @@ exec_replication_command(const char *cmd_string)
14671470
if (parse_rc!=0)
14681471
ereport(ERROR,
14691472
(errcode(ERRCODE_SYNTAX_ERROR),
1470-
(errmsg_internal("replication command parser returned %d",
1471-
parse_rc))));
1473+
errmsg_internal("replication command parser returned %d",
1474+
parse_rc)));
1475+
replication_scanner_finish();
14721476

14731477
cmd_node=replication_parse_result;
14741478

14751479
/*
1476-
* Log replication command if log_replication_commands is enabled. Even
1477-
* when it's disabled, log the command with DEBUG1 level for backward
1478-
* compatibility. Note that SQL commands are not logged here, and will be
1479-
* logged later if log_statement is enabled.
1480+
* If it's a SQL command, just clean up our mess and return false; the
1481+
* caller will take care of executing it.
14801482
*/
1481-
if (cmd_node->type!=T_SQLCmd)
1482-
ereport(log_replication_commands ?LOG :DEBUG1,
1483-
(errmsg("received replication command: %s",cmd_string)));
1483+
if (IsA(cmd_node,SQLCmd))
1484+
{
1485+
if (MyDatabaseId==InvalidOid)
1486+
ereport(ERROR,
1487+
(errmsg("cannot execute SQL commands in WAL sender for physical replication")));
1488+
1489+
MemoryContextSwitchTo(old_context);
1490+
MemoryContextDelete(cmd_context);
1491+
1492+
/* Tell the caller that this wasn't a WalSender command. */
1493+
return false;
1494+
}
14841495

14851496
/*
1486-
*CREATE_REPLICATION_SLOT ... LOGICAL exports a snapshot. If it was
1487-
*called outside of transaction the snapshot should be cleared here.
1497+
*Report query to various monitoring facilities. For this purpose, we
1498+
*report replication commands just like SQL commands.
14881499
*/
1489-
if (!IsTransactionBlock())
1490-
SnapBuildClearExportedSnapshot();
1500+
debug_query_string=cmd_string;
1501+
1502+
pgstat_report_activity(STATE_RUNNING,cmd_string);
14911503

14921504
/*
1493-
* For aborted transactions, don't allow anything except pure SQL, the
1494-
* exec_simple_query() will handle it correctly.
1505+
* Log replication command if log_replication_commands is enabled. Even
1506+
* when it's disabled, log the command with DEBUG1 level for backward
1507+
* compatibility.
14951508
*/
1496-
if (IsAbortedTransactionBlockState()&& !IsA(cmd_node,SQLCmd))
1509+
ereport(log_replication_commands ?LOG :DEBUG1,
1510+
(errmsg("received replication command: %s",cmd_string)));
1511+
1512+
/*
1513+
* Disallow replication commands in aborted transaction blocks.
1514+
*/
1515+
if (IsAbortedTransactionBlockState())
14971516
ereport(ERROR,
14981517
(errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
14991518
errmsg("current transaction is aborted, "
@@ -1509,9 +1528,6 @@ exec_replication_command(const char *cmd_string)
15091528
initStringInfo(&reply_message);
15101529
initStringInfo(&tmpbuf);
15111530

1512-
/* Report to pgstat that this process is running */
1513-
pgstat_report_activity(STATE_RUNNING,NULL);
1514-
15151531
switch (cmd_node->type)
15161532
{
15171533
caseT_IdentifySystemCmd:
@@ -1561,17 +1577,6 @@ exec_replication_command(const char *cmd_string)
15611577
}
15621578
break;
15631579

1564-
caseT_SQLCmd:
1565-
if (MyDatabaseId==InvalidOid)
1566-
ereport(ERROR,
1567-
(errmsg("cannot execute SQL commands in WAL sender for physical replication")));
1568-
1569-
/* Report to pgstat that this process is now idle */
1570-
pgstat_report_activity(STATE_IDLE,NULL);
1571-
1572-
/* Tell the caller that this wasn't a WalSender command. */
1573-
return false;
1574-
15751580
default:
15761581
elog(ERROR,"unrecognized replication command node tag: %u",
15771582
cmd_node->type);
@@ -1586,6 +1591,7 @@ exec_replication_command(const char *cmd_string)
15861591

15871592
/* Report to pgstat that this process is now idle */
15881593
pgstat_report_activity(STATE_IDLE,NULL);
1594+
debug_query_string=NULL;
15891595

15901596
return true;
15911597
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp