ALTER_DOMAIN(7pg17)
π ALTER_DOMAIN π
okay bestie, this is the ALTER DOMAIN page from the PostgreSQL 17.11 Documentation β the whole glow-up guide for changing an existing domain's definition. it's giving database makeover energy, fr fr. β¨
πΈ NAME πΈ
ALTER_DOMAIN β change the definition of a domain π
π¦ SYNOPSIS π¦
ALTER DOMAIN name
{ SET DEFAULT expression | DROP DEFAULT }
ALTER DOMAIN name
{ SET | DROP } NOT NULL
ALTER DOMAIN name
ADD domain_constraint [ NOT VALID ]
ALTER DOMAIN name
DROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ]
ALTER DOMAIN name
RENAME CONSTRAINT constraint_name TO new_constraint_name
ALTER DOMAIN name
VALIDATE CONSTRAINT constraint_name
ALTER DOMAIN name
OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
ALTER DOMAIN name
RENAME TO new_name
ALTER DOMAIN name
SET SCHEMA new_schema
where domain_constraint is:
[ CONSTRAINT constraint_name ]
{ NOT NULL | CHECK (expression) }
β¨ DESCRIPTION β¨
ALTER DOMAIN changes the definition of an existing domain. there are several sub-forms, and each one is kinda its own little side quest:
SET/DROP DEFAULTβ these forms set or remove the default value for a domain. note that defaults only apply to subsequentINSERTcommands; they do not affect rows already in a table using the domain. πSET/DROP NOT NULLβ these forms change whether a domain is marked to allowNULLvalues or to rejectNULLvalues. you can onlySET NOT NULLwhen the columns using the domain contain no null values. π«ΆADD domain_constraint [ NOT VALID ]β this form adds a new constraint to a domain. when a new constraint is added to a domain, all columns using that domain will be checked against the newly added constraint. these checks can be suppressed by adding the new constraint using theNOT VALIDoption; the constraint can later be made valid usingALTER DOMAIN ... VALIDATE CONSTRAINT. newly inserted or updated rows are always checked against all constraints, even those markedNOT VALID.NOT VALIDis only accepted forCHECKconstraints. πDROP CONSTRAINT [ IF EXISTS ]β this form drops constraints on a domain. ifIF EXISTSis specified and the constraint does not exist, no error is thrown. in this case a notice is issued instead. πRENAME CONSTRAINTβ this form changes the name of a constraint on a domain.VALIDATE CONSTRAINTβ this form validates a constraint previously added asNOT VALID, that is, it verifies that all values in table columns of the domain type satisfy the specified constraint.OWNERβ this form changes the owner of the domain to the specified user.RENAMEβ this form changes the name of the domain.SET SCHEMAβ this form changes the schema of the domain. any constraints associated with the domain are moved into the new schema as well. π¦
you must own the domain to use ALTER DOMAIN. to change the schema of a domain, you must also have CREATE privilege on the new schema. to alter the owner, you must be able to SET ROLE to the new owning role, and that role must have CREATE privilege on the domain's schema. (these restrictions enforce that altering the owner doesn't do anything you couldn't do by dropping and recreating the domain. however, a superuser can alter ownership of any domain anyway.) π
π PARAMETERS π
nameβ the name (possibly schema-qualified) of an existing domain to alter.domain_constraintβ new domain constraint for the domain.constraint_nameβ name of an existing constraint to drop or rename.NOT VALIDβ do not verify existing stored data for constraint validity.CASCADEβ automatically drop objects that depend on the constraint, and in turn all objects that depend on those objects (see Section 5.15).RESTRICTβ refuse to drop the constraint if there are any dependent objects. this is the default behavior.new_nameβ the new name for the domain.new_constraint_nameβ the new name for the constraint.new_ownerβ the user name of the new owner of the domain.new_schemaβ the new schema for the domain.
π₯ NOTES π₯
so here's the important tea: although ALTER DOMAIN ADD CONSTRAINT attempts to verify that existing stored data satisfies the new constraint, this check is not bulletproof, because the command cannot βseeβ table rows that are newly inserted or updated and not yet committed. if there is a hazard that concurrent operations might insert bad data, the way to proceed is to add the constraint using the NOT VALID option, commit that command, wait until all transactions started before that commit have finished, and then issue ALTER DOMAIN VALIDATE CONSTRAINT to search for data violating the constraint. this method is reliable because once the constraint is committed, all new transactions are guaranteed to enforce it against new values of the domain type. no cap. π₯
currently, ALTER DOMAIN ADD CONSTRAINT, ALTER DOMAIN VALIDATE CONSTRAINT, and ALTER DOMAIN SET NOT NULL will fail if the named domain or any derived domain is used within a container-type column (a composite, array, or range column) in any table in the database. they should eventually be improved to be able to verify the new constraint for such nested values. π
π EXAMPLES π
to add a NOT NULL constraint to a domain:
ALTER DOMAIN zipcode SET NOT NULL;
to remove a NOT NULL constraint from a domain:
ALTER DOMAIN zipcode DROP NOT NULL;
to add a check constraint to a domain:
ALTER DOMAIN zipcode ADD CONSTRAINT zipchk CHECK (char_length(VALUE) = 5);
to remove a check constraint from a domain:
ALTER DOMAIN zipcode DROP CONSTRAINT zipchk;
to rename a check constraint on a domain:
ALTER DOMAIN zipcode RENAME CONSTRAINT zipchk TO zip_check;
to move the domain into a different schema:
ALTER DOMAIN zipcode SET SCHEMA customers;
π§ COMPATIBILITY π§
ALTER DOMAIN conforms to the SQL standard, except for the OWNER, RENAME, SET SCHEMA, and VALIDATE CONSTRAINT variants, which are PostgreSQL extensions. the NOT VALID clause of the ADD CONSTRAINT variant is also a PostgreSQL extension. periodt. π
π SEE ALSO π
CREATE DOMAIN(CREATE_DOMAIN(7))DROP DOMAIN(DROP_DOMAIN(7))
xoxo, this cutie is from PostgreSQL 17.11, dated 2026 β page ALTER DOMAIN(7) ππ«Ά
π¦ where this page comes from
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms