ALTER_DOMAIN(7pg18)

๐Ÿ“ฆ version 18.6, in opensuse-leap 16.0

โœจ 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 subsequent INSERT commands. 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 only SET NOT NULL when 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 the NOT VALID option, and then later you can make it valid using ALTER DOMAIN ... VALIDATE CONSTRAINT. newly inserted or updated rows are always checked against all constraints, even the ones marked NOT VALID. and NOT VALID is only accepted for CHECK constraints โ€” that's the rule, periodt.
  • DROP CONSTRAINT [ IF EXISTS ] โ€” drops constraints on a domain. if IF EXISTS is 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 as NOT 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:

  1. add the constraint using the NOT VALID option
  2. commit that command
  3. wait until all transactions started before that commit have finished
  4. 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. slay, science ๐Ÿงš

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. 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 ๐Ÿ‘‘


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