ALTER_DOMAIN(7pg18)
โจ ALTER DOMAIN โจ
okay bestie, pull up a chair, because we're doing PostgreSQL 18.6 documentation today and this one is literally about changing the definition of a domain ๐
(the manual is ALTER DOMAIN(7), if you're keeping score, and yes we're gonna read the whole thing together, no skimming)
๐ 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 ๐ฆ
so ALTER DOMAIN changes the definition of an existing domain. there's a bunch of sub-forms and honestly each one is kind of its own little vibe, so let's go one by one ๐ซถ
SET/DROP DEFAULTโ these set or remove the default value for a domain. โ ๏ธ heads up though: defaults only apply to subsequentINSERTcommands. they do not affect rows already chillin' in a table that's using the domain. past is past, bestie.SET/DROP NOT NULLโ these change whether a domain is marked to allow NULL values or to reject them. you can onlySET NOT NULLwhen the columns using the domain contain no null values. vibe check first, always.ADD domain_constraint [ NOT VALID ]โ adds a new constraint to a domain. when you add one, all columns using that domain get checked against the newly added constraint. those checks can be suppressed by adding the new constraint with theNOT VALIDoption, and then later you can make it valid usingALTER DOMAIN ... VALIDATE CONSTRAINT. newly inserted or updated rows are always checked against all constraints, even the ones markedNOT VALID. andNOT VALIDis only accepted forCHECKconstraints โ that's the rule, periodt.DROP CONSTRAINT [ IF EXISTS ]โ drops constraints on a domain. ifIF EXISTSis specified and the constraint doesn't exist, no error is thrown โ you just get a notice instead. cute, no drama ๐RENAME CONSTRAINTโ changes the name of a constraint on a domain. glow-up for your constraint names ๐VALIDATE CONSTRAINTโ validates a constraint that was previously added asNOT VALID. basically it verifies that all values in table columns of the domain type actually satisfy the specified constraint. the moment of truth โจOWNERโ changes the owner of the domain to the specified user.RENAMEโ changes the name of the domain.SET SCHEMAโ changes the schema of the domain. any constraints associated with the domain get moved into the new schema as well, they're coming along, no one gets left behind ๐ฅน
okay and now the permissions tea ๐ โ you must own the domain to use ALTER DOMAIN. to change the schema of a domain, you also need 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 are there to enforce that altering the owner doesn't let you do anything you couldn't already do by dropping and recreating the domain. however, a superuser can alter ownership of any domain anyway โ main character energy, no cap.)
๐ 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). it's giving chain reaction ๐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, real talk: even though 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's a hazard that concurrent operations might insert bad data, here's the way to proceed, step by step:
- add the constraint using the
NOT VALIDoption - commit that command
- wait until all transactions started before that commit have finished
- then issue
ALTER DOMAIN VALIDATE CONSTRAINTto 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. slay, science ๐ง
currently,ALTER DOMAIN ADD CONSTRAINT,ALTER DOMAIN VALIDATE CONSTRAINT, andALTER DOMAIN SET NOT NULLwill 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. until thenโฆ it's giving limitation ๐
๐ 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. so yeah, mostly standard, with a little extra spice of our own ๐
๐ SEE ALSO ๐
CREATE DOMAIN(CREATE_DOMAIN(7))DROP DOMAIN(DROP_DOMAIN(7))
and that's the whole page bestie, you ate it ๐ this was PostgreSQL 18.6, dated 2026 โ go forth and alter those domains responsibly, ily, bye ๐ซถโจ
๐ฆ where this page comes from
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms