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 1hQRnr-0000F3-8E for pgsql-general@arkaria.postgresql.org; Tue, 14 May 2019 07:24:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hQRno-0001HY-GG for pgsql-general@arkaria.postgresql.org; Tue, 14 May 2019 07:24:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hQRno-0001HR-6a for pgsql-general@lists.postgresql.org; Tue, 14 May 2019 07:24:44 +0000 Received: from mail114-240.sinamail.sina.com.cn ([218.30.114.240]) by magus.postgresql.org with smtp (Exim 4.89) (envelope-from ) id 1hQRnf-0004Y2-4c for pgsql-general@postgresql.org; Tue, 14 May 2019 07:24:41 +0000 Received: from unknown (HELO t460p)([120.132.1.226]) by sina.com with ESMTP id 5CDA6D25000017E8; Tue, 14 May 2019 15:24:22 +0800 (CST) X-Sender: wztdyl@sina.com X-Auth-ID: wztdyl@sina.com X-SMAIL-MID: 8987225953090 Message-ID: <30cea72b59177bb5a611d540e9fe40d48ba8243b.camel@sina.com> Subject: Re: psql dones't reflect exit status if input command via stdin From: magodo To: "David G. Johnston" Cc: Postgres general Date: Tue, 14 May 2019 15:24:21 +0800 In-Reply-To: References: Content-Type: multipart/alternative; boundary="=-mCHk5EdUrGwJptsKyfXe" User-Agent: Evolution 3.32.1 MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --=-mCHk5EdUrGwJptsKyfXe Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit On Mon, 2019-05-13 at 23:39 -0700, David G. Johnston wrote: > On Mon, May 13, 2019 at 11:24 PM magodo wrote: > > I found when running command like `# echo "xxx" | psql postgres > > > > postgres`, the return code is always 0 even though the command > > ("xxx") > > > > here is of invalid syntax. While the `psql -c` way handled exit > > code > > > > correctly. > > Its only required to handle things as documented, which this is: > > "psql returns 0 to the shell if it finished normally, 1 if a fatal > error of its own occurs (e.g. out of memory, file not found), 2 if > the connection to the server went bad and the session was not > interactive, and 3 if an error occurred in a script and the variable > ON_ERROR_STOP was set." > > The pipe-version causes psql to execute stdin as a script. By > default ON_ERROR_STOP is unset. Thus psql finished processing the > script normally and while it encountered an error it continued past > the error as opposed to stopping with exit code 3. > > Adding "\set ON_ERROR_STOP 1" to your .psqlrc will result in psql > behaving in the way you expect. > > David J. Hi David, Thank you for your quick and excellent answer 😊 --- Zhaoting.Weng --=-mCHk5EdUrGwJptsKyfXe Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable
On Mon, 2019-05-13 at 23:39 -0700, David G. Johnston wrote:
<= blockquote type=3D"cite" style=3D"margin:0 0 0 .8ex; border-left:2px #729fc= f solid;padding-left:1ex">
On Mon, May 13,= 2019 at 11:24 PM magodo <wztdyl@sina= .com> wrote:
I found when running command like `# echo "xxx" | psql postgres
postgres`, the return code is always 0 even though the command ("xxx")
here is of invalid syntax. While the `psql -c` way handled exit code
correctly.

Its only required to handle th= ings as documented, which this is:

"psql returns 0 to= the shell if it finished normally, 1 if a fatal error of its own occurs (e= .g. out of memory, file not found), 2 if the connection to the server went = bad and the session was not interactive, and 3 if an error occurred in a sc= ript and the variable ON_ERROR_STOP was set."
 
Th= e pipe-version causes psql to execute stdin as a script.  By default O= N_ERROR_STOP is unset.  Thus psql finished processing the script norma= lly and while it encountered an error it continued past the error as oppose= d to stopping with exit code 3.

Adding "\set ON_ERROR_= STOP 1" to your .psqlrc will result in psql behaving in the way you expect.=

David J.

Hi David,

Thank y= ou for your quick and excellent answer =F0=9F=98=8A

---
Zhaoting.Weng

--=-mCHk5EdUrGwJptsKyfXe--