Skip to content

Instantly share code, notes, and snippets.

@jeltz
Created June 3, 2019 06:29
Show Gist options
  • Save jeltz/805b360893dd918bbabc38719f29b8ed to your computer and use it in GitHub Desktop.
Save jeltz/805b360893dd918bbabc38719f29b8ed to your computer and use it in GitHub Desktop.
postgres=# CREATE TABLE t (a int);
CREATE TABLE
postgres=# SELECT * FROM pg_attrdef;
oid | adrelid | adnum | adbin
-----+---------+-------+-------
(0 rows)
postgres=# INSERT INTO t VALUES (1);
INSERT 0 1
postgres=# ALTER TABLE t ADD b int DEFAULT 2;
ALTER TABLE
postgres=# INSERT INTO t VALUES (2);
INSERT 0 1
postgres=# SELECT * FROM pg_attrdef;
-[ RECORD 1 ]------------------------------------------------------------------------------------------------------------------------------------------------
oid | 22896
adrelid | 22893
adnum | 2
adbin | {CONST :consttype 23 :consttypmod -1 :constcollid 0 :constlen 4 :constbyval true :constisnull false :location 32 :constvalue 4 [ 2 0 0 0 0 0 0 0 ]}
postgres=# SELECT attmissingval FROM pg_attribute WHERE attname = 'b';
attmissingval
---------------
{2}
(1 row)
postgres=# SELECT * FROM t;
a | b
---+---
1 | 2
2 | 2
(2 rows)
postgres=# ALTER TABLE t ALTER b SET DEFAULT 42;
ALTER TABLE
postgres=# INSERT INTO t VALUES (3);
INSERT 0 1
postgres=# SELECT * FROM pg_attrdef;
-[ RECORD 1 ]-------------------------------------------------------------------------------------------------------------------------------------------------
oid | 22897
adrelid | 22893
adnum | 2
adbin | {CONST :consttype 23 :consttypmod -1 :constcollid 0 :constlen 4 :constbyval true :constisnull false :location 34 :constvalue 4 [ 42 0 0 0 0 0 0 0 ]}
postgres=# SELECT attmissingval FROM pg_attribute WHERE attname = 'b';
attmissingval
---------------
{2}
(1 row)
postgres=# SELECT * FROM t;
a | b
---+----
1 | 2
2 | 2
3 | 42
(3 rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment