Skip to content

Commit e7ea385

Browse files
committed
added SET INTERVAL command
1 parent eddd27d commit e7ea385

File tree

8 files changed

+78
-10
lines changed

8 files changed

+78
-10
lines changed

contrib/test_partitioning/sql/partition.sql

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ SELECT * FROM pathman_partition_list;
5656

5757
DROP TABLE abc CASCADE;
5858

59-
/* Create hash partitioned table */
6059
CREATE TABLE abc(id serial)
61-
PARTITION BY HASH (id) PARTITIONS (3);
60+
PARTITION BY HASH (id)
61+
(
62+
PARTITION abc_first,
63+
PARTITION abc_second
64+
);
6265

6366
SELECT * FROM pathman_partition_list;
6467

src/backend/commands/partition.c

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static void create_range_partitions(PartitionInfo *pinfo,
3939
Oid relid,
4040
const char *attname,
4141
PartitionDataType partition_data);
42-
static void read_interval_value(PartitionInfo *pinfo,
42+
static void read_interval_value(Node *raw_interval,
4343
Oid atttype,
4444
Oid *interval_type,
4545
Datum *interval_datum);
@@ -175,7 +175,7 @@ create_range_partitions(PartitionInfo *pinfo,
175175
atttypmod = get_atttypmod(relid, attnum);
176176

177177
/* Interval */
178-
read_interval_value(pinfo, atttype, &interval_type, &interval_datum);
178+
read_interval_value(pinfo->interval, atttype, &interval_type, &interval_datum);
179179

180180
/*
181181
* Start value. It is always non-NULL whenever partition_data = True.
@@ -257,7 +257,7 @@ create_range_partitions(PartitionInfo *pinfo,
257257
* Converts interval from Value
258258
*/
259259
static void
260-
read_interval_value(PartitionInfo *pinfo,
260+
read_interval_value(Node *raw_interval,
261261
Oid atttype,
262262
Oid *interval_type,
263263
Datum *interval_datum)
@@ -268,13 +268,13 @@ read_interval_value(PartitionInfo *pinfo,
268268
*interval_datum = (Datum) 0;
269269

270270
/* If interval is set then convert it to a suitable Datum value */
271-
if (pinfo->interval != NULL)
271+
if (raw_interval != NULL)
272272
{
273273
Const *interval_const;
274274

275-
if (IsA(pinfo->interval, A_Const))
275+
if (IsA(raw_interval, A_Const))
276276
{
277-
A_Const *con = (A_Const *) pinfo->interval;
277+
A_Const *con = (A_Const *) raw_interval;
278278
Value *val = &con->val;
279279

280280
interval_const = make_const(pstate, val, con->location);
@@ -662,8 +662,32 @@ RangeVarGetNamespaceId(const RangeVar *rangevar)
662662
return namespace_id;
663663
}
664664

665+
665666
void
666-
partition_existing_table(Oid relid, AlterTableCmd *cmd)
667+
partitioned_table_set_interval(Oid relid, AlterTableCmd *cmd)
667668
{
669+
const char *attname;
670+
AttrNumber attnum;
671+
Oid atttype;
672+
Datum interval_datum;
673+
Oid interval_type;
674+
bool interval_isnull = (cmd->def == NULL);
675+
676+
if (SPI_connect() != SPI_OK_CONNECT)
677+
elog(ERROR, "could not connect using SPI");
668678

679+
/* Partitioning attribute parameters */
680+
attname = pm_get_partition_key(relid);
681+
attnum = get_attnum(relid, attname);
682+
atttype = get_atttype(relid, attnum);
683+
684+
/* Convert A_Const to Datum */
685+
read_interval_value(cmd->def,
686+
atttype,
687+
&interval_type,
688+
&interval_datum);
689+
690+
pm_set_interval(relid, interval_datum, interval_type, interval_isnull);
691+
692+
SPI_finish();
669693
}

src/backend/commands/pathman_wrapper.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,3 +592,28 @@ pm_partition_table_concurrently(Oid relid)
592592
if (!ret)
593593
elog(ERROR, "Unable to start concurrent data migration");
594594
}
595+
596+
597+
/*
598+
*
599+
*/
600+
void
601+
pm_set_interval(Oid relid, Datum interval, Oid interval_type, bool interval_isnull)
602+
{
603+
FuncArgs args;
604+
bool ret;
605+
606+
InitFuncArgs(&args, 2);
607+
PG_SETARG_DATUM(&args, 0, OIDOID, relid);
608+
609+
if (interval_isnull)
610+
PG_SETARG_NULL(&args, 1, interval_type);
611+
else
612+
PG_SETARG_DATUM(&args, 1, interval_type, interval);
613+
614+
ret = pathman_invoke("set_interval($1, $2)", &args);
615+
FreeFuncArgs(&args);
616+
617+
if (!ret)
618+
elog(ERROR, "Unable to set interval");
619+
}

src/backend/commands/tablecmds.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3074,6 +3074,7 @@ AlterTableGetLockLevel(List *cmds)
30743074
case AT_RenamePartition:
30753075
case AT_DropPartition:
30763076
case AT_MovePartition:
3077+
case AT_SetInterval:
30773078
break;
30783079

30793080
default: /* oops */
@@ -3408,6 +3409,7 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
34083409
case AT_RenamePartition:
34093410
case AT_DropPartition:
34103411
case AT_MovePartition:
3412+
case AT_SetInterval:
34113413
pass = AT_PASS_MISC;
34123414
break;
34133415

@@ -3751,6 +3753,9 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, Relation rel,
37513753
case AT_MovePartition:
37523754
move_partition(rel->rd_id, cmd);
37533755
break;
3756+
case AT_SetInterval:
3757+
partitioned_table_set_interval(rel->rd_id, cmd);
3758+
break;
37543759
default: /* oops */
37553760
elog(ERROR, "unrecognized alter table type: %d",
37563761
(int) cmd->subtype);

src/backend/parser/gram.y

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2340,6 +2340,14 @@ alter_table_cmd:
23402340
n->name = $3;
23412341
$$ = (Node *)n;
23422342
}
2343+
/* ALTER TABLE <name> SET INTERVAL <const> */
2344+
| SET INTERVAL '(' b_expr ')'
2345+
{
2346+
AlterTableCmd *n = makeNode(AlterTableCmd);
2347+
n->subtype = AT_SetInterval;
2348+
n->def = $4;
2349+
$$ = (Node *)n;
2350+
}
23432351
/* ALTER TABLE <name> SET (...) */
23442352
| SET reloptions
23452353
{

src/include/commands/partition.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ extern void rename_partition(Oid parent, AlterTableCmd *cmd);
2929
extern void drop_partition(Oid parent, AlterTableCmd *cmd);
3030
extern void move_partition(Oid parent, AlterTableCmd *cmd);
3131
extern void partition_existing_table(Oid relid, AlterTableCmd *cmd);
32+
extern void partitioned_table_set_interval(Oid relid, AlterTableCmd *cmd);
3233

3334
#endif /* PARTITION_H */

src/include/commands/pathman_wrapper.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,5 @@ void pm_split_range_partition(Oid part_relid,
7676
const char *tablespace);
7777
void pm_alter_partition(Oid relid, const char *new_relname, Oid new_namespace, const char *new_tablespace);
7878
void pm_drop_range_partition_expand_next(Oid relid);
79-
void pm_partition_table_concurrently(Oid relid);
79+
void pm_partition_table_concurrently(Oid relid);
80+
void pm_set_interval(Oid relid, Datum interval, Oid interval_type, bool interval_isnull);

src/include/nodes/parsenodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,7 @@ typedef enum AlterTableType
15681568
AT_RenamePartition, /* RENAME PARTITION */
15691569
AT_DropPartition, /* DROP PARTITION */
15701570
AT_MovePartition, /* MOVE PARTITION */
1571+
AT_SetInterval /* SET INTERVAL (...) */
15711572
} AlterTableType;
15721573

15731574
typedef struct ReplicaIdentityStmt

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