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 1w5SOi-003GoX-1U for pgsql-general@arkaria.postgresql.org; Wed, 25 Mar 2026 17:52:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w5SOf-00FknP-30 for pgsql-general@arkaria.postgresql.org; Wed, 25 Mar 2026 17:52:02 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w5SOf-00FknA-08 for pgsql-general@lists.postgresql.org; Wed, 25 Mar 2026 17:52:02 +0000 Received: from fout-b4-smtp.messagingengine.com ([202.12.124.147]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1w5SOd-000000010Vw-1cEZ for pgsql-general@postgresql.org; Wed, 25 Mar 2026 17:52:00 +0000 Received: from phl-compute-02.internal (phl-compute-02.internal [10.202.2.42]) by mailfout.stl.internal (Postfix) with ESMTP id D62051D001E1; Wed, 25 Mar 2026 13:51:57 -0400 (EDT) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-02.internal (MEProxy); Wed, 25 Mar 2026 13:51:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm3; t=1774461117; x=1774547517; bh=vtqcAoOPwhCxF/eg6xCrj2nrX/lxOm2VryVBHSMyeiw=; b= Wfm/vBOPRoaAkp7truM/tV6vrpLISePgTg0xaID3NJJ4JirQSN4W4bQN6joVMDGJ HWPVWWfojlxMs0hKDNGg7OjDSeQtwUsc9Tf6hfHTlEbsJbt6DLo6sYQfr1RMFi6w PKWL4Fq92D1pZF4kVYndoyIGiFowtjoRT+h+6JiZfjoN1r/2qFG9gMwkDfePV4sR xZZ++zjG5q1c0uXAAjKAOKQbE+Br1c51DPvJebSNneazEQg/FZD5HHIt7AsIdsaY S6szoPGUNaBnvSCoS7vbh6AfFkG2UQhJfa5iZZMT1G6CbawzL8iE7Y6HJ+FpBxyB UPCgeHJaoYNXS0q6BdCKVQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; t=1774461117; x=1774547517; bh=v tqcAoOPwhCxF/eg6xCrj2nrX/lxOm2VryVBHSMyeiw=; b=duqAYTIq4/OqGoQ+b JtBaL7QXHml16OuVDqnumeeGE1R+RbgJbh69nJo8fhGtosvEJevL4fsYiV1i+Vtf 5bnSBLLNkmDA96jB4CYmX6P9MJIda1X3ZbChni7xUrKJgD3esKvp1j0nSWWCGBp6 OFATPZiq/eK2uTP/LhIlVeXyoXR/30xVf5fDnuDCT9OTmFuRLjO+uAOuqqBMYXqb a6SccbUNBSI9Z8K5U/cb76x89oF9DJc4ND9eUqisdMw+iM7cv/g6P/z92qp1nQgB istsV3HKiKmvRQ6LySMUFw7sM7yIWWkn/tqUli7fAE46AcnNbopyF+YebmlSJq96 9QW6A== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgdefvdehuddtucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhepkfffgggfuffvfhfhjggtgfesthekredttddvjeenucfhrhhomheptegurhhirghn ucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmqe enucggtffrrghtthgvrhhnpeelgeevkeekkeeuiefgtdevieeluefhfedufeetkeejffek jeeujeehgeehgeektdenucffohhmrghinhepphhoshhtghhrvghsqhhlrdhorhhgnecuve hluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghn rdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhdpnhgspghrtghpthhtohepvddpmhhoug gvpehsmhhtphhouhhtpdhrtghpthhtohepughurhhumhgurghrrgesghhmrghilhdrtgho mhdprhgtphhtthhopehpghhsqhhlqdhgvghnvghrrghlsehpohhsthhgrhgvshhqlhdroh hrgh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 25 Mar 2026 13:51:56 -0400 (EDT) Message-ID: <7f77d4cf-0570-464f-944d-25a56746fc61@aklaver.com> Date: Wed, 25 Mar 2026 10:51:56 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Where to store some session based info? To: Durumdara , Postgres General References: Content-Language: en-US From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 3/25/26 10:20 AM, Durumdara wrote: > Hello! > > Sometimes we have to use "Current User ID", and "User Name" in the > Triggers to make a log into a table. These values are based on our User, > not in the PSQL role. > > Now we use a temporary table to do this. > When the user logged into the application, we created a temporary table > with the same name (user_info) and structure. This holds the data (id, > name, machine info, ip address). > > In the trigger we try to find this table (in the LOCAL_TEMPORARY schema). > Then we read the row into a JSON record, and then into PLPGSQL variables. > Tables can exist with the same name, so this is the safest solution. > If the User  ID is invalid (none or empty) that means this is a > background operation, and then we don't need to log the changes. > > But maybe there is a better way to somehow store some session based data > and use it in the triggers. > Because if these selects are slow, the trigger is also slow. So when I > start an UPDATE command in a big table, maybe this slows down the whole > operation. > > Note: > A table with the PID key is not enough, because the PID is a repeated > value. > I logged it and in the Windows system there are many of the same values > (10001, 10004, etc.). > Ok, I can combine with session creation time. But for this I also need > to start a select in the pg_stat_activty table. > > So maybe you have an easier way to point to a record in a session. > Important: the PG servers are different, the lesser version is 11, and > we have only a Database Owner role. We can't configure the server. > > What is your opinion? Is there any way to get session based data? > As I read before, we can't set the session variables onfly. Maybe SET?: https://www.postgresql.org/docs/current/sql-set.html With LOCAL it is scoped to a transaction. Otherwise it persists for session unless a transaction is rolled back. As example: CREATE OR REPLACE FUNCTION public.session_test() RETURNS void LANGUAGE plpgsql AS $function$ DECLARE _test_var varchar := current_setting('test.session_var', 't'); BEGIN RAISE NOTICE 'Variable is %', _test_var; END; $function$ No variable set: test=# select session_test(); NOTICE: Variable is session_test -------------- (1 row) Variable set: test=# begin ; BEGIN test=*# set local test.session_var = 'test'; SET test=*# select session_test(); NOTICE: Variable is test session_test -------------- (1 row) > > Best regards > dd > > -- Adrian Klaver adrian.klaver@aklaver.com