postgres temporary table unlogged - ghdrako/doc_snipets GitHub Wiki
In PostgreSQL, each table or index is stored in one or more files. When a table or index exceeds 1 GB, it is divided into gigabyte-sized segments.
Temporary table
begin work;
create temp table if not exists temp_users_transaction (
pk int GENERATED ALWAYS AS IDENTITY
,username text NOT NULL
,gecos text
,email text NOT NULL
,PRIMARY KEY( pk )
,UNIQUE ( username )
) on commit drop;
\d temp_users_transaction
commit work;
\d temp_users_transaction # table dissapear
Unlogged table
Unlogged tables are much faster than classic tables (also known as logged tables) but are not crash-safe. This means that the consistency of the data is not guaranteed in the event of a crash.
CREATE UNLOGGED TABLE staging_table ( /* table definition */ );
create unlogged table if not exists unlogged_users (
pk int GENERATED ALWAYS AS IDENTITY
,username text NOT NULL
,gecos text
,email text NOT NULL
Chapter 4
89
,PRIMARY KEY( pk )
,UNIQUE ( username )
);
When you have data in your unlogged table and you shutdown Postgres with a restart, for example, then the data will still be there.
However, when there is a crash, Postgres will truncate it. And that's in a sense a feature. The reason that it truncates is because it's not clear what the data is.
Unlogged to Logged
When working with unlogged tables, if you plan to make them logged later, keep in mind that transition will create a lot of Write Ahead Log (WAL) changes suddenly, which can cause a lot of resource load.
disable Autovacuum
Autovacuum can also be disabled, which keeps it from consuming resources.
ALTER TABLE trip_requests_intermediate
SET (autovacuum_enabled = false)