-- This file and its contents are licensed under the Timescale License.
-- Please see the included NOTICE for copyright information and
-- LICENSE-TIMESCALE for a copy of the license.
CREATE UNLOGGED TABLE t1(time timestamptz) WITH (tsdb.hypertable);
NOTICE:  using column "time" as partitioning column
SELECT oid::regclass,relpersistence from pg_class where oid = 't1'::regclass;
 oid | relpersistence 
-----+----------------
 t1  | u

INSERT INTO t1 SELECT '2025-01-01';
SELECT count(compress_chunk(chunk)) FROM show_chunks('t1') chunk;
 count 
-------
     1

-- check that all objects related to hypertable are unlogged
SELECT oid::regclass,relpersistence from pg_class where relpersistence = 'u' AND relnamespace <> 'pg_toast'::regnamespace ORDER BY oid::regclass;
                                       oid                                        | relpersistence 
----------------------------------------------------------------------------------+----------------
 t1                                                                               | u
 t1_time_idx                                                                      | u
 _timescaledb_internal._hyper_1_1_chunk                                           | u
 _timescaledb_internal._hyper_1_1_chunk_t1_time_idx                               | u
 _timescaledb_internal.compress_hyper_2_2_chunk                                   | u
 _timescaledb_internal.compress_hyper_2_2_chunk__ts_meta_min_1__ts_meta_max_1_idx | u

ALTER TABLE t1 SET LOGGED;
SELECT oid::regclass,relpersistence from pg_class where relpersistence = 'u' AND relnamespace <> 'pg_toast'::regnamespace ORDER BY oid::regclass;
 oid | relpersistence 
-----+----------------

SELECT show_chunks('t1') AS "CHUNK" \gset
ALTER TABLE :CHUNK SET UNLOGGED;
-- only chunk and compressed chunk should be unlogged now but not hypertable
SELECT oid::regclass,relpersistence from pg_class where relpersistence = 'u' AND relnamespace <> 'pg_toast'::regnamespace ORDER BY oid::regclass;
                                       oid                                        | relpersistence 
----------------------------------------------------------------------------------+----------------
 _timescaledb_internal._hyper_1_1_chunk                                           | u
 _timescaledb_internal._hyper_1_1_chunk_t1_time_idx                               | u
 _timescaledb_internal.compress_hyper_2_2_chunk                                   | u
 _timescaledb_internal.compress_hyper_2_2_chunk__ts_meta_min_1__ts_meta_max_1_idx | u

ALTER TABLE t1 SET LOGGED;
-- all objects should be logged now
SELECT oid::regclass,relpersistence from pg_class where relpersistence = 'u' AND relnamespace <> 'pg_toast'::regnamespace ORDER BY oid::regclass;
 oid | relpersistence 
-----+----------------

DROP TABLE t1;
CREATE TABLE t2(time timestamptz) WITH (tsdb.hypertable);
NOTICE:  using column "time" as partitioning column
ALTER TABLE t2 SET UNLOGGED;
SELECT oid::regclass,relpersistence from pg_class where relpersistence = 'u' AND relnamespace <> 'pg_toast'::regnamespace ORDER BY oid::regclass;
     oid     | relpersistence 
-------------+----------------
 t2          | u
 t2_time_idx | u

INSERT INTO t2 SELECT '2025-01-01';
-- new chunk should be unlogged
SELECT oid::regclass,relpersistence from pg_class where relpersistence = 'u' AND relnamespace <> 'pg_toast'::regnamespace ORDER BY oid::regclass;
                        oid                         | relpersistence 
----------------------------------------------------+----------------
 t2                                                 | u
 t2_time_idx                                        | u
 _timescaledb_internal._hyper_3_3_chunk             | u
 _timescaledb_internal._hyper_3_3_chunk_t2_time_idx | u

ALTER TABLE t2 SET LOGGED;
-- everything should be logged now
SELECT oid::regclass,relpersistence from pg_class where relpersistence = 'u' AND relnamespace <> 'pg_toast'::regnamespace ORDER BY oid::regclass;
 oid | relpersistence 
-----+----------------

