pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedFor SELECT statement (just a reading one, no 'FOR UPDATE'), is COMMIT or ROLLBACK preferred?
5+ messages / 4 participants
[nested] [flat]
* For SELECT statement (just a reading one, no 'FOR UPDATE'), is COMMIT or ROLLBACK preferred?
@ 2019-08-25 19:09 David Wall <d.wall@computer.org>
2019-08-25 19:40 ` Re: For SELECT statement (just a reading one, no 'FOR UPDATE'), is COMMIT or ROLLBACK preferred? Rob Sargent <robjsargent@gmail.com>
0 siblings, 1 reply; 5+ messages in thread
From: David Wall @ 2019-08-25 19:09 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
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
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: For SELECT statement (just a reading one, no 'FOR UPDATE'), is COMMIT or ROLLBACK preferred?
2019-08-25 19:09 For SELECT statement (just a reading one, no 'FOR UPDATE'), is COMMIT or ROLLBACK preferred? David Wall <d.wall@computer.org>
@ 2019-08-25 19:40 ` Rob Sargent <robjsargent@gmail.com>
2019-08-25 20:12 ` Re: For SELECT statement (just a reading one, no 'FOR UPDATE'), is COMMIT or ROLLBACK preferred? David Wall <d.wall@computer.org>
0 siblings, 1 reply; 5+ messages in thread
From: Rob Sargent @ 2019-08-25 19:40 UTC (permalink / raw)
To: David Wall <d.wall@computer.org>; +Cc: pgsql-general@lists.postgresql.org
> On Aug 25, 2019, at 1:09 PM, David Wall <d.wall@computer.org> 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?
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: For SELECT statement (just a reading one, no 'FOR UPDATE'), is COMMIT or ROLLBACK preferred?
2019-08-25 19:09 For SELECT statement (just a reading one, no 'FOR UPDATE'), is COMMIT or ROLLBACK preferred? David Wall <d.wall@computer.org>
2019-08-25 19:40 ` Re: For SELECT statement (just a reading one, no 'FOR UPDATE'), is COMMIT or ROLLBACK preferred? Rob Sargent <robjsargent@gmail.com>
@ 2019-08-25 20:12 ` David Wall <d.wall@computer.org>
2019-08-25 20:27 ` Re: For SELECT statement (just a reading one, no 'FOR UPDATE'), is COMMIT or ROLLBACK preferred? Tom Lane <tgl@sss.pgh.pa.us>
2019-08-26 10:23 ` Re: For SELECT statement (just a reading one, no 'FOR UPDATE'), is COMMIT or ROLLBACK preferred? Luca Ferrari <fluca1978@gmail.com>
0 siblings, 2 replies; 5+ messages in thread
From: David Wall @ 2019-08-25 20:12 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 8/25/19 12:40 PM, Rob Sargent wrote
>> On Aug 25, 2019, at 1:09 PM, David Wall <d.wall@computer.org> 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.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: For SELECT statement (just a reading one, no 'FOR UPDATE'), is COMMIT or ROLLBACK preferred?
2019-08-25 19:09 For SELECT statement (just a reading one, no 'FOR UPDATE'), is COMMIT or ROLLBACK preferred? David Wall <d.wall@computer.org>
2019-08-25 19:40 ` Re: For SELECT statement (just a reading one, no 'FOR UPDATE'), is COMMIT or ROLLBACK preferred? Rob Sargent <robjsargent@gmail.com>
2019-08-25 20:12 ` Re: For SELECT statement (just a reading one, no 'FOR UPDATE'), is COMMIT or ROLLBACK preferred? David Wall <d.wall@computer.org>
@ 2019-08-25 20:27 ` Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 5+ messages in thread
From: Tom Lane @ 2019-08-25 20:27 UTC (permalink / raw)
To: David Wall <d.wall@computer.org>; +Cc: pgsql-general@lists.postgresql.org
David Wall <d.wall@computer.org> writes:
> ... 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.
They're different code paths, but I'd expect any performance difference
to be at the noise level; if nothing happened in the transaction then
no WAL traffic will be emitted in either case.
A more useful thing to think about, IMO, is this: if your app thinks
that the statement had no side-effects but actually it did (maybe it
called a volatile function that did something), would you want those
effects to be persisted or not?
regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: For SELECT statement (just a reading one, no 'FOR UPDATE'), is COMMIT or ROLLBACK preferred?
2019-08-25 19:09 For SELECT statement (just a reading one, no 'FOR UPDATE'), is COMMIT or ROLLBACK preferred? David Wall <d.wall@computer.org>
2019-08-25 19:40 ` Re: For SELECT statement (just a reading one, no 'FOR UPDATE'), is COMMIT or ROLLBACK preferred? Rob Sargent <robjsargent@gmail.com>
2019-08-25 20:12 ` Re: For SELECT statement (just a reading one, no 'FOR UPDATE'), is COMMIT or ROLLBACK preferred? David Wall <d.wall@computer.org>
@ 2019-08-26 10:23 ` Luca Ferrari <fluca1978@gmail.com>
1 sibling, 0 replies; 5+ messages in thread
From: Luca Ferrari @ 2019-08-26 10:23 UTC (permalink / raw)
To: David Wall <d.wall@computer.org>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>
On Sun, Aug 25, 2019 at 10:12 PM David Wall <d.wall@computer.org> wrote:
> 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.
Quite frankly I would redesign your application workflow. Sounds like
you are building a framework to issue queries, and I suggest you to
clearly mark transactions only when needed because, disregarding
performances, it does not make much sense to commit/rollback on a
"data quantity" discrimintation. At least, as far as you described it.
Moreover, as Tom pointed out, there could be a SELECT against a
function (that could return nothing at all) with side effects. How are
you going to discriminate such case?
Luca
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2019-08-26 10:23 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-08-25 19:09 For SELECT statement (just a reading one, no 'FOR UPDATE'), is COMMIT or ROLLBACK preferred? David Wall <d.wall@computer.org>
2019-08-25 19:40 ` Rob Sargent <robjsargent@gmail.com>
2019-08-25 20:12 ` David Wall <d.wall@computer.org>
2019-08-25 20:27 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-08-26 10:23 ` Luca Ferrari <fluca1978@gmail.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox