@@ -74,7 +74,7 @@ static ObjectAddress create_table_using_stmt(CreateStmt *create_stmt,
74
74
Oid relowner );
75
75
76
76
static void copy_foreign_keys (Oid parent_relid , Oid partition_oid );
77
- static void copy_relation_attributes (Oid partition_relid , Datum reloptions );
77
+ static void copy_rel_attributes (Oid parent_relid , Oid partition_relid );
78
78
static void postprocess_child_table_and_atts (Oid parent_relid , Oid partition_relid );
79
79
80
80
static Oid text_to_regprocedure (text * proname_args );
@@ -672,9 +672,6 @@ create_single_partition_internal(Oid parent_relid,
672
672
RangeVar * partition_rv ,
673
673
char * tablespace )
674
674
{
675
- HeapTuple tuple = NULL ;
676
- Relation parentrel ;
677
-
678
675
/* Value to be returned */
679
676
Oid partition_relid = InvalidOid ; /* safety */
680
677
@@ -695,7 +692,6 @@ create_single_partition_internal(Oid parent_relid,
695
692
Oid save_userid ;
696
693
int save_sec_context ;
697
694
bool need_priv_escalation = !superuser (); /* we might be a SU */
698
- Datum reloptions = (Datum ) 0 ;
699
695
700
696
/* Lock parent and check if it exists */
701
697
LockRelationOid (parent_relid , ShareUpdateExclusiveLock );
@@ -736,24 +732,6 @@ create_single_partition_internal(Oid parent_relid,
736
732
/* Make up parent's RangeVar */
737
733
parent_rv = makeRangeVar (parent_nsp_name , parent_name , -1 );
738
734
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
- bool isNull ;
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
-
757
735
/* If no 'tablespace' is provided, get parent's tablespace */
758
736
if (!tablespace )
759
737
tablespace = get_tablespace_name (get_rel_tablespace (parent_relid ));
@@ -804,8 +782,7 @@ create_single_partition_internal(Oid parent_relid,
804
782
child_relowner ).objectId ;
805
783
806
784
/* Copy attributes to partition */
807
- if (reloptions )
808
- copy_relation_attributes (partition_relid , reloptions );
785
+ copy_rel_attributes (parent_relid , partition_relid );
809
786
810
787
/* Copy FOREIGN KEYS of the parent table */
811
788
copy_foreign_keys (parent_relid , partition_relid );
@@ -843,9 +820,6 @@ create_single_partition_internal(Oid parent_relid,
843
820
if (need_priv_escalation )
844
821
SetUserIdAndSecContext (save_userid , save_sec_context );
845
822
846
- if (tuple != NULL )
847
- ReleaseSysCache (tuple );
848
-
849
823
return partition_relid ;
850
824
}
851
825
@@ -1104,7 +1078,7 @@ postprocess_child_table_and_atts(Oid parent_relid, Oid partition_relid)
1104
1078
heap_close (pg_attribute_rel , RowExclusiveLock );
1105
1079
}
1106
1080
1107
- /* Copy foreign keys of parent table */
1081
+ /* Copy foreign keys of parent table (updates pg_class) */
1108
1082
static void
1109
1083
copy_foreign_keys (Oid parent_relid , Oid partition_oid )
1110
1084
{
@@ -1135,38 +1109,71 @@ copy_foreign_keys(Oid parent_relid, Oid partition_oid)
1135
1109
1136
1110
/* Invoke the callback */
1137
1111
FunctionCallInvoke (& copy_fkeys_proc_fcinfo );
1112
+
1113
+ /* Make changes visible */
1114
+ CommandCounterIncrement ();
1138
1115
}
1139
1116
1140
- /* Copy attributes to partition. Updates partition's tuple in pg_class */
1117
+ /* Copy reloptions of foreign table (updates pg_class) */
1141
1118
static void
1142
- copy_relation_attributes (Oid partition_relid , Datum reloptions )
1119
+ copy_rel_attributes (Oid parent_relid , Oid partition_relid )
1143
1120
{
1144
- Relation classRel ;
1145
- HeapTuple tuple ,
1146
- newtuple ;
1147
- Datum new_val [Natts_pg_class ];
1148
- bool new_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
+ Relation pg_class_rel ;
1122
+
1123
+ HeapTuple parent_htup ,
1124
+ partition_htup ,
1125
+ new_htup ;
1126
+
1127
+ Datum reloptions ;
1128
+ bool reloptions_null ;
1129
+ Datum relpersistence ;
1130
+
1131
+ Datum values [Natts_pg_class ];
1132
+ bool isnull [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 ();
1170
1177
}
1171
1178
1172
1179
0 commit comments