I changed DISTSTYLE of my Redshift table to KEY:
ALTER TABLE table_name ALTER DISTSTYLE KEY DISTKEY column_name;
everything is working fine. Next, I'd like to change DISTSTYLE to EVEN. I use the following query:
ALTER TABLE table_name ALTER DISTSTYLE EVEN;
but it fails with the following error:
[XX000] ERROR: Unsupported ALTER TABLE ALTER DISTEVEN
What am I doing wrong and how to fix it?
Related
I hit the int limit on a large table I use.
The table is in single user mode and has no FK constraints.
CREATE TABLE my_table_bigint (LIKE my_table INCLUDING ALL);
ALTER TABLE my_table_bigint ALTER id DROP DEFAULT;
ALTER TABLE my_table_bigint alter column id set data type bigint;
CREATE SEQUENCE my_table_bigint_id_seq;
INSERT INTO my_table_bigint SELECT * FROM my_table;
ALTER TABLE my_table_bigint ALTER id SET DEFAULT nextval('my_table_bigint_id_seq');
ALTER SEQUENCE my_table_bigint_id_seq OWNED BY my_table_bigint.id;
SELECT setval('my_table_bigint_id_seq', (SELECT max(id) FROM my_table_bigint), true);
At this point I tested that I could insert new rows without any problems. Success, I thought.
I went about renaming the tables.
alter table my_table rename my_table_old
alter table my_table_bigint rename my_table
ALTER INDEX post_comments_pkey RENAME TO post_comments_old_pkey
ALTER INDEX post_comments_pkey_bigint RENAME TO post_comments_pkey
Now, when I checked the schema.... the table ID type had changed BACK to integer, instead of bigint.
Copying took about 3 days - so I am really, really hoping that I don't need to do this again. This is postgres10 on RDS.
EDIT
I'm going to take care of this problem like this:
Create a new table - call it my_table_bigint2.
Do this:
CREATE TABLE my_table_bigint2 (LIKE my_table INCLUDING ALL);
ALTER TABLE my_table_bigint2 ALTER id DROP DEFAULT;
ALTER TABLE my_table_bigint2 alter column id set data type bigint;
CREATE SEQUENCE my_table_bigint2_id_seq;
ALTER TABLE my_table_bigint2 ALTER id SET DEFAULT nextval('my_table_bigint2_id_seq');
ALTER SEQUENCE my_table_bigint2_id_seq OWNED BY my_table_bigint2.id;
And start populating that table with the new data. (This is fine given the usecase.)
In the meantime, I'm going to run
ALTER TABLE post_comments alter column id set data type bigint;
And finally, once that's done, I'm going to
INSERT INTO my_table SELECT * FROM my_table_bigint2;
My follow-up question - is this allowed? Will this create some interaction between the sequences? Should I use a new sequence?
I cannot drop a constraint in postgres even using adminer
ALTER TABLE table_name
DROP CONSTRAINT constraint_name;
Error in query : ERROR: constraint "constraint_name" of relation "table_name" does not exist
but if I create a new one like :
ALTER TABLE table_name
ADD CONSTRAINT my_new_constraint(column1, column2, ... column_n);
Then it works and I can drop it.
The one who made the constraint I try to drop did it this way two years ago:
create unique index constraint_name on table_name (column1,lower(column2),coalesce(deleted_at,\'19000101\')
If anyone has got any idea to drop this constraint?
CREATE UNIQUE INDEX creates an index that needs to be dropped with DROP INDEX, not a table constraint.
I've been getting this error when trying to alter sortkeys on a table (the table has ~1M rows x ~12 columns). The table has no sortkeys before doing the alter as seen below:
alter table table_name
alter sortkey (date_col, col2, col3);
This tries to run for a few seconds before returning the following:
ERROR: Too much content of 'table_name' are deleted during executing alter distkey command. Please Retry.
Has anyone run into this particular error before? My solution has been to create a new table with my desired sortkeys, which works fine.
create table table_name_sorted
sortkey (date_col, col2, col3)
as (
select * from table_name
);
I created a table in PostgreSQL like this:
CREATE TABLE Table1 (
Id varchar(100) PRIMARY KEY CHECK (Id ~ '^[a-z0-9]{3,15}$'),
...
);
This will automatically create a constraint called table1_id_check.
Now I would like to change the check constraint to
(Id ~ '^[a-z0-9]{3,}$')
How can I do this in PostgreSQL as a single statement without dropping the constraint and recreating it again?
Using multiple statements within a transaction works on all SQL dbms that support using this DDL in a transaction.
begin transaction;
alter table table1
drop constraint table1_id_check;
alter table table1
add constraint table1_id_check CHECK (Id ~ '^[a-z0-9]{3,}$');
commit;
PostgreSQL lets you use multiple clauses within an ALTER TABLE statement.
alter table table1
drop constraint table1_id_check,
add constraint table1_id_check CHECK (Id ~ '^[a-z0-9]{3,}$');
Can I ALTER an existing table to be UNLOGGED?
PostgreSQL 9.5+ allows setting an existing table as LOGGED / UNLOGGED with the ALTER TABLE command... detailed better here.
For e.g.
ALTER TABLE table_test SET LOGGED;
ALTER TABLE table_test SET UNLOGGED;
The following solution is for PostgreSQL versions<=9.4:
You can do:
create unlogged table your_table_alt as
select * from your_table;
Then:
drop table your_table;
alter table your_table_alt rename to your_table;