TL; DR
my_base=# SELECT *, pg_size_pretty(total_bytes) AS total
, pg_size_pretty(index_bytes) AS INDEX
, pg_size_pretty(toast_bytes) AS toast
, pg_size_pretty(table_bytes) AS TABLE
FROM (
SELECT *, total_bytes-index_bytes-COALESCE(toast_bytes,0) AS table_bytes FROM (
SELECT c.oid,nspname AS table_schema, relname AS TABLE_NAME
, c.reltuples AS row_estimate
, pg_total_relation_size(c.oid) AS total_bytes
, pg_indexes_size(c.oid) AS index_bytes
, pg_total_relation_size(reltoastrelid) AS toast_bytes
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE relkind = 'r' and relname = 'my_table'
) a
) a;
-[ RECORD 1 ]+----------------
oid | 24116771
table_schema | public
table_name | my_table
row_estimate | 0
total_bytes | 13250125824
index_bytes | 2306334720
toast_bytes | 8192
table_bytes | 10943782912
total | 12 GB
index | 2199 MB
toast | 8192 bytes
table | 10 GB
这怎么可能?
其余的......
大家好。
我必须在我的工作站上导入一个巨大的转储文件(12 Go)。我通过以下命令创建了这个转储文件:
pg_dump -U my_user -d my_base --data-only --table=my_table > my_table.sql
然后我在我的工作站上导入了转储文件:
psql -U my_base -f my_table.sql
1小时后,导入完成,输出如下:
SET
SET
SET
SET
SET
SET
SET
psql:my_table.sql:23308453: ERROR: Insert or update on table "my_table" violates custom foreign key trigger my_other_table_id_my_table_fk_"
DETAILS : Key (my_other_table_id = 474804) is not present in my_other_table
CONTEXT : COPY my_table, line 23118758: "... data ..."
setval
----------
23354338
(1 line)
根据我的理解,错误应该只是导入跳过文件的行。导入后,我的表已经增长,但它不包含任何行:
my_base=# SELECT *, pg_size_pretty(total_bytes) AS total
, pg_size_pretty(index_bytes) AS INDEX
, pg_size_pretty(toast_bytes) AS toast
, pg_size_pretty(table_bytes) AS TABLE
FROM (
SELECT *, total_bytes-index_bytes-COALESCE(toast_bytes,0) AS table_bytes FROM (
SELECT c.oid,nspname AS table_schema, relname AS TABLE_NAME
, c.reltuples AS row_estimate
, pg_total_relation_size(c.oid) AS total_bytes
, pg_indexes_size(c.oid) AS index_bytes
, pg_total_relation_size(reltoastrelid) AS toast_bytes
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE relkind = 'r' and relname = 'my_table'
) a
) a;
-[ RECORD 1 ]+----------------
oid | 24116771
table_schema | public
table_name | my_table
row_estimate | 0
total_bytes | 13250125824
index_bytes | 2306334720
toast_bytes | 8192
table_bytes | 10943782912
total | 12 GB
index | 2199 MB
toast | 8192 bytes
table | 10 GB
如果没有添加任何数据,我的桌子怎么会增长?几个小时后(我让我的工作站在夜间运行),我有以下结果(我猜autovacuum发挥了它的魔力):
my_base=# SELECT *, pg_size_pretty(total_bytes) AS total
, pg_size_pretty(index_bytes) AS INDEX
, pg_size_pretty(toast_bytes) AS toast
, pg_size_pretty(table_bytes) AS TABLE
FROM (
SELECT *, total_bytes-index_bytes-COALESCE(toast_bytes,0) AS table_bytes FROM (
SELECT c.oid,nspname AS table_schema, relname AS TABLE_NAME
, c.reltuples AS row_estimate
, pg_total_relation_size(c.oid) AS total_bytes
, pg_indexes_size(c.oid) AS index_bytes
, pg_total_relation_size(reltoastrelid) AS toast_bytes
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE relkind = 'r' and relname = 'my_table'
) a
) a;
-[ RECORD 1 ]+----------------
oid | 24116771
table_schema | public
table_name | my_table
row_estimate | 0
total_bytes | 2306359296
index_bytes | 2306334720
toast_bytes | 8192
table_bytes | 16384
total | 2200 MB
index | 2199 MB
toast | 8192 bytes
table | 16 kB
有人知道这里发生了什么吗?