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 1oqCNo-0006iB-K0 for pgsql-hackers@arkaria.postgresql.org; Wed, 02 Nov 2022 11:58:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oqCNn-0004dN-9X for pgsql-hackers@arkaria.postgresql.org; Wed, 02 Nov 2022 11:58:11 +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 1oqCNm-0004dD-RN for pgsql-hackers@lists.postgresql.org; Wed, 02 Nov 2022 11:58:11 +0000 Received: from mail-io1-xd35.google.com ([2607:f8b0:4864:20::d35]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oqCNg-0000Hm-P2 for pgsql-hackers@lists.postgresql.org; Wed, 02 Nov 2022 11:58:10 +0000 Received: by mail-io1-xd35.google.com with SMTP id z3so14794074iof.3 for ; Wed, 02 Nov 2022 04:58:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20210112.gappssmtp.com; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=7PISbU6ZQ4pYwqW8otvenFbny0I9ftqtRQA15+Ht2ew=; b=D1dV0hefWbthNUEATiMmbd1MCskU2S6wU9g2HmZNXyaxBRN9yv7bLtwBZdCIBu7ZFe +BZzMHSIeXt+maoKyoh0cvUXQBE90Gak8HK8e6o917wS2Cup/NYY4Nhi6hYwllNxbI26 e5OIqRRektPDW+/ZH1opD0YJEgytJg1IW0D2hlfMz3pB+5XGlag8p5mfcPex5Dt9EuTO BJ5TFjnByhrT1itEmOKXHvqwFT3GoVeFOqvrcVRJqkhUZI6FRwJoA0wE0DJTTTfna9Nq 7iMw9UdG5GHgiaJJRvQfVk/s8H7pq/0FsfItfUspBgH4gB8P2vdjcxWPR5aBAsCXNbaU 3awg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7PISbU6ZQ4pYwqW8otvenFbny0I9ftqtRQA15+Ht2ew=; b=sHnoH5N0DS7FanOEe1avae8BvZOkfqUwOvRUfQdE6scPmgQKegsRPvGDnKcON7E6SC nL8UDg3IguKBlLLRcnD68bdn8Nzkz7ejXy7A+cRlibKY/OYdlf8d+7PwXwAJ/WdbVQjP vCWCWvqz5vKZQnIPLQdhpSn2bd+AR5xgj93fUDbM8MiMvwIahrghMiBhbz1IxJ0Edho8 bNW5k9xnzQOGt8aMBCO9b+cHwrGQPDl43z5SFxGJvmXkfuPEhn1HlW6MfzJGrFU/CmZY sfL84fOc/UBEpk1xXkOVno0L4WcgviQ7esGX5fFhhjSl+INdlgcSLmAKxP/0GxjFM5D4 MYcA== X-Gm-Message-State: ACrzQf1IxK/O19gMURNX2fSntQd0GCv4IIgiexZkW+i0qVE+TCmNRP1D E3rZj4aOLz/D/iWWZy4bv4B014nS/nqNnbsL X-Google-Smtp-Source: AMsMyM5h1BfwQ8eVIeTLVcOwpJmL5a+V2VjXbAlsFaK/0fmSYDN9HomGo9AYO7RYMi3SQ3jP0v6+ew== X-Received: by 2002:a05:6638:2691:b0:363:cc6e:76b7 with SMTP id o17-20020a056638269100b00363cc6e76b7mr15037588jat.18.1667390282767; Wed, 02 Nov 2022 04:58:02 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id h15-20020a92d08f000000b002eb5eb4f8f9sm4452320ilh.77.2022.11.02.04.58.02 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 02 Nov 2022 04:58:02 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 719828008F8; Wed, 2 Nov 2022 06:58:01 -0500 (CDT) Date: Wed, 2 Nov 2022 06:58:01 -0500 From: Justin Pryzby To: bt22nakamorit Cc: pgsql-hackers@lists.postgresql.org Subject: Re: Make ON_ERROR_STOP stop on shell script failure Message-ID: <20221102115801.GG16921@telsasoft.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Sep 16, 2022 at 03:55:33PM +0900, bt22nakamorit wrote: > Hi, > > """\set ON_ERROR_STOP on""" stops any subsequent incoming query that comes > after an error of an SQL, but does not stop after a shell script ran by > """\! """ returning values other than 0, -1, or 127, which > suggests a failure in the result of the shell script. Actually, I think this could be described as a wider problem (not just ON_ERROR_STOP). The shell's exit status is being ignored (except for -1 and 127). Shouldn't the user be able to do something with the exit status ? Right now, it seems like they'd need to wrap the shellscript with "if ! ...; then echo failed; fi" and then \gset and compare with "failed" I think it'd be a lot better to expose the script status to psql. (without having to write "foo; echo status=$?"). Another consideration is that shellscripts can exit with a nonzero status due to the most recent conditional (like: if || &&). For example, consider shell command like: "if foo; then bar; fi" or "foo && bar" If foo has nonzero status, then bar isn't run. If that's the entire shell script, the shell will *also* exit with foo's nonzero status. (That's the reason why people write "exit 0" as the last line of a shell script. It's easy to believe that it was going to "exit 0" in any case; but, what it was actually going to do was to "exit $?", and $? can be nonzero after conditionals, even in "set -e" mode). So a psql script like this would start to report as a failure any time "foo" was false, even if that's the normal/typical case. -- Justin