From 0883832be1e7e41b9c7b8bbdf5533c0e61ea902d Mon Sep 17 00:00:00 2001 From: Tatsuo Ishii Date: Sun, 6 Sep 2020 19:52:56 +0900 Subject: [PATCH] Fix relcache query sometimes sent to other than primary. In streaming replication mode, relcache queries are supposed to be sent to the primary node. But actually they were not sent the primary node if primary node was not the master node. Typically this could happen when the primary is not node 0. --- src/utils/pool_relcache.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/utils/pool_relcache.c b/src/utils/pool_relcache.c index cb2ff5182..e27cbb6e5 100644 --- a/src/utils/pool_relcache.c +++ b/src/utils/pool_relcache.c @@ -5,7 +5,11 @@ * pgpool: a language independent connection pool server for PostgreSQL * written by Tatsuo Ishii * +<<<<<<< HEAD * Copyright (c) 2003-2017 PgPool Global Development Group +======= + * Copyright (c) 2003-2020 PgPool Global Development Group +>>>>>>> 6197ffec... Fix relcache query sometimes sent to other than primary. * * Permission to use, copy, modify, and distribute this software and * its documentation for any purpose and without fee is hereby @@ -103,6 +107,7 @@ void *pool_search_relcache(POOL_RELCACHE *relcache, POOL_CONNECTION_POOL *backen time_t now; void *result; ErrorContextCallback callback; + int node_id; local_session_id = pool_get_local_session_id(); if (local_session_id < 0) @@ -111,6 +116,15 @@ void *pool_search_relcache(POOL_RELCACHE *relcache, POOL_CONNECTION_POOL *backen /* Obtain database name */ dbname = MASTER_CONNECTION(backend)->sp->database; + /* + * If in streaming replication mode, prefer to send query to the + * primary node if it exists. + */ + if (STREAM && PRIMARY_NODE_ID >= 0) + node_id = PRIMARY_NODE_ID; + else + node_id = MASTER_NODE_ID; + now = time(NULL); /* Look for cache first */ @@ -152,7 +166,7 @@ void *pool_search_relcache(POOL_RELCACHE *relcache, POOL_CONNECTION_POOL *backen /* Not in cache. Check the system catalog */ snprintf(query, sizeof(query), relcache->sql, table); - per_node_statement_log(backend, MASTER_NODE_ID, query); + per_node_statement_log(backend, node_id, query); /* * Register a error context callback to throw proper context message -- 2.39.5