Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gYsFT-00083r-38 for pgsql-general@arkaria.postgresql.org; Mon, 17 Dec 2018 12:43:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gYsFR-00080x-Pn for pgsql-general@arkaria.postgresql.org; Mon, 17 Dec 2018 12:43:49 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gYsFR-00080q-Ef for pgsql-general@lists.postgresql.org; Mon, 17 Dec 2018 12:43:49 +0000 Received: from mail.postgrespro.ru ([93.174.131.138]) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gYsFO-0004Jo-P7 for pgsql-general@lists.postgresql.org; Mon, 17 Dec 2018 12:43:48 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id 0E17B21D322D for ; Mon, 17 Dec 2018 15:43:46 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=x tagged_above=-99 required=4 WHITELISTED tests=[] autolearn=unavailable Received: from [192.168.27.175] (gw.postgrespro.ru [93.174.131.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id D77ED21C89C6 for ; Mon, 17 Dec 2018 15:43:45 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1545050625; bh=uTiLCEmD2/Sk1JBF4RERB6oAt2WoTs3jiPu9ivbXdOw=; h=Subject:To:References:From:Date:In-Reply-To; b=VQkJcY49J3vtJ9yag3GOAKCloHntna2BFMEJmXqd2vY6b7qP9emuTR2Trmm+6UXOe KQKzKMQwA48lUHYAZpd3GARS9ydmitiHrK3RZTB4f7oLnsqn8g1UOQSurDmAqd7EDN NH/MzwW2rHCgF1UtTVBhmru7QlbtpHJcOAYmrVoM= Subject: Re: conditionally terminate psql script To: pgsql-general@lists.postgresql.org References: From: Pavel Luzanov Message-ID: <41416202-da55-3bdb-edab-c101fc7e69de@postgrespro.ru> Date: Mon, 17 Dec 2018 15:43:45 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------CFDA1D678C6A13F4F4F2442F" Content-Language: ru-RU List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------CFDA1D678C6A13F4F4F2442F Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Hi > is there a way to stop execution of a psql script if a select returns some rows (or no rows) > The idea is to add a safety check on data, specifically to select all new rows that would conflict > on a bulk insert, show them and stop Look at \if command in psql (since v10): select count(*) as total from pg_class where 1 = 1\gset select :total = 0 as notfound\gset \if :notfound    \echo Nothing found.    \q \endif \echo :total records found. ----- Pavel Luzanov Postgres Professional: http://www.postgrespro.com The Russian Postgres Company --------------CFDA1D678C6A13F4F4F2442F Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit
Hi
is there a way to stop execution of a psql script if a select returns some rows (or no rows)
The idea is to add a safety check on data, specifically to select all new rows that would conflict
on a bulk insert, show them and stop
Look at \if command in psql (since v10):
select count(*) as total from pg_class where 1 = 1\gset
select :total = 0 as notfound\gset
\if :notfound
   \echo Nothing found.
   \q
\endif
\echo :total records found.

    
-----
Pavel Luzanov
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company

  


--------------CFDA1D678C6A13F4F4F2442F--