Skip to content

Commit c3b3c7c

Browse files
author
Nikita Glukhov
committed
Add ALTER TYPE
1 parent 7bd0ebc commit c3b3c7c

File tree

9 files changed

+141
-2
lines changed

9 files changed

+141
-2
lines changed

src/backend/commands/typecmds.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3622,3 +3622,29 @@ AlterTypeNamespaceInternal(Oid typeOid, Oid nspOid,
36223622

36233623
return oldNspOid;
36243624
}
3625+
3626+
/*
3627+
* Execute ALTER TYPE <typeName> <command>, ...
3628+
*/
3629+
void
3630+
AlterType(AlterTypeStmt *stmt)
3631+
{
3632+
TypeName *typename;
3633+
Oid typeid;
3634+
ListCell *lcmd;
3635+
3636+
/* Make a TypeName so we can use standard type lookup machinery */
3637+
typename = makeTypeNameFromNameList(stmt->typeName);
3638+
typeid = typenameTypeId(NULL, typename);
3639+
3640+
foreach(lcmd, stmt->cmds)
3641+
{
3642+
AlterTypeCmd *cmd = (AlterTypeCmd *) lfirst(lcmd);
3643+
3644+
switch (cmd->cmdtype)
3645+
{
3646+
default:
3647+
elog(ERROR, "unknown ALTER TYPE command %d", cmd->cmdtype);
3648+
}
3649+
}
3650+
}

src/backend/nodes/copyfuncs.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4556,6 +4556,28 @@ _copyDropSubscriptionStmt(const DropSubscriptionStmt *from)
45564556
return newnode;
45574557
}
45584558

4559+
static AlterTypeStmt *
4560+
_copyAlterTypeStmt(const AlterTypeStmt *from)
4561+
{
4562+
AlterTypeStmt *newnode = makeNode(AlterTypeStmt);
4563+
4564+
COPY_NODE_FIELD(typeName);
4565+
COPY_NODE_FIELD(cmds);
4566+
4567+
return newnode;
4568+
}
4569+
4570+
static AlterTypeCmd *
4571+
_copyAlterTypeCmd(const AlterTypeCmd *from)
4572+
{
4573+
AlterTypeCmd *newnode = makeNode(AlterTypeCmd);
4574+
4575+
COPY_SCALAR_FIELD(cmdtype);
4576+
COPY_NODE_FIELD(def);
4577+
4578+
return newnode;
4579+
}
4580+
45594581
/* ****************************************************************
45604582
* pg_list.h copy functions
45614583
* ****************************************************************
@@ -5550,6 +5572,14 @@ copyObjectImpl(const void *from)
55505572
retval = _copyForeignKeyCacheInfo(from);
55515573
break;
55525574

5575+
case T_AlterTypeStmt:
5576+
retval = _copyAlterTypeStmt(from);
5577+
break;
5578+
5579+
case T_AlterTypeCmd:
5580+
retval = _copyAlterTypeCmd(from);
5581+
break;
5582+
55535583
default:
55545584
elog(ERROR, "unrecognized node type: %d", (int) nodeTag(from));
55555585
retval = 0; /* keep compiler quiet */

src/backend/nodes/equalfuncs.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2875,6 +2875,24 @@ _equalPartitionCmd(const PartitionCmd *a, const PartitionCmd *b)
28752875
return true;
28762876
}
28772877

2878+
static bool
2879+
_equalAlterTypeStmt(const AlterTypeStmt *a, const AlterTypeStmt *b)
2880+
{
2881+
COMPARE_NODE_FIELD(typeName);
2882+
COMPARE_NODE_FIELD(cmds);
2883+
2884+
return true;
2885+
}
2886+
2887+
static bool
2888+
_equalAlterTypeCmd(const AlterTypeCmd *a, const AlterTypeCmd *b)
2889+
{
2890+
COMPARE_SCALAR_FIELD(cmdtype);
2891+
COMPARE_NODE_FIELD(def);
2892+
2893+
return true;
2894+
}
2895+
28782896
/*
28792897
* Stuff from pg_list.h
28802898
*/
@@ -3688,6 +3706,12 @@ equal(const void *a, const void *b)
36883706
case T_PartitionCmd:
36893707
retval = _equalPartitionCmd(a, b);
36903708
break;
3709+
case T_AlterTypeStmt:
3710+
retval = _equalAlterTypeStmt(a, b);
3711+
break;
3712+
case T_AlterTypeCmd:
3713+
retval = _equalAlterTypeCmd(a, b);
3714+
break;
36913715

