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.94.2) (envelope-from ) id 1sufRb-008fhI-Cu for pgsql-general@arkaria.postgresql.org; Sat, 28 Sep 2024 21:57:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sufRa-00DuBq-8V for pgsql-general@arkaria.postgresql.org; Sat, 28 Sep 2024 21:57:38 +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.94.2) (envelope-from ) id 1sufRY-00DuBT-O4 for pgsql-general@lists.postgresql.org; Sat, 28 Sep 2024 21:57:37 +0000 Received: from fout-a4-smtp.messagingengine.com ([103.168.172.147]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sufRR-001XRi-Ky for pgsql-general@lists.postgresql.org; Sat, 28 Sep 2024 21:57:35 +0000 Received: from phl-compute-01.internal (phl-compute-01.phl.internal [10.202.2.41]) by mailfout.phl.internal (Postfix) with ESMTP id A339E13801CF; Sat, 28 Sep 2024 17:57:27 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-01.internal (MEProxy); Sat, 28 Sep 2024 17:57:27 -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=fm1; t=1727560647; x=1727647047; bh=0QJwienNx93T6pQRIUqaRFZll4JysEvQFYN6DFQCt+8=; b= anC1yGLzUZU2MOmXYouLxiywixqVdcgJLN3C5B0yVFtR20zQ2PNsQflTjwJjrlg2 S5tc4B5gVUMloyU/OCOGWwQnRkVkHbUeJofjngAKiuZYq/oCQW+5BcDiwZYCDsZp 6KJ0V7L5iSCWqh+tvrGgRFVrt7JpK9MGrUbdE77aJFM3b2+ucXvUBbqztIPBGYKv fXwQkvKtPHgzECey4meoLS8jey9AVZESTVwkO5DAYnU+CWaU0L7rJ78FpaNiH3Ja 4qxYigGAF+RMpj1N6E7io7/BVNIDVxOa3G0If5u6NtDs1+StRDqYiYP4eRnaTKtU FT1ulpWdBVz79vk/D1njmQ== 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-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1727560647; x= 1727647047; bh=0QJwienNx93T6pQRIUqaRFZll4JysEvQFYN6DFQCt+8=; b=g iTqnoRN3TqvuMVHkgdc7SzzaDUO6SMpJL3VxRVOoooOamgCfg7GhJKxSAUbdBmkt HsLaMk2RpFuGlTEYou9aamgUR5cEbdrLZ5HVq5sWzvnWQOO4247+L10dcnzZ+2nO 1zAUG7+7drWMn5zfafij2ytZt9hhyekU8WQtIiEBwRdUenewwk/JPekB7ZXcEM3N c3ALd7fQxJYNJYXrdAnCjMZV0fe3no/9m/b4Ak680QpGkbklWuzFzo/yXet3/YIS UdwdzIPoFuzcl38h1xn7YNrfQnIONHBabKSIij7F807JcBbSKsrY8UPb2ddhWfjd NuFYNq68vcH3O05aMbSMg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduvddgtdehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefkffggfg fuvfhfhfgjtgfgsehtkeertddtvdejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghr uceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecuggftrfgrth htvghrnhepleegveekkeekueeigfdtveeileeuhfefudefteekjeffkeejueejheegheeg kedtnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghenucevlhhushhtvghruf hiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghr segrkhhlrghvvghrrdgtohhmpdhnsggprhgtphhtthhopedvpdhmohguvgepshhmthhpoh huthdprhgtphhtthhopehlvggrrhhnvghruggrthgrsggrshgvleelsehgmhgrihhlrdgt ohhmpdhrtghpthhtohepphhgshhqlhdqghgvnhgvrhgrlheslhhishhtshdrphhoshhtgh hrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 28 Sep 2024 17:57:26 -0400 (EDT) Message-ID: Date: Sat, 28 Sep 2024 14:57:25 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Context variable in application and trigger code To: yudhi s , pgsql-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 9/28/24 14:37, yudhi s wrote: > Hi, > We have a trigger function called from a trigger which executes before > inserting rows in the table (say TAB1). This trigger function does some > conversion of code to description and persists the description in the > table in respective columns. We want to keep this trigger as light as > possible as there will be 100's million rows inserted into this table > from multiple input sources and the conversion should only happen  the > inserts which happen from a specific input data stream. > > There are 4-5 different input sources which will ingest data into this > table (some are using file based processing framework and some are using > other streaming technology). Say for example we want this description to > only be fetched for input source - 1, but not others. We don't have any > such column persisted in the table which can be checked for the input > data stream such as this code to describe decoding can be made conditional. > > Are there any techniques possible in which we can set some parameter or > session context variable in application code level to determine the > input data source, which can then be checked within the trigger function > code at the very first and thus will avoid querying the "CODE" table > every time the trigger executes? There is: https://www.postgresql.org/docs/16/runtime-config-logging.html#GUC-APPLICATION-NAME Though if you have multiple inputs happening concurrently I am not sure how you would sort out which is supplying the data for a given trigger instance. Also, by this time you are basically done anyway so I don't see how would save enough time. Seems to me this calls for either doing the conversion in the application or include a throw way field in the stream data that indicates it is coming from the stream or stream to an intake table and then convert when transferring to final table. > > > *Existing sample trigger code:-* > > CREATE OR REPLACE FUNCTION code_to_desc( ) > RETURNS trigger > LANGUAGE plpgsql > AS $function$ > declare code_description code.code%TYPE; >  begin >      select currency_description_text into code_description >      from code >     where code = new.code_input; > >   IF FOUND THEN NEW.code_input := code_description; > END IF; > >     return NEW; >  end; > $function$; > > Regards > Yudhi -- Adrian Klaver adrian.klaver@aklaver.com