v10-0003-Refactor-index_copy_data.patch
text/x-patch
Filename: v10-0003-Refactor-index_copy_data.patch
Type: text/x-patch
Part: 2
Patch
Format: format-patch
Series: patch v10-0003
Subject: Refactor index_copy_data
| File | + | − |
|---|---|---|
| src/backend/commands/tablecmds.c | 42 | 26 |
| src/include/commands/tablecmds.h | 5 | 0 |
| src/tools/pgindent/typedefs.list | 1 | 0 |
From 44b2398e89fdf4dee1bd072d7d19299e4fac45ae Mon Sep 17 00:00:00 2001
From: Dilip Kumar <dilipkumar@localhost.localdomain>
Date: Fri, 24 Sep 2021 18:13:25 +0530
Subject: [PATCH v10 3/6] Refactor index_copy_data
Make separate interface for copying relation storage, this will
be used by later patch for copying the database relations.
---
src/backend/commands/tablecmds.c | 68 +++++++++++++++++++++++++---------------
src/include/commands/tablecmds.h | 5 +++
src/tools/pgindent/typedefs.list | 1 +
3 files changed, 48 insertions(+), 26 deletions(-)
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 3e83f37..a57d6b0 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -14580,54 +14580,70 @@ AlterTableMoveAll(AlterTableMoveAllStmt *stmt)
return new_tablespaceoid;
}
-static void
-index_copy_data(Relation rel, RelFileNode newrnode)
+/*
+ * Copy source smgr relation's all fork's data to the destination.
+ *
+ * copy_storage - storage copy function, which is passed by the caller.
+ */
+void
+RelationCopyAllFork(SMgrRelation src_smgr, SMgrRelation dst_smgr,
+ char relpersistence, copy_relation_storage copy_storage)
{
- SMgrRelation dstrel;
-
- dstrel = smgropen(newrnode, rel->rd_backend);
-
/*
- * Since we copy the file directly without looking at the shared buffers,
- * we'd better first flush out any pages of the source relation that are
- * in shared buffers. We assume no new changes will be made while we are
- * holding exclusive lock on the rel.
- */
- FlushRelationBuffers(rel);
-
- /*
- * Create and copy all forks of the relation, and schedule unlinking of
- * old physical files.
+ * Create and copy all forks of the relation.
*
* NOTE: any conflict in relfilenode value will be caught in
* RelationCreateStorage().
*/
- RelationCreateStorage(newrnode, rel->rd_rel->relpersistence);
+ RelationCreateStorage(dst_smgr->smgr_rnode.node, relpersistence);
/* copy main fork */
- RelationCopyStorage(RelationGetSmgr(rel), dstrel, MAIN_FORKNUM,
- rel->rd_rel->relpersistence);
+ copy_storage(src_smgr, dst_smgr, MAIN_FORKNUM, relpersistence);
/* copy those extra forks that exist */
for (ForkNumber forkNum = MAIN_FORKNUM + 1;
forkNum <= MAX_FORKNUM; forkNum++)
{
- if (smgrexists(RelationGetSmgr(rel), forkNum))
+ if (smgrexists(src_smgr, forkNum))
{
- smgrcreate(dstrel, forkNum, false);
+ smgrcreate(dst_smgr, forkNum, false);
/*
* WAL log creation if the relation is persistent, or this is the
* init fork of an unlogged relation.
*/
- if (RelationIsPermanent(rel) ||
- (rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED &&
+ if (relpersistence == RELPERSISTENCE_PERMANENT ||
+ (relpersistence == RELPERSISTENCE_UNLOGGED &&
forkNum == INIT_FORKNUM))
- log_smgrcreate(&newrnode, forkNum);
- RelationCopyStorage(RelationGetSmgr(rel), dstrel, forkNum,
- rel->rd_rel->relpersistence);
+ log_smgrcreate(&dst_smgr->smgr_rnode.node, forkNum);
+
+ /* Copy a fork's data, block by block. */
+ copy_storage(src_smgr, dst_smgr, forkNum, relpersistence);
}
}
+}
+
+static void
+index_copy_data(Relation rel, RelFileNode newrnode)
+{
+ SMgrRelation dstrel;
+
+ dstrel = smgropen(newrnode, rel->rd_backend);
+
+ /*
+ * Since we copy the file directly without looking at the shared buffers,
+ * we'd better first flush out any pages of the source relation that are
+ * in shared buffers. We assume no new changes will be made while we are
+ * holding exclusive lock on the rel.
+ */
+ FlushRelationBuffers(rel);
+
+ /*
+ * Create and copy all forks of the relation, and schedule unlinking of
+ * old physical files.
+ */
+ RelationCopyAllFork(RelationGetSmgr(rel), dstrel,
+ rel->rd_rel->relpersistence, RelationCopyStorage);
/* drop old relation, and close new one */
RelationDropStorage(rel);
diff --git a/src/include/commands/tablecmds.h b/src/include/commands/tablecmds.h
index 5d4037f..cd49471 100644
--- a/src/include/commands/tablecmds.h
+++ b/src/include/commands/tablecmds.h
@@ -19,10 +19,13 @@
#include "catalog/objectaddress.h"
#include "nodes/parsenodes.h"
#include "storage/lock.h"
+#include "storage/smgr.h"
#include "utils/relcache.h"
struct AlterTableUtilityContext; /* avoid including tcop/utility.h here */
+typedef void (*copy_relation_storage) (SMgrRelation src, SMgrRelation dst,
+ ForkNumber forkNum, char relpersistence);
extern ObjectAddress DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
ObjectAddress *typaddress, const char *queryString);
@@ -42,6 +45,8 @@ extern void AlterTableInternal(Oid relid, List *cmds, bool recurse);
extern Oid AlterTableMoveAll(AlterTableMoveAllStmt *stmt);
+extern void RelationCopyAllFork(SMgrRelation src_smgr, SMgrRelation dst_smgr,
+ char relpersistence, copy_relation_storage copy_storage);
extern ObjectAddress AlterTableNamespace(AlterObjectSchemaStmt *stmt,
Oid *oldschema);
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index d9b83f7..c1400d3 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -3059,6 +3059,7 @@ config_var_value
contain_aggs_of_level_context
convert_testexpr_context
copy_data_source_cb
+copy_relation_storage
core_YYSTYPE
core_yy_extra_type
core_yyscan_t
--
1.8.3.1