Skip to content

Commit 215aa2e

Browse files
committed
refactoring, extract function copy_rel_attributes()
1 parent 9620cef commit 215aa2e

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
Oid relowner);
7575

7676
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);
7878
static void postprocess_child_table_and_atts(Oid parent_relid, Oid partition_relid);
7979

8080
static Oid text_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-
HeapTuple tuple = NULL;
676-
Relation parentrel;
677-
678675
/* Value to be returned */
679676
Oid partition_relid = InvalidOid; /* safety */
680677

@@ -695,7 +692,6 @@ create_single_partition_internal(Oid parent_relid,
695692
Oid save_userid;
696693
int save_sec_context;
697694
bool need_priv_escalation = !superuser(); /* we might be a SU */
698-
Datum reloptions = (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-
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-
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
return partition_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
static void
11091083
copy_foreign_keys(Oid parent_relid, Oid partition_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-
/* Copy attributes to partition. Updates partition's tuple in pg_class */
1117+
/* Copy reloptions of foreign table (updates pg_class) */
11411118
static void
1142-
copy_relation_attributes(Oid partition_relid, Datum reloptions)
1119+
copy_rel_attributes(Oid parent_relid, Oid partition_relid)
11431120
{
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();
11701177
}
11711178

11721179

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy