From 91acf75aef203d2201ab462e21f26d36d12dad67 Mon Sep 17 00:00:00 2001 From: Dilip Kumar Date: Fri, 24 Sep 2021 18:13:25 +0530 Subject: [PATCH v7 3/7] 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 | 61 ++++++++++++++++++++++++---------------- src/include/commands/tablecmds.h | 5 ++++ src/tools/pgindent/typedefs.list | 1 + 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 5e9cae2..25f897f 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -14237,21 +14237,15 @@ 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. @@ -14259,32 +14253,51 @@ index_copy_data(Relation rel, RelFileNode newrnode) * 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); + + 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 336549c..e0e0aa5 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 da6ac8e..bb3097f 100644 --- a/src/tools/pgindent/typedefs.list +++ b/src/tools/pgindent/typedefs.list @@ -3050,6 +3050,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