pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
SIGQUIT and lost sequence WAL records
4+ messages / 2 participants
[nested] [flat]

* SIGQUIT and lost sequence WAL records
@ 2016-02-17 13:55  Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Michael Paquier @ 2016-02-17 13:55 UTC (permalink / raw)
  To: pgsql-bugs

Hi all,

Lately a couple of colleagues have pinged me regarding the fact that a
server signaled with SIGQUIT, or "pg_ctl stop -m immediate", may
ignore sequence WAL records. See for example the script attached
seqtest.py (simplified test case provided from my colleague Nikhil
Deshpande), that will fail while for example the following command is
run in parallel:
for i in {1..1000}; do pg_ctl restart -m immediate -w; sleep 1; done
The failure happening being for example:
AssertionError: DB returned reused sequence value prev=1118 curr=958
Here it means that the last return value of nextval() was 958, even if
the highest value ever fetched was 1118. Note that this test script
uses psycopg2, but that it does *not* commit transactions on purpose.
Also note that those WAL records do not disappear all the time.
Sometimes they are here and the sequence keeps increasing monotically
after server is restarted.

Trying to find out if there were records ignored I added some logs in
the server thanks to the patch attached. In the case of this last
failure there have been a couple of records after the last one found
at recovery:
WARNING:  Logged new sequence value at 0/1505570 for nextval = 957
WARNING:  Logged new sequence value at 0/15066D8 for nextval = 990
WARNING:  Logged new sequence value at 0/1507828 for nextval = 1023
WARNING:  Logged new sequence value at 0/1508990 for nextval = 1056
WARNING:  Logged new sequence value at 0/1509AE0 for nextval = 1089
WARNING:  Logged new sequence value at 0/150AC48 for nextval = 1122
And if I looked at the WAL records those were not of course not
present. That's not really surprising as recovery didn't use them to
restore the sequence up to where it should have.

func.sgml mentions that a nextval() operation is *never* rolled back
even if a transaction is aborted, though in this case this is exactly
what is happening, even if when the server is stopped the existing
transactions running are forcibly aborted.

I am lacking of fuel to look more at that today, but at this point I
wanted to get some input to see if I am missing something obvious. So,
code bug or documentation bug?
-- 
Michael

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

sequence-log-info.patch (text/x-diff, 509B) download diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c index c98f981..3c658ce 100644 --- a/src/backend/commands/sequence.c +++ b/src/backend/commands/sequence.c @@ -750,6 +750,8 @@ nextval_internal(Oid relid) XLogRegisterData((char *) seqtuple.t_data, seqtuple.t_len); recptr = XLogInsert(RM_SEQ_ID, XLOG_SEQ_LOG); + elog(WARNING, "Logged new sequence value at %X/%X for nextval = %ld", + (uint32) (recptr >> 32), (uint32) recptr, next); PageSetLSN(page, recptr); }
seqtest.py (text/x-python-script, 2.8K) download #!/usr/bin/env python import psycopg2 import time def log(msg): print msg def GetConn(): global conn global seq retryCount = 20 retrySleep = 1 # sec while retryCount > 0: try: conn = psycopg2.connect("dbname='ioltas' user='ioltas' host='localhost'") if seq is None: seq = PgSeq(seqName) return except psycopg2.OperationalError as e: log('Error connecting due to: %s, retrying' % (e)) retryCount -= 1 time.sleep(retrySleep) except psycopg2.DatabaseError as e: log('Error connecting due to: %s, retrying' % (e)) retryCount -= 1 time.sleep(retrySleep) assert False, 'GetConn retry failed' def ExecSql(sql, returnOutput=True): global conn retryCount = 5 while retryCount > 0: try: with conn.cursor() as cur: cur.execute(sql) if returnOutput: result = cur.fetchall() else: result = None # Make changes persistent # Not committing here enforces everything to be part # of the same transaction for the same connection. # conn.commit() return result except psycopg2.OperationalError as e: retryCount -= 1 log('Error executing %s due to: %s, retrying' % (sql, e)) GetConn() except psycopg2.DatabaseError as e: assert 'duplicate key value' not in str(e), str(e) retryCount -= 1 log('Error executing %s due to: %s, retrying' % (sql, e)) GetConn() assert False, 'ExecSql retry failed for %s' % sql class PgSeq(object): def __init__(self, name): self.name = name self.currentKey = -1 def GetNext(self): prevKey = self.currentKey self.currentKey = ExecSql("SELECT NEXTVAL('%s');" % self.name)[0][0] assert prevKey <= self.currentKey, 'DB returned reused sequence '\ 'value prev=%d curr=%d' % (prevKey, self.currentKey) log('Fetched %s. prev=%d nextval=%d' % \ (self.name, prevKey, self.currentKey)) return self.currentKey seqName = 'testseq' tblName = 'tbl' schemaSql = ['DROP SEQUENCE IF EXISTS %s;' % seqName, 'CREATE SEQUENCE %s INCREMENT BY 1;' % seqName, 'DROP TABLE IF EXISTS %s;' % tblName, 'CREATE TABLE %s (id NUMERIC(38) PRIMARY KEY);' % tblName, 'COMMIT;', ] conn = None seq = None def InitSchema(): for sql in schemaSql: ExecSql(sql, False) def RunWorkload(): while True: seqVal = seq.GetNext() ExecSql('INSERT INTO %s VALUES(%d);' % (tblName, seqVal), False) log('Inserted %d into %s' % (seqVal, tblName)) def main(): GetConn() InitSchema() RunWorkload() if __name__ == "__main__": main()
^ permalink raw reply [nested|flat] 4+ messages in thread

