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 1m3TEB-0005xm-C3 for pgsql-hackers@arkaria.postgresql.org; Wed, 14 Jul 2021 00:58:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m3TE9-0001BU-Ps for pgsql-hackers@arkaria.postgresql.org; Wed, 14 Jul 2021 00:58:17 +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 1m3TE9-0001BM-12 for pgsql-hackers@lists.postgresql.org; Wed, 14 Jul 2021 00:58:17 +0000 Received: from new2-smtp.messagingengine.com ([66.111.4.224]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m3TE1-0007qk-U2 for pgsql-hackers@lists.postgresql.org; Wed, 14 Jul 2021 00:58:15 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailnew.nyi.internal (Postfix) with ESMTP id C669E580450; Tue, 13 Jul 2021 20:58:07 -0400 (EDT) Received: from imap21 ([10.202.2.71]) by compute1.internal (MEProxy); Tue, 13 Jul 2021 20:58:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eulerto.com; h= mime-version:message-id:in-reply-to:references:date:from:to:cc :subject:content-type; s=fm2; bh=LGLMl4FkIvF5pbXAYrg8/L36aoZ8Jef Bb1bfz+ZX0u8=; b=GlCqNLbUIXKD9cG+nOfhHlJoHZfTEUGiHtBdzGBL+CJBXSm uTnV2W9zOyDxATF4MQL9tgnRK4DPJnT4C/9EMOKc+lveva+bK9/z961eQA/rQD0K sRCkw31fxz/tlHv3gvTKP/hULFqNPRYi+7n9l0oCXpsmdI+Q6LhwAKobN24RdGeM btS+AvvAM0TP0n2znkuV15y5D1+NhCKYrW/yIdmNr94VhUmrGv6QEKMjVXrNbXtm upB91AUCeesa6h/mnzuYSisZOWBV/5fsxyIG5Wm/d4k5jGCQw4ENkkuabHUE2Hgz FEDvOH2QiypmoIDJKcoYZ2bnlDIUlLS7lJeaBxQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=LGLMl4 FkIvF5pbXAYrg8/L36aoZ8JefBb1bfz+ZX0u8=; b=qtj+8fZ6+atA6uKdqwHSlw lbJCj9xhFvphkVt5bU/tOGM49p9dfhmXY/K1uZJe3xZtCJOfMPlV94CLWXJZDgj0 30iFGMi38TWz6/5cTGjzT3l4uPpjYVx1MZ7URmfkmwR0VG/1tYEPTfZScOgI+5Kx abZclNCqelgsjG9+lBSXIXmPRsxJamz5+vKeQCYBfarzM4NDAflgsyASpJMxGq6b 4AKg54veWQzTL2IuhFxYazZUFAJZGwGybKUjL7qcJ2yXd6jjbdTIUe2FLwpmVy7N KTPVks7TmgJUWtuXRXC2ZhIjvbL7YjEUipZ5C+8FS6U2zqftKpCFcluyVbXAwQUg == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrudejgdeffecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefofgggkfgjfhffhffvufgtsegrtderreerredtnecuhfhrohhmpedfgfhulhgv rhcuvfgrvhgvihhrrgdfuceovghulhgvrhesvghulhgvrhhtohdrtghomheqnecuggftrf grthhtvghrnhepjeethfeuvdfgffdtteevudeftdejueffkeelveekhfevkeegleefhfej hedtjeegnecuffhomhgrihhnpegvnhhtvghrphhrihhsvggusgdrtghomhenucevlhhush htvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegvuhhlvghrsegvuhhl vghrthhordgtohhm X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id BAAB651C0060; Tue, 13 Jul 2021 20:58:06 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.5.0-alpha0-533-gf73e617b8a-fm-20210712.002-gf73e617b Mime-Version: 1.0 Message-Id: <48e13621-10f7-49ef-bcbe-ed530195efd2@www.fastmail.com> In-Reply-To: <202107132106.wvjgvjgcyezo@alvherre.pgsql> References: <202107132106.wvjgvjgcyezo@alvherre.pgsql> Date: Tue, 13 Jul 2021 21:57:44 -0300 From: "Euler Taveira" To: "Alvaro Herrera" Cc: "Peter Smith" , "Amit Kapila" , "Rahila Syed" , "Peter Eisentraut" , =?UTF-8?Q?=C3=96nder_Kalac=C4=B1?= , japin , "Michael Paquier" , "David Steele" , "Craig Ringer" , "Tomas Vondra" , "Amit Langote" , "PostgreSQL Hackers" Subject: Re: row filtering for logical replication Content-Type: multipart/alternative; boundary=17b1a0f9b3fe454db8fa8039cd59419a List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --17b1a0f9b3fe454db8fa8039cd59419a Content-Type: text/plain On Tue, Jul 13, 2021, at 6:06 PM, Alvaro Herrera wrote: > 1. if you use REPLICA IDENTITY FULL, then the expressions would work > even if they use any other column with DELETE. Maybe it would be > reasonable to test for this in the code and raise an error if the > expression requires a column that's not part of the replica identity. > (But that could be relaxed if the publication does not publish > updates/deletes.) I thought about it but came to the conclusion that it doesn't worth it. Even with REPLICA IDENTITY FULL expression evaluates to false if the column allows NULL values. Besides that REPLICA IDENTITY is changed via another DDL (ALTER TABLE) and you have to make sure you don't allow changing REPLICA IDENTITY because some row filter uses the column you want to remove from it. > 2. For UPDATE, does the expression apply to the old tuple or to the new > tuple? You say it's the new tuple, but from the user point of view I > think it would make more sense that it would apply to the old tuple. > (Of course, if you're thinking that the R.I. is the PK and the PK is > never changed, then you don't really care which one it is, but I bet > that some people would not like that assumption.) New tuple. The main reason is that new tuple is always there for UPDATEs. Hence, row filter might succeed even if the row filter contains a column that is not part of PK or REPLICA IDENTITY. pglogical also chooses to use new tuple when it is available (e.g. for INSERT and UPDATE). If you don't like this approach we can (a) create a new publication option to choose between old tuple and new tuple for UPDATEs or (b) qualify columns using a special reference (such as NEW.id or OLD.foo). Both options can provide flexibility but (a) is simpler. > I think it is sensible that it's the old tuple that is matched, not the > new; consider what happens if you change the PK in the update and the > replica already has that tuple. If you match on the new tuple and it > doesn't match the expression (so you filter out the update), but the old > tuple does match the expression, then the replica will retain the > mismatching tuple forever. > > 3. You say that a NULL value in any of those columns causes the > expression to become false and thus the tuple is not published. This > seems pretty unfriendly, but maybe it would be useful to have examples > of the behavior. Does ExecInitCheck() handle things in the other way, > and if so does using a similar trick give more useful behavior? ExecInitCheck() is designed for CHECK constraints and SQL standard requires taht NULL constraint conditions are not treated as errors. This feature uses a WHERE clause and behaves like it. I mean, a NULL result does not return the row. See ExecQual(). -- Euler Taveira EDB https://www.enterprisedb.com/ --17b1a0f9b3fe454db8fa8039cd59419a Content-Type: text/html Content-Transfer-Encoding: quoted-printable
On Tue, Jul 13,= 2021, at 6:06 PM, Alvaro Herrera wrote:
1. if you use REPLICA IDENTITY FULL, then = the expressions would work
even if they use any other colu= mn with DELETE.  Maybe it would be
reasonable to test= for this in the code and raise an error if the
expression= requires a column that's not part of the replica identity.
(But that could be relaxed if the publication does not publish
updates/deletes.)
I thought about it b= ut came to the conclusion that it doesn't worth it.  Even
=
with REPLICA IDENTITY FULL expression evaluates to false if the col= umn allows
NULL values. Besides that REPLICA IDENTITY is c= hanged via another DDL (ALTER
TABLE) and you have to make = sure you don't allow changing REPLICA IDENTITY
because som= e row filter uses the column you want to remove from it.
<= br>
2. For UPDA= TE, does the expression apply to the old tuple or to the new
tuple?  You say it's the new tuple, but from the user point of v= iew I
think it would make more sense that it would apply t= o the old tuple.
(Of course, if you're thinking that the R= .I. is the PK and the PK is
never changed, then you don't = really care which one it is, but I bet
that some people wo= uld not like that assumption.)
New tuple. The= main reason is that new tuple is always there for UPDATEs.
Hence, row filter might succeed even if the row filter contains a colu= mn that
is not part of PK or REPLICA IDENTITY. pglogical a= lso chooses to use new tuple
when it is available (e.g. fo= r INSERT and UPDATE). If you don't like this
approach we c= an (a) create a new publication option to choose between old tuple
and new tuple for UPDATEs or (b) qualify columns using a specia= l reference
(such as NEW.id or OLD.foo). Both options can = provide flexibility but (a) is
simpler.

=
I think it is = sensible that it's the old tuple that is matched, not the
= new; consider what happens if you change the PK in the update and the
replica already has that tuple.  If you match on the ne= w tuple and it
doesn't match the expression (so you filter= out the update), but the old
tuple does match the express= ion, then the replica will retain the
mismatching tuple fo= rever.

3. You say that a NULL value in any = of those columns causes the
expression to become false and= thus the tuple is not published.  This
seems pretty = unfriendly, but maybe it would be useful to have examples
= of the behavior.  Does ExecInitCheck() handle things in the other w= ay,
and if so does using a similar trick give more useful = behavior?
ExecInitCheck() is designed for CHE= CK constraints and SQL standard requires
taht NULL constra= int conditions are not treated as errors. This feature uses a
<= div>WHERE clause and behaves like it. I mean, a NULL result does not ret= urn the
row. See ExecQual().

=
--
=
Euler Taveira

--17b1a0f9b3fe454db8fa8039cd59419a--