From 081c30f4d2b86ca7e6ec3a718619c0198c4465d0 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Mon, 20 Apr 2015 17:07:56 +0300 Subject: [PATCH] Avoid WAL-logging nextval on temporary sequences. Also, make sure that the first change on permanent sequences are WAL-logged after a checkpoint, even if the AM doesn't request it. You might end up with a torn page otherwise, and that's not cool, no matter what the AM thinks. --- src/backend/access/sequence/seqlocal.c | 5 +---- src/backend/commands/sequence.c | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/backend/access/sequence/seqlocal.c b/src/backend/access/sequence/seqlocal.c index 6ae150888b..2424b4afb7 100644 --- a/src/backend/access/sequence/seqlocal.c +++ b/src/backend/access/sequence/seqlocal.c @@ -127,7 +127,7 @@ seqam_local_init(PG_FUNCTION_ARGS) /* * seqam_local_alloc() * - * Allocate new range of values for a local sequence. + * Allocate a new range of values for a local sequence. */ Datum seqam_local_alloc(PG_FUNCTION_ARGS) @@ -186,10 +186,7 @@ seqam_local_alloc(PG_FUNCTION_ARGS) logit = true; } else if (sequence_needs_wal(seqh)) - { fetch = log = fetch + SEQ_LOG_VALS; - logit = true; - } /* Fetch new result value if is_called. */ if (is_called) diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c index a2ad7a5bbf..0cb220956c 100644 --- a/src/backend/commands/sequence.c +++ b/src/backend/commands/sequence.c @@ -1040,7 +1040,7 @@ sequence_save_tuple(SequenceHandle *seqh, HeapTuple newtup, bool do_wal) * Force the change to be WAL-logged, if we the tuple hasn't been logged * since the last checkpoint. */ - if (RelationNeedsWAL(seqh->rel) && sequence_needs_wal(seqh)) + if (sequence_needs_wal(seqh)) do_wal = true; /* @@ -1125,17 +1125,28 @@ sequence_release_tuple(SequenceHandle *seqh) } /* - * Returns true if sequence was not WAL logged since checkpoint + * Returns true, if the next update to the sequence tuple needs to be + * WAL-logged because it's the first update after a checkpoint. + * + * The sequence AM can use this as a hint, if it wants to piggyback some extra + * actions on WAL-logged updates. + * + * NB: This is just a hint. even when sequence_needs_wal() returns 'false', + * sequence_save_tuple might decide to WAL-log an update anyway. */ bool sequence_needs_wal(SequenceHandle *seqh) { Page page; - XLogRecPtr redoptr = GetRedoRecPtr(); + XLogRecPtr redoptr; Assert(BufferIsValid(seqh->buf)); + if (!RelationNeedsWAL(seqh->rel)) + return false; + page = BufferGetPage(seqh->buf); + redoptr = GetRedoRecPtr(); return (PageGetLSN(page) <= redoptr); } -- 2.39.5