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 1lCAC2-0005Vv-54 for pgsql-general@arkaria.postgresql.org; Tue, 16 Feb 2021 23:55:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lCAC0-0007Fl-Ny for pgsql-general@arkaria.postgresql.org; Tue, 16 Feb 2021 23:55:44 +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 1lCAC0-0007Fc-DP for pgsql-general@lists.postgresql.org; Tue, 16 Feb 2021 23:55:44 +0000 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lCABy-0005K3-6N for pgsql-general@lists.postgresql.org; Tue, 16 Feb 2021 23:55:43 +0000 Received: by mail-pf1-x430.google.com with SMTP id b145so7224636pfb.4 for ; Tue, 16 Feb 2021 15:55:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=references:user-agent:from:to:cc:subject:date:in-reply-to :message-id:mime-version; bh=BH0Lanjah+gcuHTTsQ07mn07Ek+pW7D1wQNBUzjv9OA=; b=GFTFhnWqAeixjgX1SBlU3WzFi+m7v5Py/69h3nca8vdqud53Lu2Xxz+MMUeHWV7RAo 84yf7Lfh0r5K7+NU0TEXftQvSp0yRUjE7gg4LVCqZrugrSc6EQ0+so/5zTXChR1cUcoN B/SCVFHPpmmGB7nzrM3WocYqjrRPIiSEih3Hoav3+UyOocLRplqJ2tEYE0RIeCpKjUuL dn4640OxN2GGMOBoRaexdfnC/axxL/AX1BAy2foljdkY2sGKlRecUGUR0B1aw/eMvnfr 3n4nYow43ZgeEQX1fHqdorN5aeDmu51UR8yNx7399CIB/c6P4Qj32819P35Ci5GxT8bA QmHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:cc:subject:date :in-reply-to:message-id:mime-version; bh=BH0Lanjah+gcuHTTsQ07mn07Ek+pW7D1wQNBUzjv9OA=; b=BxBer0GNk7LmFB4K74ZSMFb0AIn294g3f0AYxLn3CPV+9srZDaynRYdZVxJC3nnnoy 9HQD3L2hZTlwSug7prlHV1vqp/9y+/R15+QoRI7zIUft8/tCxs/Lu/PKZdMpIqn6OJIQ N72gAc/ewSO/+1d+tMGeSr84C1gW0ZCtxJlcINkXFd/isgdCHS707oa54X/ojorw+gYu Y20YyfuHbRZFHUCryWbg79umw44+0oI+gmZ4UyBsqNhIbDri4Jw/jplruWI86yt8Spq3 26Td4Dy2nsI6vADtoRgXz3+hXhiZ83SBOAvFrI1T5b7KNvo72Qz//1+LFmBk4ouGJ2vC 3ehA== X-Gm-Message-State: AOAM530dnyIhlgfK7mwf3w+dYgIoTKLqq17ZsM2Wgm5NClqk56Eisu4b W2W7SrJmqQfxP5AnOEmIhnlq+/cGhJg= X-Google-Smtp-Source: ABdhPJw4OgCvRHj7XgF0R4woEQrRpS6fiY2l6UgtKXZ0L1dgf0+pe+Nx+lhOq+Krw2UUsEoEl7bHFg== X-Received: by 2002:aa7:824e:0:b029:1d2:a000:b0d8 with SMTP id e14-20020aa7824e0000b02901d2a000b0d8mr121248pfn.35.1613519740740; Tue, 16 Feb 2021 15:55:40 -0800 (PST) Received: from tim-desktop (106-69-135-243.dyn.iinet.net.au. [106.69.135.243]) by smtp.gmail.com with ESMTPSA id a19sm70533pff.186.2021.02.16.15.55.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Feb 2021 15:55:40 -0800 (PST) References: <87r1lfpp7k.fsf@gmail.com> User-agent: mu4e 1.5.8; emacs 27.1.91 From: Tim Cross To: "David G. Johnston" Cc: "pgsql-generallists.postgresql.org" Subject: Re: Syntax checking DO blocks and ALTER TABLE statements? Date: Wed, 17 Feb 2021 10:44:24 +1100 In-reply-to: Message-ID: <87im6rpnye.fsf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk David G. Johnston writes: > On Tue, Feb 16, 2021 at 4:28 PM Tim Cross wrote: > >> >> David G. Johnston writes: >> >> > On Tue, Feb 16, 2021 at 3:43 PM Ron wrote: >> > >> >> >> >> How does one go about syntax checking this? >> >> >> >> (There are 222 ALTER TABLE ADD FOREIGN KEY statements that I'm wrapping >> in >> >> similar DO blocks, and want to make sure the statements are clean.) >> >> >> >> >> > Begin a transaction, execute the DO, capture an error if there is one, >> > rollback the transaction. >> > >> >> As David points out, wrapping the whole thing in a transaction will at >> least guarantee it all succeeds or it is all rollled back. This can be >> frustrating if the statements are slow and there are a lot of them as it >> can result in a very tedious do-run-fix cycle. >> >> > I do presume that someone wanting to test their code in this manner would > be doing so in a test environment and an empty database. Which makes the > execution time very small. > True. However, it never ceases to amaze me how many places don't have such environments. Far too often, my first task when commencing a new engagement is to sort out environments and procedures to manage change. > I personally would also solve the "lot of them" problem by using dynamic > SQL, so one pretty much only has to test the code generator instead of all > the actual executions - which can simply be confirmed fairly quickly once > on a test database without the need for transactions. > Given the number, I think I would do the same. A good example of why being 'lazy' can be a virtue. Faster and easier to write a procedure to generate dynamic SQL than write out all those alter statements manually or even write it using a scripting language and ODBC if there is sufficient variation in the statements to make writing it in plsql 'messy'. -- Tim Cross