简体   繁体   English

如何在 citus postgres 中获取分片表的聚合行数

[英]How to get aggregate row count for sharded tables in citus postgres

Requirement is to get sum of reltuples for all distributed tables in Citus Postgres database with a certain format which is explained below.要求是获取 Citus Postgres 数据库中所有分布式表的 reltuples 总和,格式如下所述。

When i run the below command, the query propagates to workers and depending on the shard counts on each worker, results are fetched for each table:当我运行以下命令时,查询传播到工作人员,并根据每个工作人员的分片计数,为每个表获取结果:

SELECT result from run_command_on_workers( $cmd$ select json_agg(json_build_object(reltuples, relname)) from pg_class c join pg_catalog.pg_namespace n on n.oid=c.relnamespace where n.nspname not in ('citus', 'pg_toast', 'pg_catalog')

For ex, the results consists of <row_count, table_name_and_shardid> {10, table_A_shardid0}, {20,table_A_shardid1}, {15, table_B_shardid0} Is it possible to combine counts from table_A_shardid0 and table_A_shardid1 so that the result is {30, table_A}例如,结果由 <row_count, table_name_and_shardid> {10, table_A_shardid0}, {20,table_A_shardid1}, {15, table_B_shardid0}是否可以组合来自table_A_shardid0table_A_shardid1的计数,以便结果为{30, table_A}

You can use run_command_on_shards to query on all shards of a table.您可以使用run_command_on_shards查询表的所有分片。 This way you can get all reltuple values from shards and combine them later in the coordinator to get your desired format.通过这种方式,您可以从分片中获取所有 reltuple 值,并稍后在协调器中将它们组合以获得您想要的格式。 You can learn more about the command here at the Citus Official Documentation您可以在Citus 官方文档中了解有关该命令的更多信息

The catalog table pg_dist_partition stores metadata about which tables in the database are distributed.目录表pg_dist_partition存储有关数据库中哪些表被分发的元数据。

Using what I have explained above, the query can look something like this:使用我上面解释的内容,查询看起来像这样:

WITH shards_reltuples AS (
  SELECT logicalrelid,
  (run_command_on_shards(logicalrelid,
     $$ SELECT reltuples FROM pg_class WHERE oid = '%s'::regclass::oid $$ 
  )).result::real AS reltuples
  FROM pg_dist_partition)

SELECT logicalrelid, sum(reltuples) AS reltuples
FROM shards_reltuples GROUP BY logicalrelid;

You can elaborate on top of it to get your desired json format.您可以在上面详细说明以获得所需的 json 格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM