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 1j3oNo-000133-Bs for pgsql-hackers@arkaria.postgresql.org; Mon, 17 Feb 2020 21:56:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j3oNl-0003jf-8h for pgsql-hackers@arkaria.postgresql.org; Mon, 17 Feb 2020 21:56:49 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j3oNk-0003jY-MP for pgsql-hackers@lists.postgresql.org; Mon, 17 Feb 2020 21:56:49 +0000 Received: from mail-qt1-x843.google.com ([2607:f8b0:4864:20::843]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1j3oNh-0001Z3-KI for pgsql-hackers@lists.postgresql.org; Mon, 17 Feb 2020 21:56:47 +0000 Received: by mail-qt1-x843.google.com with SMTP id d18so13124274qtj.10 for ; Mon, 17 Feb 2020 13:56:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding:user-agent; bh=Z4eDcyJ3C/LQWCE9OMTu7jqESDgofFhdvRdYOulZoCQ=; b=Mn7c9587COfgt/a0VMea+24VrlT4dNdWCpBGQ+g7UQMylI/pVejXiCsNaxp6Oa/0vT YfdibvsmxTA+WFHOVQrUpKjTIVSRuXhMApGZhBSXnt7kT5nMAK2YeQ5eSG1bKLfdBCZc CIwOTK/dsJCgM1PbMjKOmKIu6K9/OmoMujEe8bOPYKB2HFgXIMY8RUO85Z1Xn58c3Qa8 GChuPwcGcS6aC3lJSCrp5AQ/CF1ZPTWMvD/I6tWM2+DhIrOl7y5y1DvRXdS06AMXSvbg QFYPLt1PpEPljLjffaDNp9u1qaY13jtjrooFjGjuSwl0q/ScCQWR6HhQ+86mxvCkzm38 Gjxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:content-transfer-encoding:user-agent; bh=Z4eDcyJ3C/LQWCE9OMTu7jqESDgofFhdvRdYOulZoCQ=; b=qUAwxhmrWcNaOmpHbrR3T5dGWCjj9XeF85k3yHWp8CMJknBC293g4lNArO25SnaooZ tgcZrsU6xAoNe0cD0MMcOXbQ/lKZKoyzGtpVLNkiWN4WAy4iOzf5KHc0keb3Yq5QhF6C l9exe7KS3+u1k63PwsEmSmTuqfoDFV8Qfddmo4ze9fr10NC378WjwRQzdQGy2R1M1+Jt HNpgvsxOw2RIdP7h5Z15bKhdCnAyL/A7ol4xXqiPelFk+6iGYiFECbDDvBibbdP80jVd X7+1/x3HP/OU5wKh99TycmhefXRXxKGd+CJ6WAy0XtF7OaWbohLUJL50o6tS6KyKY5JH Fh8g== X-Gm-Message-State: APjAAAXaozjhjhRt84D5Ppou8UwLYbAsKwkxRwRA9kH0mpXx9L5o6ipt DNAmZuGvLsSSXCqxaxavVY2Zeg== X-Google-Smtp-Source: APXvYqy6uUbIxezdInmXRBQHT+riD+V9vniuIas/sS9Kr5MVBVOX32V+3Gg7IUoJo8shtxWRx/xJOg== X-Received: by 2002:ac8:65d4:: with SMTP id t20mr15025379qto.6.1581976604508; Mon, 17 Feb 2020 13:56:44 -0800 (PST) Received: from nimloth.alvh.no-ip.org ([201.186.208.119]) by smtp.gmail.com with ESMTPSA id n7sm879621qkk.41.2020.02.17.13.56.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Feb 2020 13:56:43 -0800 (PST) Received: by nimloth.alvh.no-ip.org (Postfix, from userid 1000) id A8AB8300760; Mon, 17 Feb 2020 18:56:41 -0300 (-03) Date: Mon, 17 Feb 2020 18:56:41 -0300 From: Alvaro Herrera To: Pg Hackers Cc: Amit Langote Subject: pg_trigger.tgparentid Message-ID: <20200217215641.GA29784@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="9amGYk9869ThD9tj" Content-Disposition: inline Content-Transfer-Encoding: 8bit User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --9amGYk9869ThD9tj Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit As mentioned in https://postgr.es/m/20191231194759.GA24692@alvherre.pgsql I propose to add a new column to pg_trigger, which allows us to remove a pg_depend scan when cloning triggers when adding/attaching partitions. (It's not that I think the scan is a performance problem, but rather than notionally we try not to depend on pg_depend contents for this kind of semantic derivation.) -- Álvaro Herrera 39°49'30"S 73°17'W --9amGYk9869ThD9tj Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-Record-parents-of-triggers.patch" From 8f1be4b2c24385bbfe2ecba941545ffa84f60b47 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Mon, 17 Feb 2020 17:34:47 -0300 Subject: [PATCH] Record parents of triggers This let us get rid of a recently introduced ugly hack (commit 1fa846f1c9af). --- doc/src/sgml/catalogs.sgml | 7 +++++ src/backend/commands/tablecmds.c | 52 ++------------------------------ src/backend/commands/trigger.c | 1 + src/include/catalog/pg_trigger.h | 1 + 4 files changed, 11 insertions(+), 50 deletions(-) diff --git a/doc/src/sgml/catalogs.sgml b/doc/src/sgml/catalogs.sgml index a10b66569b..0bba96ee52 100644 --- a/doc/src/sgml/catalogs.sgml +++ b/doc/src/sgml/catalogs.sgml @@ -6951,6 +6951,13 @@ SCRAM-SHA-256$<iteration count>:&l The table this trigger is on + + tgparentid + oid + pg_trigger.oid + Parent trigger of this trigger, if any + + tgname name diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index b7c8d663fc..ae39bf1056 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -16447,54 +16447,6 @@ out: MemoryContextDelete(cxt); } -/* - * isPartitionTrigger - * Subroutine for CloneRowTriggersToPartition: determine whether - * the given trigger has been cloned from another one. - * - * We use pg_depend as a proxy for this, since we don't have any direct - * evidence. This is an ugly hack to cope with a catalog deficiency. - * Keep away from children. Do not stare with naked eyes. Do not propagate. - */ -static bool -isPartitionTrigger(Oid trigger_oid) -{ - Relation pg_depend; - ScanKeyData key[2]; - SysScanDesc scan; - HeapTuple tup; - bool found = false; - - pg_depend = table_open(DependRelationId, AccessShareLock); - - ScanKeyInit(&key[0], Anum_pg_depend_classid, - BTEqualStrategyNumber, - F_OIDEQ, - ObjectIdGetDatum(TriggerRelationId)); - ScanKeyInit(&key[1], Anum_pg_depend_objid, - BTEqualStrategyNumber, - F_OIDEQ, - ObjectIdGetDatum(trigger_oid)); - - scan = systable_beginscan(pg_depend, DependDependerIndexId, - true, NULL, 2, key); - while ((tup = systable_getnext(scan)) != NULL) - { - Form_pg_depend dep = (Form_pg_depend) GETSTRUCT(tup); - - if (dep->refclassid == TriggerRelationId) - { - found = true; - break; - } - } - - systable_endscan(scan); - table_close(pg_depend, AccessShareLock); - - return found; -} - /* * CloneRowTriggersToPartition * subroutine for ATExecAttachPartition/DefineRelation to create row @@ -16541,7 +16493,7 @@ CloneRowTriggersToPartition(Relation parent, Relation partition) * * However, if our parent is a partitioned relation, there might be * internal triggers that need cloning. In that case, we must skip - * clone it if the trigger on parent depends on another trigger. + * cloning it if the trigger on parent depends on another trigger. * * Note we dare not verify that the other trigger belongs to an * ancestor relation of our parent, because that creates deadlock @@ -16549,7 +16501,7 @@ CloneRowTriggersToPartition(Relation parent, Relation partition) */ if (trigForm->tgisinternal && (!parent->rd_rel->relispartition || - !isPartitionTrigger(trigForm->oid))) + !OidIsValid(trigForm->tgparentid))) continue; /* diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c index b9b1262e30..6e8b7223fe 100644 --- a/src/backend/commands/trigger.c +++ b/src/backend/commands/trigger.c @@ -847,6 +847,7 @@ CreateTrigger(CreateTrigStmt *stmt, const char *queryString, values[Anum_pg_trigger_oid - 1] = ObjectIdGetDatum(trigoid); values[Anum_pg_trigger_tgrelid - 1] = ObjectIdGetDatum(RelationGetRelid(rel)); + values[Anum_pg_trigger_tgparentid - 1] = ObjectIdGetDatum(parentTriggerOid); values[Anum_pg_trigger_tgname - 1] = DirectFunctionCall1(namein, CStringGetDatum(trigname)); values[Anum_pg_trigger_tgfoid - 1] = ObjectIdGetDatum(funcoid); diff --git a/src/include/catalog/pg_trigger.h b/src/include/catalog/pg_trigger.h index 2f30c7c2f9..9612b9bdd6 100644 --- a/src/include/catalog/pg_trigger.h +++ b/src/include/catalog/pg_trigger.h @@ -35,6 +35,7 @@ CATALOG(pg_trigger,2620,TriggerRelationId) { Oid oid; /* oid */ Oid tgrelid; /* relation trigger is attached to */ + Oid tgparentid; /* OID of parent trigger, if any */ NameData tgname; /* trigger's name */ Oid tgfoid; /* OID of function to be called */ int16 tgtype; /* BEFORE/AFTER/INSTEAD, UPDATE/DELETE/INSERT, -- 2.20.1 --9amGYk9869ThD9tj--