Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query using subset of path in Postgres

Given this table:

 id |            points (path)                 |
----+------------------------------------------+
  1 | ((1,2),(3,4),(5,6),(7,8))                |

Is it possible to achieve the following using a single geometric operator and a path argument (sequential subset of the containing path), like ((3,4),(5,6))?

select * from things where points @> '(3,4)' and points @> '(5,6)';

like image 705
pdoherty926 Avatar asked Apr 23 '26 21:04

pdoherty926


1 Answers

Maybe just convert it to string and match using LIKE (you need double because the path is closed):

select points::text  from things 
where (points::text || points::text) like '%(3,4),(5,6)%';

If you have a lot of things it`s worth to build index for the path, which will be used in the like query (you need trgm extension)

CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE INDEX thing_text_paths ON things USING gin ( (points::text || points::text) gin_trgm_ops);

You can see it by running

SET enable_seqscan = OFF;
EXPLAIN select points::text  from things 
where (points::text || points::text) like '%(3,4),(5,6)%';

See http://sqlfiddle.com/#!17/bd760/2/0

like image 171
luki Avatar answered Apr 28 '26 04:04

luki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!