【问题标题】:Cannot refer to table in public schema by different schema无法通过不同的架构引用公共架构中的表
【发布时间】:2022-01-13 02:49:02
【问题描述】:

我有以下example

CREATE TABLE dt_totals (
    dt_total date NOT NULL,
    geo varchar(2) not null,
    impressions integer DEFAULT 0 NOT NULL,
    sales integer DEFAULT 0 NOT NULL
)
PARTITION BY RANGE (dt_total);



CREATE TABLE dt_totals_201801
PARTITION OF dt_totals
FOR VALUES FROM ('2018-01-01') TO ('2018-01-31')
PARTITION BY LIST (geo);


CREATE TABLE dt_totals_UK_201801 PARTITION OF dt_totals_201801 FOR VALUES IN ('UK');
CREATE TABLE dt_totals_US_201801 PARTITION OF dt_totals_201801 FOR VALUES IN ('US');
CREATE TABLE dt_totals_AU_201801 PARTITION OF dt_totals_201801 FOR VALUES IN ('AU');

在我的环境中生成一个名为 level_part 的架构;

CREATE SCHEMA IF NOT EXISTS level_part
    AUTHORIZATION postgres;

到目前为止一切顺利,我生成了主分区表:

CREATE TABLE IF NOT EXISTS level_part.dt_totals
(
    dt_total date NOT NULL,
    geo character varying(2) COLLATE pg_catalog."default" NOT NULL,
    impressions integer NOT NULL DEFAULT 0,
    sales integer NOT NULL DEFAULT 0
) PARTITION BY RANGE (dt_total);

ALTER TABLE IF EXISTS level_part.dt_totals
    OWNER to postgres;

-- Partitions SQL

CREATE TABLE public.dt_totals_201801 PARTITION OF level_part.dt_totals
    FOR VALUES FROM ('2018-01-01') TO ('2018-01-31')
    PARTITION BY LIST (geo);

ALTER TABLE IF EXISTS public.dt_totals_201801
    OWNER to postgres;
    

问题

  • 为什么指向公共架构
  • 是 pgadmin 错误吗?

当我尝试按如下方式生成分区时:

CREATE TABLE dt_totals_AU_201801 PARTITION OF level_part.dt_totals_201801 FOR VALUES IN ('AU');

我收到一个错误:

ERROR:  relation "level_part.dt_totals_201801" does not exist
SQL state: 42P01

但如果我将架构更改为公开,我可以参考该表:

CREATE TABLE dt_totals_AU_201801 PARTITION OF public.dt_totals_201801 FOR VALUES IN ('AU');

为什么我只能引用公共架构中的表?

【问题讨论】:

  • 好吧,您在架构 public 中创建了表/分区 dt_totals_201801 - 为什么您希望它在架构 level_part 中?
  • 顺便说一句:您不应该以超级用户身份创建所有表。这是一个非常糟糕的主意

标签: postgresql database-design partitioning database-partitioning postgresql-12


【解决方案1】:

当您第一次创建表时,您没有指定架构,因此 postgres 将表放在公共架构中。

"...没有 [指定] 架构名称的表。[...] 默认情况下,此类表(和其他对象)会自动放入名为“public”的架构中

更多:5.7.2. The Public Schema

【讨论】:

    猜你喜欢
    • 2020-01-17
    • 2019-05-20
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 2011-11-20
    • 2019-08-18
    • 1970-01-01
    相关资源
    最近更新 更多