36923716
default:
36933717
elog(ERROR, "unrecognized node type: %d",

src/backend/parser/gram.y

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
282282
CreateMatViewStmt RefreshMatViewStmt CreateAmStmt
283283
CreatePublicationStmt AlterPublicationStmt
284284
CreateSubscriptionStmt AlterSubscriptionStmt DropSubscriptionStmt
285+
AlterTypeStmt
285286

286287
%type <node> select_no_parens select_with_parens select_clause
287288
simple_select values_clause
@@ -290,8 +291,8 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
290291
%type <ival> add_drop opt_asc_desc opt_nulls_order
291292

292293
%type <node> alter_table_cmd alter_type_cmd opt_collate_clause
293-
replica_identity partition_cmd
294-
%type <list> alter_table_cmds alter_type_cmds
294+
replica_identity partition_cmd alterTypeCmd
295+
%type <list> alter_table_cmds alter_type_cmds alterTypeCmds
295296
%type <list> alter_identity_column_option_list
296297
%type <defelt> alter_identity_column_option
297298

@@ -846,6 +847,7 @@ stmt :
846847
| AlterSubscriptionStmt
847848
| AlterTSConfigurationStmt
848849
| AlterTSDictionaryStmt
850+
| AlterTypeStmt
849851
| AlterUserMappingStmt
850852
| AlterUserSetStmt
851853
| AlterUserStmt
@@ -2740,6 +2742,25 @@ PartitionRangeDatum:
27402742
* really variants of the ALTER TABLE subcommands with different spellings
27412743
*****************************************************************************/
27422744

2745+
AlterTypeStmt:
2746+
ALTER TYPE_P any_name alterTypeCmds
2747+
{
2748+
AlterTypeStmt *n = makeNode(AlterTypeStmt);
2749+
n->typeName = $3;
2750+
n->cmds = $4;
2751+
$$ = (Node *) n;
2752+
}
2753+
;
2754+
2755+
alterTypeCmds:
2756+
alterTypeCmd { $$ = list_make1($1); }
2757+
| alterTypeCmds ',' alterTypeCmd { $$ = lappend($1, $3); }
2758+
;
2759+
2760+
alterTypeCmd:
2761+
/*EMPTY*/
2762+
;
2763+
27432764
AlterCompositeTypeStmt:
27442765
ALTER TYPE_P any_name alter_type_cmds
27452766
{

src/backend/tcop/utility.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ check_xact_readonly(Node *parsetree)
217217
case T_CreateSubscriptionStmt:
218218
case T_AlterSubscriptionStmt:
219219
case T_DropSubscriptionStmt:
220+
case T_AlterTypeStmt:
220221
PreventCommandIfReadOnly(CreateCommandTag(parsetree));
221222
PreventCommandIfParallelMode(CreateCommandTag(parsetree));
222223
break;
@@ -1652,6 +1653,10 @@ ProcessUtilitySlow(ParseState *pstate,
16521653
address = AlterCollation((AlterCollationStmt *) parsetree);
16531654
break;
16541655

1656+
case T_AlterTypeStmt:
1657+
AlterType((AlterTypeStmt *) parsetree);
1658+
break;
1659+
16551660
default:
16561661
elog(ERROR, "unrecognized node type: %d",
16571662
(int) nodeTag(parsetree));
@@ -2866,6 +2871,10 @@ CreateCommandTag(Node *parsetree)
28662871
}
28672872
break;
28682873

2874+
case T_AlterTypeStmt:
2875+
tag = "ALTER TYPE";
2876+
break;
2877+
28692878
default:
28702879
elog(WARNING, "unrecognized node type: %d",
28712880
(int) nodeTag(parsetree));
@@ -3311,6 +3320,10 @@ GetCommandLogLevel(Node *parsetree)
33113320
lev = LOGSTMT_DDL;
33123321
break;
33133322

3323+
case T_AlterTypeStmt:
3324+
lev = LOGSTMT_DDL;
3325+
break;
3326+
33143327
/* already-planned queries */
33153328
case T_PlannedStmt:
33163329
{

src/include/commands/typecmds.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@ extern Oid AlterTypeNamespaceInternal(Oid typeOid, Oid nspOid,
5353
bool isImplicitArray,
5454
bool errorOnTableType,
5555
ObjectAddresses *objsMoved);
56+
extern void AlterType(AlterTypeStmt *stmt);
5657

5758
#endif /* TYPECMDS_H */

src/include/fmgr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#ifndef FMGR_H
1919
#define FMGR_H
2020

21+
#include "utils/expandeddatum.h"
22+
2123
/* We don't want to include primnodes.h here, so make some stub references */
2224
typedef struct Node *fmNodePtr;
2325
typedef struct Aggref *fmAggrefPtr;

src/include/nodes/nodes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ typedef enum NodeTag
414414
T_DropSubscriptionStmt,
415415
T_CreateStatsStmt,
416416
T_AlterCollationStmt,
417+
T_AlterTypeStmt,
418+
T_AlterTypeCmd,
417419

418420
/*
419421
* TAGS FOR PARSE TREE NODES (parsenodes.h)

src/include/nodes/parsenodes.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3407,4 +3407,24 @@ typedef struct DropSubscriptionStmt
34073407
bool missing_ok; /* Skip error if missing? */
34083408
} DropSubscriptionStmt;
34093409

3410+
3411+
typedef enum AlterTypeCmdType
3412+
{
3413+
AT_AlterTypeInvalidCommand
3414+
} AlterTypeCmdType;
3415+
3416+
typedef struct AlterTypeCmd
3417+
{
3418+
NodeTag type;
3419+
AlterTypeCmdType cmdtype;
3420+
Node *def;
3421+
} AlterTypeCmd;
3422+
3423+
typedef struct AlterTypeStmt
3424+
{
3425+
NodeTag type;
3426+
List *typeName;
3427+
List *cmds;
3428+
} AlterTypeStmt;
3429+
34103430
#endif /* PARSENODES_H */

0 commit comments

Comments
 (0)