pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Function to set up variable inside it
6+ messages / 3 participants
[nested] [flat]

* Function to set up variable inside it
@ 2018-05-15 12:28 Łukasz Jarych <jaryszek@gmail.com>
  2018-05-15 12:55 ` Re: Function to set up variable inside it Pavel Stehule <pavel.stehule@gmail.com>
  2018-05-15 12:58 ` Re: Function to set up variable inside it Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 2 replies; 6+ messages in thread

From: Łukasz Jarych @ 2018-05-15 12:28 UTC (permalink / raw)
  To: pgsql-general

Hi Guys,

I am using postgres 10.3 (or 4?).
IT is possible to set up variable inside function?

Best,
Jacek

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Function to set up variable inside it
  2018-05-15 12:28 Function to set up variable inside it Łukasz Jarych <jaryszek@gmail.com>
@ 2018-05-15 12:55 ` Pavel Stehule <pavel.stehule@gmail.com>
  1 sibling, 0 replies; 6+ messages in thread

From: Pavel Stehule @ 2018-05-15 12:55 UTC (permalink / raw)
  To: Łukasz Jarych <jaryszek@gmail.com>; +Cc: pgsql-general

2018-05-15 14:28 GMT+02:00 Łukasz Jarych <jaryszek@gmail.com>:

> Hi Guys,
>
> I am using postgres 10.3 (or 4?).
> IT is possible to set up variable inside function?
>

I don't understand to the question. What do you think?

Regards

Pavel


>
> Best,
> Jacek
>

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Function to set up variable inside it
  2018-05-15 12:28 Function to set up variable inside it Łukasz Jarych <jaryszek@gmail.com>
@ 2018-05-15 12:58 ` Adrian Klaver <adrian.klaver@aklaver.com>
  2018-05-16 04:52   ` Re: Function to set up variable inside it Łukasz Jarych <jaryszek@gmail.com>
  1 sibling, 1 reply; 6+ messages in thread

From: Adrian Klaver @ 2018-05-15 12:58 UTC (permalink / raw)
  To: Łukasz Jarych <jaryszek@gmail.com>; pgsql-general

On 05/15/2018 05:28 AM, Łukasz Jarych wrote:
> Hi Guys,
> 
> I am using postgres 10.3 (or 4?).
> IT is possible to set up variable inside function?

Like this?:

https://www.postgresql.org/docs/10/static/plpgsql-declarations.html

> 
> Best,
> Jacek


-- 
Adrian Klaver
adrian.klaver@aklaver.com




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Function to set up variable inside it
  2018-05-15 12:28 Function to set up variable inside it Łukasz Jarych <jaryszek@gmail.com>
  2018-05-15 12:58 ` Re: Function to set up variable inside it Adrian Klaver <adrian.klaver@aklaver.com>
@ 2018-05-16 04:52   ` Łukasz Jarych <jaryszek@gmail.com>
  2018-05-16 07:07     ` Re: Function to set up variable inside it Pavel Stehule <pavel.stehule@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Łukasz Jarych @ 2018-05-16 04:52 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: pgsql-general

Hi Guys,

thank you for your help !

Hmm yes something like this.

I was wondering if is possible to pass variable into function:

CREATE FUNCTION change_trigger() RETURNS trigger AS $$

        BEGIN

                IF      TG_OP = 'INSERT'

                THEN

                        INSERT INTO logging.t_history (tabname,
schemaname, operation, new_val)

                                VALUES (TG_RELNAME, TG_TABLE_SCHEMA,
TG_OP, row_to_json(NEW));

                        RETURN NEW;

                ELSIF   TG_OP = 'UPDATE'

                THEN

                        INSERT INTO logging.t_history (tabname,
schemaname, operation, new_val, old_val)

                                VALUES (TG_RELNAME, TG_TABLE_SCHEMA, TG_OP,

                                        row_to_json(NEW), row_to_json(OLD));

                        RETURN NEW;

                ELSIF   TG_OP = 'DELETE'

                THEN

                        INSERT INTO logging.t_history (tabname,
schemaname, operation, old_val)

                                VALUES (TG_RELNAME, TG_TABLE_SCHEMA,
TG_OP, row_to_json(OLD));

                        RETURN OLD;

                END IF;

        END;

$$ LANGUAGE 'plpgsql' SECURITY DEFINER;

Best,
Jacek

2018-05-15 14:58 GMT+02:00 Adrian Klaver <adrian.klaver@aklaver.com>:

> On 05/15/2018 05:28 AM, Łukasz Jarych wrote:
>
>> Hi Guys,
>>
>> I am using postgres 10.3 (or 4?).
>> IT is possible to set up variable inside function?
>>
>
> Like this?:
>
> https://www.postgresql.org/docs/10/static/plpgsql-declarations.html
>
>
>> Best,
>> Jacek
>>
>
>
> --
> Adrian Klaver
> adrian.klaver@aklaver.com
>

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Function to set up variable inside it
  2018-05-15 12:28 Function to set up variable inside it Łukasz Jarych <jaryszek@gmail.com>
  2018-05-15 12:58 ` Re: Function to set up variable inside it Adrian Klaver <adrian.klaver@aklaver.com>
  2018-05-16 04:52   ` Re: Function to set up variable inside it Łukasz Jarych <jaryszek@gmail.com>
@ 2018-05-16 07:07     ` Pavel Stehule <pavel.stehule@gmail.com>
  2018-05-17 05:27       ` Re: Function to set up variable inside it Łukasz Jarych <jaryszek@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Pavel Stehule @ 2018-05-16 07:07 UTC (permalink / raw)
  To: Łukasz Jarych <jaryszek@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general

Hi

2018-05-16 6:52 GMT+02:00 Łukasz Jarych <jaryszek@gmail.com>:

> Hi Guys,
>
> thank you for your help !
>
> Hmm yes something like this.
>
> I was wondering if is possible to pass variable into function:
>
> CREATE FUNCTION change_trigger() RETURNS trigger AS $$
>
>         BEGIN
>
>                 IF      TG_OP = 'INSERT'
>
>                 THEN
>
>                         INSERT INTO logging.t_history (tabname, schemaname, operation, new_val)
>
>                                 VALUES (TG_RELNAME, TG_TABLE_SCHEMA, TG_OP, row_to_json(NEW));
>
>                         RETURN NEW;
>
>                 ELSIF   TG_OP = 'UPDATE'
>
>                 THEN
>
>                         INSERT INTO logging.t_history (tabname, schemaname, operation, new_val, old_val)
>
>                                 VALUES (TG_RELNAME, TG_TABLE_SCHEMA, TG_OP,
>
>                                         row_to_json(NEW), row_to_json(OLD));
>
>                         RETURN NEW;
>
>                 ELSIF   TG_OP = 'DELETE'
>
>                 THEN
>
>                         INSERT INTO logging.t_history (tabname, schemaname, operation, old_val)
>
>                                 VALUES (TG_RELNAME, TG_TABLE_SCHEMA, TG_OP, row_to_json(OLD));
>
>                         RETURN OLD;
>
>                 END IF;
>
>         END;
>
> $$ LANGUAGE 'plpgsql' SECURITY DEFINER;
>
> Best,
> Jacek
>

It is possible to pass values into functions -

postgres=# select fx(10);
NOTICE:  >>>10<<<
┌────┐
│ fx │
╞════╡
│    │
└────┘
(1 row)

postgres=# \sf fx
CREATE OR REPLACE FUNCTION public.fx(a integer)
 RETURNS void
 LANGUAGE plpgsql
AS $function$
begin
  raise notice '>>>%<<<', a;
end;
$function$

but it is not possible to pass parameters to trigger functions. You can
define trigger parameters when you define trigger, but these values must be
constant.

a) the request of trigger parametrization is usually signal of bad using of
triggers - probably you should to use a function, not trigger

b) there is workaround - but you should not to use it if it is not really
necessary

There are few implementations of session variables in postgres - you can
find via google. Then you can set session variable before SQL command, and
you can read this session variable inside trigger function.

Regards

Pavel


>
> 2018-05-15 14:58 GMT+02:00 Adrian Klaver <adrian.klaver@aklaver.com>:
>
>> On 05/15/2018 05:28 AM, Łukasz Jarych wrote:
>>
>>> Hi Guys,
>>>
>>> I am using postgres 10.3 (or 4?).
>>> IT is possible to set up variable inside function?
>>>
>>
>> Like this?:
>>
>> https://www.postgresql.org/docs/10/static/plpgsql-declarations.html
>>
>>
>>> Best,
>>> Jacek
>>>
>>
>>
>> --
>> Adrian Klaver
>> adrian.klaver@aklaver.com
>>
>
>

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Function to set up variable inside it
  2018-05-15 12:28 Function to set up variable inside it Łukasz Jarych <jaryszek@gmail.com>
  2018-05-15 12:58 ` Re: Function to set up variable inside it Adrian Klaver <adrian.klaver@aklaver.com>
  2018-05-16 04:52   ` Re: Function to set up variable inside it Łukasz Jarych <jaryszek@gmail.com>
  2018-05-16 07:07     ` Re: Function to set up variable inside it Pavel Stehule <pavel.stehule@gmail.com>
@ 2018-05-17 05:27       ` Łukasz Jarych <jaryszek@gmail.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Łukasz Jarych @ 2018-05-17 05:27 UTC (permalink / raw)
  To: Pavel Stehule <pavel.stehule@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general

