How to create a Gin index on a geometry column in Postgresql? - postgresql

According to pgAdmin 4 4.21 documentation » Creating or Modifying a Table »
Select gin to create a GIN index. A GIN index may improve performance when managing two-dimensional geometric data types and nearest-neighbor searches
We should create a Gin index for geometric column if we intend to use Nearest-neighbor searches, Which I do!
However, when defining Gin index it asks for Operator Class and there are two options there (jsonb_path_obs and gin_int_ops) but none of them works with Geometry type.
Could someone please tell me how to create a Gin index on a Geometry type column?
P.S by geometry I mean PostGIS's geometry column type

Please link to the thing you are quoting so we don't have to go searching for it.
That looks like a bug in the pgadmin4 docs. They seem to have the GIN and GiST labels reversed in those descriptions. GIN supports multiple keys better than GiST does, but doesn't support nearest-neighbor or spatial. You want a GiST index.

Related

What is the difference between LTree and LTree_Gist in Postgresql?

In create table wizard of PGAdmin 4 when you open the column type drop-down menu, there is a type named ltree_gist.
Knowing that GIST is probably the best index option to use upon ltree columns, I suspect that ltree_gist is just ltree with an index defined on it as it is reasonable to create a ltree type column with a GIST index in just one move. But looks like its not that!
Long story short, could someone please explain the difference between ltree and ltree_gist in PGAdmin4 interface?
I could not find anything in the documentation.
ltree_gist is an implementation detail that is used for the implementation of GiST index support for ltree values. It is used for storing index entries.
That type cannot be used in SQL or table definitions directly.

jsonb data type lookup cost in postgres

This might be an obvious and simple question.
But I read through the jsonb data type documentation, but nowhere it mentions the lookup cost of a key in jsonb data.
For example, let's say I have a table with following schema:
CREATE TABLE A (id character varying (20),
info jsonb);
I want to know how postgres would parse a where query as below:
SELECT * FROM A WHERE info->>'city' = 'portland';
While going through the jsonb field of a row, is the lookup constant time (O(1)) or linear time (checking each key one by one in the row's jsonb dictionary) within that jsonb data dictionary?
My intuition is that it must be constant time (else what's the point of a dictionary style data?) but I can't see it in the official documentation to convince my team.
Any help would be great!
Thanks!
As with any WHERE condition in SQL: if there is no index, the database has to go through all rows of the table to find those that satisfy your condition.
You can either index a specific expression, or you can index the whole json value using a GIN index which then enables Postgres to use the index if any of the supported operators are used.
If you always check for the city, you can create a regular B-Tree index:
create index on a ( (info->>'city') );
If you don't know what you will be looking for, a GIN index might be a better choice:
create index on a using gin (info);
But you will need to change your query to use one of the operators that are supported by a GIN index, e.g. using the contains operator #>
select *
from a
where info #> '{"city": "portland"}::jsonb;
Note that an index lookup is not always the most efficient solution. Sometimes it's faster to simply go through all rows, sometimes the index lookup is faster.
If you want to learn more about indexes in relational database, go through the material here: http://use-the-index-luke.com/

Postgresql BTREE_GIN index with gin_trgm_ops option?

On https://www.postgresql.org/docs/current/static/pgtrgm.html it is explained how special GIN idexes with gin_trgm_ops option can be used to facilitate trigram similarity operator performance.
CREATE INDEX trgm_idx ON test_trgm USING GIN (t gin_trgm_ops);
It is also said:
These indexes do not support equality nor simple comparison operators,
so you may need a regular B-tree index too.
However, there is also BTREE_GIN extension which should allow GIN indexes to be used as substitute for BTREE indexes. https://www.postgresql.org/docs/current/static/btree-gin.html
My question is: If I install BTREE_GIN extension, could pg_trgm GIN index (with gin_trgm_ops option) be used as substitute for BTREE index? Does it combine properties of both BTREE_GIN and trigram GIN index, or additional BTREE index is still needed for joining and equality expressions etc.?
No, if you install btree_gin, you can create a GIN index over “basic” data types like integer, varchar or text.
This is normally useless, since you can use such an index for nothing that wouldn't be done better by a regular B-tree index, but it is very useful if you want to create a multicolumn GIN index including a column with such a data type, for example if you want to create a combined index for an expression like tscol ## to_tsquery('big data') AND intcol = 42.

Multicolumn index on 3 fields with heterogenous data types

I have a postgres table with 3 fields:
a : postgis geometry
b : array varchar[]
c : integer
and I have a query that involves all of them. I would like to add a multicolumn index to speed it up but I cannot as the 3 fields cannot go under the same index because of their nature.
What is the strategy in this case? Adding 3 indexes gist, gin and btree and postgres will use them all during the query?
Single-column index
Postgres can combine multiple indexes very efficiently in a single query with bitmap index scans. Most of the time, the most selective index is picked (or two, combined with bitmap index scans) and the rest is filtered. Once the result set is narrow enough, it's not efficient to scan more indexes.
Multicolumn index
It is still faster to have a perfectly matching multicolumn index, but not by orders of magnitude.
Since you want to include an array type I suggest to use a GIN index. AFAIK, operator classes are missing for general-purpose GiST indexes on array type. (The exception being intarray for integer arrays.)
To include the integer column, first install the additional module btree_gin, which provides the necessary GIN operator classes. Run once per database:
CREATE EXTENSION btree_gin;
Then you should be able to create your multicolumn index:
CREATE INDEX tbl_abc_gin_idx ON tbl USING GIN(a, b, c);
The order of index columns is irrelevant for GIN indexes. The manual:
A multicolumn GIN index can be used with query conditions that involve
any subset of the index's columns. Unlike B-tree or GiST, index search
effectiveness is the same regardless of which index column(s) the
query conditions use.
Nearest neighbour search
Since you are including a PostGis geometry type, chances are you want to do a nearest neighbour search, for which you need a GiST index. In this case I suggest two indexes:
CREATE INDEX tbl_ac_gist_idx ON tbl USING GiST(a, c); -- geometry type
CREATE INDEX tbl_bc_gin_idx ON tbl USING GIN(b, c);
You could add the integer column c to either or both. It depends.
For that, you need either btree_gin or btree_gist or both, respectively.
the 3 fields cannot go under the same index because of their nature
The 3 fields can go under the same index using the btree-gist module.

Postgres Indexing?

I am a newbie in postgres. I have a column named host (string varchar2) in a table which has around 20 million rows. How do I use indexing to optimize my search to find particular host. Also, this column will be updated daily do I need to write trigger indexing at particular interval? If yes, how do I do that? (For Records I am using Ruby and Rails 3)
Assuming you're doing exact matches, you should just be able to create the index and leave it:
CREATE INDEX host_index ON table_name (host)
The query optimizer should just use that automatically.
You may wish to specify other options such as the collation to use.
See the PostgreSQL docs for CREATE INDEX for more information.
I'd suggest using BRIN Index since its introduction from PostgreSQL 9.5 rather than the conventional btree index.
For text search, it is recommended that you use GIN or GiST index types.
https://www.postgresql.org/docs/9.5/static/textsearch-indexes.html
Another possibility is that if you were only performing exact matching in the host column, i.e., no inequality comparisons (>, <) and partial matching (like, wildcard) involved, you may consider converting host to a hash integer to speed up the search significantly.