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 1kwBTs-0007ZW-Bl for pgsql-general@arkaria.postgresql.org; Sun, 03 Jan 2021 22:04:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kwBTq-00068p-R4 for pgsql-general@arkaria.postgresql.org; Sun, 03 Jan 2021 22:04:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kwBTp-00068h-3f for pgsql-general@lists.postgresql.org; Sun, 03 Jan 2021 22:04:06 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kwBTk-0004ml-LO for pgsql-general@lists.postgresql.org; Sun, 03 Jan 2021 22:04:04 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 5AEC55C00AB; Sun, 3 Jan 2021 17:03:57 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Sun, 03 Jan 2021 17:03:57 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=1 mMCCycVNjvsc4lNS3vhJUZvL4xy4TfDGiNjkfk03Fw=; b=M5Bk/EJ0W9euHliWf cvlxrISolo5sZlS6rMRU7AQzdUvdwWQ0TpRmSWNjwWHq66MTCACNsCj17/S0ozl6 f9zJjNoH4O1Nm1zmEOpIPc+zgXvLw+TVYjq/T/HoL+CNpbjX1TVouWJ6iInq6WFa N0e+eDmZYgqALfBD5lDvC9XAGmeYACSYC0+Mc8gVCpI2/wN0jSW/kutC30D/xnGd mnrf84NQh3pca5DPhDyyyYcXmnLBsvX69Wr9SAjkEGpZHJqzcITV56wna6bs6O4w QwR1hxm894/PzGONVzjyXHQgwHmYkj+Qm4TU94fUT+oKRxIz1mOna8OrJc5rLQDD 8zo8g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=1mMCCycVNjvsc4lNS3vhJUZvL4xy4TfDGiNjkfk03 Fw=; b=ZCQ4h+8q9VKq2ke0zSS/yiImZFAKKRSu6e7BLV+SKrmzCVlzzhLzTWd54 OkfH3ZOTvPCbDd6iqUxxsjLYkLP9cEwrkhE88FwUt7SkrmpfeKaIBc8X1QoBosRs tSKUhFHsec2zY+TgzQ/HOc0FscGuZhd0C5wds6ZeYM8jN04et5nLFAIDjeIpwkeR atpiQneyk1oJTdG3m4JD6v3nCY6wfH/WoS3JJlb4VW5RF4jb19VxyffLncqNyj0x Cdzu5lZWs1k3CMJ6QZh9w9VbQmidikVphkcllkeVYkXqoM2DvS1bRWGb2Q99dN2G 0+FbxUH8ciLyaU0SNlpTiTgsbJaWA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrvdefuddgudehjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecuggftrfgrthhtvghrnhepveevfeevfeeugeffkeekhfegieeufffgteeujedv gffhueejjefhkeejleegtdegnecukfhppeejhedrudejvddrudegrddujeenucevlhhush htvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Received: from [192.168.1.4] (75-172-14-17.tukw.qwest.net [75.172.14.17]) by mail.messagingengine.com (Postfix) with ESMTPA id 1003D24005A; Sun, 3 Jan 2021 17:03:55 -0500 (EST) Subject: Re: Possible trigger bug? function call argument literalised To: Thiemo Kellner , PostgreSQL General References: <20210103180840.Horde.Fe4qNP3Uviz_9c6aPfxBTS2@webmail.gelassene-pferde.biz> <2692292.1609695452@sss.pgh.pa.us> <20210103184529.Horde.LO8-skyriHu3oMxD4rNG2WV@webmail.gelassene-pferde.biz> <20210103192741.Horde.RQ2ogTUGBJnpkIZYTeDRPO-@webmail.gelassene-pferde.biz> <20210103201914.Horde.Y3MZ07YKSmzeRto-vZPs3wF@webmail.gelassene-pferde.biz> <853f01d0-09c1-fdb3-990a-976bffeff4de@aklaver.com> <20210103224436.Horde.i8vtcy4uDPq20_POx_3SJmI@webmail.gelassene-pferde.biz> From: Adrian Klaver Message-ID: <8777d1f6-8a99-a997-41ea-a14d1dd9b433@aklaver.com> Date: Sun, 3 Jan 2021 14:03:55 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: <20210103224436.Horde.i8vtcy4uDPq20_POx_3SJmI@webmail.gelassene-pferde.biz> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 1/3/21 1:44 PM, Thiemo Kellner wrote: > >> So if I am following you are trying to set up a dynamic FK like >> process to INFORMATION_SCHEMA.ROUTINES on INSERT to CALCULATION_METHOD? > > Perfectly summarised. > >> If that is the case my previous suggestion of finding the >> CURRENT_SCHEMA inside the function would work? > > Actually, I did not follow it. I decided to try to go for TG_TABLE_SCHEMA. Yeah, forgot about that. > >> Personally I would create a script the built and populated >> CALCULATION_METHOD table as you added the functions to the database >> and schema. So: >> >> BEGIN; >> CREATE TABLE CALCULATION_METHOD ... >> >> CREATE the_schema.some_dd_fnc(); >> >> INSERT INTO CALCULATION_METHOD VALUES() >> >> --Where db_routine_name would be set to the function name. >> ... >> >> COMMIT; > > To me, it does not seem to have FK function. I figure, I also could > > insert into CALCULATION_METHOD (DB_ROUTINE_NAME) select ROUTINE_NAME > from INFORMATION_SCHEMA.ROUTINES; > > But again, I had no FK functionality and I would have only the routine > name. Remarks and other attributes would need to be maintained in extra > steps. So is the below still only going to fire on INSERT? If so it will not deal with functions that disappear after the INSERT, which in the end makes it similar to my suggestion:) The point being you are taking a snapshot in time and hoping that holds going forward. Of course when a calculation fails because the function is no longer there or has changed you will know a change has occurred. Is there some process to deal with the preceding? > > So, I implemented a non-general solution. > > create or replace function METHOD_CHECK() >   returns trigger >   language plpgsql >   volatile >   as > $body$ >     declare >         V_COUNT     smallint; >     begin >         select COUNT(*) into V_COUNT >           from INFORMATION_SCHEMA.ROUTINES >          where ROUTINE_SCHEMA   = TG_TABLE_SCHEMA >            and ROUTINE_NAME     = NEW.DB_ROUTINE_NAME; >         if V_COUNT != 1 then >             raise >               exception >               using >                 message = 'Routine "' || NEW.DB_ROUTINE_NAME || >                             '" could not be found in schema "' || >                             TG_TABLE_SCHEMA || '!', >                 hint = 'Install the routine beforehand.'; >         end if; >         return NEW; -- If NULL was returned, the row would get skipped! >     end; > $body$; > > > > -- Adrian Klaver adrian.klaver@aklaver.com