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 1nf0GE-00022F-JY for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Apr 2022 14:15:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nf0GD-0001gX-3r for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Apr 2022 14:15:49 +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 1nf0GC-0001gN-7r for pgsql-hackers@lists.postgresql.org; Thu, 14 Apr 2022 14:15:48 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nf0G7-0003RX-8K for pgsql-hackers@lists.postgresql.org; Thu, 14 Apr 2022 14:15:47 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 2881E5C029D; Thu, 14 Apr 2022 10:15:42 -0400 (EDT) Received: from imap49 ([10.202.2.99]) by compute2.internal (MEProxy); Thu, 14 Apr 2022 10:15:42 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eulerto.com; h= cc:cc:content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to; s=fm1; t=1649945742; x=1650032142; bh=zyzyFQUzAS szjCd86FipBnyuWXClPg/NIB85aTNVYsI=; b=kfSG0JtqJtSJJJsVFmhW9NPMgI lkfilCNpkXTc5nrzlLU1pfucxsPia0/fjHMpJjZ2jmjeJ03jqNH46TpWylhCRjOm mCdLTAtbwb+ICY8IXY9DVJzjZs0/e7Ci1HQRZ9iIvyHKxyJZIwQSCXplpPpqUpKD FsKFmcdXHzRMNV9pwpPZ6c/JXlkjhikOqRA7Jec3EBgjaGXG7mhvJiEOgMfknV42 VvNVcBTFMSY+HJ7a7+PlDHHMA6hXK23diI3MPWTtXKJxiiGit92KovoXdaSzzFAV 2bT1YL1bmmgNbxp7PRVrz+9YR+vmHHCJCWzvaWHiD1oWHqoGhM/z50ruTIPg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1649945742; x= 1650032142; bh=zyzyFQUzASszjCd86FipBnyuWXClPg/NIB85aTNVYsI=; b=W RYVaRZLc06iwkscHddaWT9tGo8j7ZmL2Ueyz+HN/6egudN8GaJFmpv2IDt7BERck j0PkAZQR6nqBfPYN4XLmtdrjneZwXngw1NYSh9OQ2M2WyR1/JuIg45TgJW0DNBz7 zeX8avPelTzo9bBHJUvFnUSXFa148OFwB8+IE7Nv3i+o3vT4aRGjTu3K434Ubuz6 bg8NfxnCkNlD9esHHMzRfHJ9EUUsgAc6H/xGXt4RsonVRFWjcRpFqQ2Q3pwwo036 g8voNpnj9V/H54t7NIiDLHRxSuLomiCJkzPnBGj8zkYzuUEL8i24CYBbGz4DWtCV m0fIK5aJLNGfG2iDv40Hw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvvddrudelfedgjeehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesrgdtreerreertdenucfhrhhomhepfdfguhhl vghrucfvrghvvghirhgrfdcuoegvuhhlvghrsegvuhhlvghrthhordgtohhmqeenucggtf frrghtthgvrhhnpeejtefhuedvgffftdetvedufedtjeeuffekleevkefhveekgeelfefh jeehtdejgeenucffohhmrghinhepvghnthgvrhhprhhishgvuggsrdgtohhmnecuvehluh hsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepvghulhgvrhesvghu lhgvrhhtohdrtghomh X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id BBBD215A0069; Thu, 14 Apr 2022 10:15:41 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.7.0-alpha0-387-g7ea99c4045-fm-20220413.002-g7ea99c40 Mime-Version: 1.0 Message-Id: <3c646317-df34-4cb3-9365-14abeada6587@www.fastmail.com> In-Reply-To: References: Date: Thu, 14 Apr 2022 11:15:20 -0300 From: "Euler Taveira" To: "Dilip Kumar" , "Amit Kapila" Cc: "Zheng Li" , "PostgreSQL Hackers" , "rajesh singarapu" Subject: Re: Support logical replication of DDLs Content-Type: multipart/alternative; boundary=97ac00df980c4323b0adadf1a1aebfec List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --97ac00df980c4323b0adadf1a1aebfec Content-Type: text/plain On Thu, Apr 14, 2022, at 6:26 AM, Dilip Kumar wrote: > I agree. But here the bigger question is what is the correct behavior > in case of the Alter Table? I mean for example in the publisher the > table gets rewritten due to the Access Method change then what should > be the behavior of the subscriber. One expected behavior is that on > subscriber also the access method gets changed and the data remains > the same as on the subscriber table(table can be locally rewritten > based on new AM). Which seems quite sensible behavior to me. But if > we want this behavior then we can not replay the logical messages > generated by DML WAL because of table rewrite, otherwise we will get > duplicate data, unless we plan to get rid of the current data and just > get all new data from the publisher. And if we do that then the data > will be as per the latest data in the table based on the publisher, so > I think first we need to define the correct behavior and then we can > design it accordingly. You should forbid it. Unless you can decompose the command into multiple SQL commands to make it a safe operation for logical replication. Let's say you want to add a column with a volatile default. ALTER TABLE foo ADD COLUMN bar double precision DEFAULT random(); If you replicate the DDL command as is, you will have different data downstream. You should forbid it. However, this operation can be supported if the DDL command is decomposed in multiple steps. -- add a new column without DEFAULT to avoid rewrite ALTER TABLE foo ADD COLUMN bar double precision; -- future rows could use the DEFAULT expression -- it also doesn't rewrite the table ALTER TABLE foo ALTER COLUMN bar SET DEFAULT random(); -- it effectively rewrites the table -- all rows are built from one source node -- data will be the same on all nodes UPDATE foo SET bar = random(); The ALTER TABLE ... ALTER COLUMN ... TYPE has a similar issue. This DDL command can be decomposed to avoid the rewrite. If you are changing the data type, in general, you add a new column and updates all rows doing the proper conversion. (If you are updating in batches, you usually add a trigger to automatically adjust the new column value for INSERTs and UPDATEs. Another case is when you are reducing the the typmod (for example, varchar(100) to varchar(20)). In this case, the DDL command can de decomposed removing the typmod information (ALTER TABLE ... ALTER COLUMN ... TYPE varchar) and replacing it with a CHECK constraint. I didn't review this patch in depth but we certainly need to impose some DDL restrictions if we are replicating DDLs. There are other cases that should be treated accordingly such as a TABLESPACE specification or a custom data type. -- Euler Taveira EDB https://www.enterprisedb.com/ --97ac00df980c4323b0adadf1a1aebfec Content-Type: text/html Content-Transfer-Encoding: quoted-printable
On Thu, Apr 14,= 2022, at 6:26 AM, Dilip Kumar wrote:
I agree.  But here the bigger question i= s what is the correct behavior
in case of the Alter Table?=   I mean for example in the publisher the
table gets = rewritten due to the Access Method change then what should
be the behavior of the subscriber.  One expected behavior is that = on
subscriber also the access method gets changed and the = data remains
the same as on the subscriber table(table can= be locally rewritten
based on new AM).  Which seems = quite sensible behavior to me.  But if
we want this b= ehavior then we can not replay the logical messages
genera= ted by DML WAL because of table rewrite, otherwise we will get
=
duplicate data, unless we plan to get rid of the current data and j= ust
get all new data from the publisher. And if we do that= then the data
will be as per the latest data in the table= based on the publisher, so
I think first we need to defin= e the correct behavior and then we can
design it according= ly.
You should forbid it. Unless you can deco= mpose the command into multiple SQL
commands to make it a = safe operation for logical replication.

Let= 's say you want to add a column with a volatile default.
<= br>
ALTER TABLE foo ADD COLUMN bar double precision DEFAULT ra= ndom();

If you replicate the DDL command as= is, you will have different data
downstream. You should f= orbid it. However, this operation can be supported if
the = DDL command is decomposed in multiple steps.

-- add a new column without DEFAULT to avoid rewrite
ALT= ER TABLE foo ADD COLUMN bar double precision;

-- future rows could use the DEFAULT expression
-- it a= lso doesn't rewrite the table
ALTER TABLE foo ALTER COLUMN= bar SET DEFAULT random();

-- it effectivel= y rewrites the table
-- all rows are built from one source= node
-- data will be the same on all nodes
= UPDATE foo SET bar =3D random();

The ALTER = TABLE ... ALTER COLUMN ... TYPE has a similar issue. This DDL command
can be decomposed to avoid the rewrite. If you are changing = the data type, in
general, you add a new column and update= s all rows doing the proper conversion.
(If you are updati= ng in batches, you usually add a trigger to automatically
= adjust the new column value for INSERTs and UPDATEs. Another case is whe= n you
are reducing the the typmod (for example, varchar(10= 0) to varchar(20)). In this
case, the DDL command can de d= ecomposed removing the typmod information (ALTER
TABLE ...= ALTER COLUMN ... TYPE varchar) and replacing it with a CHECK
<= div>constraint.

I didn't review this patch = in depth but we certainly need to impose some DDL
restrict= ions if we are replicating DDLs. There are other cases that should be
treated accordingly such as a TABLESPACE specification or a = custom data type.


--
= Euler Taveira

--97ac00df980c4323b0adadf1a1aebfec--