Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Postgres FD Implementation
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Abuhujair Javed
Postgres FD Implementation
Commits
38b9693f
Commit
38b9693f
authored
Apr 03, 2012
by
Peter Eisentraut
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for renaming domain constraints
parent
c2cc5c34
Changes
11
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
168 additions
and
24 deletions
+168
-24
doc/src/sgml/ref/alter_domain.sgml
doc/src/sgml/ref/alter_domain.sgml
+28
-1
src/backend/catalog/objectaddress.c
src/backend/catalog/objectaddress.c
+1
-1
src/backend/catalog/pg_constraint.c
src/backend/catalog/pg_constraint.c
+60
-2
src/backend/commands/tablecmds.c
src/backend/commands/tablecmds.c
+45
-16
src/backend/commands/typecmds.c
src/backend/commands/typecmds.c
+1
-2
src/backend/parser/gram.y
src/backend/parser/gram.y
+10
-0
src/backend/parser/parse_utilcmd.c
src/backend/parser/parse_utilcmd.c
+1
-1
src/include/catalog/pg_constraint.h
src/include/catalog/pg_constraint.h
+2
-1
src/include/commands/typecmds.h
src/include/commands/typecmds.h
+3
-0
src/test/regress/expected/domain.out
src/test/regress/expected/domain.out
+7
-0
src/test/regress/sql/domain.sql
src/test/regress/sql/domain.sql
+10
-0
No files found.
doc/src/sgml/ref/alter_domain.sgml
View file @
38b9693f
...
...
@@ -31,6 +31,8 @@ ALTER DOMAIN <replaceable class="PARAMETER">name</replaceable>
ADD <replaceable class="PARAMETER">domain_constraint</replaceable> [ NOT VALID ]
ALTER DOMAIN <replaceable class="PARAMETER">name</replaceable>
DROP CONSTRAINT [ IF EXISTS ] <replaceable class="PARAMETER">constraint_name</replaceable> [ RESTRICT | CASCADE ]
ALTER DOMAIN <replaceable class="PARAMETER">name</replaceable>
RENAME CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> TO <replaceable class="PARAMETER">new_constraint_name</replaceable>
ALTER DOMAIN <replaceable class="PARAMETER">name</replaceable>
VALIDATE CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable>
ALTER DOMAIN <replaceable class="PARAMETER">name</replaceable>
...
...
@@ -102,6 +104,15 @@ ALTER DOMAIN <replaceable class="PARAMETER">name</replaceable>
</listitem>
</varlistentry>
<varlistentry>
<term>RENAME CONSTRAINT</term>
<listitem>
<para>
This form changes the name of a constraint on a domain.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>VALIDATE CONSTRAINT</term>
<listitem>
...
...
@@ -182,7 +193,7 @@ ALTER DOMAIN <replaceable class="PARAMETER">name</replaceable>
<term><replaceable class="PARAMETER">constraint_name</replaceable></term>
<listitem>
<para>
Name of an existing constraint to drop.
Name of an existing constraint to drop
or rename
.
</para>
</listitem>
</varlistentry>
...
...
@@ -225,6 +236,15 @@ ALTER DOMAIN <replaceable class="PARAMETER">name</replaceable>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="PARAMETER">new_constraint_name</replaceable></term>
<listitem>
<para>
The new name for the constraint.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="PARAMETER">new_owner</replaceable></term>
<listitem>
...
...
@@ -288,6 +308,13 @@ ALTER DOMAIN zipcode DROP CONSTRAINT zipchk;
</programlisting>
</para>
<para>
To rename a check constraint on a domain:
<programlisting>
ALTER DOMAIN zipcode RENAME CONSTRAINT zipchk TO zip_check;
</programlisting>
</para>
<para>
To move the domain into a different schema:
<programlisting>
...
...
src/backend/catalog/objectaddress.c
View file @
38b9693f
...
...
@@ -753,7 +753,7 @@ get_object_address_relobject(ObjectType objtype, List *objname,
case
OBJECT_CONSTRAINT
:
address
.
classId
=
ConstraintRelationId
;
address
.
objectId
=
get_constraint_oid
(
reloid
,
depname
,
missing_ok
);
get_
relation_
constraint_oid
(
reloid
,
depname
,
missing_ok
);
address
.
objectSubId
=
0
;
break
;
default:
...
...
src/backend/catalog/pg_constraint.c
View file @
38b9693f
...
...
@@ -736,12 +736,12 @@ AlterConstraintNamespaces(Oid ownerId, Oid oldNspId,
}
/*
* get_constraint_oid
* get_
relation_
constraint_oid
* Find a constraint on the specified relation with the specified name.
* Returns constraint's OID.
*/
Oid
get_constraint_oid
(
Oid
relid
,
const
char
*
conname
,
bool
missing_ok
)
get_
relation_
constraint_oid
(
Oid
relid
,
const
char
*
conname
,
bool
missing_ok
)
{
Relation
pg_constraint
;
HeapTuple
tuple
;
...
...
@@ -793,6 +793,64 @@ get_constraint_oid(Oid relid, const char *conname, bool missing_ok)
return
conOid
;
}
/*
* get_domain_constraint_oid
* Find a constraint on the specified domain with the specified name.
* Returns constraint's OID.
*/
Oid
get_domain_constraint_oid
(
Oid
typid
,
const
char
*
conname
,
bool
missing_ok
)
{
Relation
pg_constraint
;
HeapTuple
tuple
;
SysScanDesc
scan
;
ScanKeyData
skey
[
1
];
Oid
conOid
=
InvalidOid
;
/*
* Fetch the constraint tuple from pg_constraint. There may be more than
* one match, because constraints are not required to have unique names;
* if so, error out.
*/
pg_constraint
=
heap_open
(
ConstraintRelationId
,
AccessShareLock
);
ScanKeyInit
(
&
skey
[
0
],
Anum_pg_constraint_contypid
,
BTEqualStrategyNumber
,
F_OIDEQ
,
ObjectIdGetDatum
(
typid
));
scan
=
systable_beginscan
(
pg_constraint
,
ConstraintTypidIndexId
,
true
,
SnapshotNow
,
1
,
skey
);
while
(
HeapTupleIsValid
(
tuple
=
systable_getnext
(
scan
)))
{
Form_pg_constraint
con
=
(
Form_pg_constraint
)
GETSTRUCT
(
tuple
);
if
(
strcmp
(
NameStr
(
con
->
conname
),
conname
)
==
0
)
{
if
(
OidIsValid
(
conOid
))
ereport
(
ERROR
,
(
errcode
(
ERRCODE_DUPLICATE_OBJECT
),
errmsg
(
"domain
\"
%s
\"
has multiple constraints named
\"
%s
\"
"
,
format_type_be
(
typid
),
conname
)));
conOid
=
HeapTupleGetOid
(
tuple
);
}
}
systable_endscan
(
scan
);
/* If no such constraint exists, complain */
if
(
!
OidIsValid
(
conOid
)
&&
!
missing_ok
)
ereport
(
ERROR
,
(
errcode
(
ERRCODE_UNDEFINED_OBJECT
),
errmsg
(
"constraint
\"
%s
\"
for domain
\"
%s
\"
does not exist"
,
conname
,
format_type_be
(
typid
))));
heap_close
(
pg_constraint
,
AccessShareLock
);
return
conOid
;
}
/*
* Determine whether a relation can be proven functionally dependent on
* a set of grouping columns. If so, return TRUE and add the pg_constraint
...
...
src/backend/commands/tablecmds.c
View file @
38b9693f
...
...
@@ -2333,22 +2333,32 @@ renameatt(RenameStmt *stmt)
*/
static
void
rename_constraint_internal
(
Oid
myrelid
,
Oid
mytypid
,
const
char
*
oldconname
,
const
char
*
newconname
,
bool
recurse
,
bool
recursing
,
int
expected_parents
)
{
Relation
targetrelation
;
Relation
targetrelation
=
NULL
;
Oid
constraintOid
;
HeapTuple
tuple
;
Form_pg_constraint
con
;
AssertArg
(
!
myrelid
||
!
mytypid
);
if
(
mytypid
)
{
constraintOid
=
get_domain_constraint_oid
(
mytypid
,
oldconname
,
false
);
}
else
{
targetrelation
=
relation_open
(
myrelid
,
AccessExclusiveLock
);
/* don't tell it whether we're recursing; we allow changing typed tables here */
renameatt_check
(
myrelid
,
RelationGetForm
(
targetrelation
),
false
);
constraintOid
=
get_constraint_oid
(
myrelid
,
oldconname
,
false
);
constraintOid
=
get_relation_constraint_oid
(
myrelid
,
oldconname
,
false
);
}
tuple
=
SearchSysCache1
(
CONSTROID
,
ObjectIdGetDatum
(
constraintOid
));
if
(
!
HeapTupleIsValid
(
tuple
))
...
...
@@ -2356,7 +2366,7 @@ rename_constraint_internal(Oid myrelid,
constraintOid
);
con
=
(
Form_pg_constraint
)
GETSTRUCT
(
tuple
);
if
(
con
->
contype
==
CONSTRAINT_CHECK
&&
!
con
->
conisonly
)
if
(
myrelid
&&
con
->
contype
==
CONSTRAINT_CHECK
&&
!
con
->
conisonly
)
{
if
(
recurse
)
{
...
...
@@ -2376,7 +2386,7 @@ rename_constraint_internal(Oid myrelid,
if
(
childrelid
==
myrelid
)
continue
;
rename_constraint_internal
(
childrelid
,
oldconname
,
newconname
,
false
,
true
,
numparents
);
rename_constraint_internal
(
childrelid
,
InvalidOid
,
oldconname
,
newconname
,
false
,
true
,
numparents
);
}
}
else
...
...
@@ -2407,24 +2417,43 @@ rename_constraint_internal(Oid myrelid,
ReleaseSysCache
(
tuple
);
if
(
targetrelation
)
relation_close
(
targetrelation
,
NoLock
);
/* close rel but keep lock */
}
void
RenameConstraint
(
RenameStmt
*
stmt
)
{
Oid
relid
;
Oid
relid
=
InvalidOid
;
Oid
typid
=
InvalidOid
;
if
(
stmt
->
relationType
==
OBJECT_DOMAIN
)
{
Relation
rel
;
HeapTuple
tup
;
typid
=
typenameTypeId
(
NULL
,
makeTypeNameFromNameList
(
stmt
->
object
));
rel
=
heap_open
(
TypeRelationId
,
RowExclusiveLock
);
tup
=
SearchSysCache1
(
TYPEOID
,
ObjectIdGetDatum
(
typid
));
if
(
!
HeapTupleIsValid
(
tup
))
elog
(
ERROR
,
"cache lookup failed for type %u"
,
typid
);
checkDomainOwner
(
tup
);
ReleaseSysCache
(
tup
);
heap_close
(
rel
,
NoLock
);
}
else
{
/* lock level taken here should match rename_constraint_internal */
relid
=
RangeVarGetRelidExtended
(
stmt
->
relation
,
AccessExclusiveLock
,
false
,
false
,
RangeVarCallbackForRenameAttribute
,
NULL
);
}
rename_constraint_internal
(
relid
,
rename_constraint_internal
(
relid
,
typid
,
stmt
->
subname
,
stmt
->
newname
,
interpretInhOption
(
stmt
->
relation
->
inhOpt
)
,
/* recursive? */
stmt
->
relation
?
interpretInhOption
(
stmt
->
relation
->
inhOpt
)
:
false
,
/* recursive? */
false
,
/* recursing? */
0
/* expected inhcount */
);
}
...
...
src/backend/commands/typecmds.c
View file @
38b9693f
...
...
@@ -98,7 +98,6 @@ static Oid findRangeCanonicalFunction(List *procname, Oid typeOid);
static
Oid
findRangeSubtypeDiffFunction
(
List
*
procname
,
Oid
subtype
);
static
void
validateDomainConstraint
(
Oid
domainoid
,
char
*
ccbin
);
static
List
*
get_rels_with_domain
(
Oid
domainOid
,
LOCKMODE
lockmode
);
static
void
checkDomainOwner
(
HeapTuple
tup
);
static
void
checkEnumOwner
(
HeapTuple
tup
);
static
char
*
domainAddConstraint
(
Oid
domainOid
,
Oid
domainNamespace
,
Oid
baseTypeOid
,
...
...
@@ -2794,7 +2793,7 @@ get_rels_with_domain(Oid domainOid, LOCKMODE lockmode)
* Check that the type is actually a domain and that the current user
* has permission to do ALTER DOMAIN on it. Throw an error if not.
*/
static
void
void
checkDomainOwner
(
HeapTuple
tup
)
{
Form_pg_type
typTup
=
(
Form_pg_type
)
GETSTRUCT
(
tup
);
...
...
src/backend/parser/gram.y
View file @
38b9693f
...
...
@@ -6529,6 +6529,16 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
n->missing_ok = false;
$$ = (Node *)n;
}
| ALTER DOMAIN_P any_name RENAME CONSTRAINT name TO name
{
RenameStmt *n = makeNode(RenameStmt);
n->renameType = OBJECT_CONSTRAINT;
n->relationType = OBJECT_DOMAIN;
n->object = $3;
n->subname = $6;
n->newname = $8;
$$ = (Node *)n;
}
| ALTER FOREIGN DATA_P WRAPPER name RENAME TO name
{
RenameStmt *n = makeNode(RenameStmt);
...
...
src/backend/parser/parse_utilcmd.c
View file @
38b9693f
...
...
@@ -802,7 +802,7 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
/* Copy comment on constraint */
if
((
table_like_clause
->
options
&
CREATE_TABLE_LIKE_COMMENTS
)
&&
(
comment
=
GetComment
(
get_constraint_oid
(
RelationGetRelid
(
relation
),
(
comment
=
GetComment
(
get_
relation_
constraint_oid
(
RelationGetRelid
(
relation
),
n
->
conname
,
false
),
ConstraintRelationId
,
0
))
!=
NULL
)
...
...
src/include/catalog/pg_constraint.h
View file @
38b9693f
...
...
@@ -244,7 +244,8 @@ extern char *ChooseConstraintName(const char *name1, const char *name2,
extern
void
AlterConstraintNamespaces
(
Oid
ownerId
,
Oid
oldNspId
,
Oid
newNspId
,
bool
isType
);
extern
Oid
get_constraint_oid
(
Oid
relid
,
const
char
*
conname
,
bool
missing_ok
);
extern
Oid
get_relation_constraint_oid
(
Oid
relid
,
const
char
*
conname
,
bool
missing_ok
);
extern
Oid
get_domain_constraint_oid
(
Oid
typid
,
const
char
*
conname
,
bool
missing_ok
);
extern
bool
check_functional_grouping
(
Oid
relid
,
Index
varno
,
Index
varlevelsup
,
...
...
src/include/commands/typecmds.h
View file @
38b9693f
...
...
@@ -14,6 +14,7 @@
#ifndef TYPECMDS_H
#define TYPECMDS_H
#include "access/htup.h"
#include "nodes/parsenodes.h"
...
...
@@ -35,6 +36,8 @@ extern void AlterDomainValidateConstraint(List *names, char *constrName);
extern
void
AlterDomainDropConstraint
(
List
*
names
,
const
char
*
constrName
,
DropBehavior
behavior
,
bool
missing_ok
);
extern
void
checkDomainOwner
(
HeapTuple
tup
);
extern
List
*
GetDomainConstraints
(
Oid
typeOid
);
extern
void
RenameType
(
RenameStmt
*
stmt
);
...
...
src/test/regress/expected/domain.out
View file @
38b9693f
...
...
@@ -659,3 +659,10 @@ create domain testdomain1 as int;
alter domain testdomain1 rename to testdomain2;
alter type testdomain2 rename to testdomain3; -- alter type also works
drop domain testdomain3;
--
-- Renaming domain constraints
--
create domain testdomain1 as int constraint unsigned check (value > 0);
alter domain testdomain1 rename constraint unsigned to unsigned_foo;
alter domain testdomain1 drop constraint unsigned_foo;
drop domain testdomain1;
src/test/regress/sql/domain.sql
View file @
38b9693f
...
...
@@ -496,3 +496,13 @@ create domain testdomain1 as int;
alter
domain
testdomain1
rename
to
testdomain2
;
alter
type
testdomain2
rename
to
testdomain3
;
-- alter type also works
drop
domain
testdomain3
;
--
-- Renaming domain constraints
--
create
domain
testdomain1
as
int
constraint
unsigned
check
(
value
>
0
);
alter
domain
testdomain1
rename
constraint
unsigned
to
unsigned_foo
;
alter
domain
testdomain1
drop
constraint
unsigned_foo
;
drop
domain
testdomain1
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment