ALTER TABLE
- Version
- 19
- Review
- Draft
Summary#
ALTER TABLE changes an existing table. Some changes only update the catalog, while others scan or rewrite the table, and many forms take an ACCESS EXCLUSIVE lock.
Syntax#
ALTER TABLE table_name ADD COLUMN column_name data_type DEFAULT value;
ALTER TABLE table_name RENAME COLUMN old_name TO new_name;
ALTER TABLE table_name ALTER COLUMN column_name TYPE data_type USING expression;
ALTER TABLE table_name ADD CONSTRAINT name CHECK (condition) [NOT VALID];
ALTER TABLE table_name ADD CONSTRAINT name NOT NULL column_name [NOT VALID];
ALTER TABLE table_name VALIDATE CONSTRAINT name;
Example#
The output below is from PostgreSQL 19.0, produced with psql -X -q -A -P null=NULL -v ON_ERROR_STOP=1 -f example.sql in a scratch database. The script rolls back at the end, so it leaves nothing behind.
BEGIN;
CREATE TEMPORARY TABLE members (
member_id integer PRIMARY KEY,
mail text,
age text
);
INSERT INTO members VALUES (1, 'a@example.com', '34'), (2, 'b@example.com', '27');
CREATE TEMPORARY TABLE file_before AS
SELECT pg_relation_filenode('members') AS filenode;
-- A constant default is added without rewriting existing rows.
ALTER TABLE members ADD COLUMN plan text NOT NULL DEFAULT 'free';
SELECT pg_relation_filenode('members') = filenode AS same_file_after_add
FROM file_before;
ALTER TABLE members RENAME COLUMN mail TO email;
ALTER TABLE members ALTER COLUMN age TYPE integer USING age::integer;
-- Enforce new writes first, then check the existing rows.
ALTER TABLE members
ADD CONSTRAINT email_present NOT NULL email NOT VALID,
ADD CONSTRAINT adult CHECK (age >= 18) NOT VALID;
SELECT conname, convalidated
FROM pg_constraint
WHERE conrelid = 'members'::regclass AND conname IN ('adult', 'email_present')
ORDER BY conname;
ALTER TABLE members VALIDATE CONSTRAINT email_present;
ALTER TABLE members VALIDATE CONSTRAINT adult;
SELECT conname, convalidated
FROM pg_constraint
WHERE conrelid = 'members'::regclass AND conname IN ('adult', 'email_present')
ORDER BY conname;
SELECT member_id, email, age, plan FROM members ORDER BY member_id;
ROLLBACK;
Expected output#
same_file_after_add
t
(1 row)
conname|convalidated
adult|f
email_present|f
(2 rows)
conname|convalidated
adult|t
email_present|t
(2 rows)
member_id|email|age|plan
1|a@example.com|34|free
2|b@example.com|27|free
(2 rows)
Common mistakes#
NOT VALID skips checking existing rows when you add the constraint. It still checks new writes. In a migration, commit after adding the constraint so the stronger lock is released, then validate in a later transaction under a SHARE UPDATE EXCLUSIVE lock. A volatile default such as clock_timestamp() or a column type change can rewrite the table.
Version applicability#
Since PostgreSQL 11, adding a column with a non-volatile default avoids a table rewrite. Adding a not-null constraint as NOT VALID requires PostgreSQL 18; NOT VALID check constraints work on earlier versions too.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0