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 1lRysS-0007Vp-Bc for pgsql-general@arkaria.postgresql.org; Thu, 01 Apr 2021 15:04:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lRysR-0006ic-A6 for pgsql-general@arkaria.postgresql.org; Thu, 01 Apr 2021 15:04:55 +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 1lRys6-0001iH-H3 for pgsql-general@lists.postgresql.org; Thu, 01 Apr 2021 15:04:34 +0000 Received: from mail-oo1-xc34.google.com ([2607:f8b0:4864:20::c34]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lRys4-0002pq-7X for pgsql-general@lists.postgresql.org; Thu, 01 Apr 2021 15:04:33 +0000 Received: by mail-oo1-xc34.google.com with SMTP id w1-20020a4adec10000b02901bc77feac3eso631873oou.3 for ; Thu, 01 Apr 2021 08:04:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=R3HFrTbjMG+PzfRxmSA3PxX2N9wMppzdgHT7AMabstU=; b=dwOEMg1MZ8vt+Dnf0b7xpYnWGCsMotkDCEd+r1txu0TV64eos4aUvagJUPZX0FNeL/ 6JxW425sNKzCshTvD3XEQdnmwBPInEpFhDmt6WKXCnEk7AynMrET+PrBKbv12sYvHYz1 Hd88J/tQ16+12VbC6sNeNKqR5P+QibHMKafAkNxEI34XH0Isc6fYHsYKlYrsDy9PIusa hBJBJIO+r3vKfCrCliIamxc9BpHjaAVVpFc6G7suw1WYq/Afi9K+6dzhrQSWeeFFPNIs lB4ZKHJg/1oyG4syoH3bwwMSieN7txhOsn6m/oc2IPpQcStBhdkGvl+BUFVFReycB9Lt JWjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=R3HFrTbjMG+PzfRxmSA3PxX2N9wMppzdgHT7AMabstU=; b=TErL0OiteGUoU42/LD2oqDDpR/5/S3BXXooFzQ0ICnJ2IxJlhM8JGZt/Bfl++CUi9g Y2NKhV4WpdAVbmXE9137IxuND8TXh1MpP+GSL66nwLsPm9VYjmedfu/rgDxf5tm3LUe0 Enl81rEvK0L2CDTcngvVjB0+cAO5/XS87eUAoPAmHk0Vvp72Skg6OR7t/YDyjr9FnLj4 8OLl+lxBrR+CJgsVNUOQ8g1d4oDdx0CPTG2QCywnpR/yCdiwhEMF9C2aH5KBqW/jl2P6 KlfIvPkSa+bH2ksr2yC3Xrg6qJy114bgL5wtC8CHm3oVktBhi76VncDvyJ9A01MkvLIx +GNA== X-Gm-Message-State: AOAM530hFvQSwzvApCLouC1SYLcDKnRGAF+RNqG/UdIsca5XRSIvlGoG 0Rwjq79Rn247sgEx+TWqOeIkykJFmmo= X-Google-Smtp-Source: ABdhPJy+cVCWBtBSHIWs5E5FycJkxsNMbWQEnKz9a78B+rHjpfvZSaT7WrVw3eNHEZ4SroI0bgPo3Q== X-Received: by 2002:a4a:d10f:: with SMTP id k15mr7579711oor.82.1617289471051; Thu, 01 Apr 2021 08:04:31 -0700 (PDT) Received: from ?IPv6:2601:681:5500:dde0:24d6:a833:731f:baf3? ([2601:681:5500:dde0:24d6:a833:731f:baf3]) by smtp.gmail.com with ESMTPSA id n12sm1261676otq.42.2021.04.01.08.04.30 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 01 Apr 2021 08:04:30 -0700 (PDT) Subject: Re: Is replacing transactions with CTE a good idea? To: pgsql-general@lists.postgresql.org References: From: Rob Sargent Message-ID: <02654280-7b22-1231-8de7-c74a1a62e426@gmail.com> Date: Thu, 1 Apr 2021 09:04:29 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------A83706ABF33363C356A675A5" Content-Language: en-CA List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------A83706ABF33363C356A675A5 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 4/1/21 8:58 AM, Brian Dunavant wrote: > 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 myself in the foot down the road? > > > I do this all the time and makes code way cleaner.   It's very > straightforward with inserts queries.  When you deal with > updates/deletes, things can be trickier.  I usually leave these in a > transaction if there is any concern. > > They can also be hard for future programmers that may not understand > SQL.  Make sure you comment your queries for maintainability long term. > > I have yet to regret replacing a transaction with a CTE over the past > decade.  YMMV This must assume auto-commit mode where every single statement is "committed"? --------------A83706ABF33363C356A675A5 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit
On 4/1/21 8:58 AM, Brian Dunavant wrote:
On Thu, Apr 1, 2021 at 10:49 AM Glen Huang <heyhgl@gmail.com> wrote:
If I decide to replace all my transaction code with CTE, will I shoot myself in the foot down the road?

I do this all the time and makes code way cleaner.   It's very straightforward with inserts queries.  When you deal with updates/deletes, things can be trickier.  I usually leave these in a transaction if there is any concern.

They can also be hard for future programmers that may not understand SQL.  Make sure you comment your queries for maintainability long term.  

I have yet to regret replacing a transaction with a CTE over the past decade.  YMMV
 
This must assume auto-commit mode where every single statement is "committed"?
--------------A83706ABF33363C356A675A5--