public inbox for [email protected]  
help / color / mirror / Atom feed
From: Tatsuo Ishii <[email protected]>
To: [email protected]
Cc: [email protected]
Subject: Re: Proposal: recent access based routing for primary-replica setups
Date: Thu, 08 Jan 2026 11:19:11 +0900 (JST)
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>
	<CACeKOO13m3A7bXTePaEw33iOdkWKXBPiNYzb+c6g40zZb_fX8Q@mail.gmail.com>
	<[email protected]>

Hi Nadav,

I have just noticed that check_replication_time_lag_with_cmd() can be
simplified by using StringInfo functions. Patch attached. What do you
think?

Best regards,
--
Tatsuo Ishii
SRA OSS K.K.
English: http://www.sraoss.co.jp/index_en/
Japanese:http://www.sraoss.co.jp


Attachments:

  [application/octet-stream] refactor_check_replication_time_lag_with_cmd.patch (2.3K, 2-refactor_check_replication_time_lag_with_cmd.patch)
  download | inline diff:
diff --git a/src/streaming_replication/pool_worker_child.c b/src/streaming_replication/pool_worker_child.c
index ee4796437..311b63865 100644
--- a/src/streaming_replication/pool_worker_child.c
+++ b/src/streaming_replication/pool_worker_child.c
@@ -692,7 +692,6 @@ check_replication_time_lag_with_cmd(void)
 	char	   *temp_token;
 	char	   *endptr;
 	char	   *ident;
-	const char *base_command;
 	double		delay_ms;
 	uint64		delay;
 	uint64		delay_threshold_by_time;
@@ -700,8 +699,6 @@ check_replication_time_lag_with_cmd(void)
 	int			primary_node_id;
 	int			save_errno;
 	int			i;
-	size_t		total_len;
-	size_t		current_len;
 	BackendInfo *bkinfo;
 	ErrorContextCallback callback;
 	int			pipefd[2] = {-1, -1};
@@ -712,6 +709,7 @@ check_replication_time_lag_with_cmd(void)
 	ssize_t		bytes_read;
 	int			status;
 	int			num_replicas;
+	StringInfoData strbuf;
 
 	if (NUM_BACKENDS <= 1)
 	{
@@ -760,46 +758,19 @@ check_replication_time_lag_with_cmd(void)
 	/* Execute command as current process user */
 	PG_TRY();
 	{
-		base_command = pool_config->replication_delay_source_cmd;
-		total_len = strlen(base_command) + 1;	/* +1 for NUL */
-
+		initStringInfo(&strbuf);
+		appendStringInfoString(&strbuf,
+							   pool_config->replication_delay_source_cmd);
 		/* Build command with replica-only arguments (omit primary) */
-
-		/*
-		 * Calculate total command length including space-separated replica
-		 * identifiers
-		 */
-		for (i = 0; i < NUM_BACKENDS; i++)
-		{
-			if (i == primary_node_id)
-				continue;		/* Skip primary node */
-
-			ident = build_instance_identifier_for_node(i);
-
-			total_len += 1 /* space */ + strlen(ident);
-			pfree(ident);
-		}
-
-		command = palloc(total_len);
-		strlcpy(command, base_command, total_len);
-
-		/* Append replica identifiers */
-		current_len = strlen(command);
-
 		for (i = 0; i < NUM_BACKENDS; i++)
 		{
 			if (i == primary_node_id)
 				continue;		/* Skip primary node */
-
 			ident = build_instance_identifier_for_node(i);
-
-			/* Append space and identifier */
-			snprintf(command + current_len, total_len - current_len, " %s", ident);
-			current_len += strlen(command + current_len);
-
+			appendStringInfo(&strbuf, " %s", ident);
 			pfree(ident);
 		}
-
+		command = strbuf.data;
 		ereport(DEBUG1,
 				(errmsg("executing replication delay command: %s", command)));
 


reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected]
  Subject: Re: Proposal: recent access based routing for primary-replica setups
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox