pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #6497: Error sent to client, but data written anyway
11+ messages / 6 participants
[nested] [flat]

* BUG #6497: Error sent to client, but data written anyway
@ 2012-02-28 16:23  rlowe@pablowe.net
  0 siblings, 1 reply; 11+ messages in thread

From: rlowe@pablowe.net @ 2012-02-28 16:23 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      6497
Logged by:          Ryan Lowe
Email address:      rlowe@pablowe.net
PostgreSQL version: 9.1.3
Operating system:   Linux
Description:        

There is an edge case where Postgres will return an error to the client, but
commit the operation anyway, thus breaking the contract with the client:

postgres=# begin; insert into s.t (c) values (1);
<postgres process core dumps (note that the forked processes were not
affected)>
postgres=# commit;
<this returns an error to the application>
<postgress process restarts>
The data is now on both the master and slave, but the application
believes that the transaction was rolled back.  A well-behaved
application will dutifully retry the transaction because it *should*
have rolled back.  However, the data is there so we'll have had the
transaction execute *twice*.




^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BUG #6497: Error sent to client, but data written anyway
@ 2012-02-28 21:38  Tom Lane <tgl@sss.pgh.pa.us>
  parent: rlowe@pablowe.net
  0 siblings, 1 reply; 11+ messages in thread

From: Tom Lane @ 2012-02-28 21:38 UTC (permalink / raw)
  To: rlowe@pablowe.net; +Cc: pgsql-bugs

rlowe@pablowe.net writes:
> There is an edge case where Postgres will return an error to the client, but
> commit the operation anyway, thus breaking the contract with the client:

> postgres=# begin; insert into s.t (c) values (1);
> <postgres process core dumps (note that the forked processes were not
> affected)>
> postgres=# commit;
> <this returns an error to the application>
> <postgress process restarts>
> The data is now on both the master and slave, but the application
> believes that the transaction was rolled back.  A well-behaved
> application will dutifully retry the transaction because it *should*
> have rolled back.  However, the data is there so we'll have had the
> transaction execute *twice*.

Huh?  If the backend dumped core before you sent it the commit, the
data will certainly not be committed.  It might be physically present on
disk, but it won't be considered valid.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BUG #6497: Error sent to client, but data written anyway
@ 2012-02-28 22:01  Kevin Grittner <Kevin.Grittner@wicourts.gov>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 11+ messages in thread

From: Kevin Grittner @ 2012-02-28 22:01 UTC (permalink / raw)
  To: rlowe@pablowe.net; Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs

Tom Lane <tgl@sss.pgh.pa.us> wrote:
> rlowe@pablowe.net writes:
>> There is an edge case where Postgres will return an error to the
>> client, but commit the operation anyway, thus breaking the
>> contract with the client:
> 
>> postgres=# begin; insert into s.t (c) values (1);
>> <postgres process core dumps (note that the forked processes were
>> not affected)>
>> postgres=# commit;
>> <this returns an error to the application>
>> <postgress process restarts>
>> The data is now on both the master and slave, but the application
>> believes that the transaction was rolled back.  A well-behaved
>> application will dutifully retry the transaction because it
>> *should* have rolled back.  However, the data is there so we'll
>> have had the transaction execute *twice*.
> 
> Huh?  If the backend dumped core before you sent it the commit,
> the data will certainly not be committed.  It might be physically
> present on disk, but it won't be considered valid.
 
I suppose that there is a window of time between the commit becoming
effective and the return to the application which, from a user
perspective, would be hard to distinguish from what the OP
described.  I don't really see how that can be avoided, though,
short of a transaction manager using 2PC.  Any time the server
crashes while a COMMIT is pending, one must check to see whether it
"took".
 
As long as the client application sees the connection as dead in
this situation, I think PostgreSQL is doing everything just as it
should.
 
-Kevin



^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BUG #6497: Error sent to client, but data written anyway
@ 2012-02-28 22:42  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Kevin Grittner <Kevin.Grittner@wicourts.gov>
  0 siblings, 1 reply; 11+ messages in thread

From: Tom Lane @ 2012-02-28 22:42 UTC (permalink / raw)
  To: Kevin Grittner <Kevin.Grittner@wicourts.gov>; +Cc: rlowe@pablowe.net; pgsql-bugs

"Kevin Grittner" <Kevin.Grittner@wicourts.gov> writes:
> Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> Huh?  If the backend dumped core before you sent it the commit,
>> the data will certainly not be committed.  It might be physically
>> present on disk, but it won't be considered valid.
 
> I suppose that there is a window of time between the commit becoming
> effective and the return to the application which, from a user
> perspective, would be hard to distinguish from what the OP
> described.  I don't really see how that can be avoided, though,
> short of a transaction manager using 2PC.  Any time the server
> crashes while a COMMIT is pending, one must check to see whether it
> "took".

Yeah, a post-commit crash leaves you in doubt about whether the commit
"took", but you get into byzantine-generals issues as soon as you try to
design that out.  Consider the possibility that the commit went fine but
the network eats the reply packet saying so.  Really, any application
that is constructed to reconnect and retry has got to have logic to test
whether the commit occurred, not just assume that it did or didn't.

(2PC is hardly a magic bullet for this, either.)

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BUG #6497: Error sent to client, but data written anyway
@ 2012-02-29 17:26  Ryan Lowe <rlowe@pablowe.net>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 11+ messages in thread

From: Ryan Lowe @ 2012-02-29 17:26 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Kevin Grittner <Kevin.Grittner@wicourts.gov>; pgsql-bugs

Thanks for all the responses, but I think I'm being unclear here.  Let's
walk through this case step-by-step.  I start with a happy instance of
Postgres:

<pre>
% pstree -anpcul postgres
postgres,1944 -D /data/pgsql     # Main process
  ├─postgres,1948                      # Logger process
  ├─postgres,1950                     # Writer process
  ├─postgres,1952                     # WAL writer process
  ├─postgres,1953          # Auto vacuum launcher process
  ├─postgres,1954                 # Archiver process
  ├─postgres,1955              # Stats collector process
  ├─postgres,1963         # WAL sender process
</pre>

(formatted for readability)

To mimic the failure scenario I am describing, let's start by creating a
table and opening a transaction:

<pre>
postgres=# CREATE TABLE test.t1 (c1 int);
postgres=# BEGIN; INSERT INTO test.t1 (c1) VALUES (1);
</pre>

At this point (in another shell), let's mimic a crash of the main process:

<pre>
% kill -9 1944 # Note this was postgres -D /data/pgsql from the pstree above
<pre>

Now let's see what is running:

<pre>
% kill -9 1944
% pstree -anpcul postgres
postgres,1948    # logger process
postgres,1950    # writer process
postgres,1955    # stats collector process
</pre>

The processes have all been adopted by init (expected).  If we go back to
the original psql session after postgres is in the above state and complete
the transaction, this is what happens:

<pre>
postgres=# COMMIT;
The connection to the server was lost. Attempting reset: Failed.
</pre>

Horray.  We've received an error because the transaction couldn't be
committed, right?  Let's verify:

<pre>
% service postgresql-9.1 start
% psql
postgres=# SELECT * FROM test.t1;
 c1
------
    1
(1 row)
</pre>

Clearly this is not correct behavior.  There has been talk in this thread
that the application should simply always try and validate that its
transactions have in fact failed, but that is not a feasible solution (for
many reasons).  Thoughts?

-- Ryan Lowe


On Tue, Feb 28, 2012 at 2:42 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

> "Kevin Grittner" <Kevin.Grittner@wicourts.gov> writes:
> > Tom Lane <tgl@sss.pgh.pa.us> wrote:
> >> Huh?  If the backend dumped core before you sent it the commit,
> >> the data will certainly not be committed.  It might be physically
> >> present on disk, but it won't be considered valid.
>
> > I suppose that there is a window of time between the commit becoming
> > effective and the return to the application which, from a user
> > perspective, would be hard to distinguish from what the OP
> > described.  I don't really see how that can be avoided, though,
> > short of a transaction manager using 2PC.  Any time the server
> > crashes while a COMMIT is pending, one must check to see whether it
> > "took".
>
> Yeah, a post-commit crash leaves you in doubt about whether the commit
> "took", but you get into byzantine-generals issues as soon as you try to
> design that out.  Consider the possibility that the commit went fine but
> the network eats the reply packet saying so.  Really, any application
> that is constructed to reconnect and retry has got to have logic to test
> whether the commit occurred, not just assume that it did or didn't.
>
> (2PC is hardly a magic bullet for this, either.)
>
>                        regards, tom lane
>

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BUG #6497: Error sent to client, but data written anyway
@ 2012-02-29 18:56  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Ryan Lowe <rlowe@pablowe.net>
  1 sibling, 1 reply; 11+ messages in thread

From: Tom Lane @ 2012-02-29 18:56 UTC (permalink / raw)
  To: Ryan Lowe <rlowe@pablowe.net>; +Cc: Kevin Grittner <Kevin.Grittner@wicourts.gov>; pgsql-bugs

Ryan Lowe <rlowe@pablowe.net> writes:
> Thanks for all the responses, but I think I'm being unclear here.  Let's
> walk through this case step-by-step.  I start with a happy instance of
> Postgres:

This example does not have anything to do with the actual behavior of
Postgres, at least not on any system I know about.  Killing the
postmaster does not cause child backends to die, and it certainly
doesn't cause them to commit open transactions and then die.

The system would be quite seriously broken if it acted as you describe.
I tested this, just to see if somebody had broken it when I wasn't
looking.  The behavior that I see here is:
1. Killing the postmaster doesn't affect open transactions.
2. Killing the specific backend prevents the transaction from committing.
Which is what I expected.

> ... There has been talk in this thread
> that the application should simply always try and validate that its
> transactions have in fact failed, but that is not a feasible solution (for
> many reasons).  Thoughts?

You might wish to believe that you can ignore the problem, but you can't.
No matter what Postgres does or doesn't do, external issues such as
network failures can create the problem of a transaction possibly being
committed while the client remains in doubt whether it happened or not.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BUG #6497: Error sent to client, but data written anyway
@ 2012-02-29 21:05  Christophe Pettus <xof@thebuild.com>
  parent: Ryan Lowe <rlowe@pablowe.net>
  1 sibling, 1 reply; 11+ messages in thread

From: Christophe Pettus @ 2012-02-29 21:05 UTC (permalink / raw)
  To: Ryan Lowe <rlowe@pablowe.net>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Kevin Grittner <Kevin.Grittner@wicourts.gov>; pgsql-bugs


On Feb 29, 2012, at 9:26 AM, Ryan Lowe wrote:

> Thanks for all the responses, but I think I'm being unclear here.

The point Tom was making is that this is indistinguishable from the scenario:

1. Client sends commit.
2. Server successfully commits data.
3. Server starts to write response.
4. Network fails.
5. Client receives abnormal disconnection response from its network connection to the server.

--
-- Christophe Pettus
   xof@thebuild.com




^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BUG #6497: Error sent to client, but data written anyway
@ 2012-02-29 21:15  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Christophe Pettus <xof@thebuild.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Tom Lane @ 2012-02-29 21:15 UTC (permalink / raw)
  To: Christophe Pettus <xof@thebuild.com>; +Cc: Ryan Lowe <rlowe@pablowe.net>; Kevin Grittner <Kevin.Grittner@wicourts.gov>; pgsql-bugs

Christophe Pettus <xof@thebuild.com> writes:
> On Feb 29, 2012, at 9:26 AM, Ryan Lowe wrote:
>> Thanks for all the responses, but I think I'm being unclear here.

> The point Tom was making is that this is indistinguishable from the scenario:

> 1. Client sends commit.
> 2. Server successfully commits data.
> 3. Server starts to write response.
> 4. Network fails.
> 5. Client receives abnormal disconnection response from its network connection to the server.

Well, that's the argument as to why the client code has to be capable of
checking whether the commit happened if it's going to attempt a
reconnect and retry.  But there's a quite separate question as to
whether the behavior Ryan is claiming for a pre-commit crash is actually
possible.  I don't believe it, and I failed to reproduce his test
scenario.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BUG #6497: Error sent to client, but data written anyway
@ 2012-02-29 21:22  Christophe Pettus <xof@thebuild.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 11+ messages in thread

