Re: Firing trigger if only

Поиск
Список
Период
Сортировка
От Fabrízio de Royes Mello
Тема Re: Firing trigger if only
Дата
Msg-id CAFcNs+oe2Coun6BU0a9y3aW0JhwSS2Y1tHOXfU1o-wN+1_sEEw@mail.gmail.com
обсуждение исходный текст
Ответ на Firing trigger if only  (Gabriel <yu1nis@gmail.com>)
Ответы Re: Firing trigger if only  (Gabriel <yu1nis@gmail.com>)
Список pgsql-hackers

On Thu, Apr 3, 2014 at 8:50 AM, Gabriel <yu1nis@gmail.com> wrote:
Good afternoon all.I have some problem with triggers on PostgreSQL 8.4.I have
a trigger on specific table(articles) that fires on update statement:

CREATE OR REPLACE FUNCTION trigger_articles_update()
  RETURNS trigger AS
$BODY$BEGIN
      INSERT INTO
article_change(article_id,change_type)VALUES(OLD.article_id,2);
   RETURN NULL;
END$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;
ALTER FUNCTION trigger_articles_update() OWNER TO postgres;

I have 2 different applications that performs update on table
articles(written in Delphi and using ZeosDB). My problem is that I want
trigger to fire only when performing update with first application, but not
with second.I know that triggers supposed to fire on every change on table,
but this is a specific problem that I have.Any hint appreciated. 8)


Since 9.0 version of PostgreSQL you can set 'application_name' in connection [1] and test it in your trigger using a query like:

regress=# SELECT application_name FROM pg_stat_activity WHERE pid = pg_backend_pid();
 application_name
------------------
 psql
(1 registro)

I strongly recommend you to upgrade your 8.4 because the EOL [2] is July 2014.

But if an upgrade isn't an option for now then you can use the old "custom_variable_classes" to set your application, i.e.:

1) Add to your postgresql.conf:

custom_variable_classes = 'foo'
foo.application_name = 'undefined'

2) Reload your PostgreSQL

3) You can use the following functions to get/set the 'foo.application_name' custom variable:

-- get
SELECT current_setting('foo.application_name');

-- set
SELECT set_config('foo.application_name', 'myapp');

4) Now you can use this functions do register the name of your application an use it in your trigger.

Regards,
[3] http://www.postgresql.org/docs/8.4/static/runtime-config-custom.html
--
Fabrízio de Royes Mello
Consultoria/Coaching PostgreSQL

В списке pgsql-hackers по дате отправления:

Предыдущее
От: Stephen Frost
Дата:
Сообщение: Re: B-Tree support function number 3 (strxfrm() optimization)
Следующее
От: Stephen Frost
Дата:
Сообщение: Re: B-Tree support function number 3 (strxfrm() optimization)