-- pid
SELECT pg_backend_pid();

-- version
SELECT version();

-- search_path
SHOW search_path;

-- 查询表的 COLLATION VERSION
SELECT
    datname,
    datcollversion,
    pg_collation_actual_version((SELECT oid FROM pg_collation WHERE collname = 'default')) as system_version
FROM pg_database
WHERE datname IN ('fortern', 'postgres', 'kt_shop')
ORDER BY datname;

-- 重建索引
REINDEX DATABASE postgres;
ALTER DATABASE postgres REFRESH COLLATION VERSION;

REINDEX DATABASE fortern;
ALTER DATABASE fortern REFRESH COLLATION VERSION;

REINDEX DATABASE kt_shop;
ALTER DATABASE kt_shop REFRESH COLLATION VERSION;

-- 重置某表的某列的自增序列 [表名]_[列名]_seq
ALTER SEQUENCE person_id_seq RESTART WITH 4284858;

-- ctid 会返回一个元组，元组中第一个数字是 page number, 第二个是 slot number。
select r.ctid, r.* from study.r;
-- 得到
-- "(0,1)",1,101
-- "(0,2)",2,102
-- "(0,3)",3,103

-- 删除一条
delete from study.r where id = 2;
-- 再重新查询，得到
-- "(0,1)",1,101
-- "(0,3)",3,103
-- PostgreSQL 会保留空槽

-- 插入新数据
insert into study.r values (104, 4);
-- 再重新查询，得到
-- "(0,1)",1,101
-- "(0,3)",3,103
-- "(0,4)",4,104
-- 可以看到使用了末尾的新槽位，而不是使用中间的新槽位

-- 执行清理
vacuum full study.r;
-- 再次扫面，得到
-- "(0,1)",1,101
-- "(0,2)",3,103
-- "(0,3)",4,104
-- 看到空间已经回收

-- 查看数据类型
select pg_typeof(EXTRACT(EPOCH FROM current_timestamp(0)))
