age: DBMS does not use indexes within where clause

Describe the bug Age DBMS uses (gin) indexes only when the condition is specified within the match clause and not in the where clause. Moreover, the same issue seems to be present also when trying to use a btree index on a node graphid.

How are you accessing AGE (Command line, driver, etc.)? From SQL script, DBeaver CE v22.3.2.202301060755

What data setup do we need to do?

SET search_path = ag_catalog, "$user", public;

select * from create_graph('graph') ;

CREATE OR REPLACE FUNCTION public.create_random_nodes() RETURNS setof agtype AS $$
	SELECT agtype_build_map(
		'Id',
		substr(md5(random()::text), 1, 1000),
		'Value',
		(random() * 70 + 10)::integer,
		'Date',
		DATE '2018-01-01' + (random() * 700)::integer
	)
	FROM generate_series(1, 1000000);
$$ LANGUAGE sql;

select * from cypher('graph', $$
	unwind [public.create_random_nodes()] as node
	create (v:Visit {Id: node.Id, Value: node.Value, Date: node.Date})
$$) as (a agtype);

create index if not exists graph_visit_properties_index on graph."Visit" using gin (properties);
create index if not exists graph_visit_id_index on graph."Visit" using btree (id);

What is the command that caused the error? 1.

select * from cypher('graph', $$
	explain analyze
	MATCH (v:Visit)
	where v.Id = "2f10d37f58b51ffd47777bd988194be7"
	RETURN v
$$) as (a agtype);
Gather  (cost=1000.00..27085.75 rows=5000 width=32) (actual time=0.289..4360.647 rows=1 loops=1)
  Workers Planned: 2
  Workers Launched: 2
  ->  Parallel Seq Scan on "Visit" v  (cost=0.00..25585.75 rows=2083 width=32) (actual time=2849.575..4299.827 rows=0 loops=3)
        Filter: (agtype_access_operator(VARIADIC ARRAY[_agtype_build_vertex(id, _label_name('97847'::oid, id), properties), '"Id"'::agtype]) = '"2f10d37f58b51ffd47777bd988194be7"'::agtype)
        Rows Removed by Filter: 333333
Planning Time: 0.101 ms
Execution Time: 4360.860 ms
select * from cypher('graph', $$
	explain analyze
	MATCH (v:Visit)
	where id(v) = 9570149208162305
	RETURN v
$$) as (a agtype);
Gather  (cost=1000.00..27085.75 rows=5000 width=32) (actual time=0.373..2663.148 rows=1 loops=1)
  Workers Planned: 2
  Workers Launched: 2
  ->  Parallel Seq Scan on "Visit" v  (cost=0.00..25585.75 rows=2083 width=32) (actual time=1738.598..2625.160 rows=0 loops=3)
        Filter: (age_id(_agtype_build_vertex(id, _label_name('97847'::oid, id), properties)) = '9570149208162305'::agtype)
        Rows Removed by Filter: 333333
Planning Time: 0.159 ms
Execution Time: 2663.197 ms

Expected behavior I expect the behaviour of the DBMS to be the same as the following query:

select * from cypher('graph', $$
	explain analyze
	MATCH (v:Visit {Id: "2f10d37f58b51ffd47777bd988194be7"})
	RETURN v
$$) as (a agtype);
Bitmap Heap Scan on "Visit" v  (cost=43.75..3259.40 rows=1000 width=32) (actual time=0.060..0.061 rows=1 loops=1)
  Recheck Cond: (properties @> agtype_build_map('Id'::text, '"2f10d37f58b51ffd47777bd988194be7"'::agtype))
  Heap Blocks: exact=1
  ->  Bitmap Index Scan on graph_visit_properties_index  (cost=0.00..43.50 rows=1000 width=0) (actual time=0.047..0.047 rows=1 loops=1)
        Index Cond: (properties @> agtype_build_map('Id'::text, '"2f10d37f58b51ffd47777bd988194be7"'::agtype))
Planning Time: 24.067 ms
Execution Time: 0.087 ms

Environment:

  • Docker image: apache/age:v1.1.0

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

@endriux @vladiksun I’ve looked into this and found a few things worth pointing out. I will mention that I am no expert on indexes. What follows is from my understanding of the code underneath and how these commands are processed. And yes, as I learn and debug more of it, that can change.