From: Christophe Pettus @ 2012-02-29 21:22 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Ryan Lowe <rlowe@pablowe.net>; Kevin Grittner <Kevin.Grittner@wicourts.gov>; pgsql-bugs


On Feb 29, 2012, at 1:15 PM, Tom Lane wrote:

> But there's a quite separate question as to
> whether the behavior Ryan is claiming for a pre-commit crash is actually
> possible.  I don't believe it, and I failed to reproduce his test
> scenario.

Did you check it with killing the postmaster (as Ryan was) as opposed to the backend?

--
-- Christophe Pettus
   xof@thebuild.com




^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BUG #6497: Error sent to client, but data written anyway
@ 2012-02-29 21:27  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Christophe Pettus <xof@thebuild.com>
  0 siblings, 0 replies; 11+ messages in thread

From: Tom Lane @ 2012-02-29 21:27 UTC (permalink / raw)
  To: Christophe Pettus <xof@thebuild.com>; +Cc: Ryan Lowe <rlowe@pablowe.net>; Kevin Grittner <Kevin.Grittner@wicourts.gov>; pgsql-bugs

Christophe Pettus <xof@thebuild.com> writes:
> On Feb 29, 2012, at 1:15 PM, Tom Lane wrote:
>> But there's a quite separate question as to
>> whether the behavior Ryan is claiming for a pre-commit crash is actually
>> possible.  I don't believe it, and I failed to reproduce his test
>> scenario.

> Did you check it with killing the postmaster (as Ryan was) as opposed to the backend?

I tried both, and got the expected (though different) results both
ways.  See my previous response.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BUG #6497: Error sent to client, but data written anyway
@ 2012-02-29 23:10  Tatsuo Ishii <ishii@postgresql.org>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 11+ messages in thread

From: Tatsuo Ishii @ 2012-02-29 23:10 UTC (permalink / raw)
  To: tgl@sss.pgh.pa.us; +Cc: rlowe@pablowe.net; Kevin.Grittner@wicourts.gov; pgsql-bugs

> You might wish to believe that you can ignore the problem, but you can't.
> No matter what Postgres does or doesn't do, external issues such as
> network failures can create the problem of a transaction possibly being
> committed while the client remains in doubt whether it happened or not.

I think this is a well known issue and the only solution is a
transaction system covering the communication path. Since there's no
such a transaction system exists in PostgreSQL, clients should be very
carefull when try to resend DMLs as you already pointed out.

F.Y.I. pgpool-II disconnects the connection to PostgreSQL upon failure
including network problem, rather than trying to resend packet to
PostgreSQL due to the reason stated above.
--
Tatsuo Ishii
SRA OSS, Inc. Japan
English: http://www.sraoss.co.jp/index_en.php
Japanese: http://www.sraoss.co.jp



^ permalink  raw  reply  [nested|flat] 11+ messages in thread


end of thread, other threads:[~2012-02-29 23:10 UTC | newest]

Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2012-02-28 16:23 BUG #6497: Error sent to client, but data written anyway rlowe@pablowe.net
2012-02-28 21:38 ` Tom Lane <tgl@sss.pgh.pa.us>
2012-02-28 22:01   ` Kevin Grittner <Kevin.Grittner@wicourts.gov>
2012-02-28 22:42     ` Tom Lane <tgl@sss.pgh.pa.us>
2012-02-29 17:26       ` Ryan Lowe <rlowe@pablowe.net>
2012-02-29 18:56         ` Tom Lane <tgl@sss.pgh.pa.us>
2012-02-29 23:10           ` Tatsuo Ishii <ishii@postgresql.org>
2012-02-29 21:05         ` Christophe Pettus <xof@thebuild.com>
2012-02-29 21:15           ` Tom Lane <tgl@sss.pgh.pa.us>
2012-02-29 21:22             ` Christophe Pettus <xof@thebuild.com>
2012-02-29 21:27               ` Tom Lane <tgl@sss.pgh.pa.us>

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