在PostgreSQL上,我如何修改一个表,使其从多个父表继承?

huangapple go评论85阅读模式
英文:

On PostgreSQL, how can I alter a table to make it an inheritor from multiple parents?

问题

我正在开发一个在 Apache AGE 扩展中用 C 语言创建函数的功能,该功能执行 ALTER TABLE 命令以继承自父表。

根据 PostgreSQL 文档的 CREATE TABLE 部分,INHERITS 关键字可以接受父表的列表。然而,在 ALTER TABLE 的文档中,只提到了使用语法 INHERIT parent_table 继承单个父表。

我的问题是在 PostgreSQL 中是否可以使用 ALTER TABLE 命令继承来自父表列表的内容。如果可以,实现这一目标的正确语法是什么?附注:我正在使用 PostgreSQL 的版本 11。

这是我正在创建的函数(它没有运行,系统意外关闭):

英文:

I am working on creating a function in C-language in Apache AGE extension that performs an ALTER TABLE command to inherit from a parent table.

According to the PostgreSQL documentation for CREATE TABLE, the INHERITS keyword can take a list of parent tables. However, in the documentation for ALTER TABLE, it only mentions inheriting from a single parent table using the syntax INHERIT parent_table.

My question is whether it's possible to inherit from a list of parent tables using the ALTER TABLE command in PostgreSQL. If so, what would be the correct syntax to achieve this? PS: I'm using the version 11 of PostgreSQL.

Here's the function I'm creating (it's not working, system shutting down unexpectedly)

static void alter_table_inherit(char *graph_name, char *label_name,
                                    char *schema_name, char *seq_name,
                                    Oid relid, List* parents)
{
    ParseState *pstate;
    AlterTableStmt *tbl_stmt;
    AlterTableCmd *tbl_cmd;
    RangeVar *rv;

    /* RangeVar represents the table being altered */
    rv = makeRangeVar(schema_name, label_name, -1);

    pstate = make_parsestate(NULL);
    pstate->p_sourcetext = "(generated ALTER TABLE command)";

    tbl_stmt = makeNode(AlterTableStmt);
    tbl_stmt->relation = rv;
    tbl_stmt->missing_ok = false;

    tbl_cmd = makeNode(AlterTableCmd);
    tbl_cmd->subtype = AT_AddInherit;
    tbl_cmd->def = (Node *) parents;
    tbl_stmt->cmds = lappend(tbl_stmt->cmds, tbl_cmd);

    tbl_stmt->cmds = list_make1(tbl_cmd);

    AlterTable(relid, AccessExclusiveLock, tbl_stmt);

    CommandCounterIncrement();
}

答案1

得分: 2

你可以像这样添加多重继承父类:

ALTER TABLE tab
   INHERIT parent1,
   INHERIT parent2;

在C语言中,这将导致AlterTableStmt,其中cmds将是一个由两个AlterTableCmd组成的List*,分别对应每个父表。不要将tbl_cmd->def设置为List*

我注意到的另一件事是你没有设置tbl_stmt->objtype,所以它可能被设置为OBJECT_ACCESS_METHOD,这是不正确的。

可能更简单的方法是使用SPI接口来执行SQL语句。

我不会使用旧版本的PostgreSQL,比如v11进行开发。如果Apache Age不支持更多更新的版本,请放弃它。

英文:

You can add multiple inheritance parents like this:

ALTER TABLE tab
   INHERIT parent1,
   INHERIT parent2;

In C, that will result in an AlterTableStmt, where cmds will be a List* consisting of two AlterTableCmd, one for each parent table. Don't set tbl_cmd->def to a List*!

Another thing that I notice is that you didn't set tbl_stmt->objtype, so it is probably set to OBJECT_ACCESS_METHOD, which is not correct.

It might be simpler to use the SPI interface to execute SQL statements.

I would not develop with an old PostgreSQL version like v11. If Apache Age doesn't support anything more recent, abandon it.

huangapple
  • 本文由 发表于 2023年4月11日 07:51:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981528.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定