2

Following the instructions at Postgres trgm docs, I'm able to create indexes using both GIN and 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

... but I can't seem to do so in an existent table "expenditures", column "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

The column is exactly the same type and characteristics as this example "t" column in the table test_trgm, but it throws a syntax error when I try to create the index:

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...

It fails differently if I misstype the column name:

home_accounting_dev=# CREATE INDEX asdf_qwer ON expenditures USING gin (dec gin_trgm_ops);
ERROR:  column "dec" does not exist
0

1 Answer 1

3

desc is a reserved word. Enclose it in double quotes and it will work:

CREATE INDEX asdf_qwer ON expenditures USING gin ("desc" gin_trgm_ops);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.