Avoid WAL-logging nextval on temporary sequences.
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Mon, 20 Apr 2015 14:07:56 +0000 (17:07 +0300)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Mon, 20 Apr 2015 14:07:56 +0000 (17:07 +0300)
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
src/backend/commands/sequence.c

index 6ae150888bad8398f6332d795fbb420431eff783..2424b4afb76b9e037ee4248ccd022cbd7b02f73c 100644 (file)
@@ -127,7 +127,7 @@ seqam_local_init(PG_FUNCTION_ARGS)
 /*
  * seqam_local_alloc()
  *
- * Allocate new range of values for a local sequence.
+ * Allocate 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)
index a2ad7a5bbf1c1e95c298c75b5630382e8bc6554d..0cb220956c8bd0d4b49668ff812b295e13e007b8 100644 (file)
@@ -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);
 }