From c66cf2e63cefb65ef9b3d31a037b554e183c5e44 Mon Sep 17 00:00:00 2001 From: "Andrei V. Lepikhov" Date: Mon, 9 Mar 2026 13:41:55 +0100 Subject: [PATCH 1/2] Prevent debug_query_string from producing garbage in apply worker logs Initialize debug_query_string to NULL at apply worker startup, since it is not a regular backend and has no client query string. Also clear debug_query_string in execute_sql_command_error_cb after the errcontext call, which already includes the SQL statement, to prevent it from appearing a second time in the LOG output. --- src/spock_apply.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/spock_apply.c b/src/spock_apply.c index b93b3db9..fa86a74e 100644 --- a/src/spock_apply.c +++ b/src/spock_apply.c @@ -3259,6 +3259,12 @@ static void execute_sql_command_error_cb(void *arg) { errcontext("during execution of queued SQL statement: %s", (char *) arg); + /* + * The errcontext above already includes the SQL statement, so clear + * debug_query_string to prevent it from appearing a second time in + * the LOG output. + */ + debug_query_string = NULL; } /* @@ -3779,6 +3785,13 @@ spock_apply_main(Datum main_arg) Assert(MySpockWorker->worker_type == SPOCK_WORKER_APPLY); MyApplyWorker = &MySpockWorker->worker.apply; + /* + * The apply worker is not a regular backend and has no client query + * string. Initialize debug_query_string to NULL so that LOG reports + * do not print arbitrary memory contents. + */ + debug_query_string = NULL; + /* Setup synchronous commit according to the user's wishes */ SetConfigOption("synchronous_commit", spock_synchronous_commit ? "local" : "off", From aba416437df7418705d86dfc38efbd558a5ff483 Mon Sep 17 00:00:00 2001 From: "Andrei V. Lepikhov" Date: Mon, 9 Mar 2026 15:01:20 +0100 Subject: [PATCH 2/2] Fix stale local_tuple pointer in INSERT exception for missing relation When spock_read_insert returns NULL (relation not found), the exception log entry's local_tuple may still hold a pointer from a previous operation. Clear it before calling log_insert_exception to prevent get_tuple_origin from dereferencing freed memory. --- src/spock_apply.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/spock_apply.c b/src/spock_apply.c index fa86a74e..ae308ab7 100644 --- a/src/spock_apply.c +++ b/src/spock_apply.c @@ -1226,6 +1226,13 @@ handle_insert(StringInfo s) */ xact_had_exception = true; exception_command_counter++; + + /* + * Clear the local tuple pointer if it was left over from a + * previous operation. + */ + exception_log_ptr[my_exception_log_index].local_tuple = NULL; + log_insert_exception(true, "Spock can't find relation", NULL, NULL, NULL, "INSERT"); end_replication_step();