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 1qV2iG-00GAXk-Nz for pgsql-general@arkaria.postgresql.org; Sun, 13 Aug 2023 04:28:25 +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 1qV2iD-005n1V-Mn for pgsql-general@arkaria.postgresql.org; Sun, 13 Aug 2023 04:28:22 +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.94.2) (envelope-from ) id 1qV2iC-005n1N-32 for pgsql-general@lists.postgresql.org; Sun, 13 Aug 2023 04:28:21 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qV2i8-002Cfz-Jr for pgsql-general@lists.postgresql.org; Sun, 13 Aug 2023 04:28:19 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 2BB1A5C00E6; Sun, 13 Aug 2023 00:28:12 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Sun, 13 Aug 2023 00:28:12 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc: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:sender:subject:subject:to:to; s=fm2; t= 1691900892; x=1691987292; bh=556e8jgHf3LQZ082Nm7/5i7CQXwABpy2Ff7 pJT8yO8o=; b=pLRNEI0F/W9iawiRPDs5IQBhFvqY5hP/bHPYDHHsGQkHoiz9j5/ panhG+isJMzOVMyrAXDiizTD72HHbnoby8F8MVt+VnkEeQHf6xWhaQWCREZURMKv HSHtJPAyMEAFv2C51x4F+tX50WebmA/+AH8b1PE1epC4jrqjvgQZJsCiO7JaNk6b Ohu0915+hREAXEWVxE9Sxjh2RRS1OJfxJExhDZoI2Q8reaitDaG3BW1Egz80aNFI LJ07LK6x+gssrZuTaKgoJSidA3dvdfg3/wf2hYYUJIB5eoThmlA9ZkLMJhAbYxjN 6f6mgS80TIbxr0LEOU/NBznFbCSYOwQtt3A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t= 1691900892; x=1691987292; bh=556e8jgHf3LQZ082Nm7/5i7CQXwABpy2Ff7 pJT8yO8o=; b=cPxMG10NLoGS2AjlvbJsu7aZ767S1iectHwK3MtYGTNhAo0KLk+ 0apInUF4GtZhuFcBRMFKJaArbfdLmcoZ0NC7IMeRDqS+I4Gu1q2XU+akcpaRqG00 Hth0BV8xWs8adJKK1wMwcSz6kf4t7Per/iiWfPytXX4OyE1BKUpLAb+GMj9jBcjZ v46C7gbByQ5Tx+5v5+kdfQmVL6JIBMbs61ehxV4zd/e453eEVHqAGTT/7Ss92wv6 CoHXzHD1CxEYXbdTAlJYLa9Tix97o2yTH+bW3faxvxCheJ0Zhh292tMS23/uchil HMEruK143+bDl0dnTGQJR+NdVb7Ye8rwGDw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedruddtuddgkedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepkfffgggfuffvvehfhfgjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecuggftrfgrthhtvghrnhepjeegtdfhuefhtefgffdvheduleetgeejudevffek teegjeeiheeuudellefgffffnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpe hmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 13 Aug 2023 00:28:11 -0400 (EDT) Message-ID: <40a49cf0-c1ae-3c96-ad62-35fc44746774@aklaver.com> Date: Sat, 12 Aug 2023 21:28:10 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.14.0 Subject: Re: Dynamically accessing columns from a row type in a trigger To: "Rhys A.D. Stewart" Cc: pgsql-general@lists.postgresql.org References: <068f5479-5b58-6084-1141-fd320b722782@aklaver.com> Content-Language: en-US From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 8/12/23 20:21, Rhys A.D. Stewart wrote: > Hey Adrian, > > Thanks for your response. I don't think I explained myself clearly. > pk_col is not the column name. pk_col is a variable that holds the > name of a column. This is one trigger for several tables with varying > structures. So pk_col would be a column specific to the current > TG_TABLE_NAME when the trigger is invoked. This is why in my example I > had to use EXECUTE to get the value of the pk_col from OLD. So you are looking for a generic solution. > > Actually, now that I'm thinking about it, I don't really want to store > the value into a variable because the pk_col might be of any given > type. So ideally, I'd love a way to just get the value from OLD and > use it directly in another query. Something along the lines of: The issue then is determining what value to get from OLD. Is that generic e.g always the Primary Key or will it vary? > > `EXECUTE format('SELECT * FROM %1$I.sometable WHERE pk = $1', myschma) > USING OLD['pk_col']`. > > I reckon I may have to look at just generating a trigger function per > table, or maybe look into using TG_ARGS. > > > Rhys > Peace & Love | Live Long & Prosper > -- Adrian Klaver adrian.klaver@aklaver.com