-- -- Testing ALTER TABLE on columnar tables. -- CREATE SCHEMA columnar_alter; SET search_path tO columnar_alter, public; CREATE TABLE test_alter_table (a int, b int, c int) USING columnar; WITH sample_data AS (VALUES (1, 2, 3), (4, 5, 6), (7, 8, 9) ) INSERT INTO test_alter_table SELECT * FROM sample_data; -- drop a column ALTER TABLE test_alter_table DROP COLUMN a; -- test analyze ANALYZE test_alter_table; -- verify select queries run as expected SELECT * FROM test_alter_table; b | c --------------------------------------------------------------------- 2 | 3 5 | 6 8 | 9 (3 rows) SELECT a FROM test_alter_table; ERROR: column "a" does not exist SELECT b FROM test_alter_table; b --------------------------------------------------------------------- 2 5 8 (3 rows) -- verify insert runs as expected INSERT INTO test_alter_table (SELECT 3, 5, 8); ERROR: INSERT has more expressions than target columns INSERT INTO test_alter_table (SELECT 5, 8); -- add a column with no defaults ALTER TABLE test_alter_table ADD COLUMN d int; SELECT * FROM test_alter_table; b | c | d --------------------------------------------------------------------- 2 | 3 | 5 | 6 | 8 | 9 | 5 | 8 | (4 rows) INSERT INTO test_alter_table (SELECT 3, 5, 8); SELECT * FROM test_alter_table; b | c | d --------------------------------------------------------------------- 2 | 3 | 5 | 6 | 8 | 9 | 5 | 8 | 3 | 5 | 8 (5 rows) -- add a fixed-length column with default value ALTER TABLE test_alter_table ADD COLUMN e int default 3; SELECT * from test_alter_table; b | c | d | e --------------------------------------------------------------------- 2 | 3 | | 3 5 | 6 | | 3 8 | 9 | | 3 5 | 8 | | 3 3 | 5 | 8 | 3 (5 rows) INSERT INTO test_alter_table (SELECT 1, 2, 4, 8); SELECT * from test_alter_table; b | c | d | e --------------------------------------------------------------------- 2 | 3 | | 3 5 | 6 | | 3 8 | 9 | | 3 5 | 8 | | 3 3 | 5 | 8 | 3 1 | 2 | 4 | 8 (6 rows) -- add a variable-length column with default value ALTER TABLE test_alter_table ADD COLUMN f text DEFAULT 'TEXT ME'; SELECT * from test_alter_table; b | c | d | e | f --------------------------------------------------------------------- 2 | 3 | | 3 | TEXT ME 5 | 6 | | 3 | TEXT ME 8 | 9 | | 3 | TEXT ME 5 | 8 | | 3 | TEXT ME 3 | 5 | 8 | 3 | TEXT ME 1 | 2 | 4 | 8 | TEXT ME (6 rows) INSERT INTO test_alter_table (SELECT 1, 2, 4, 8, 'ABCDEF'); SELECT * from test_alter_table; b | c | d | e | f --------------------------------------------------------------------- 2 | 3 | | 3 | TEXT ME 5 | 6 | | 3 | TEXT ME 8 | 9 | | 3 | TEXT ME 5 | 8 | | 3 | TEXT ME 3 | 5 | 8 | 3 | TEXT ME 1 | 2 | 4 | 8 | TEXT ME 1 | 2 | 4 | 8 | ABCDEF (7 rows) -- drop couple of columns ALTER TABLE test_alter_table DROP COLUMN c; ALTER TABLE test_alter_table DROP COLUMN e; ANALYZE test_alter_table; SELECT * from test_alter_table; b | d | f --------------------------------------------------------------------- 2 | | TEXT ME 5 | | TEXT ME 8 | | TEXT ME 5 | | TEXT ME 3 | 8 | TEXT ME 1 | 4 | TEXT ME 1 | 4 | ABCDEF (7 rows) SELECT count(*) from test_alter_table; count --------------------------------------------------------------------- 7 (1 row) SELECT count(t.*) from test_alter_table t; count --------------------------------------------------------------------- 7 (1 row) -- unsupported default values ALTER TABLE test_alter_table ADD COLUMN g boolean DEFAULT isfinite(current_date); ALTER TABLE test_alter_table ADD COLUMN h DATE DEFAULT current_date; SELECT * FROM test_alter_table; ERROR: unsupported default value for column "g" HINT: Expression is either mutable or does not evaluate to constant value ALTER TABLE test_alter_table ALTER COLUMN g DROP DEFAULT; SELECT * FROM test_alter_table; ERROR: unsupported default value for column "h" HINT: Expression is either mutable or does not evaluate to constant value ALTER TABLE test_alter_table ALTER COLUMN h DROP DEFAULT; ANALYZE test_alter_table; SELECT * FROM test_alter_table; b | d | f | g | h --------------------------------------------------------------------- 2 | | TEXT ME | | 5 | | TEXT ME | | 8 | | TEXT ME | | 5 | | TEXT ME | | 3 | 8 | TEXT ME | | 1 | 4 | TEXT ME | | 1 | 4 | ABCDEF | | (7 rows) -- unsupported type change ALTER TABLE test_alter_table ADD COLUMN i int; ALTER TABLE test_alter_table ADD COLUMN j float; ALTER TABLE test_alter_table ADD COLUMN k text; -- this is valid type change ALTER TABLE test_alter_table ALTER COLUMN i TYPE float; -- this is not valid ALTER TABLE test_alter_table ALTER COLUMN j TYPE int; -- text / varchar conversion is valid both ways ALTER TABLE test_alter_table ALTER COLUMN k TYPE varchar(20); ALTER TABLE test_alter_table ALTER COLUMN k TYPE text; DROP TABLE test_alter_table; -- https://github.com/citusdata/citus/issues/4602 create domain str_domain as text not null; create table domain_test (a int, b int) using columnar; insert into domain_test values (1, 2); insert into domain_test values (1, 2); -- the following should error out since the domain is not nullable alter table domain_test add column c str_domain; ERROR: domain str_domain does not allow null values -- but this should succeed alter table domain_test add column c str_domain DEFAULT 'x'; SELECT * FROM domain_test; a | b | c --------------------------------------------------------------------- 1 | 2 | x 1 | 2 | x (2 rows) set default_table_access_method TO 'columnar'; CREATE TABLE has_volatile AS SELECT * FROM generate_series(1,10) id; ALTER TABLE has_volatile ADD col4 int DEFAULT (random() * 10000)::int; SELECT id, col4 < 10000 FROM has_volatile ORDER BY id; id | ?column? --------------------------------------------------------------------- 1 | t 2 | t 3 | t 4 | t 5 | t 6 | t 7 | t 8 | t 9 | t 10 | t (10 rows) -- https://github.com/citusdata/citus/issues/4601 CREATE TABLE itest13 (a int) using columnar; INSERT INTO itest13 VALUES (1), (2), (3); ALTER TABLE itest13 ADD COLUMN c int GENERATED BY DEFAULT AS IDENTITY; SELECT * FROM itest13 ORDER BY a; a | c --------------------------------------------------------------------- 1 | 1 2 | 2 3 | 3 (3 rows) create table atacc1 (a int) using columnar; insert into atacc1 values(1); -- should error out. It previously crashed. alter table atacc1 add column b float8 not null default random(), add primary key(a); ERROR: indexes not supported for columnar tables -- Add a generate column with an expression value create table test_gen_ex (x int) using columnar; INSERT INTO test_gen_ex VALUES (1), (2), (3); ALTER TABLE test_gen_ex ADD COLUMN y int generated always as (x+1) stored; SELECT * FROM test_gen_ex; x | y --------------------------------------------------------------------- 1 | 2 2 | 3 3 | 4 (3 rows) SET client_min_messages TO WARNING; DROP SCHEMA columnar_alter CASCADE;