Thank you Pavel!  I wilk set Up variable before  function and use it Inside function , best jacek

Pobierz aplikację Outlook dla systemu iOS<https://aka.ms/o0ukef;
________________________________
From: Pavel Stehule <pavel.stehule@gmail.com>
Sent: Wednesday, May 16, 2018 9:07:15 AM
To: Łukasz Jarych
Cc: Adrian Klaver; pgsql-general@postgresql.org >> PG-General Mailing List
Subject: Re: Function to set up variable inside it

Hi

2018-05-16 6:52 GMT+02:00 Łukasz Jarych <jaryszek@gmail.com<mailto:jaryszek@gmail.com>>:
Hi Guys,

thank you for your help !

Hmm yes something like this.

I was wondering if is possible to pass variable into function:


CREATE FUNCTION change_trigger() RETURNS trigger AS $$

        BEGIN

                IF      TG_OP = 'INSERT'

                THEN

                        INSERT INTO logging.t_history (tabname, schemaname, operation, new_val)

                                VALUES (TG_RELNAME, TG_TABLE_SCHEMA, TG_OP, row_to_json(NEW));

                        RETURN NEW;

                ELSIF   TG_OP = 'UPDATE'

                THEN

                        INSERT INTO logging.t_history (tabname, schemaname, operation, new_val, old_val)

                                VALUES (TG_RELNAME, TG_TABLE_SCHEMA, TG_OP,

                                        row_to_json(NEW), row_to_json(OLD));

                        RETURN NEW;

                ELSIF   TG_OP = 'DELETE'

                THEN

                        INSERT INTO logging.t_history (tabname, schemaname, operation, old_val)

                                VALUES (TG_RELNAME, TG_TABLE_SCHEMA, TG_OP, row_to_json(OLD));

                        RETURN OLD;

                END IF;

        END;

$$ LANGUAGE 'plpgsql' SECURITY DEFINER;

Best,
Jacek

It is possible to pass values into functions -

postgres=# select fx(10);
NOTICE:  >>>10<<<
┌────┐
│ fx │
╞════╡
│    │
└────┘
(1 row)

postgres=# \sf fx
CREATE OR REPLACE FUNCTION public.fx(a integer)
 RETURNS void
 LANGUAGE plpgsql
AS $function$
begin
  raise notice '>>>%<<<', a;
end;
$function$

but it is not possible to pass parameters to trigger functions. You can define trigger parameters when you define trigger, but these values must be constant.

a) the request of trigger parametrization is usually signal of bad using of triggers - probably you should to use a function, not trigger

b) there is workaround - but you should not to use it if it is not really necessary

There are few implementations of session variables in postgres - you can find via google. Then you can set session variable before SQL command, and you can read this session variable inside trigger function.

Regards

Pavel


2018-05-15 14:58 GMT+02:00 Adrian Klaver <adrian.klaver@aklaver.com<mailto:adrian.klaver@aklaver.com>>:
On 05/15/2018 05:28 AM, Łukasz Jarych wrote:
Hi Guys,

I am using postgres 10.3 (or 4?).
IT is possible to set up variable inside function?

Like this?:

https://www.postgresql.org/docs/10/static/plpgsql-declarations.html


Best,
Jacek


--
Adrian Klaver
adrian.klaver@aklaver.com<mailto:adrian.klaver@aklaver.com>




^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2018-05-17 05:27 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-05-15 12:28 Function to set up variable inside it Łukasz Jarych <jaryszek@gmail.com>
2018-05-15 12:55 ` Pavel Stehule <pavel.stehule@gmail.com>
2018-05-15 12:58 ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-05-16 04:52   ` Łukasz Jarych <jaryszek@gmail.com>
2018-05-16 07:07     ` Pavel Stehule <pavel.stehule@gmail.com>
2018-05-17 05:27       ` Łukasz Jarych <jaryszek@gmail.com>

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