ALTER_DOMAIN(7pg15)

πŸ“¦ version 15.19, in opensuse-leap 16.0

✨ ALTER DOMAIN ✨

Okay bestie, so you know how sometimes you make a whole custom data type and then you're like "wait, I need to tweak it"? That's literally what ALTER DOMAIN is for β€” it changes the definition of an existing domain. This is from the PostgreSQL 15.19 Documentation and it's giving main character energy for real. πŸ’…


🌸 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 are several sub-forms, and honestly each one is a little vibe:

πŸŽ€ SET/DROP DEFAULT

These forms 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 in a table using the domain. So if you're expecting retroactive magic, sorry bestie, that's not the vibe. πŸ™ˆ

πŸŽ€ SET/DROP NOT NULL

These forms change whether a domain is marked to allow NULL values or to reject NULL values. You can only SET NOT NULL when the columns using the domain contain no null values. No cap. πŸ’€

πŸŽ€ 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 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 those marked NOT VALID. Also NOT VALID is only accepted for CHECK constraints. Periodt. ✨

πŸŽ€ DROP CONSTRAINT [ IF EXISTS ]

This form drops constraints on a domain. If IF EXISTS is specified and the constraint does not exist, no error is thrown. In this case a notice is issued instead. So it's like, gentle. 🌸

πŸŽ€ RENAME CONSTRAINT

This form changes the name of a constraint on a domain. Iconic rename moment. πŸŽ€

πŸŽ€ VALIDATE CONSTRAINT

This form validates a constraint previously added as NOT VALID β€” that is, it verifies that all values in table columns of the domain type satisfy the specified constraint. It's literally the vibe check your data needed. βœ…

πŸŽ€ OWNER

This form changes the owner of the domain to the specified user. πŸ‘‘

πŸŽ€ RENAME

This form changes the name of the domain. Fresh name, fresh me. πŸ’…

πŸŽ€ SET SCHEMA

This form changes the schema of the domain. Any constraints associated with the domain are moved into the new schema as well. Moving day! 🚚✨


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 also be a direct or indirect member of 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.) So like, rules exist but also superusers do what they want. πŸ’€


πŸ’– PARAMETERS πŸ’–

  • name β€” The name (possibly schema-qualified) of an existing domain to alter. Gotta know which one you're messing with.
  • 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. It's giving "I'll deal with it later."
  • CASCADE β€” Automatically drop objects that depend on the constraint, and in turn all objects that depend on those objects (see Section 5.14). This is the dramatic one. 😭
  • RESTRICT β€” Refuse to drop the constraint if there are any dependent objects. This is the default behavior. Safe and cozy. 🫢
  • 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 🌈

Okay so here's the 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. So it's like, a whole process, but it works. 🀝

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. So like, not me finding a limitation. 😭 But it's real and you should know it.


πŸ”₯ 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 like, mostly standard but also a little extra. As it should be. ✨


πŸŽ€ SEE ALSO πŸŽ€


This absolute icon of a page is from PostgreSQL 15.19, dated 2026. Now go forth and alter those domains, bestie! You got this. πŸ’…βœ¨πŸ«Ά

πŸ“¦ where this page comes from