How to apply PostgreSQL UNLOGGED feature to an existing table? - postgresql

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;

Related

bigint id changed back to int during table rename

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?

PostgreSQL add auto increment to empty ID column

I create table in PostgreSQL but I forgot to add auto increment.
How to alter empty Id column in Postgres to add auto increment?
Starting with Postgres 10 it's recommended to use identity columns for this.
You can turn an existing column into an identity column using an ALTER TABLE:
alter table the_table
alter id add generated always as identity;
If you already have data in the table, you will need to sync the sequence:
select setval(pg_get_serial_sequence('the_table', 'id'), (select max(id) from the_table));
You will need to create a sequence owned by that column and set that as the default value.
e.g.
CREATE TABLE mytable (id int);
CREATE SEQUENCE mytable_id_seq OWNED BY mytable.id;
ALTER TABLE mytable ALTER COLUMN id SET DEFAULT nextval('mytable_id_seq');

Altering tablespace for partitioned table in Postgres

In my database i have a partitioned table with name 'record_partitioned' on PostgreSQL 11.2.
i want to change its tablespace to a new tablespace 'fast_ssd' so all new dervied tables from this table be in 'fast_ssd' tablespace.
when i try to alter tablespace to 'fast_ssd'.
alter table record_partitioned set tablespace fast_ssd;
i see:
ALTER TABLE
but it seems nothing happened! i check tablespace like this:
SELECT tablespace,tablename FROM pg_tables where tablename='record_partitioned';
and output is:
tablespace | tablename
------------+--------------------
| record_partitioned
tablespace does not change.
There is no way to do this for a partitioned table. You'll have to add an explicit TABLESPACE clause whenever you create a partition.
An alternative is to set the default_tablespace parameter, but that would affect all other tables too.

How to rename a table inside a schema?

I'm using PostgreSQL 9.x, I want to rename a table. This SQL code:
CREATE TABLE new (id int);
ALTER TABLE new RENAME TO old;
DROP TABLE old;
renames the table correctly. But this SQL code:
CREATE SCHEMA domain;
CREATE TABLE domain.old (id int);
ALTER TABLE domain.old RENAME TO domain.new;
fails, with error:
ERROR: syntax error at or near "."
The "." underlined is the one between 'domain' and 'new'
One way to do this:
ALTER TABLE domain.old RENAME TO new
Other way:
SET search_path TO domain;
ALTER TABLE old RENAME TO new;
Documentation for search_path.
SET search_path TO domain;
ALTER TABLE IF EXISTS old_table_name RENAME TO new_table_name;
Switch to your database
machine$\c my_database
Tename the db
my_databse=# alter table old_name rename to new_name;

Copy Postgres table while maintaining primary key autoincrement

I am trying to copy a table with this postgres command however the primary key autoincrement feature does not copy over. Is there any quick and simple way to accomplish this? Thanks!
CREATE TABLE table2 AS TABLE table;
Here's what I'd do:
BEGIN;
LOCK TABLE oldtable;
CREATE TABLE newtable (LIKE oldtable INCLUDING ALL);
INSERT INTO newtable SELECT * FROM oldtable;
SELECT setval('the_seq_name', (SELECT max(id) FROM oldtable)+1);
COMMIT;
... though this is a moderately unusual thing to need to do and I'd be interested in what problem you're trying to solve.