Fix relcache query sometimes sent to other than primary.
authorTatsuo Ishii <ishii@sraoss.co.jp>
Sun, 6 Sep 2020 10:52:56 +0000 (19:52 +0900)
committerTatsuo Ishii <ishii@sraoss.co.jp>
Sun, 6 Sep 2020 11:43:56 +0000 (20:43 +0900)
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

index cb2ff51828c8d71449ad68f6641bfc7f7812a0de..e27cbb6e5fc9113e5bea7cc18567330529ede115 100644 (file)
@@ -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