It is also worth pointing out that Cypher is its own language and some things are not directly translatable to SQL and vice versa. Just because something is possible or done a particular way in SQL doesn’t necessarily translate to it being possible or done that way in Cypher. Just stating the obvious, just in case.

In the following MATCH command the two pattern matches are not the same. One is part of the MATCH clause, the other is part of the WHERE clause. The MATCH selects stuff based on its criteria and then passes it to the WHERE which then filters out stuff based on its criteria.

MATCH (u: {key: value}) WHERE u.key = value RETURN u

When the indexes are applied, they are tested against the clauses, one at a time, from my understanding. When matching an index to a specific clause, the components need to match. If you look at the MATCH above, while the pattern matches are similar, they are not the same. One is done in the MATCH, the other in the WHERE. MATCH works on a path, WHERE works on an expression. An index for one won’t work for the other.

Comparing the two MATCH commands that you listed, shown below, they also are not the same. One WHERE is for the id function on v, the other is for the access operator function on v. You can see this in the EXPLAIN outputs later on.

MATCH (v:Visit) where id(v) = 9570149208162305 RETURN v
MATCH (v:Visit) where v.Id = "2f10d37f58b51ffd47777bd988194be7" RETURN v

What this means is that you would need to create an index for each of these WHERE clauses, which is now possible to do.

psql-15.4-5432-pgsql=# CREATE INDEX btree_check_id_func ON graph."Visit" USING BTREE (age_id(_agtype_build_vertex(id, _label_name('16955'::oid, id), properties)));
CREATE INDEX
psql-15.4-5432-pgsql=#
psql-15.4-5432-pgsql=# CREATE INDEX btree_check_id ON graph."Visit" USING BTREE (agtype_access_operator(VARIADIC ARRAY[_agtype_build_vertex(id, _label_name('16955'::oid, id), properties), '"Id"'::agtype]));
CREATE INDEX
psql-15.4-5432-pgsql=# 

Now both of your queries will run using those indexes.

psql-15.4-5432-pgsql=# select * from cypher('graph', $$ explain analyze MATCH (v:Visit) where id(v) = 9570149208162305 RETURN v $$) as (a agtype);
                                                           QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on "Visit" v  (cost=139.18..11138.88 rows=5000 width=32) (actual time=0.078..0.080 rows=0 loops=1)
   Recheck Cond: (age_id(_agtype_build_vertex(id, _label_name('16955'::oid, id), properties)) = '9570149208162305'::agtype)
   ->  Bitmap Index Scan on btree_check_id_func  (cost=0.00..137.93 rows=5000 width=0) (actual time=0.073..0.074 rows=0 loops=1)
         Index Cond: (age_id(_agtype_build_vertex(id, _label_name('16955'::oid, id), properties)) = '9570149208162305'::agtype)
 Planning Time: 0.575 ms
 Execution Time: 0.196 ms
(6 rows)

psql-15.4-5432-pgsql=# select * from cypher('graph', $$ explain analyze MATCH (v:Visit) where v.Id = "2f10d37f58b51ffd47777bd988194be7" RETURN v $$) as (a agtype);
                                                                                            QUERY PLAN

------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------
 Bitmap Heap Scan on "Visit" v  (cost=207.18..11206.88 rows=5000 width=32) (actual time=0.129..0.131 rows=0 loops=1)
   Recheck Cond: (agtype_access_operator(VARIADIC ARRAY[_agtype_build_vertex(id, _label_name('16955'::oid, id), properties), '"Id"':
:agtype]) = '"2f10d37f58b51ffd47777bd988194be7"'::agtype)
   ->  Bitmap Index Scan on btree_check_id  (cost=0.00..205.93 rows=5000 width=0) (actual time=0.123..0.124 rows=0 loops=1)
         Index Cond: (agtype_access_operator(VARIADIC ARRAY[_agtype_build_vertex(id, _label_name('16955'::oid, id), properties), '"I
d"'::agtype]) = '"2f10d37f58b51ffd47777bd988194be7"'::agtype)
 Planning Time: 0.527 ms
 Execution Time: 0.256 ms
(6 rows)

psql-15.4-5432-pgsql=#

Hopefully, this is helps to clarify the indexes a bit. Again, this is based on debugging the index matching code.

As I understand this issue might me already partly fixed in https://github.com/apache/age/issues/1000. But as described in the https://github.com/apache/age/issues/1000 we extracted a functional call from a query plan and created a functional index. As I understand GIN indexes are structural indexes hence they kick out in the MATCH clause only.