From f81b8f198fde1f1ba1758d7e6f70eddb7b8bbf8e Mon Sep 17 00:00:00 2001 From: Tatsuo Ishii Date: Thu, 11 Oct 2018 17:52:57 +0900 Subject: [PATCH] Fix memory leak when query cache enabled in streaming replication mode + extended query case. If BEGIN is executed in extended protocol, allocated temp buffer for the command never discarded. This is because in streaming replication mode + extended query case, when handle_query_cache gets called at the command complete timing, the reference counter to query context is not 0, which in turn prevents from calling pool_discard_temp_query_cache(). In reality, we don't need to care about the query context here, so let's proceed in streaming replication mode + extended query case. Note that the memory leak case can be easily reproduced by "pgbench -M extended" command (standard TPC-B). --- src/query_cache/pool_memqcache.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/query_cache/pool_memqcache.c b/src/query_cache/pool_memqcache.c index 92fb921cf..d73d98ab7 100644 --- a/src/query_cache/pool_memqcache.c +++ b/src/query_cache/pool_memqcache.c @@ -1726,7 +1726,8 @@ pool_reset_memqcache_buffer(void) * if the query context is still under use, we cannot discard * temporary cache. */ - if (can_query_context_destroy(session_context->query_context)) + if ((SL_MODE && pool_is_doing_extended_query_message()) || + can_query_context_destroy(session_context->query_context)) { ereport(DEBUG1, (errmsg("memcache reset buffer"), -- 2.39.5