Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kNdIY-0003Sc-BU for pgsql-general@arkaria.postgresql.org; Wed, 30 Sep 2020 14:41:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kNdIX-0005EQ-Ae for pgsql-general@arkaria.postgresql.org; Wed, 30 Sep 2020 14:41:37 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kNdIX-0005EJ-2t for pgsql-general@lists.postgresql.org; Wed, 30 Sep 2020 14:41:37 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kNdIV-00031d-3T for pgsql-general@lists.postgresql.org; Wed, 30 Sep 2020 14:41:36 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 08UEfTOR841735; Wed, 30 Sep 2020 10:41:29 -0400 From: Tom Lane To: Michael Lewis cc: v.brusa@joinsnc.com, PostgreSQL General Subject: Re: temp table same name real table In-reply-to: References: <30bcf5f9-239c-ced7-1518-1c4709c0e697@joinsnc.com> Comments: In-reply-to Michael Lewis message dated "Wed, 30 Sep 2020 08:34:48 -0600" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <841733.1601476889.1@sss.pgh.pa.us> Date: Wed, 30 Sep 2020 10:41:29 -0400 Message-ID: <841734.1601476889@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Michael Lewis writes: > IMO, you are asking for strange behavior when you overload a table name. > I expect that one of the people who work down deep down in the code will > say it is a caching thing that saves system table lookups. Yeah, I think that on the second call, plpgsql caches a query plan that references the permanent table, and then it sees no reason to reconsider that plan on the third call. (Change of search_path is one of the conditions that would prompt throwing away the cached plan, which fits that part of the report. But creating a temp table that happens to shadow a real table is not one.) If you really really need to do this, I'd counsel using EXECUTE to ensure no caching happens. But I concur with Michael that it's fundamentally a bad idea. regards, tom lane