UNLOGGED Table Description
Features of UNLOGGED Tables
1. An UNLOGGED table
An UNLOGGED table is a special type of table provided by PostgreSQL. Its characteristic is that it does not record WAL (Write-Ahead Log) logs in exchange for higher write performance.
2. UNLOGGED table characteristics
Performance characteristics
Write performance improvement: Skips WAL logging
Reduction in I/O overhead: No need to wait for log flushing to disk
Reduction in storage pressure: Decreases WAL log space consumption.
Data characteristics
Data are still written to disk: Normal data file storage.
No durability guarantee: Data loss occurs following a database restart.
Not supported for replication: Not synchronized to replicas.
3. Identify UNLOGGED tables
-- View table type
SELECT
schemaname,
tablename,
CASE
WHEN relpersistence = 'u' THEN 'UNLOGGED'
WHEN relpersistence = 'p' THEN 'PERMANENT'
ELSE 'OTHER'
END as table_type
FROM pg_tables t
JOIN pg_class c ON c.relname = t.tablename
WHERE schemaname = 'public';
UNLOGGED Table Risks
1. Data security risks
Data loss risk
Server restart: All UNLOGGED table data is cleared.
Database crash: Abnormal shutdown causes data loss.
Hardware failure: Cannot be recovered through WAL logs.
2. High availability limitation
Master-slave replication issues
Replica contains no data: UNLOGGED tables are not replicated to replicas.
Failure switchover risk: Data loss after master-slave switchover.
3. Backup and recovery limitations
Backup policy limitations
Logical backup: pg_dump does not include UNLOGGED table data by default.
Physical backup: Base backup does not include UNLOGGED table data.
Point-in-time recovery: Cannot recover historical data of UNLOGGED tables.
4. Feature compatibility limitations
Unsupported features
Logical replication: Cannot be used as a publication table.
Pub/Sub: Logical decoding is not supported.
Certain extensions: Some extensions may be incompatible.
UNLOGGED Table Use Cases
1. Suitable usage scenarios
ETL data processing
Cache and session storage
Ad hoc computing and statistics
2. Performance optimization scenarios
Batch data import
Temporary index building
3. Unsuitable usage scenarios
Business-critical data
Configuration data requiring persistence
Convert UNLOGGED tables to LOGGED tables
1. Syntax transformation
The basic conversion commands are as follows:
-- Convert UNLOGGED tables to regular tables, where unlogged_table_name is the table name
ALTER TABLE unlogged_table_name SET LOGGED;
-- Lock status monitoring: the mode field indicates the lock type, AccessExclusiveLock is the most restrictive lock, and unlogged_table_name is the table name
SELECT
l.locktype,
l.mode,
l.granted,
l.pid,
a.state,
a.query_start,
now() - a.query_start as duration,
CASE
WHEN l.granted THEN 'GRANTED'
ELSE 'WAITING'
END as lock_status,
substring(a.query, 1, 80) as query_snippet
FROM pg_locks l
LEFT JOIN pg_stat_activity a ON l.pid = a.pid
WHERE l.relation IS NOT NULL
AND EXISTS (
SELECT 1 FROM pg_class c
WHERE c.oid = l.relation
AND c.relname = 'unlogged_table_name'
)
ORDER BY l.granted, a.query_start;
2. Conversion considerations
Conversion process: requires rewriting the entire table.
Locking time: Conversion of large tables can be lengthy, during which the tables remain locked throughout the process.
Space requirements: Additional storage space is needed during the conversion process.
3. Conversion best practices
It is recommended to adopt a batch conversion policy, as shown in the following example:
-- For large tables, it is recommended to convert them in batches
-- 1. Create a new LOGGED table
CREATE TABLE user_cache_new (LIKE user_cache);
-- 2. Migrate data in batches
INSERT INTO user_cache_new
SELECT * FROM user_cache
WHERE created_at >= '2024-01-01' AND created_at < '2024-01-02';
-- 3. Rename the table
BEGIN;
DROP TABLE user_cache;
ALTER TABLE user_cache_new RENAME TO user_cache;
COMMIT;
TencentDB for PostgreSQL Parameters
TencentDB for PostgreSQL introduces the tencentdb_log_unlogged_table parameter, while maintaining PostgreSQL's native functionality, to provide users with enhanced data security. For modifying parameters, see Setting Instance Parameters. Note:
It is recommended to keep tencentdb_log_unlogged_table = on in production environments.
It is recommended to regularly check and monitor existing UNLOGGED tables and convert them promptly.
The versions supported by this feature are v11.22_r1.26, v12.22_r1.29, v13.20_r1.24, v14.17_r1.33, v15.12_r1.18, v16.8_r1.13, v17.4_r1.7, v18.0_r1.0 and above. It is recommended that you upgrade the kernel minor version as soon as possible. Parameter Specifications
When tencentdb_log_unlogged_table = on (default):
All newly created UNLOGGED tables are automatically converted to LOGGED tables.
A WARNING prompt is displayed when a user creates an UNLOGGED table.
Existing UNLOGGED tables remain unchanged (require manual conversion).
Provides enhanced data security
-- Create UNLOGGED table
amy=> CREATE UNLOGGED TABLE test_table (id INT, data TEXT);
-- System WARNING:
WARNING: change unlogged table to logged table, If you want to use unlogged tables, please set tencentdb_log_unlogged_table to false.
CREATE TABLE
-- Verify table type, returns: 'p' (PERMANENT/LOGGED)
am=> SELECT relpersistence FROM pg_class WHERE relname = 'test_table';
relpersistence
----------------
p
(1 row
When tencentdb_log_unlogged_table = off:
All behaviors are consistent with native PostgreSQL.
Allows creation of genuine UNLOGGED tables
Users are responsible for assuming the risk of data loss.
Frequently Asked Questions (FAQ)
Does Modifying the tencentdb_log_unlogged_table Parameter Require Rebooting the Instance?
No. This parameter is dynamic and changes take effect immediately.
After the Parameter Is Set to on, Will Existing UNLOGGED Tables Be Converted Automatically?
No. The parameter only affects newly created tables. Existing UNLOGGED tables require manual ALTER TABLE SET LOGGED conversion.