From 7da22d866d45d940ba40b77881fbb2be803304a5 Mon Sep 17 00:00:00 2001 From: Fujii Masao Date: Thu, 5 Jul 2018 02:21:15 +0900 Subject: [PATCH] Improve the performance of relation deletes during recovery. When multiple relations are deleted at the same transaction, the files of those relations are deleted by one call to smgrdounlinkall(), which leads to scan whole shared_buffers only one time. OTOH, previously, during recovery, smgrdounlink() (not smgrdounlinkall()) was called for each file to delete, which led to scan shared_buffers multiple times. Obviously this could cause to increase the WAL replay time very much especially when shared_buffers was huge. To alleviate this situation, this commit changes the recovery so that it also calls smgrdounlinkall() only one time to delete multiple relation files. This is just fix for oversight of commit 279628a0a7, not new feature. So, per discussion on pgsql-hackers, we concluded to backpatch this to all supported versions. Author: Fujii Masao Reviewed-by: Michael Paquier, Andres Freund, Thomas Munro, Kyotaro Horiguchi, Takayuki Tsunakawa Discussion: https://postgr.es/m/CAHGQGwHVQkdfDqtvGVkty+19cQakAydXn1etGND3X0PHbZ3+6w@mail.gmail.com --- src/backend/access/transam/twophase.c | 9 ++----- src/backend/access/transam/xact.c | 25 +++--------------- src/backend/storage/smgr/md.c | 38 +++++++++++++++++++++++++++ src/include/storage/smgr.h | 1 + 4 files changed, 44 insertions(+), 29 deletions(-) diff --git a/src/backend/access/transam/twophase.c b/src/backend/access/transam/twophase.c index c7cde25f83..a23eed5683 100644 --- a/src/backend/access/transam/twophase.c +++ b/src/backend/access/transam/twophase.c @@ -1346,7 +1346,6 @@ FinishPreparedTransaction(const char *gid, bool isCommit) RelFileNode *delrels; int ndelrels; SharedInvalidationMessage *invalmsgs; - int i; /* * Validate the GID, and lock the GXACT to ensure that two backends do not @@ -1438,13 +1437,9 @@ FinishPreparedTransaction(const char *gid, bool isCommit) delrels = abortrels; ndelrels = hdr->nabortrels; } - for (i = 0; i < ndelrels; i++) - { - SMgrRelation srel = smgropen(delrels[i], InvalidBackendId); - smgrdounlink(srel, false); - smgrclose(srel); - } + /* Make sure files supposed to be dropped are dropped */ + DropRelationFiles(delrels, ndelrels, false); /* * Handle cache invalidation messages. diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c index 150a0e8914..225775ceb4 100644 --- a/src/backend/access/transam/xact.c +++ b/src/backend/access/transam/xact.c @@ -5349,7 +5349,6 @@ xact_redo_commit(xl_xact_parsed_commit *parsed, RepOriginId origin_id) { TransactionId max_xid; - int i; TimestampTz commit_time; max_xid = TransactionIdLatest(xid, parsed->nsubxacts, parsed->subxacts); @@ -5467,16 +5466,8 @@ xact_redo_commit(xl_xact_parsed_commit *parsed, */ XLogFlush(lsn); - for (i = 0; i < parsed->nrels; i++) - { - SMgrRelation srel = smgropen(parsed->xnodes[i], InvalidBackendId); - ForkNumber fork; - - for (fork = 0; fork <= MAX_FORKNUM; fork++) - XLogDropRelation(parsed->xnodes[i], fork); - smgrdounlink(srel, true); - smgrclose(srel); - } + /* Make sure files supposed to be dropped are dropped */ + DropRelationFiles(parsed->xnodes, parsed->nrels, true); } /* @@ -5515,7 +5506,6 @@ xact_redo_commit(xl_xact_parsed_commit *parsed, static void xact_redo_abort(xl_xact_parsed_abort *parsed, TransactionId xid) { - int i; TransactionId max_xid; /* @@ -5577,16 +5567,7 @@ xact_redo_abort(xl_xact_parsed_abort *parsed, TransactionId xid) } /* Make sure files supposed to be dropped are dropped */ - for (i = 0; i < parsed->nrels; i++) - { - SMgrRelation srel = smgropen(parsed->xnodes[i], InvalidBackendId); - ForkNumber fork; - - for (fork = 0; fork <= MAX_FORKNUM; fork++) - XLogDropRelation(parsed->xnodes[i], fork); - smgrdounlink(srel, true); - smgrclose(srel); - } + DropRelationFiles(parsed->xnodes, parsed->nrels, true); } void diff --git a/src/backend/storage/smgr/md.c b/src/backend/storage/smgr/md.c index a94828b32c..bfa065d9fb 100644 --- a/src/backend/storage/smgr/md.c +++ b/src/backend/storage/smgr/md.c @@ -26,6 +26,7 @@ #include #include "miscadmin.h" +#include "access/xlogutils.h" #include "access/xlog.h" #include "catalog/catalog.h" #include "portability/instr_time.h" @@ -1701,6 +1702,43 @@ ForgetDatabaseFsyncRequests(Oid dbid) } } +/* + * DropRelationFiles -- drop files of all given relations + */ +void +DropRelationFiles(RelFileNode *delrels, int ndelrels, bool isRedo) +{ + SMgrRelation *srels; + int i; + + srels = palloc(sizeof(SMgrRelation) * ndelrels); + for (i = 0; i < ndelrels; i++) + { + SMgrRelation srel = smgropen(delrels[i], InvalidBackendId); + + if (isRedo) + { + ForkNumber fork; + + for (fork = 0; fork <= MAX_FORKNUM; fork++) + XLogDropRelation(delrels[i], fork); + } + srels[i] = srel; + } + + smgrdounlinkall(srels, ndelrels, isRedo); + + /* + * Call smgrclose() in reverse order as when smgropen() is called. + * This trick enables remove_from_unowned_list() in smgrclose() + * to search the SMgrRelation from the unowned list, + * with O(1) performance. + */ + for (i = ndelrels - 1; i >= 0; i--) + smgrclose(srels[i]); + pfree(srels); +} + /* * _fdvec_alloc() -- Make a MdfdVec object. diff --git a/src/include/storage/smgr.h b/src/include/storage/smgr.h index a8e7877f70..38a4eaf24f 100644 --- a/src/include/storage/smgr.h +++ b/src/include/storage/smgr.h @@ -139,6 +139,7 @@ extern void RememberFsyncRequest(RelFileNode rnode, ForkNumber forknum, BlockNumber segno); extern void ForgetRelationFsyncRequests(RelFileNode rnode, ForkNumber forknum); extern void ForgetDatabaseFsyncRequests(Oid dbid); +extern void DropRelationFiles(RelFileNode *delrels, int ndelrels, bool isRedo); /* smgrtype.c */ extern Datum smgrout(PG_FUNCTION_ARGS);