* Re: SIGQUIT and lost sequence WAL records
@ 2016-02-17 14:54  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Tom Lane @ 2016-02-17 14:54 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; +Cc: pgsql-bugs

Michael Paquier <michael.paquier@gmail.com> writes:
> Lately a couple of colleagues have pinged me regarding the fact that a
> server signaled with SIGQUIT, or "pg_ctl stop -m immediate", may
> ignore sequence WAL records.

I've not gone through this in detail, but I think what is happening
is that since the test case never commits any transactions, the
sequence-advance WAL records may not get flushed to disk, and thus
of course are not available for replay.  This is not a bug IMO, as
by the exact same token, no uses of those nextval() values can appear
in any committed tuples, so no database inconsistency is possible.

If an application is using nextval() results *externally to the
database*, it's unsafe for it to rely on those values being unique
unless/until it commits the nextval() calls.

I seem to recall some past discussions about whether a transaction
that commits after writing only a sequence advance (that is, it
did nextval() and nothing else) needs to flush WAL.  I think at one
point it did not but we changed it because of this consideration.

[ digs around... ]  That was a long time ago: see 01747692f.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



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

* Re: SIGQUIT and lost sequence WAL records
@ 2016-02-18 00:10  Michael Paquier <michael.paquier@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 4+ messages in thread

From: Michael Paquier @ 2016-02-18 00:10 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs

On Wed, Feb 17, 2016 at 11:54 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Michael Paquier <michael.paquier@gmail.com> writes:
>> Lately a couple of colleagues have pinged me regarding the fact that a
>> server signaled with SIGQUIT, or "pg_ctl stop -m immediate", may
>> ignore sequence WAL records.
>
> I've not gone through this in detail, but I think what is happening
> is that since the test case never commits any transactions, the
> sequence-advance WAL records may not get flushed to disk, and thus
> of course are not available for replay.  This is not a bug IMO, as
> by the exact same token, no uses of those nextval() values can appear
> in any committed tuples, so no database inconsistency is possible.
>
> If an application is using nextval() results *externally to the
> database*, it's unsafe for it to rely on those values being unique
> unless/until it commits the nextval() calls.

That's for the confirmation. Yeah I already suggested those folks to
issue a commit to ensure that nextval() remains consistent.

> I seem to recall some past discussions about whether a transaction
> that commits after writing only a sequence advance (that is, it
> did nextval() and nothing else) needs to flush WAL.  I think at one
> point it did not but we changed it because of this consideration.
>
> [ digs around... ]  That was a long time ago: see 01747692f.

That's a part of history...

The documentation is stating the following regarding this behavior:
       <important>
        <para>
         To avoid blocking concurrent transactions that obtain numbers from the
         same sequence, a <function>nextval</function> operation is never
         rolled back; that is, once a value has been fetched it is considered
         used, even if the transaction that did the
         <function>nextval</function> later aborts.  This means that aborted
         transactions might leave unused <quote>holes</quote> in the sequence
         of assigned values.
        </para>
       </important>
Do you think we should add a mention regarding SIGQUIT/stop-immediate
regarding the fact that the application needs to commit the
transaction that used nextval() with synchronous_commit = on to keep
it consistent at recovery?
-- 
Michael


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



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

* Re: SIGQUIT and lost sequence WAL records
@ 2016-02-18 00:17  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Tom Lane @ 2016-02-18 00:17 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; +Cc: pgsql-bugs

Michael Paquier <michael.paquier@gmail.com> writes:
> Do you think we should add a mention regarding SIGQUIT/stop-immediate
> regarding the fact that the application needs to commit the
> transaction that used nextval() with synchronous_commit = on to keep
> it consistent at recovery?

Meh.  In the normal case where you just do "SELECT nextval()" and use the
result externally, there's going to be an implicit commit before you get
the data back.  So I think you really have to be taking deliberate aim at
your foot in order to get burnt like this.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



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


end of thread, other threads:[~2016-02-18 00:17 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-02-17 13:55 SIGQUIT and lost sequence WAL records Michael Paquier <michael.paquier@gmail.com>
2016-02-17 14:54 ` Tom Lane <tgl@sss.pgh.pa.us>
2016-02-18 00:10   ` Michael Paquier <michael.paquier@gmail.com>
2016-02-18 00:17     ` 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