Need help : Weird referencial Integrity triggers - Mailing list pgsql-general

From Vilson farias
Subject Need help : Weird referencial Integrity triggers
Date
Msg-id 015901c02fb5$87880bc0$98a0a8c0@dti.digitro.com.br
Whole thread Raw
Responses Re: Need help : Weird referencial Integrity triggers  (Jan Wieck <janwieck@Yahoo.com>)
List pgsql-general
I've been changing some tables and I saw a strange behavior in pg_trigger.
Check out the commented code below :


I will create two tables and later I'll make a foreing key from
prog_tabelanumero to prog_gruponumero :

CREATE TABLE prog_gruponumero (
       cod_gruponumero      integer NOT NULL,
       descricao            varchar(30) NOT NULL,
       CONSTRAINT XPKprog_gruponumer
                            PRIMARY KEY (cod_gruponumero)
);


NOTICE:  CREATE TABLE/PRIMARY KEY will create implicit index
'xpkprog_gruponumer' for table 'prog_gruponumero'
CREATE

CREATE TABLE prog_tabelanumero (
       cod_gruponumero      integer NOT NULL,
       numero               varchar(25) NOT NULL,
              CONSTRAINT XPKprog_tabelanume
       PRIMARY KEY (cod_gruponumero, numero)
);

NOTICE:  CREATE TABLE/PRIMARY KEY will create implicit index
'xpkprog_tabelanume' for table 'prog_tabelanumero'
CREATE

ALTER TABLE prog_tabelanumero
       ADD CONSTRAINT RGrupoXNumero FOREIGN KEY (cod_gruponumero)
                             REFERENCES prog_gruponumero
                             ON DELETE CASCADE;

NOTICE:  ALTER TABLE ... ADD CONSTRAINT will create implicit trigger(s) for
FOREIGN KEY check(s)
CREATE



Now, if I select the corresponding triggers from pg_trigger, I'll find
three. Why 3? One for update, one for insert and one for delete ?

SELECT  * FROM pg_trigger;

 tgrelid |           tgname            | tgfoid | tgtype | tgenabled |
tgisconstraint | tgconstrname  | tgconstrrelid | tgdeferrable |
tginitdeferred | tgnargs | tgattr |
                                               tgargs
---------+-----------------------------+--------+--------+-----------+------
----------+---------------+---------------+--------------+----------------+-
--------+--------+------
----------------------------------------------------------------------------
-------------------------------
    1260 | pg_sync_pg_pwd              |     12 |     29 | t         | f
|               |             0 | f            | f              |       0 |
|
  430893 | RI_ConstraintTrigger_430906 |   1644 |     21 | t         | t
| rgrupoxnumero |        430880 | f            | f              |       6 |
| rgrup
oxnumero\000prog_tabelanumero\000prog_gruponumero\000UNSPECIFIED\000cod_grup
onumero\000cod_gruponumero\000
  430880 | RI_ConstraintTrigger_430908 |   1646 |      9 | t         | t
| rgrupoxnumero |        430893 | f            | f              |       6 |
| rgrup
oxnumero\000prog_tabelanumero\000prog_gruponumero\000UNSPECIFIED\000cod_grup
onumero\000cod_gruponumero\000
  430880 | RI_ConstraintTrigger_430910 |   1655 |     17 | t         | t
| rgrupoxnumero |        430893 | f            | f              |       6 |
| rgrup
oxnumero\000prog_tabelanumero\000prog_gruponumero\000UNSPECIFIED\000cod_grup
onumero\000cod_gruponumero\000
(4 rows)



Now the strange thing I said : If I make that alter table again, I'll get 6
triggers. There are no checking if triggers already exists. Any comments
about this?

ALTER TABLE prog_tabelanumero
       ADD CONSTRAINT RGrupoXNumero FOREIGN KEY (cod_gruponumero)
                             REFERENCES prog_gruponumero
                             ON DELETE CASCADE;

NOTICE:  ALTER TABLE ... ADD CONSTRAINT will create implicit trigger(s) for
FOREIGN KEY check(s)
CREATE

SELECT  * FROM pg_trigger;

tgrelid |           tgname            | tgfoid | tgtype | tgenabled |
tgisconstraint | tgconstrname  | tgconstrrelid | tgdeferrable |
tginitdeferred | tgnargs | tgattr |
                                               tgargs
---------+-----------------------------+--------+--------+-----------+------
----------+---------------+---------------+--------------+----------------+-
--------+--------+------
----------------------------------------------------------------------------
-------------------------------
    1260 | pg_sync_pg_pwd              |     12 |     29 | t         | f
  |               |             0 | f            | f              |       0
|        |
  430893 | RI_ConstraintTrigger_430906 |   1644 |     21 | t         | t
| rgrupoxnumero |        430880 | f            | f              |       6 |
| rgrup
oxnumero\000prog_tabelanumero\000prog_gruponumero\000UNSPECIFIED\000cod_grup
onumero\000cod_gruponumero\000
  430880 | RI_ConstraintTrigger_430908 |   1646 |      9 | t         | t
| rgrupoxnumero |        430893 | f            | f              |       6 |
| rgrup
oxnumero\000prog_tabelanumero\000prog_gruponumero\000UNSPECIFIED\000cod_grup
onumero\000cod_gruponumero\000
  430880 | RI_ConstraintTrigger_430910 |   1655 |     17 | t         | t
| rgrupoxnumero |        430893 | f            | f              |       6 |
| rgrup
oxnumero\000prog_tabelanumero\000prog_gruponumero\000UNSPECIFIED\000cod_grup
onumero\000cod_gruponumero\000
  430893 | RI_ConstraintTrigger_430944 |   1644 |     21 | t         | t
| rgrupoxnumero |        430880 | f            | f              |       6 |
| rgrup
oxnumero\000prog_tabelanumero\000prog_gruponumero\000UNSPECIFIED\000cod_grup
onumero\000cod_gruponumero\000
  430880 | RI_ConstraintTrigger_430946 |   1646 |      9 | t         | t
| rgrupoxnumero |        430893 | f            | f              |       6 |
| rgrup
oxnumero\000prog_tabelanumero\000prog_gruponumero\000UNSPECIFIED\000cod_grup
onumero\000cod_gruponumero\000
  430880 | RI_ConstraintTrigger_430948 |   1655 |     17 | t         | t
| rgrupoxnumero |        430893 | f            | f              |       6 |
| rgrup
oxnumero\000prog_tabelanumero\000prog_gruponumero\000UNSPECIFIED\000cod_grup
onumero\000cod_gruponumero\000
(7 rows)




pgsql-general by date:

Previous
From: Frank Joerdens
Date:
Subject: How does TOAST compare to other databases' mechanisms?
Next
From: Jim Mercer
Date:
Subject: Re: postgres via shell scripts