【问题标题】:How to rename a partitions table with child tables如何用子表重命名分区表
【发布时间】:2020-09-10 10:18:13
【问题描述】:

我正在尝试重命名分区中的父表。我按年创建了 3 个子表。我可以更改父表的名称,但我不知道如何更改 3 个子表引用。

这是我的分区表结构。

CREATE TABLE IF NOT EXISTS test_demos(
id bigserial NOT NULL,  
partition_by integer NOT NULL,
names character varying (80) NOT NULL,
age integer,
aed character varying (5) NOT NULL,
entered_user_id integer,
entered_post_id integer,
entered_office_id integer,
dept_code character varying (25) NOT NULL,
owner_dept_code character varying (25) NOT NULL,
approval character varying (5) NOT NULL,
which inet,
whom macaddr,
who character varying(50),
row_created_at timestamp(0) WITHOUT TIME ZONE NOT NULL DEFAULT 
CURRENT_TIMESTAMP)
PARTITION BY LIST(partition_by);

CREATE TABLE IF NOT EXISTS test_demos2019s PARTITION OF test_demos FOR VALUES IN (2019);

CREATE TABLE IF NOT EXISTS test_demos2020s PARTITION OF test_demos FOR VALUES IN (2020);

CREATE TABLE IF NOT EXISTS test_demos2021s PARTITION OF test_demos FOR VALUES IN (2021);

【问题讨论】:

  • 你想在子表中改变什么?
  • 我也想更改子表名
  • 为您提供解决方案

标签: mysql postgresql postgresql-9.1


【解决方案1】:

在 PostgreSQL 中,我认为这是有效的。

ALTER TABLE test_demos2019s RENAME TO new_table_name;
ALTER TABLE test_demos2020s RENAME TO new_table_name2;
ALTER TABLE test_demos2021s RENAME TO new_table_name3;

【讨论】:

    【解决方案2】:

    改变表名的语法是,

    alter table table_name to new_table_name;
    

    在你的情况下是:

    alter table test_demos2019s to new_table_name;
    alter table test_demos2020s to new_table_name;
    alter table test_demos2021s to new_table_name;
    

    【讨论】:

      【解决方案3】:

      一般来说,你可以创建一个函数,它可以使用某种模式重命名所有子分区

      create or replace function rename_partitions()
          returns void as
      $BODY$
      declare 
          table_rec record;
      begin
          for table_rec in
              select 
                  inhrelid::regclass::text as c_tbl_name, 
                  -- you can use any postgres function to generate new names for child tables
                  split_part(inhrelid::regclass::text, '_', 2) as new_tbl_name
                               from   pg_catalog.pg_inherits
                               wgere  inhparent = 'test_demos'::regclass
          loop
              EXECUTE format($$alter table %I rename to %I$$, 
                         table_rec.c_tbl_name, table_rec.new_tbl_name);
          end loop;
      END;
      $BODY$
        LANGUAGE plpgsql;
      

      当然,你可以为这个函数添加参数,让它更通用。

      更多信息请点击此处:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-30
        • 2020-07-11
        • 2013-09-30
        • 1970-01-01
        • 1970-01-01
        • 2021-05-01
        相关资源
        最近更新 更多