Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit215aa2e

Browse files
committed
refactoring, extract function copy_rel_attributes()
1 parent9620cef commit215aa2e

File tree

1 file changed

+64
-57
lines changed

1 file changed

+64
-57
lines changed

‎src/partition_creation.c

Lines changed: 64 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static ObjectAddress create_table_using_stmt(CreateStmt *create_stmt,
7474
Oidrelowner);
7575

7676
staticvoidcopy_foreign_keys(Oidparent_relid,Oidpartition_oid);
77-
staticvoidcopy_relation_attributes(Oidpartition_relid,Datumreloptions);
77+
staticvoidcopy_rel_attributes(Oidparent_relid,Oidpartition_relid);
7878
staticvoidpostprocess_child_table_and_atts(Oidparent_relid,Oidpartition_relid);
7979

8080
staticOidtext_to_regprocedure(text*proname_args);
@@ -672,9 +672,6 @@ create_single_partition_internal(Oid parent_relid,
672672
RangeVar*partition_rv,
673673
char*tablespace)
674674
{
675-
HeapTupletuple=NULL;
676-
Relationparentrel;
677-
678675
/* Value to be returned */
679676
Oidpartition_relid=InvalidOid;/* safety */
680677

@@ -695,7 +692,6 @@ create_single_partition_internal(Oid parent_relid,
695692
Oidsave_userid;
696693
intsave_sec_context;
697694
boolneed_priv_escalation= !superuser();/* we might be a SU */
698-
Datumreloptions= (Datum)0;
699695

700696
/* Lock parent and check if it exists */
701697
LockRelationOid(parent_relid,ShareUpdateExclusiveLock);
@@ -736,24 +732,6 @@ create_single_partition_internal(Oid parent_relid,
736732
/* Make up parent's RangeVar */
737733
parent_rv=makeRangeVar(parent_nsp_name,parent_name,-1);
738734

739-
/* Copy attributes */
740-
parentrel=heap_open(parent_relid,NoLock);
741-
newrel_rv->relpersistence=parentrel->rd_rel->relpersistence;
742-
if (parentrel->rd_options)
743-
{
744-
boolisNull;
745-
746-
tuple=SearchSysCache1(RELOID,ObjectIdGetDatum(parent_relid));
747-
if (!HeapTupleIsValid(tuple))
748-
elog(ERROR,"cache lookup failed for relation %u",parent_relid);
749-
750-
reloptions=SysCacheGetAttr(RELOID,tuple,Anum_pg_class_reloptions,
751-
&isNull);
752-
if (isNull)
753-
reloptions= (Datum)0;
754-
}
755-
heap_close(parentrel,NoLock);
756-
757735
/* If no 'tablespace' is provided, get parent's tablespace */
758736
if (!tablespace)
759737
tablespace=get_tablespace_name(get_rel_tablespace(parent_relid));
@@ -804,8 +782,7 @@ create_single_partition_internal(Oid parent_relid,
804782
child_relowner).objectId;
805783

806784
/* Copy attributes to partition */
807-
if (reloptions)
808-
copy_relation_attributes(partition_relid,reloptions);
785+
copy_rel_attributes(parent_relid,partition_relid);
809786

810787
/* Copy FOREIGN KEYS of the parent table */
811788
copy_foreign_keys(parent_relid,partition_relid);
@@ -843,9 +820,6 @@ create_single_partition_internal(Oid parent_relid,
843820
if (need_priv_escalation)
844821
SetUserIdAndSecContext(save_userid,save_sec_context);
845822

846-
if (tuple!=NULL)
847-
ReleaseSysCache(tuple);
848-
849823
returnpartition_relid;
850824
}
851825

@@ -1104,7 +1078,7 @@ postprocess_child_table_and_atts(Oid parent_relid, Oid partition_relid)
11041078
heap_close(pg_attribute_rel,RowExclusiveLock);
11051079
}
11061080

1107-
/* Copy foreign keys of parent table */
1081+
/* Copy foreign keys of parent table(updates pg_class)*/
11081082
staticvoid
11091083
copy_foreign_keys(Oidparent_relid,Oidpartition_oid)
11101084
{
@@ -1135,38 +1109,71 @@ copy_foreign_keys(Oid parent_relid, Oid partition_oid)
11351109

11361110
/* Invoke the callback */
11371111
FunctionCallInvoke(&copy_fkeys_proc_fcinfo);
1112+
1113+
/* Make changes visible */
1114+
CommandCounterIncrement();
11381115
}
11391116

1140-
/* Copyattributes to partition. Updates partition's tuple inpg_class */
1117+
/* Copyreloptions of foreign table (updatespg_class) */
11411118
staticvoid
1142-
copy_relation_attributes(Oidpartition_relid,Datumreloptions)
1119+
copy_rel_attributes(Oidparent_relid,Oidpartition_relid)
11431120
{
1144-
RelationclassRel;
1145-
HeapTupletuple,
1146-
newtuple;
1147-
Datumnew_val[Natts_pg_class];
1148-
boolnew_null[Natts_pg_class],
1149-
new_repl[Natts_pg_class];
1150-
1151-
classRel=heap_open(RelationRelationId,RowExclusiveLock);
1152-
tuple=SearchSysCacheCopy1(RELOID,
1153-
ObjectIdGetDatum(partition_relid));
1154-
if (!HeapTupleIsValid(tuple))
1155-
elog(ERROR,"cache lookup failed for relation %u",
1156-
partition_relid);
1157-
1158-
/* Fill in relpartbound value */
1159-
memset(new_val,0,sizeof(new_val));
1160-
memset(new_null, false,sizeof(new_null));
1161-
memset(new_repl, false,sizeof(new_repl));
1162-
new_val[Anum_pg_class_reloptions-1]=reloptions;
1163-
new_null[Anum_pg_class_reloptions-1]= false;
1164-
new_repl[Anum_pg_class_reloptions-1]= true;
1165-
newtuple=heap_modify_tuple(tuple,RelationGetDescr(classRel),
1166-
new_val,new_null,new_repl);
1167-
CatalogTupleUpdate(classRel,&newtuple->t_self,newtuple);
1168-
heap_freetuple(newtuple);
1169-
heap_close(classRel,RowExclusiveLock);
1121+
Relationpg_class_rel;
1122+
1123+
HeapTupleparent_htup,
1124+
partition_htup,
1125+
new_htup;
1126+
1127+
Datumreloptions;
1128+
boolreloptions_null;
1129+
Datumrelpersistence;
1130+
1131+
Datumvalues[Natts_pg_class];
1132+
boolisnull[Natts_pg_class],
1133+
replace[Natts_pg_class]= { false };
1134+
1135+
pg_class_rel=heap_open(RelationRelationId,RowExclusiveLock);
1136+
1137+
parent_htup=SearchSysCache1(RELOID,ObjectIdGetDatum(parent_relid));
1138+
partition_htup=SearchSysCache1(RELOID,ObjectIdGetDatum(partition_relid));
1139+
1140+
if (!HeapTupleIsValid(parent_htup))
1141+
elog(ERROR,"cache lookup failed for relation %u",parent_relid);
1142+
1143+
if (!HeapTupleIsValid(partition_htup))
1144+
elog(ERROR,"cache lookup failed for relation %u",partition_relid);
1145+
1146+
/* Extract parent's reloptions */
1147+
reloptions=SysCacheGetAttr(RELOID,parent_htup,
1148+
Anum_pg_class_reloptions,
1149+
&reloptions_null);
1150+
1151+
/* Extract parent's relpersistence */
1152+
relpersistence= ((Form_pg_class)GETSTRUCT(parent_htup))->relpersistence;
1153+
1154+
/* Fill in reloptions */
1155+
values[Anum_pg_class_reloptions-1]=reloptions;
1156+
isnull[Anum_pg_class_reloptions-1]=reloptions_null;
1157+
replace[Anum_pg_class_reloptions-1]= true;
1158+
1159+
/* Fill in relpersistence */
1160+
values[Anum_pg_class_relpersistence-1]=relpersistence;
1161+
isnull[Anum_pg_class_relpersistence-1]= false;
1162+
replace[Anum_pg_class_relpersistence-1]= true;
1163+
1164+
new_htup=heap_modify_tuple(partition_htup,
1165+
RelationGetDescr(pg_class_rel),
1166+
values,isnull,replace);
1167+
CatalogTupleUpdate(pg_class_rel,&new_htup->t_self,new_htup);
1168+
heap_freetuple(new_htup);
1169+
1170+
ReleaseSysCache(parent_htup);
1171+
ReleaseSysCache(partition_htup);
1172+
1173+
heap_close(pg_class_rel,RowExclusiveLock);
1174+
1175+
/* Make changes visible */
1176+
CommandCounterIncrement();
11701177
}
11711178

11721179

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp