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 1lC9lq-0004d0-BV for pgsql-general@arkaria.postgresql.org; Tue, 16 Feb 2021 23:28:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lC9lp-0004b9-1g for pgsql-general@arkaria.postgresql.org; Tue, 16 Feb 2021 23:28:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lC9lo-0004b2-O9 for pgsql-general@lists.postgresql.org; Tue, 16 Feb 2021 23:28:40 +0000 Received: from mail-pj1-x102c.google.com ([2607:f8b0:4864:20::102c]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lC9lm-0005xf-2J for pgsql-general@lists.postgresql.org; Tue, 16 Feb 2021 23:28:40 +0000 Received: by mail-pj1-x102c.google.com with SMTP id l18so353838pji.3 for ; Tue, 16 Feb 2021 15:28:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=references:user-agent:from:to:subject:date:in-reply-to:message-id :mime-version; bh=uNznYNrf0kSgGt7g+SBfK+g/RxIIXOdqWHdv1ch5ajI=; b=mg6hh9+dLVSpsvvveHN4hXIh5LeAbYCqdfL3iRAtZuXLMSPYJYvgN9m0Rpd6FHuTpL D3q77Hl54ntaXYuxOAmwFCjEWceHhjLlM3B0A7F/kMmLFqTpWcNy9qAXskIPwLRXcINZ ZlETFxyJK8bWj/0Zs8laJtA8IXGB2osjcOTpclnQeQUQL3K4UCFM9bJNy7r/9oeylFhc AIgzsj7pGO6ets5ZYQiIlycGEcCQciKMf+ut5+32m97O+p6lFE8QlxGWYhTSMECXtuMs B9c12h9l5t8A4g8hY4mC7JDr5YvD0RIY58GS5OOpH/0KwiLAZlDiCloLYda+U2v74vpM PNiA== 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:subject:date :in-reply-to:message-id:mime-version; bh=uNznYNrf0kSgGt7g+SBfK+g/RxIIXOdqWHdv1ch5ajI=; b=kvBph1M/BPWzzxjQs9IAo1cEZHBHAgZINCwqGoJYjXPF15UhRYLtjjyQOiirYcZyFV 39Tz6dc3HDBB6JUfKw0Jhk/swPk/81ldyFO0C8p8eIY87fSAOHsQtBXCK40d1RZ/7owF LC2VhW94Kn585K/t8BIAoOljkwdoR3C6RQ3JQr/5fFTo7RPwLyv13tWzgYKHBpn32O+0 RAy5kVK8dAwsvLo9mNc5ugHHM/006PnJlNSbEMoRJQdtqu1phd67fITHS40huXXXJ5Hh lV50nfac+kMc2cwQpFylo4H86vdu0mgx7DYa+BAetQgV/VsFoQbR0xPIqwMZKm3AHru0 MVZQ== X-Gm-Message-State: AOAM530hMSrhIegGE0oeSYcsVnQIIkot5iuLB64rcRSasyjmcvDHgwPp unpQjO7Ldq15VLVxgeLI2vPXrZmjHoY= X-Google-Smtp-Source: ABdhPJxPgBmyu+jKWCdoWMosauTuUQf/jAdbsFEZ5QwbQBRb0raQUlWS7jQHXn2TX+Yw10wCuRrGXw== X-Received: by 2002:a17:90a:6901:: with SMTP id r1mr6084001pjj.131.1613518115130; Tue, 16 Feb 2021 15:28:35 -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 e24sm71261pjr.13.2021.02.16.15.28.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Feb 2021 15:28:34 -0800 (PST) References: User-agent: mu4e 1.5.8; emacs 27.1.91 From: Tim Cross To: pgsql-general@lists.postgresql.org Subject: Re: Syntax checking DO blocks and ALTER TABLE statements? Date: Wed, 17 Feb 2021 10:16:14 +1100 In-reply-to: Message-ID: <87r1lfpp7k.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 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. Something which can help is using an editor with good font highlighting and parsing support. One interesting area I've not yet looked at is the development of LSP (Language Server Protocol) servers for SQL. I've used LSP for other languages with great success. The challenge with databases is that there is enough variation between different vendor implementations to make accurate parsing and validation tedious to implement, so most solutions only focus on ANSI compliance. Still, that can be very useful. See https://github.com/lighttiger2505/sqls for one example of an LSP server for SQL and https://microsoft.github.io/language-server-protocol/ for more background on LSP and what it can provide. Many editors, including VSCode, VI, Emacs, TextMate etc now have some support for LSP. -- Tim Cross