在 Postgresql 中将一列的注释设置为另一列的注释

发布于 2024-07-08 17:18:46 字数 759 浏览 7 评论 0原文

假设我在 Postgresql 中创建一个表,其中有一列有注释:

create table t1 (
   c1 varchar(10)
);
comment on column t1.c1 is 'foo';

一段时间后,我决定添加另一列:

alter table t1 add column c2 varchar(20);

我想查找第一列的注释内容,并与新列关联:

select comment_text from (what?) where table_name = 't1' and column_name = 'c1'

(什么?)将是一个系统表,但是在 pgAdmin 中查看并在网络上搜索后,我还没有知道它的名称。

理想情况下,我希望能够:

comment on column t1.c1 is (select ...);

但我有一种感觉,事情有点牵强了。 感谢您的任何想法。

更新:根据我在这里收到的建议,我最终编写了一个程序来自动执行传输注释的任务,作为更改 Postgresql 列数据类型的更大过程的一部分。 您可以在我的博客上阅读相关内容。

Suppose I create a table in Postgresql with a comment on a column:

create table t1 (
   c1 varchar(10)
);
comment on column t1.c1 is 'foo';

Some time later, I decide to add another column:

alter table t1 add column c2 varchar(20);

I want to look up the comment contents of the first column, and associate with the new column:

select comment_text from (what?) where table_name = 't1' and column_name = 'c1'

The (what?) is going to be a system table, but after having looked around in pgAdmin and searching on the web I haven't learnt its name.

Ideally I'd like to be able to:

comment on column t1.c1 is (select ...);

but I have a feeling that's stretching things a bit far. Thanks for any ideas.

Update: based on the suggestions I received here, I wound up writing a program to automate the task of transferring comments, as part of a larger process of changing the datatype of a Postgresql column. You can read about that on my blog.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝情姑娘 2024-07-15 17:18:46

接下来要知道的是如何获取表 oid。 我认为,正如您所怀疑的那样,将此用作评论的一部分是行不通的。

    postgres=# create table comtest1 (id int, val varchar);
    CREATE TABLE
    postgres=# insert into comtest1 values (1,'a');
    INSERT 0 1
    postgres=# select distinct tableoid from comtest1;
     tableoid
    ----------
        32792
    (1 row)

    postgres=# comment on column comtest1.id is 'Identifier Number One';
    COMMENT
    postgres=# select col_description(32792,1);
        col_description
    -----------------------
     Identifier Number One
    (1 row)

不管怎样,我创建了一个快速的 plpgsql 函数来将注释从一个表/列对复制到另一个。 您必须在数据库上 createlang plpgsql 并像这样使用它:

    Copy the comment on the first column of table comtest1 to the id 
    column of the table comtest2. Yes, it should be improved but 
    that's left as work for the reader.

    postgres=# select copy_comment('comtest1',1,'comtest2','id');
     copy_comment
    --------------
                1
    (1 row)
CREATE OR REPLACE FUNCTION copy_comment(varchar,int,varchar,varchar) RETURNS int AS $PROC$
DECLARE
        src_tbl ALIAS FOR $1;
        src_col ALIAS FOR $2;
        dst_tbl ALIAS FOR $3;
        dst_col ALIAS FOR $4;
        row RECORD;
        oid INT;
        comment VARCHAR;
BEGIN
        FOR row IN EXECUTE 'SELECT DISTINCT tableoid FROM ' || quote_ident(src_tbl) LOOP
                oid := row.tableoid;
        END LOOP;

        FOR row IN EXECUTE 'SELECT col_description(' || quote_literal(oid) || ',' || quote_literal(src_col) || ')' LOOP
                comment := row.col_description;
        END LOOP;

        EXECUTE 'COMMENT ON COLUMN ' || quote_ident(dst_tbl) || '.' || quote_ident(dst_col) || ' IS ' || quote_literal(comment);

        RETURN 1;
END;
$PROC$ LANGUAGE plpgsql;

The next thing to know is how to obtain the table oid. I think that using this as part of comment on will not work, as you suspect.

    postgres=# create table comtest1 (id int, val varchar);
    CREATE TABLE
    postgres=# insert into comtest1 values (1,'a');
    INSERT 0 1
    postgres=# select distinct tableoid from comtest1;
     tableoid
    ----------
        32792
    (1 row)

    postgres=# comment on column comtest1.id is 'Identifier Number One';
    COMMENT
    postgres=# select col_description(32792,1);
        col_description
    -----------------------
     Identifier Number One
    (1 row)

Anyhow, I whipped up a quick plpgsql function to copy comments from one table/column pair to another. You have to createlang plpgsql on the database and use it like this:

    Copy the comment on the first column of table comtest1 to the id 
    column of the table comtest2. Yes, it should be improved but 
    that's left as work for the reader.

    postgres=# select copy_comment('comtest1',1,'comtest2','id');
     copy_comment
    --------------
                1
    (1 row)
CREATE OR REPLACE FUNCTION copy_comment(varchar,int,varchar,varchar) RETURNS int AS $PROC$
DECLARE
        src_tbl ALIAS FOR $1;
        src_col ALIAS FOR $2;
        dst_tbl ALIAS FOR $3;
        dst_col ALIAS FOR $4;
        row RECORD;
        oid INT;
        comment VARCHAR;
BEGIN
        FOR row IN EXECUTE 'SELECT DISTINCT tableoid FROM ' || quote_ident(src_tbl) LOOP
                oid := row.tableoid;
        END LOOP;

        FOR row IN EXECUTE 'SELECT col_description(' || quote_literal(oid) || ',' || quote_literal(src_col) || ')' LOOP
                comment := row.col_description;
        END LOOP;

        EXECUTE 'COMMENT ON COLUMN ' || quote_ident(dst_tbl) || '.' || quote_ident(dst_col) || ' IS ' || quote_literal(comment);

        RETURN 1;
END;
$PROC$ LANGUAGE plpgsql;
像你 2024-07-15 17:18:46

您可以使用系统函数 col_description(table_oid, column_number) 检索列上的注释。 请参阅此页面了解更多细节。

You can retrieve comments on columns using the system function col_description(table_oid, column_number). See this page for further details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文