From: Michael Paquier <michael.paquier@gmail.com>
To: PostgreSQL mailing lists <pgsql-bugs@postgresql.org>
Subject: SIGQUIT and lost sequence WAL records
Date: Wed, 17 Feb 2016 13:55:16 +0000
Message-ID: <CAB7nPqQ6+0LNkitj507eDBMh-OBdm_1_X-LmLOQfsjiWXe9SvA@mail.gmail.com> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-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-bugsAttachments:sequence-log-info.patch (text/x-diff, 509B) downloaddiff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.cindex 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()
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: michael.paquier@gmail.com
Subject: Re: SIGQUIT and lost sequence WAL records
In-Reply-To: <CAB7nPqQ6+0LNkitj507eDBMh-OBdm_1_X-LmLOQfsjiWXe9SvA@mail.gmail.com>
References: <CAB7nPqQ6+0LNkitj507eDBMh-OBdm_1_X-LmLOQfsjiWXe9SvA@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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