通过来自不同未链接表的不同字段的连续循环

Consecutive LOOP through different fields from different not linked tables

我有几个 table 在网站上展示不同的实体。它们之间的联系对我的任务无关紧要。 (这也不是我的设计)。 我想通过一些文本字段来完成翻译、拼写检查等艰苦的工作。有 很多 文本。

我的问题是:我可以通过所有这些 table 按顺序查询 FOR 循环吗? array_cat 是否像下面的示例那样是一个适当的决定,或者它会吃掉我所有的 ram?还有另一种变体 - 创建第二个函数,该函数接收 table 名称作为参数。我很乐意 select 一个更简单的决定。

下面的测试示例不适用于 array_cat 中的语法错误。运算符 ||也给我语法错误。

DO
$$
DECLARE
  qs TEXT;
BEGIN
  FOR qs IN array_cat( -- as @ammoQ suggested to do UNION ALL
    SELECT array_agg(messages) FROM posts UNION ALL
    SELECT array_agg(comments) FROM boards )
  LOOP
    -- here I need: count chars really processed
    -- to do not exceed the billing quota of external api
    -- then make an UPSERT into cache table for another long process
    -- if quota is not exceeded
  END LOOP;
-- and return total of processed symbols
END;
$$ LANGUAGE plpgsql;

使用union

编辑:删除 array_agg

DO
$$
DECLARE
  qs TEXT;
BEGIN
  FOR qs IN (
    SELECT messages x FROM posts union all
    SELECT comments FROM boards )
  LOOP
    RAISE DEBUG 'TEXT is %', qs.x;
  END LOOP;
END;
$$ LANGUAGE plpgsql;