Skip to content

Commit 96376f1

Browse files
committed
Copy WITH options to partitions
1 parent 07a9c88 commit 96376f1

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

expected/pathman_basic.out

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,34 @@ NOTICE: 1000 rows copied from test.num_range_rel_3
13391339
DROP TABLE test.num_range_rel CASCADE;
13401340
DROP TABLE test.range_rel CASCADE;
13411341
NOTICE: drop cascades to 10 other objects
1342+
/* Test attributes copying */
1343+
CREATE UNLOGGED TABLE test.range_rel (
1344+
id SERIAL PRIMARY KEY,
1345+
dt DATE NOT NULL)
1346+
WITH (fillfactor = 70);
1347+
INSERT INTO test.range_rel (dt)
1348+
SELECT g FROM generate_series('2015-01-01', '2015-02-15', '1 month'::interval) AS g;
1349+
SELECT pathman.create_range_partitions('test.range_rel', 'dt',
1350+
'2015-01-01'::date, '1 month'::interval);
1351+
create_range_partitions
1352+
-------------------------
1353+
2
1354+
(1 row)
1355+
1356+
SELECT reloptions, relpersistence FROM pg_class WHERE oid='test.range_rel'::REGCLASS;
1357+
reloptions | relpersistence
1358+
-----------------+----------------
1359+
{fillfactor=70} | u
1360+
(1 row)
1361+
1362+
SELECT reloptions, relpersistence FROM pg_class WHERE oid='test.range_rel_1'::REGCLASS;
1363+
reloptions | relpersistence
1364+
-----------------+----------------
1365+
{fillfactor=70} | u
1366+
(1 row)
1367+
1368+
DROP TABLE test.range_rel CASCADE;
1369+
NOTICE: drop cascades to 3 other objects
13421370
/* Test automatic partition creation */
13431371
CREATE TABLE test.range_rel (
13441372
id SERIAL PRIMARY KEY,

sql/pathman_basic.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,19 @@ DROP TABLE test.num_range_rel CASCADE;
390390

391391
DROP TABLE test.range_rel CASCADE;
392392

393+
/* Test attributes copying */
394+
CREATE UNLOGGED TABLE test.range_rel (
395+
id SERIAL PRIMARY KEY,
396+
dt DATE NOT NULL)
397+
WITH (fillfactor = 70);
398+
INSERT INTO test.range_rel (dt)
399+
SELECT g FROM generate_series('2015-01-01', '2015-02-15', '1 month'::interval) AS g;
400+
SELECT pathman.create_range_partitions('test.range_rel', 'dt',
401+
'2015-01-01'::date, '1 month'::interval);
402+
SELECT reloptions, relpersistence FROM pg_class WHERE oid='test.range_rel'::REGCLASS;
403+
SELECT reloptions, relpersistence FROM pg_class WHERE oid='test.range_rel_1'::REGCLASS;
404+
DROP TABLE test.range_rel CASCADE;
405+
393406
/* Test automatic partition creation */
394407
CREATE TABLE test.range_rel (
395408
id SERIAL PRIMARY KEY,

src/partition_creation.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +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);
7778
static void postprocess_child_table_and_atts(Oid parent_relid, Oid partition_relid);
7879

7980
static Oid text_to_regprocedure(text *proname_args);
@@ -671,6 +672,7 @@ create_single_partition_internal(Oid parent_relid,
671672
RangeVar *partition_rv,
672673
char *tablespace)
673674
{
675+
HeapTuple tuple = NULL;
674676
Relation parentrel;
675677

676678
/* Value to be returned */
@@ -693,6 +695,7 @@ create_single_partition_internal(Oid parent_relid,
693695
Oid save_userid;
694696
int save_sec_context;
695697
bool need_priv_escalation = !superuser(); /* we might be a SU */
698+
Datum reloptions = (Datum) 0;
696699

697700
/* Lock parent and check if it exists */
698701
LockRelationOid(parent_relid, ShareUpdateExclusiveLock);
@@ -736,6 +739,19 @@ create_single_partition_internal(Oid parent_relid,
736739
/* Copy attributes */
737740
parentrel = heap_open(parent_relid, NoLock);
738741
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+
}
739755
heap_close(parentrel, NoLock);
740756

741757
/* If no 'tablespace' is provided, get parent's tablespace */
@@ -787,6 +803,10 @@ create_single_partition_internal(Oid parent_relid,
787803
partition_relid = create_table_using_stmt((CreateStmt *) cur_stmt,
788804
child_relowner).objectId;
789805

806+
/* Copy attributes to partition */
807+
if (reloptions)
808+
copy_relation_attributes(partition_relid, reloptions);
809+
790810
/* Copy FOREIGN KEYS of the parent table */
791811
copy_foreign_keys(parent_relid, partition_relid);
792812

@@ -823,6 +843,9 @@ create_single_partition_internal(Oid parent_relid,
823843
if (need_priv_escalation)
824844
SetUserIdAndSecContext(save_userid, save_sec_context);
825845

846+
if (tuple != NULL)
847+
ReleaseSysCache(tuple);
848+
826849
return partition_relid;
827850
}
828851

@@ -1114,6 +1137,38 @@ copy_foreign_keys(Oid parent_relid, Oid partition_oid)
11141137
FunctionCallInvoke(&copy_fkeys_proc_fcinfo);
11151138
}
11161139

1140+
/* Copy attributes to partition. Updates partition's tuple in pg_class */
1141+
static void
1142+
copy_relation_attributes(Oid partition_relid, Datum reloptions)
1143+
{
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);
1170+
}
1171+
11171172

11181173
/*
11191174
* -----------------------------

0 commit comments

Comments
 (0)