Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w5Z0w-003NRu-2b for pgpool-hackers@arkaria.postgresql.org; Thu, 26 Mar 2026 00:55:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w5Z0u-000IEX-0k for pgpool-hackers@arkaria.postgresql.org; Thu, 26 Mar 2026 00:55:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w5Z0t-000IEQ-2o for pgpool-hackers@lists.postgresql.org; Thu, 26 Mar 2026 00:55:56 +0000 Received: from meldrar.postgresql.org ([2a02:c0:301:0:ffff::31]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1w5Z0r-00000001BaP-0nlU for pgpool-hackers@lists.postgresql.org; Thu, 26 Mar 2026 00:55:56 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Content-Transfer-Encoding:Content-Type: Mime-Version:References:In-Reply-To:From:Subject:To:Message-Id:Date:Sender: Reply-To:Cc:Content-ID:Content-Description; bh=6Dq5ZHC54XatjJaVrR8G04Q0+DLkli9PVWl2vUYHync=; b=1TjXXs/jui3qmUHuIacyU9M1dU LDgwnaVGeLiOqK5iHJpB9r8GuyzPXMoieg9Z/mzN2yRikLe3+ZZpBm4CYrF+/RVMBIYHkOz3Pg/IQ RX7X7WQvWS5uHq6vpGNVDK5Uhk0w/zqtIXsokIRczMk9dyuCL6fnK9BQWKnsJF9eqg7NA+Rim3KED YpcZz8Q5BuEQLBhu5K3YXG+1+0qrSoaVh/Lqx2yKE2918p1Qq0XANunZRD2k2fiG5Aa6p/kSx8CQ0 pl3k9GY4Wh6T7of9F2jFyS6GAEsv7UdkmlC8UVM7XwHPiDXR7tlozua90bRYeG7spsqeW4hBT4DtQ aPItVlcA==; Received: from [2409:11:4120:300:ce3b:f06:9d3f:3b67] (helo=localhost) by meldrar.postgresql.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w5Z0n-006DKP-1U for pgpool-hackers@lists.postgresql.org; Thu, 26 Mar 2026 00:55:52 +0000 Date: Thu, 26 Mar 2026 09:55:41 +0900 (JST) Message-Id: <20260326.095541.1427711606336049810.ishii@postgresql.org> To: pgpool-hackers@lists.postgresql.org Subject: Re: pool_search_relcach does not consider session local cache case From: Tatsuo Ishii In-Reply-To: <20260325.184611.1088077863621666688.ishii@postgresql.org> References: <20260320.115354.1808938375091443061.ishii@postgresql.org> <20260325.184611.1088077863621666688.ishii@postgresql.org> X-Mailer: Mew version 6.8 on Emacs 29.3 Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 2409:11:4120:300:ce3b:f06:9d3f:3b67 (failed) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk >> While looking into https://github.com/pgpool/pgpool2/issues/154 >> I found a bug with pool_search_relcach. >> >> pool_search_relcache does not consider the session local cache case >> such as temp table search. It creates the search result in shared >> relation cache even if the relation cache is session local. As a >> result subsequent query against the session local cache is returned >> from the shared relation cache. This could cause wrong >> result. Example: >> >> create table t1(i int); >> select * from t1; -- info "t1 is not a temp table" is registered in shared cache >> drop table t1; >> create temp table t1(i int); >> select * from t1; -- shared cache tells that t1 is not temp table and it could be load balanced, >> -- it could access non existent table from standby nodes. >> >> Fix is, to not register a temp table to shared relation cache if it's >> a session local cache. >> >> Patch attached. > > Patch pushed to down to v4.4. A test is also added down to v4.5 > (v4.4's test script is not suitable to apply the same patch as master > - v4.5). Even with this commit I see this: I repeated the sequence above twice. Each cycle should produce the same result but actually it doen't. In the second cycle "select * from t1;" is executed. At this point t1 is an ordinary table and the select is expected to be forwarded to standby but actually it's not. select * from t1; -- expected to forward to standby psql:b.sql:15: NOTICE: DB node id: 0 statement: select * from t1; -- Problem! I think the reason is in pool_at_command_success(). /* * If the query was CREATE TEMP TABLE, discard temp table relcache * because we might have had persistent table relation cache which has * table name as the temp table. */ if (IsA(node, CreateStmt)) { CreateStmt *create_table_stmt = (CreateStmt *) node; if (create_table_stmt->relation->relpersistence == 't') discard_temp_table_relcache(); } So, when a temp table is created, local relation cache for temporary table is deleted. But when an ordinary table is created, it is not deleted. Thus the select hits the temp table cache and fowarded to primary. I will invest further how I can fix this. ============================================================================= -- the first cycle drop table t1; psql:b.sql:2: NOTICE: DB node id: 0 statement: drop table t1; psql:b.sql:2: ERROR: table "t1" does not exist create table t1(i int); -- create ordinary table psql:b.sql:3: NOTICE: DB node id: 0 statement: create table t1(i int); CREATE TABLE select pg_sleep(1); -- wait for 1 second so that standy gets replicated psql:b.sql:4: NOTICE: DB node id: 0 statement: select pg_sleep(1); pg_sleep ---------- (1 row) select * from t1; -- expected to forward to standby psql:b.sql:5: NOTICE: DB node id: 1 statement: select * from t1; i --- (0 rows) drop table t1; psql:b.sql:6: NOTICE: DB node id: 0 statement: drop table t1; DROP TABLE create temp table t1(i int); -- create temp table psql:b.sql:7: NOTICE: DB node id: 0 statement: create temp table t1(i int); CREATE TABLE select pg_sleep(1); -- wait for 1 second so that standy gets replicated psql:b.sql:8: NOTICE: DB node id: 0 statement: select pg_sleep(1); pg_sleep ---------- (1 row) select * from t1; -- expected to forward to primary psql:b.sql:9: NOTICE: DB node id: 0 statement: select * from t1; i --- (0 rows) -- the second cycle drop table t1; psql:b.sql:12: NOTICE: DB node id: 0 statement: drop table t1; DROP TABLE create table t1(i int); -- create ordinary table psql:b.sql:13: NOTICE: DB node id: 0 statement: create table t1(i int); CREATE TABLE select pg_sleep(1); -- wait for 1 second so that standy gets replicated psql:b.sql:14: NOTICE: DB node id: 0 statement: select pg_sleep(1); pg_sleep ---------- (1 row) select * from t1; -- expected to forward to standby psql:b.sql:15: NOTICE: DB node id: 0 statement: select * from t1; -- Problem! i --- (0 rows) drop table t1; psql:b.sql:16: NOTICE: DB node id: 0 statement: drop table t1; DROP TABLE create temp table t1(i int); -- create temp table psql:b.sql:17: NOTICE: DB node id: 0 statement: create temp table t1(i int); CREATE TABLE select pg_sleep(1); -- wait for 1 second so that standy gets replicated psql:b.sql:18: NOTICE: DB node id: 0 statement: select pg_sleep(1); pg_sleep ---------- (1 row) select * from t1; -- expected to forward to primary psql:b.sql:19: NOTICE: DB node id: 0 statement: select * from t1; i --- (0 rows) ============================================================================= Regards, -- Tatsuo Ishii SRA OSS K.K. English: http://www.sraoss.co.jp/index_en/ Japanese:http://www.sraoss.co.jp