postgres temporary table unlogged - ghdrako/doc_snipets GitHub Wiki
Temp buffers
This area stores temporary tables required during an operation and when more temporary result sets are needed during multiple join operations. It is also used whenever Work memory can no longer support the query operation. For example, to view the current allocation, you may issue the command below:
postgres=# SHOW temp_buffers;
temp_buffers
-------------
8MB
(1 row
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.
Find unlogged tables in datababase
SELECT
n.nspname AS schema_name,
c.relname AS table_name
FROM
pg_class c
JOIN
pg_namespace n ON n.oid = c.relnamespace
WHERE
c.relkind = 'r' -- 'r' oznacza zwykłą tabelę (relację)
AND c.relpersistence = 'u' -- 'u' oznacza niezalogowaną ('unlogged')
ORDER BY
schema_name, table_name;
Wyjaśnienie
- pg_class: To główny katalog systemowy, który przechowuje informacje o tabelach, indeksach, widokach i innych obiektach podobnych do tabel.
- relkind: Ta kolumna określa typ obiektu. Dla zwykłych tabel używamy wartości 'r' (relation).
- relpersistence: Ta kolumna określa trwałość obiektu:
- 'p': Permanent (trwała, domyślna tabela logowana).
- 'u': Unlogged (niezalogowana tabela - nie generuje WAL, tracona w przypadku awarii).
- 't': Temporary (tabela tymczasowa).
SELECT schemaname, tablename, relpersistence
FROM pg_catalog.pg_tables
WHERE relpersistence = 'u';
Widok pg_tables
To systemowy widok, który PostgreSQL tworzy dla wygody użytkowników.
W jego definicji znajdziesz mniej więcej coś takiego (uproszczona wersja):
CREATE VIEW pg_tables AS SELECT n.nspname AS schemaname, c.relname AS tablename, c.relowner AS tableowner, t.spcname AS tablespace, c.hasindexes, c.hasrules, c.hastriggers c.relpersistence, -- 'p' = permanent, 'u' = unlogged, 't' = temp c.relkind, -- 'r' = table, 'v' = view, 'i' = index, 'S' = sequence, ... FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace LEFT JOIN pg_tablespace t ON t.oid = c.reltablespace WHERE c.relkind = 'r' OR c.relkind = 'p'; -- r = regular table, p = partitioned table
#### Find unlogged tables with GENERATED columns - przeszkadzaja w upgrade
SELECT c.relname AS table_name, a.attname AS column_name, a.attidentity AS identity_type FROM pg_catalog.pg_class c JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE a.attidentity IN ('a', 'd') -- 'a' for GENERATED ALWAYS, 'd' for GENERATED BY DEFAULT AND c.relkind = 'r' -- Only consider ordinary tables AND c.relpersistence = 'u' -- Include unlogged tables ORDER BY c.relname, a.attname;
#### 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
* https://pganalyze.com/blog/5mins-postgres-unlogged-tables
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)