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 1lRyu8-0007bK-Ff for pgsql-general@arkaria.postgresql.org; Thu, 01 Apr 2021 15:06:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lRyu7-0000vh-D0 for pgsql-general@arkaria.postgresql.org; Thu, 01 Apr 2021 15:06:39 +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 1lRyu7-0000vZ-5f for pgsql-general@lists.postgresql.org; Thu, 01 Apr 2021 15:06:39 +0000 Received: from mail-pj1-x1034.google.com ([2607:f8b0:4864:20::1034]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lRyu5-0002qn-6f for pgsql-general@lists.postgresql.org; Thu, 01 Apr 2021 15:06:38 +0000 Received: by mail-pj1-x1034.google.com with SMTP id gb6so1330075pjb.0 for ; Thu, 01 Apr 2021 08:06:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:content-transfer-encoding:from:mime-version:subject:date :message-id:references:cc:in-reply-to:to; bh=Z1NG6hpfJTpzgas6osOdwvvoBxvcuBr63d39SYLTgEw=; b=J/i2vNVX4I2YsKqF0p+rRm9MMvpEO91hIRE5rVILIPAl2Mr55juOPWmnnNA7vSO1tk LiQgGmornh6am4QVEEWLUp86UCpV/yn5QDYTVi1OZ2jAumaMV7mOGkbTORmctcrMYZbP GwNsFWqUSS9fx0BNE/b4Pq7pEH01BiVebZnz6N7vxO2mfA1N8T1rNcRyg+AfPY7AXeuj SBo1M2p6RxmaDUr80maRTD+4ifBZvSrBjnlmWOYSnX+yFauJO2vmK2xx2fMsZADzzivz 2hMXDG2qJgRe40Thy96NbyhkB8TdnDFKqU78dva3WSbXfrzAV67+Bg6f/W7inev5DhsI hoRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:content-transfer-encoding:from :mime-version:subject:date:message-id:references:cc:in-reply-to:to; bh=Z1NG6hpfJTpzgas6osOdwvvoBxvcuBr63d39SYLTgEw=; b=oCjEY9LB7ombTHPTcHi8HSm4ebYuYxQfnRvurbzIZs5ma4kz/VlqCeefQ+pS3Wm93t EeSEcsYrPzHjIqBE95c9qYLG/CTgVxaRdjmZOm80S8zX08Zz+tLqUu9q1CvaLY5aCz9t qHf+M7SsSj+9usu10uh862XoChyIpCCG2+4/OcVz33oZJInb5zqg/zZW4qohess1Crs5 7sj9C6bhXg1XIrIUJe/SdJRHuQJjHZd8ABrx8Da0dgM2waKxqxr/PY7Vwy4jv55JZmux kjgCd2SXSEnUBOsWYf2rPpNf50TajCf3KDUcZ82fPXiyFv/WSSxFaGo/lFgr8Wbc5bPI jTyA== X-Gm-Message-State: AOAM531AO1ZDGRCS61nc9rkzwJwTHpi/+QmBDi9M149bXEjCAdcPTMjk 1pZGuoCRxDAkMxQAwJeM4MQrx3l1UJ80xw== X-Google-Smtp-Source: ABdhPJw8QOzVeB98rEhQbecBsDCZoO1ZVnXMyrMUj/2fgX4CyjM9GVG261TqB6tRdjsecchPNQltbQ== X-Received: by 2002:a17:90a:be0e:: with SMTP id a14mr8856530pjs.131.1617289595901; Thu, 01 Apr 2021 08:06:35 -0700 (PDT) Received: from [192.168.1.21] (67.230.181.248.16clouds.com. [67.230.181.248]) by smtp.gmail.com with ESMTPSA id 11sm6051555pfn.146.2021.04.01.08.06.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 01 Apr 2021 08:06:35 -0700 (PDT) Sender: Glen Huang Content-Type: multipart/alternative; boundary=Apple-Mail-86E842A1-176B-4268-AE28-2C6ABECC3993 Content-Transfer-Encoding: 7bit From: Glen Huang Mime-Version: 1.0 (1.0) Subject: Re: Is replacing transactions with CTE a good idea? Date: Thu, 1 Apr 2021 23:06:32 +0800 Message-Id: <79097C11-B045-4D98-A427-4A82477E2634@gmail.com> References: Cc: pgsql-general@lists.postgresql.org In-Reply-To: To: Brian Dunavant X-Mailer: iPhone Mail (18D52) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail-86E842A1-176B-4268-AE28-2C6ABECC3993 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable > When you deal with updates/deletes, things can be trickier Care to expand why they are tricker? I presume they run the risk of being re= ferenced more than once? > On Apr 1, 2021, at 10:58 PM, Brian Dunavant wrote: >=20 > =EF=BB=BF >> On Thu, Apr 1, 2021 at 10:49 AM Glen Huang wrote: >> If I decide to replace all my transaction code with CTE, will I shoot mys= elf in the foot down the road? >=20 > I do this all the time and makes code way cleaner. It's very straightfor= ward with inserts queries. When you deal with updates/deletes, things can b= e trickier. I usually leave these in a transaction if there is any concern.= >=20 > They can also be hard for future programmers that may not understand SQL. = Make sure you comment your queries for maintainability long term. =20 >=20 > I have yet to regret replacing a transaction with a CTE over the past deca= de. YMMV > =20 --Apple-Mail-86E842A1-176B-4268-AE28-2C6ABECC3993 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
> When you deal with up= dates/deletes, things can be trickier

Care to expand why they are tricker? I presume they run the risk o= f being referenced more than once?

On Apr 1, 2021, at 10:58 PM, Brian Dunavant <dunavant@gmail.co= m> wrote:

=EF=BB=BF
On Thu, Apr 1, 2021 at 10:49 AM G= len Huang <heyhgl@g= mail.com> wrote:
If I decide to replace all my transaction code with CTE, will I shoot m= yself in the foot down the road?

I do this al= l the time and makes code way cleaner.   It's very straightforward= with inserts queries.  When you deal with updates/deletes, things can b= e trickier.  I usually leave these in a transaction if there is any con= cern.

They can also be hard for future programmers t= hat may not understand SQL.  Make sure you comment your queries for mai= ntainability long term.  

I have ye= t to regret replacing a transaction with a CTE over the past decade.  Y= MMV
 
= --Apple-Mail-86E842A1-176B-4268-AE28-2C6ABECC3993--