Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n9c3A-0003iJ-2n for pgsql-hackers@arkaria.postgresql.org; Tue, 18 Jan 2022 00:08:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1n9c38-0000de-Of for pgsql-hackers@arkaria.postgresql.org; Tue, 18 Jan 2022 00:08:34 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n9c38-0000dV-6W for pgsql-hackers@lists.postgresql.org; Tue, 18 Jan 2022 00:08:34 +0000 Received: from mail-io1-xd2a.google.com ([2607:f8b0:4864:20::d2a]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1n9c30-0004OS-MI for pgsql-hackers@postgresql.org; Tue, 18 Jan 2022 00:08:32 +0000 Received: by mail-io1-xd2a.google.com with SMTP id k14so19504241ion.7 for ; Mon, 17 Jan 2022 16:08:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=pvXSrFk1dPl8UsjCTSlq4Snz7qMDoge5prV5rraTXWg=; b=WBmnW/6g1W+7S03Zd1HOzoHfn46V0S0kc++ESwxADzDaUU+rxaR1eEstGLp+3J+xfA xD+VLJ0Z/2oO/B5TQdlQ7OZth6Tv7Fyqht73nCCHUGXoA2cXRyCAwC/KpCemjNBjQHxS n3317oDYnyMjqPZo28iddFL1qZjpr/eiCZrR/fRkYtRiax51o4vKt0pA9ayPzBsgqXJ3 hI2hJi0eWYz75jljR2aoRuLWOkA3fc+3w+030+6/zfpfrRKs+2YsRqadFlBT5HhciO23 GPk51WvxNgG4AHDpN1xSy7SJNL1T4FirbipoihkkY8c6wr+aEzb4HDx+y1fovycFisTs HOPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=pvXSrFk1dPl8UsjCTSlq4Snz7qMDoge5prV5rraTXWg=; b=75r/Q6XoPqCP1bhaQm62P8iGD9YRR6eSzpUQlkytI7UXI0IQvovPv3wGROpktU1tNe C4XDSARxi4BxWG0/4BHiSwNci6sgj080xbWJeH6vTB0y2m3pVWRLXXcaCjLXOvKMlOA+ s92cX5GHrx++i6wA8mdIqAxqz3+nre+A5x77cs3ueAod5LtfxrilAyYvkLXpCviIsx9J LDWQWkp6BYdlsfgJE8wGhT0TKgHNRoboBBWS1YS/AYg0TPcKyEFXsy7DczlZS7FOu7q6 ZGZO3w8/l1d93bVWo+Wnc5vNGzS+yQdyq3Z+e1nvICLIkdkcZ1pXUuBGZQ1ZSWRIcCIs YVGw== X-Gm-Message-State: AOAM532X6XtovF/bVCFXcWkeJb9pZlqK5ObYXxL0RGWtfLkqULIt+DA6 gB5QQ8duZw8OO6G7pAULptHI7xwTyPRbgA== X-Google-Smtp-Source: ABdhPJxpebERl1mBnxvOBAJVf0iDIXK/+TGQRsBMwcaQd0JAGuURnOfLQ5dzuMvXi9JNIRiXIUVQxw== X-Received: by 2002:a05:6602:2d82:: with SMTP id k2mr1648971iow.186.1642464505939; Mon, 17 Jan 2022 16:08:25 -0800 (PST) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id a8sm9845530ilt.42.2022.01.17.16.08.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 17 Jan 2022 16:08:25 -0800 (PST) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id AEE96800A3C; Mon, 17 Jan 2022 18:08:24 -0600 (CST) Date: Mon, 17 Jan 2022 18:08:24 -0600 From: Justin Pryzby To: Tom Lane Cc: Alvaro Herrera , pgsql-hackers@postgresql.org, Amit Langote Subject: Re: \d with triggers: more than one row returned by a subquery used as an expression Message-ID: <20220118000824.GC14051@telsasoft.com> References: <20211217154356.GJ17618@telsasoft.com> <20211222210308.GD17618@telsasoft.com> <152507.1642456920@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="tR2cvQ13k2aMxNOa" Content-Disposition: inline In-Reply-To: <152507.1642456920@sss.pgh.pa.us> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --tR2cvQ13k2aMxNOa Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Mon, Jan 17, 2022 at 05:02:00PM -0500, Tom Lane wrote: > Justin Pryzby writes: > > On Fri, Dec 17, 2021 at 09:43:56AM -0600, Justin Pryzby wrote: > >> I want to mention that the 2nd problem I mentioned here is still broken. > >> https://www.postgresql.org/message-id/20210717010259.GU20208@telsasoft.com > >> It happens if non-inheritted triggers on child and parent have the same name. > > > This is the fix I was proposing > > It depends on pg_partition_ancestors() to return its partitions in order: > > this partition => parent => ... => root. > > I don't think that works at all. I might be willing to accept the > assumption about pg_partition_ancestors()'s behavior, but you're also > making an assumption about how the output of pg_partition_ancestors() > is joined to "pg_trigger AS u", and I really don't think that's safe. > ISTM the real problem is the assumption that only related triggers could > share a tgname, which evidently isn't true. I think this query needs to > actually match on tgparentid, rather than taking shortcuts. I don't think that should be needed - tgparentid should match pg_partition_ancestors(). > If we don't > want to use a recursive CTE, maybe we could define it as only looking up to > the immediate parent, rather than necessarily finding the root? I think that defeats the intent of c33869cc3. Is there any reason why WITH ORDINALITY can't work ? This is passing the smoke test. -- Justin --tR2cvQ13k2aMxNOa Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-psql-Add-newlines-and-spacing-in-trigger-query-for-d.patch" From 99f8ee921258d9b2e75299e69826004fda3b8919 Mon Sep 17 00:00:00 2001 From: Justin Pryzby Date: Fri, 3 Apr 2020 22:43:26 -0500 Subject: [PATCH 1/2] psql: Add newlines and spacing in trigger query for \d cosmetic change to c33869cc3 to make the output of psql -E look pretty. --- src/bin/psql/describe.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 61cec118aca..ed9f320b015 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -2993,12 +2993,12 @@ describeOneTableDetails(const char *schemaname, "t.tgenabled, t.tgisinternal, %s\n" "FROM pg_catalog.pg_trigger t\n" "WHERE t.tgrelid = '%s' AND ", - (pset.sversion >= 130000 ? - "(SELECT (NULLIF(a.relid, t.tgrelid))::pg_catalog.regclass" - " FROM pg_catalog.pg_trigger AS u, " - " pg_catalog.pg_partition_ancestors(t.tgrelid) AS a" - " WHERE u.tgname = t.tgname AND u.tgrelid = a.relid" - " AND u.tgparentid = 0) AS parent" : + (pset.sversion >= 130000 ? "\n" + " (SELECT (NULLIF(a.relid, t.tgrelid))::pg_catalog.regclass\n" + " FROM pg_catalog.pg_trigger AS u,\n" + " pg_catalog.pg_partition_ancestors(t.tgrelid) AS a\n" + " WHERE u.tgname = t.tgname AND u.tgrelid = a.relid\n" + " AND u.tgparentid = 0) AS parent" : "NULL AS parent"), oid); -- 2.17.1 --tR2cvQ13k2aMxNOa Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0002-psql-fix-d-for-statement-triggers-with-same-name-on-.patch" From d866624abbd523afdbc3a539f14c935c42fc345f Mon Sep 17 00:00:00 2001 From: Justin Pryzby Date: Fri, 16 Jul 2021 19:57:00 -0500 Subject: [PATCH 2/2] psql: fix \d for statement triggers with same name on partition and its parent This depends on pg_partition_ancestors() to return its partitions in order: this partition => parent => ... => root. 20210716193319.GS20208@telsasoft.com --- src/bin/psql/describe.c | 4 ++-- src/test/regress/expected/triggers.out | 13 +++++++++++++ src/test/regress/sql/triggers.sql | 4 ++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index ed9f320b015..8787849e8be 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -2996,9 +2996,9 @@ describeOneTableDetails(const char *schemaname, (pset.sversion >= 130000 ? "\n" " (SELECT (NULLIF(a.relid, t.tgrelid))::pg_catalog.regclass\n" " FROM pg_catalog.pg_trigger AS u,\n" - " pg_catalog.pg_partition_ancestors(t.tgrelid) AS a\n" + " pg_catalog.pg_partition_ancestors(t.tgrelid) WITH ORDINALITY AS a(relid,depth)\n" " WHERE u.tgname = t.tgname AND u.tgrelid = a.relid\n" - " AND u.tgparentid = 0) AS parent" : + " AND u.tgparentid = 0 ORDER BY depth LIMIT 1) AS parent" : "NULL AS parent"), oid); diff --git a/src/test/regress/expected/triggers.out b/src/test/regress/expected/triggers.out index 5c0e7c2b79e..9278cc07237 100644 --- a/src/test/regress/expected/triggers.out +++ b/src/test/regress/expected/triggers.out @@ -2122,6 +2122,19 @@ Triggers: alter table trigpart attach partition trigpart3 FOR VALUES FROM (2000) to (3000); -- fail ERROR: trigger "trg1" for relation "trigpart3" already exists drop table trigpart3; +create trigger samename after delete on trigpart execute function trigger_nothing(); +create trigger samename after delete on trigpart1 execute function trigger_nothing(); +\d trigpart1 + Table "public.trigpart1" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+--------- + a | integer | | | + b | integer | | | +Partition of: trigpart FOR VALUES FROM (0) TO (1000) +Triggers: + samename AFTER DELETE ON trigpart1 FOR EACH STATEMENT EXECUTE FUNCTION trigger_nothing() + trg1 AFTER INSERT ON trigpart1 FOR EACH ROW EXECUTE FUNCTION trigger_nothing(), ON TABLE trigpart + drop table trigpart; drop function trigger_nothing(); -- diff --git a/src/test/regress/sql/triggers.sql b/src/test/regress/sql/triggers.sql index 9cb15c21dc3..5e2197e10e2 100644 --- a/src/test/regress/sql/triggers.sql +++ b/src/test/regress/sql/triggers.sql @@ -1428,6 +1428,10 @@ create trigger trg1 after insert on trigpart3 for each row execute procedure tri alter table trigpart attach partition trigpart3 FOR VALUES FROM (2000) to (3000); -- fail drop table trigpart3; +create trigger samename after delete on trigpart execute function trigger_nothing(); +create trigger samename after delete on trigpart1 execute function trigger_nothing(); +\d trigpart1 + drop table trigpart; drop function trigger_nothing(); -- 2.17.1 --tR2cvQ13k2aMxNOa--