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 1i1ysQ-0002Pf-D8 for pgsql-general@arkaria.postgresql.org; Sun, 25 Aug 2019 20:12:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i1ysP-0006oV-5M for pgsql-general@arkaria.postgresql.org; Sun, 25 Aug 2019 20:12:37 +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 1i1ysO-0006oK-Os for pgsql-general@lists.postgresql.org; Sun, 25 Aug 2019 20:12:36 +0000 Received: from smtp88.ord1c.emailsrvr.com ([108.166.43.88]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1i1ysL-0000Hn-ME for pgsql-general@lists.postgresql.org; Sun, 25 Aug 2019 20:12:35 +0000 X-Auth-ID: david.wall@myeastside.com Received: by smtp20.relay.ord1c.emailsrvr.com (Authenticated sender: david.wall-AT-myeastside.com) with ESMTPSA id BD1C5E00C1 for ; Sun, 25 Aug 2019 16:12:31 -0400 (EDT) X-Sender-Id: david.wall@myeastside.com Received: from [10.138.21.190] (50-46-115-165.evrt.wa.frontiernet.net [50.46.115.165]) (using TLSv1.2 with cipher AES128-SHA) by 0.0.0.0:465 (trex/5.7.12); Sun, 25 Aug 2019 16:12:32 -0400 Subject: Re: For SELECT statement (just a reading one, no 'FOR UPDATE'), is COMMIT or ROLLBACK preferred? To: pgsql-general@lists.postgresql.org References: <3e16442c-a60a-679a-e72e-6d895552b6f8@computer.org> <3C2ED636-A81D-4BC4-92A1-38634F5DD5D2@gmail.com> From: David Wall Message-ID: Date: Sun, 25 Aug 2019 13:12:30 -0700 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: <3C2ED636-A81D-4BC4-92A1-38634F5DD5D2@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 8/25/19 12:40 PM, Rob Sargent wrote >> On Aug 25, 2019, at 1:09 PM, David Wall wrote: >> >> Using the latest PostgreSQL, does it matter if my code does a ROLLBACK or a COMMIT on an non-modifying SELECT statement? My impression is they'd be the same as nothing is changed and therefore there's nothing to commit or rollback, but wondered if there was any difference in how they are processed by Postgres? >> >> Thanks, >> David >> >> >> > In interactive psql, both issue a warning that there is no current transaction. What is your auto-commit setting and how is your code sent to the server? We are accessing it via JDBC, and so we SQL via PreparedStatements against a Connection, and the connection is not auto-commit.  By default, the connection has a BEGIN TRANSACTION in place, so after all requests we do, we need to commit/rollback.  The main issue is that if we do a SELECT and get a ResultSet that has no rows, if we do a commit or a rollback, it seems reasonable that these are identical as no changes were made.  My inclination is to do a Connection.commit() on the connection because it wasn't in error or anything even if no rows were found, but wondered if a Connection.rollback() has any difference (positive/negative) in such a scenario.  We have SELECT sql statements that sometimes do a rollback after such queries because even though no rows was found is fine for SQL, it may be an issue in the application that expects there to be at least one row.  So we're trying to determine if there's actually any difference between commit/rollback after SELECT statements (with rows returned or not), a bit like if there's any difference for an UPDATE statement that returns zero rows were updated.