From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Thiemo Kellner <thiemo@gelassene-pferde.biz>
To: PostgreSQL General <pgsql-general@lists.postgresql.org>
Subject: Re: Possible trigger bug? function call argument literalised
Date: Sun, 3 Jan 2021 14:03:55 -0800
Message-ID: <8777d1f6-8a99-a997-41ea-a14d1dd9b433@aklaver.com> (raw)
In-Reply-To: <20210103224436.Horde.i8vtcy4uDPq20_POx_3SJmI@webmail.gelassene-pferde.biz>
References: <20210103180840.Horde.Fe4qNP3Uviz_9c6aPfxBTS2@webmail.gelassene-pferde.biz>
<2692292.1609695452@sss.pgh.pa.us>
<20210103184529.Horde.LO8-skyriHu3oMxD4rNG2WV@webmail.gelassene-pferde.biz>
<e5b3b5f5-d6ab-b1f1-6c8e-ee55b6b0159a@aklaver.com>
<20210103192741.Horde.RQ2ogTUGBJnpkIZYTeDRPO-@webmail.gelassene-pferde.biz>
<c2cd33a6-6cbf-649e-140e-4deed1783482@aklaver.com>
<20210103201914.Horde.Y3MZ07YKSmzeRto-vZPs3wF@webmail.gelassene-pferde.biz>
<853f01d0-09c1-fdb3-990a-976bffeff4de@aklaver.com>
<20210103224436.Horde.i8vtcy4uDPq20_POx_3SJmI@webmail.gelassene-pferde.biz>
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(<required_fields>)
>>
>> --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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: adrian.klaver@aklaver.com, thiemo@gelassene-pferde.biz
Subject: Re: Possible trigger bug? function call argument literalised
In-Reply-To: <8777d1f6-8a99-a997-41ea-a14d1dd9b433@aklaver.com>
References: <20210103180840.Horde.Fe4qNP3Uviz_9c6aPfxBTS2@webmail.gelassene-pferde.biz> <2692292.1609695452@sss.pgh.pa.us> <20210103184529.Horde.LO8-skyriHu3oMxD4rNG2WV@webmail.gelassene-pferde.biz> <e5b3b5f5-d6ab-b1f1-6c8e-ee55b6b0159a@aklaver.com> <20210103192741.Horde.RQ2ogTUGBJnpkIZYTeDRPO-@webmail.gelassene-pferde.biz> <c2cd33a6-6cbf-649e-140e-4deed1783482@aklaver.com> <20210103201914.Horde.Y3MZ07YKSmzeRto-vZPs3wF@webmail.gelassene-pferde.biz> <853f01d0-09c1-fdb3-990a-976bffeff4de@aklaver.com> <20210103224436.Horde.i8vtcy4uDPq20_POx_3SJmI@webmail.gelassene-pferde.biz> <8777d1f6-8a99-a997-41ea-a14d1dd9b433@aklaver.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox