PostgreSQL/PostGIS 9.6 破坏了我的复合索引

Dr.*_*YSG 8 postgresql postgis postgresql-9.6

在 PostgreSQL 9.2 中,我可以轻松创建具有地理 (postGIS) 类型和整数作为复合索引的索引。但是现在(9.6)它抱怨创建索引,我不明白它提供的提示:

列和数据都已正确创建,Postgres 抱怨创建索引。

ERROR: data type integer has no default operator class for access method "gist" 
HINT: You must specify an operator class for the index 
      or define a default operator class for the data type. 
********** Error**********  
ERROR: data type integer has no default operator class for access method "gist" 
SQL state: 42704 
Hint: You must specify an operator class for the index 
      or define a default operator class for the data type.
Run Code Online (Sandbox Code Playgroud)

模式定义如下:

- Table: portal.inventory

-- DROP TABLE portal.inventory;

CREATE TABLE portal.inventory
(
  type character varying,
  pid integer,
  size bigint,
  date timestamp without time zone,
  path character varying,
  outline geography(Polygon,4326)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE portal.inventory
  OWNER TO postgres;

-- Index: portal.inventory_compound_idx

-- DROP INDEX portal.inventory_compound_idx;

CREATE INDEX inventory_compound_idx
  ON portal.inventory
  USING gist
  (outline, pid);

-- Index: portal.inventory_icompound_idx

-- DROP INDEX portal.inventory_icompound_idx;

CREATE INDEX inventory_icompound_idx
  ON portal.inventory
  USING gist
  (pid, outline);
Run Code Online (Sandbox Code Playgroud)

joa*_*olo 8

你需要EXTENSION在你的数据库中安装一个特定的:

CREATE EXTENSION btree_gist ;
Run Code Online (Sandbox Code Playgroud)

根据btree_gist上的PostgreSQL 文档

btree_gist 提供了 GiST 索引操作符类,它们为数据类型 int2、int4、int8、float4、float8、numeric、timestamp with time zone、timestamp without time zone、time with time zone、time without time zone、date 实现 B 树等效行为、interval、oid、money、char、varchar、text、bytea、bit、varbit、macaddr、inet 和 cidr。

一般来说,这些操作符类不会胜过等效的标准 B 树索引方法,并且它们缺乏标准 B 树代码的一个主要特性:强制唯一性的能力。但是,它们提供了 B 树索引所不具备的一些其他功能,如下所述。此外,当需要多列 GiST 索引时这些运算符类很有用,其中一些列的数据类型只能用 GiST 索引,而其他列只是简单的数据类型。最后,这些操作符类对于 GiST 测试很有用,并作为开发其他 GiST 操作符类的基础。

(强调我的)

btree_gist 是标准(当前)PostgreSQL 安装的一部分,因此,您实际上不需要在系统中安装任何文件。

安装该扩展后,您可以执行所有这些指令在一个干净的安装PostgreSQL 9.6.2,无故障:

-- If there is not there, create extension PostGis as well
CREATE EXTENSION IF NOT EXISTS postgis ;

-- Create Schema `portal`
CREATE SCHEMA IF NOT EXISTS portal ;
Run Code Online (Sandbox Code Playgroud)

CREATE并无故障地执行所有语句。

CREATE TABLE portal.inventory
(
  type character varying,
  pid integer,
  size bigint,
  date timestamp without time zone,
  path character varying,
  outline geography(Polygon,4326)
);

CREATE INDEX inventory_compound_idx
  ON portal.inventory
  USING gist
  (outline, pid);

CREATE INDEX inventory_icompound_idx
  ON portal.inventory
  USING gist
  (pid, outline);
Run Code Online (Sandbox Code Playgroud)

注意:根据@Erwin Brandstetter 的评论,9.2 版也需要这样做。因此,最有可能的是,如果您对 9.2 版数据库进行转储,则CREATE EXTENSION btree_gist ;应该会出现该语句。