小编Rj_*_*j_N的帖子

错误:PRIMARY KEY 约束定义中的列不足

我最近将数据库从 PostgreSQL v9.6 升级到 v11.7。我们有一些分区表,inherence并计划将它们迁移到declaration.

表 DDL:

CREATE TABLE c_account_p
(
    billing_account_guid character varying(40)  NOT NULL,
    ingestion_process_id bigint NOT NULL DEFAULT '-1'::integer,
    load_dttm timestamp(6) without time zone NOT NULL,
    ban integer NOT NULL,
    CONSTRAINT billing_account_pkey PRIMARY KEY (billing_account_guid, ban)
) PARTITION by RANGE(load_dttm);
Run Code Online (Sandbox Code Playgroud)

当我尝试创建表时,它抛出以下错误:

ERROR:  insufficient columns in the PRIMARY KEY constraint definition
DETAIL:  PRIMARY KEY constraint on table "l_billing_account_p" lacks column "load_dttm" which is part of the partition key.
SQL state: 0A000
Run Code Online (Sandbox Code Playgroud)

partition column应该是主键是强制性/必要的吗?因为如果我包括 …

postgresql partitioning postgresql-11

5
推荐指数
1
解决办法
2476
查看次数

为 WHERE COALESCE() 条件创建索引

我使用的是 PostgreSQL V9.6.11

表DDL:

CREATE TABLE test_c ( 
          insrt_prcs_id bigint NOT NULL, 
          updt_prcs_id bigint, src_sys_id integer NOT NULL,
          load_dttm timestamp(6) with time zone NOT NULL, 
          updt_dttm timestamp(6) without time zone);

Run Code Online (Sandbox Code Playgroud)

我试图index为下面的查询创建一个:

SELECT * 
FROM test_c             
WHERE COALESCE(u_dttm,l_dttm) > '2020-04-10 15:29:44.596311-07'
AND   COALESCE(u_dttm,l_dttm) <= '2020-04-11 15:29:44.596311-07' 
Run Code Online (Sandbox Code Playgroud)

创建index为:

create index idx_test_c  on test_c(COALESCE((updt_dttm, load_dttm)))
Run Code Online (Sandbox Code Playgroud)

但查询计划器没有扫描索引:

EXPLAIN ANALYZE
SELECT *            
FROM test_c             
WHERE COALESCE(u_dttm,l_dttm) > '2020-04-10 15:29:44.596311-07'
AND   COALESCE(u_dttm,l_dttm) <= '2020-04-11 15:29:44.596311-07' 
Run Code Online (Sandbox Code Playgroud)
Seq Scan on test_c as test_c (cost=0..1857.08 …
Run Code Online (Sandbox Code Playgroud)

postgresql index coalesce postgresql-9.6

4
推荐指数
1
解决办法
6460
查看次数