使用 select 语句生成 postgres 注释的任何方法?

Any way to generate a postgres-comment using a select-statement?

是否有任何方法可以使用 select 语句(或动态生成它)来生成注释文本?

背景:

我在 table table_a 中存储了一个相当大的查询以供重用。在 table 的评论中,我存储了创建日期(以及数据状态的日期)。我使用 DbVisualizer (DbVisualizer-Variables) 的功能生成的这个日期::

comment on table table_a is 'Date: ${dbvis-date}$'

现在我使用这个 table_a 来计算一些东西并将其存储在 table table_b 中。

我现在想把table_a的评论作为table_b的评论。 注释可以按照How to retrieve the comment of a PostgreSQL database?:

中的描述获取
select description from pg_description
join pg_class on pg_description.objoid = pg_class.oid
where relname = 'table_a'

所以,如果我可以在 comment 中使用 select 语句,那将是一件容易的事 - 但显然不允许...

--doesn't work
comment on 'table_b' is (select description from pg_description
join pg_class on pg_description.objoid = pg_class.oid
where relname = 'table_a')

我使用 Postgresql 9.1

您可以在 anonymous block:

中使用 EXECUTE
DO 
$$
  DECLARE
    comment_text text;
  BEGIN
    comment_text := (
      select description 
      from pg_description 
      join pg_class on pg_description.objoid = pg_class.oid 
      where relname = 'table_a');

    EXECUTE('COMMENT ON TABLE table_b IS ''' || comment_text || '''');
  END;
$$;