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.92) (envelope-from ) id 1jEfpa-0002J8-7C for pgsql-hackers@arkaria.postgresql.org; Wed, 18 Mar 2020 21:02:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jEfpW-0007bf-Ld for pgsql-hackers@arkaria.postgresql.org; Wed, 18 Mar 2020 21:02:22 +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 1jEfpW-0007bY-99 for pgsql-hackers@lists.postgresql.org; Wed, 18 Mar 2020 21:02:22 +0000 Received: from mail-qv1-xf44.google.com ([2607:f8b0:4864:20::f44]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jEfpS-0006eQ-H1 for pgsql-hackers@lists.postgresql.org; Wed, 18 Mar 2020 21:02:21 +0000 Received: by mail-qv1-xf44.google.com with SMTP id cy12so7851822qvb.7 for ; Wed, 18 Mar 2020 14:02:18 -0700 (PDT) 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:in-reply-to:user-agent; bh=8/ABOwMxsddP9qnScECYzXaT3NCme2VYNtsU2QAAmII=; b=o9c5gnzG7jx09G/B0fj2yop4bt0WxiBZQj6gsCRm/eSbA9j9ZXRB5h4ARL/8ZZQRc1 VMeQwIIJ6TOoSPICfOQ8SjhJfdWmeMRYdg9nbuzwEAcoohSHIAerm8IfvtGv9o4PqKcR DvcaxHHs6bS175kkfBQ+df3ASHxV4jPQOL+ZyE7d/7vdNemu0skvkEyf+gEk6Fqh5bJY 1KvbAiqngNaQw+vNNUFbfrgmOJsnHOOjOCHvuyq/tXqMOUHltkY719O7LzAUYWfsbh6+ AO3ZYnEsb8rPqTjCql8M1z+L1i9OhOk7pVlVUHk+UZPuHluVDYzUbK+zkR66n8/23qLK 9zvg== 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:in-reply-to :user-agent; bh=8/ABOwMxsddP9qnScECYzXaT3NCme2VYNtsU2QAAmII=; b=GLDmxLbOJNXIheE1pRVhcZnRIcbpeU7sMu4GPEoFyFAcrbSXiU4phO1Eg+1t6b9ScI VRW+B2pJ5AxQxPAiWfUYtGTqocVL6GaOomemhDR+kIU4ZzD3Ck5HVwArjZntpq3dM/8f G84NFUmXfZmStKlHCTLmaZr9faLCpazVgJmexeFbAx9BL4/NhzueUXFz6nlVWi5Idkfb ckuAnt0w1oeM0q42DDB6HPQpWPoXb9ouSr8hh6dI0yIutlMY26Q3bB7Vkszf7ty5QDm7 AEsp7xb5btS+KwilmUEi/Xj99QHl4LdQtpfnZwpZfYBX7KCgaNfP82201iqST6x+Jq+2 Y4ug== X-Gm-Message-State: ANhLgQ05ZibqRU91+YRE0xH78gmRfrecndzTlwEomOmf4COLSv/8Gi4B cWTg6hUpuU8x0d0s2s9BDDh1Jw== X-Google-Smtp-Source: ADFU+vus0b3+VTyPZk24oytKkvcZpIXx3+GqSqKxF5KdAu07lVdzWP23V50oefp6h8c4DpVWX8ykFg== X-Received: by 2002:a05:6214:84b:: with SMTP id dg11mr6356936qvb.205.1584565336572; Wed, 18 Mar 2020 14:02:16 -0700 (PDT) Received: from nimloth.alvh.no-ip.org ([201.186.208.119]) by smtp.gmail.com with ESMTPSA id w30sm5815331qtw.21.2020.03.18.14.02.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Mar 2020 14:02:15 -0700 (PDT) Received: by nimloth.alvh.no-ip.org (Postfix, from userid 1000) id 25074300819; Wed, 18 Mar 2020 18:02:13 -0300 (-03) Date: Wed, 18 Mar 2020 18:02:13 -0300 From: Alvaro Herrera To: Ashutosh Bapat Cc: Ashutosh Bapat , Pg Hackers Subject: Re: BEFORE ROW triggers for partitioned tables Message-ID: <20200318210213.GA9781@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2020-Mar-17, Ashutosh Bapat wrote: > On Fri, 13 Mar 2020 at 21:55, Alvaro Herrera > wrote: > > Note that in this implementation I no longer know which column is the > > problematic one, but I suppose users have clue enough. Wording > > suggestions welcome. > > When we have expression as a partition key, there may not be one particular > column which causes the row to move out of partition. So, this should be > fine. True. > A slight wording suggestion below. > > - * Complain if we find an unexpected trigger type. > - */ > - if (!TRIGGER_FOR_AFTER(trigForm->tgtype)) > - elog(ERROR, "unexpected trigger \"%s\" found", > - NameStr(trigForm->tgname)); > > !AFTER means INSTEAD OF and BEFORE. Do you intend to allow INSTEAD OF > triggers as well? Hmm, yeah, this should check both types; I'll put it back. Note that this is just a cross-check that the catalogs we're going to copy don't contain bogus info; the real backstop for that at the user level is in the other one you complain about: > - */ > - if (stmt->timing != TRIGGER_TYPE_AFTER) > > Same comment as the above? Note that in this one we have a check for INSTEAD before we enter the FOR EACH ROW block, so this case is already covered -- AFAICS the code is correct. > + /* > + * After a tuple in a partition goes through a trigger, the user > + * could have changed the partition key enough that the tuple > + * no longer fits the partition. Verify that. > + */ > + if (trigger->tgisclone && > > Why do we want to restrict this check only for triggers which are > cloned from the ancestors? Because it's not our business in the other case. When the trigger is defined directly in the partition, it's the user's problem if something going amiss. > + !ExecPartitionCheck(relinfo, slot, estate, false)) > + ereport(ERROR, > + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), > + errmsg("moving row to another partition during a BEFORE trigger is not > supported"), > + errdetail("Before trigger \"%s\", row was to be in partition \"%s.%s\"", > > In the error message you removed above, we are mentioning BEFORE FOR EACH > ROW trigger. Should we continue to use the same terminology? Sounds good, I'll change that. I also changed the errdetail slightly: errdetail("Before executing trigger \"%s\", the row was to be in partition \"%s.%s\"", > I was wondering whether it would be good to check the partition > constraint only once i.e. after all before row triggers have been > executed. This would avoid throwing an error in case multiple triggers > together worked to keep the tuple in the same partition when > individual trigger/s caused it to move out of that partition. But then > we would loose the opportunity to point out the before row trigger > which actually caused the row to move out of the partition. Anyway, > wanted to bring that for the discussion here. Yeah, I too thought about a combination of triggers that move the tuple elsewhere and back. Frankly, I don't think we need to support that. It sounds devious and likely we'll miss some odd corner case -- anything involving the weird cross-partition UPDATE mechanism sounds easy to get wrong. > +-- Before triggers and partitions > > The test looks good. Should we add a test for partitioned table with > partition > key as expression? Will do. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services