From d4f577c4d2c06c46c7b3f1f02ffee5a790da7bc5 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Fri, 3 Oct 2014 20:20:52 +0300 Subject: [PATCH] Move CRC calculation to XLogInsert(), for speed. --- src/backend/access/transam/xlog.c | 20 +------------------- src/backend/access/transam/xloginsert.c | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 27edbc69f3..c8a28f9759 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -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; diff --git a/src/backend/access/transam/xloginsert.c b/src/backend/access/transam/xloginsert.c index eebbc1e008..a41ee5de18 100644 --- a/src/backend/access/transam/xloginsert.c +++ b/src/backend/access/transam/xloginsert.c @@ -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; } -- 2.39.5