Adding a new column NOT NULL in postgresql

It is difficult to add a new field in a table in postgresql with NOT NULL. However it is possible (7.3).
This will change in future versions to work without this "hack".
In this example we will add a field "foo" to table "tab". foo is BOOL NOT NULL DEFAULT FALSE.
ALTER TABLE tab ADD COLUMN foo BOOL;
ALTER TABLE tab ALTER COLUMN foo SET DEFAULT false;
SELECT oid,* FROM pg_class WHERE relname = 'tab';
-- We get the value 55555 from oid column
SELECT * FROM pg_attribute WHERE attrelid=55555 AND attname='foo';
-- We verify that it truly is the column we want to change
-- (probably need to be superuser to do this)
UPDATE pg_attribute SET attnotnull=true WHERE attrelid=55555 AND attname='foo';
So We add the column foo to table tab.
Set default value on the field.
Lookup attrelid and attname for the field in pg_attribute.
Change attnotnull to true for the row that matches.
Simplified we could do it like this
ALTER TABLE tab ADD COLUMN foo BOOL;
ALTER TABLE tab ALTER COLUMN foo SET DEFAULT false;
UPDATE pg_attribute SET attnotnull=true WHERE attrelid=(SELECT oid FROM pg_class WHERE relname='tab') AND attname='foo';

Author Per-Olof Pettersson