Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hkN0H-0007sO-T6 for pgsql-admin@arkaria.postgresql.org; Mon, 08 Jul 2019 06:19:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hkN0G-0003ST-Lx for pgsql-admin@arkaria.postgresql.org; Mon, 08 Jul 2019 06:19:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hkN0G-0003SM-Cw for pgsql-admin@lists.postgresql.org; Mon, 08 Jul 2019 06:19:56 +0000 Received: from hermes.gatewaynet.com ([193.92.121.98]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hkN0D-0004gy-R2 for pgsql-admin@lists.postgresql.org; Mon, 08 Jul 2019 06:19:55 +0000 Received: from smadev.internal.net (pc131.internal.net [10.9.200.131]) by hermes.gatewaynet.com (8.15.2/8.15.2/Debian-8) with ESMTPS id x686JigW016967 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 8 Jul 2019 09:19:49 +0300 Received: from smadev.internal.net (smadev [10.9.200.131]) by smadev.internal.net (8.15.2/8.15.2) with ESMTP id x686Ji1B024537; Mon, 8 Jul 2019 09:19:44 +0300 (EEST) (envelope-from achill@matrix.gatewaynet.com) Subject: Re: pgsql 8.3 : force FK (and consequently ON DELETE CASCADE) to be run under session_replication_role TO 'replica' To: Peter Eisentraut , pgsql-admin@lists.postgresql.org References: From: Achilleas Mantzios Message-ID: <9773fc24-ae0f-ed5d-f2cb-feb99c80beec@matrix.gatewaynet.com> Date: Mon, 8 Jul 2019 09:19:44 +0300 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:60.0) Gecko/20100101 Thunderbird/60.3.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Virus-Scanned: clamav-milter 0.100.3 at hermes X-Virus-Status: Clean X-Spam-Status: No, score=-24.9 required=5.0 tests=ALL_TRUSTED,DYN_HAM_COORD, DYN_HAM_IT,DYN_HAM_NAUTICAL,DYN_SPAM_SUBJ,DYN_SPAM_WATCHES8,TW_PG autolearn=unavailable autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on hermes.gatewaynet.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 5/7/19 7:42 μ.μ., Peter Eisentraut wrote: > On 2019-07-05 12:17, Achilleas Mantzios wrote: >> we run some replication statements code under with session_replication_role TO 'replica' , >> however this disables FK constraints and consequently some important ON DELETE CASCADE that must be run. >> >> I tested this and this works : >> psql -qt -c 'SELECT '\''ALTER TABLE mariner ENABLE ALWAYS TRIGGER "'\''|| tgname||'\''";'\'' from pg_trigger where tgconstrname='\''personal_email_sender_marinerid_fkey'\'' and tgisconstraint and >> tgrelid='\''mariner'\''::regclass' | psql -f - >> >> and then test with: >> BEGIN ; >> set session_replication_role TO 'replica'; >> DELETE FROM mariner where id = 23700; >> SELECT * from personal_email_sender where marinerid = 23700; >>  email | marinerid >> -------+----------- >> (0 rows) >> >> ^^^ and verify that the ON DELETE CASCADE is run. >> >> So, besides the obvious comment, that one should not mess with system triggers, do you see any potential gotcha with this particular one? > I think this is generally OK. > > But if you are doing this, you probably have a replication system that > is running under the replica session role. The question is then whether > that replication system is OK with it. If you create replication sets > that contain parts of foreign key relationships and not others then you > might run into constraint violations. Thank you Peter. This is based on DBMIrror, I am running a heavily changed version of it for over 15 years, the FK aspect was one of the initial concerns dealt by the code. The whole idea behind it was to satisfy FK dependencies in a minimalistic manner : include only those FK rows that are necessary. > -- Achilleas Mantzios IT DEV Lead IT DEPT Dynacom Tankers Mgmt