Move CRC calculation to XLogInsert(), for speed.
authorHeikki Linnakangas <[email protected]>
Fri, 3 Oct 2014 17:20:52 +0000 (20:20 +0300)
committerHeikki Linnakangas <[email protected]>
Mon, 10 Nov 2014 19:24:36 +0000 (21:24 +0200)
src/backend/access/transam/xlog.c
src/backend/access/transam/xloginsert.c

index 27edbc69f37f8588032e9b71e9cd12c372045080..c8a28f97595c65f45dabf389a3ad518251bbb6e3 100644 (file)
@@ -861,7 +861,6 @@ XLogRecPtr
 XLogInsertRecord(XLogRecData *rdata, XLogRecPtr fpw_lsn)
 {
        XLogCtlInsert *Insert = &XLogCtl->Insert;
-       XLogRecData *rdt;
        pg_crc32        rdata_crc;
        bool            inserted;
        XLogRecord *rechdr = (XLogRecord *) rdata->data;
@@ -877,24 +876,6 @@ XLogInsertRecord(XLogRecData *rdata, XLogRecPtr fpw_lsn)
        if (!XLogInsertAllowed())
                elog(ERROR, "cannot make new WAL entries during recovery");
 
-       /*
-        * Calculate CRC of the data
-        *
-        * Note that the record header isn't added into the CRC initially since we
-        * don't know the prev-link yet.  Thus, the CRC will represent the CRC of
-        * the whole record in the order: rdata, then backup blocks, then record
-        * header.
-        */
-       INIT_CRC32C(rdata_crc);
-       for (rdt = rdata->next; rdt != NULL; rdt = rdt->next)
-               COMP_CRC32C(rdata_crc, rdt->data, rdt->len);
-
-       /*
-        * Calculate CRC of the header, except for prev-link, because we don't
-        * know it yet.  It will be added later.
-        */
-       COMP_CRC32C(rdata_crc, ((char *) rechdr), offsetof(XLogRecord, xl_prev));
-
        /*----------
         *
         * We have now done all the preparatory work we can without holding a
@@ -982,6 +963,7 @@ XLogInsertRecord(XLogRecData *rdata, XLogRecPtr fpw_lsn)
                 * Now that xl_prev has been filled in, finish CRC calculation of the
                 * record header.
                 */
+               rdata_crc = rechdr->xl_crc;
                COMP_CRC32C(rdata_crc, ((char *) &rechdr->xl_prev), sizeof(XLogRecPtr));
                FIN_CRC32C(rdata_crc);
                rechdr->xl_crc = rdata_crc;
index eebbc1e00832126f780b16406e14f03c30748bcd..a41ee5de186938877148acfc069f10b975b41db6 100644 (file)
@@ -386,8 +386,8 @@ XLogInsert(RmgrId rmid, uint8 info)
  * Assemble a WAL record from the registered data and buffers into an
  * XLogRecData chain, ready for insertion with XLogInsertRecord().
  *
- * The record header fields are filled in, except for the xl_prev field and
- * CRC.
+ * The record header fields are filled in, except for the xl_prev field. The
+ * calculated CRC does not include xl_prev either.
  *
  * If there are any registered buffers, and a full-page image was not taken
  * of all them, *page_writes_omitted is set to true. This signals that the
@@ -403,6 +403,7 @@ XLogRecordAssemble(RmgrId rmid, uint8 info,
        uint32          total_len;
        int                     i;
        int                     used_rdatas = num_rdatas;
+       pg_crc32        rdata_crc;
 
        /*
         * Note: this function can be called multiple times for the same record.
@@ -635,6 +636,18 @@ XLogRecordAssemble(RmgrId rmid, uint8 info,
 
        rdt->next = NULL;
 
+       /*
+        * Calculate CRC of the data
+        *
+        * Note that the record header isn't added into the CRC initially since we
+        * don't know the prev-link yet.  Thus, the CRC will represent the CRC of
+        * the whole record in the order: rdata, then backup blocks, then record
+        * header.
+        */
+       INIT_CRC32C(rdata_crc);
+       for (rdt = hdr_rdt.next; rdt != NULL; rdt = rdt->next)
+               COMP_CRC32C(rdata_crc, rdt->data, rdt->len);
+
        /*
         * Fill in the fields in the record header. Prev-link is filled in later,
         * once we know where in the WAL the record will be inserted. CRC is also
@@ -647,6 +660,13 @@ XLogRecordAssemble(RmgrId rmid, uint8 info,
        rechdr->xl_rmid = rmid;
        rechdr->xl_prev = InvalidXLogRecPtr;
 
+       /*
+        * Calculate CRC of the header, except for prev-link, because we don't
+        * know it yet.  It will be added later.
+        */
+       COMP_CRC32C(rdata_crc, ((char *) rechdr), offsetof(XLogRecord, xl_prev));
+       rechdr->xl_crc = rdata_crc;
+
        return &hdr_rdt;
 }