按照 Postgres trgm docs 中的说明进行操作,我可以使用 GIN
和 GiST
创建索引:
home_accounting_dev=# \d+ test_trgm
Table "public.test_trgm"
Column | Type | Modifiers | Storage | Stats target | Description
--------+------+-----------+----------+--------------+-------------
t | text | | extended | |
Indexes:
"trgm_idx" gist (t gist_trgm_ops)
"trgm_idx_2" gin (t gin_trgm_ops)
Has OIDs: no
...但我似乎无法在现有表“支出”、“desc”列中这样做。
home_accounting_dev=# \d+ expenditures
Table "public.expenditures"
Column | Type | Modifiers | Storage | Stats target | Description
-------------+-----------------------------+-----------------------------------------------------------+----------+--------------+-------------
id | integer | not null default nextval('expenditures_id_seq'::regclass) | plain | |
desc | text | | extended | |
amount | character varying(255) | | extended | |
inserted_at | timestamp without time zone | not null | plain | |
updated_at | timestamp without time zone | not null | plain | |
expent_at | date | | plain | |
Indexes:
"expenditures_pkey" PRIMARY KEY, btree (id)
Referenced by:
TABLE "expenditures_taggings" CONSTRAINT "expenditures_taggings_assoc_id_fkey" FOREIGN KEY (assoc_id) REFERENCES expenditures(id)
Has OIDs: no
该列与表 test_trgm 中的示例“t”列的类型和特征完全相同,但当我尝试创建索引时,它会引发语法错误:
home_accounting_dev=# CREATE INDEX asdf_qwer ON expenditures USING gin (desc gin_trgm_ops);
ERROR: syntax error at or near "desc"
LINE 1: CREATE INDEX asdf_qwer ON expenditures USING gin (desc gin_t...
如果我错误输入列名称,它会以不同的方式失败:
home_accounting_dev=# CREATE INDEX asdf_qwer ON expenditures USING gin (dec gin_trgm_ops);
ERROR: column "dec" does not exist
最佳答案
desc
是保留字。将其用双引号括起来,它将起作用:
CREATE INDEX asdf_qwer ON expenditures USING gin ("desc" gin_trgm_ops);
关于postgresql - Postgres : syntax error creating index using prgm GIN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37334902/