5

我已经设置了 Postgres 9.6 并检查了并行查询正在工作的随机整数的大表。但是,对另一个表的 XML 列的简单 XPath 查询始终是顺序的。在 Postgres 中,这两个 XPath 函数都被标记为并行安全的。我试图改变 XPath 成本,因此预期成本猛增,但并没有改变任何东西。我错过了什么?

示例表 DDL: CREATE TABLE "test_table" ("xml" XML );

示例查询: SELECT xpath('/a', "xml") FROM "test_table";

示例数据: <a></a>. 请注意,真实数据包含大小为 10-1000kB 的 XML。

> select pg_size_pretty(pg_total_relation_size('test_table'));
28 MB

> explain (analyze, verbose, buffers) select xpath('/a', "xml") from test_table;
Seq Scan on public.test_table  (cost=0.00..64042.60 rows=2560 width=32) (actual time=1.420..4527.061 rows=2560 loops=1)
  Output: xpath('/a'::text, xml, '{}'::text[])
  Buffers: shared hit=10588
Planning time: 0.058 ms
Execution time: 4529.503 ms
4

1 回答 1

4

这里的相关点可能是“关系大小”和“总关系大小”之间的区别:

CREATE TABLE test_table AS
  SELECT ('<a>' || repeat('x', 1000000) || '</a>')::xml AS "xml"
  FROM generate_series(1, 2560);

SELECT
  pg_size_pretty(pg_relation_size('test_table')) AS relation_size,
  pg_size_pretty(pg_total_relation_size('test_table')) AS total_relation_size;

 relation_size | total_relation_size
---------------+---------------------
 136 kB        | 30 MB

像这样的大列值不会存储在主关系中,而是推送到其关联的TOAST 表中。此外部存储不计入pg_relation_size(),这是优化器min_parallel_relation_size在评估并行计划时似乎要与之比较的内容:

SET parallel_setup_cost = 0;
SET parallel_tuple_cost = 0;
SET min_parallel_relation_size = '144kB';
EXPLAIN SELECT xpath('/a', "xml") FROM test_table;

                          QUERY PLAN
---------------------------------------------------------------
 Seq Scan on test_table  (cost=0.00..49.00 rows=2560 width=32)
SET min_parallel_relation_size = '136kB';
EXPLAIN SELECT xpath('/a', "xml") FROM test_table;

     QUERY PLAN
------------------------------------------------------------------------------
 Gather  (cost=0.00..38.46 rows=2560 width=32)
   Workers Planned: 1
   ->  Parallel Seq Scan on test_table  (cost=0.00..35.82 rows=1506 width=32)
于 2017-06-22T23:11:50.847 回答