Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1jEZUj-0002a6-0v for pgsql-general@arkaria.postgresql.org; Wed, 18 Mar 2020 14:16:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jEZUh-00047T-Hd for pgsql-general@arkaria.postgresql.org; Wed, 18 Mar 2020 14:16:27 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1jEZUh-00046l-4I for pgsql-general@lists.postgresql.org; Wed, 18 Mar 2020 14:16:27 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jEZUe-0006LR-Lo for pgsql-general@lists.postgresql.org; Wed, 18 Mar 2020 14:16:25 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id 02IEGNWV000746; Wed, 18 Mar 2020 10:16:23 -0400 From: Tom Lane To: Kostya cc: pgsql-general@lists.postgresql.org Subject: Re: SET LOCAL doesn't become undefined after transaction is commited In-reply-to: References: Comments: In-reply-to Kostya message dated "Wed, 18 Mar 2020 13:22:20 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <744.1584540983.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 18 Mar 2020 10:16:23 -0400 Message-ID: <745.1584540983@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Kostya writes: > However, once I run the following transaction > BEGIN READ WRITE > SET LOCAL session.my_tenant_id=3D"f00"; > SELECT * FROM someTable; > COMMIT; > Then > SELECT current_setting("session.my_tenant_id"); > will return an empty string "". Basically "session.my_tenant_id" becomes > defined. > Is this supposed to work like this or could this be a bug? > Is it possible to entirely undefine "session.my_tenant_id" after the > transaction was committed? Basically I would like to return to the initi= al > state of the PSQL connection. It is supposed to work like that. There is no provision for rolling back the existence of a GUC altogether, and if there were, it would break the actually intended use-case, namely GUCs created by dynamically loaded extensions. If an extension is loaded during a transaction, it won't disappear if the transaction is rolled back, so neither should its GUCs. The real problem here is that you're abusing a feature that was never meant to be used for user-defined variables. It does not have the right behavior in corner cases (as you're seeing here), it does not have the features you'd want (eg ability to declare the type of a variable), and it definitely doesn't have the scalability to lots of variables that I'd expect a user-focused feature to have. There's a fairly long-running thread about creating a feature that *is* meant for user variables: https://www.postgresql.org/message-id/flat/CAFj8pRDY+m9OOxfO10R7J0PAkCCauM= -TweaTrdsrsLGMb1VbEQ@mail.gmail.com I haven't checked on the state of that lately, but you might read up on it and help review/test it, or try to push the definition in the direction you need if it doesn't seem like quite the right thing. regards, tom lane