I wanted to create a materialized view from a label table and then create indexes on it. However, when I type the query to create this view, postgres pops an error.
Here is the query that I type to return all the vertices containing the "Book" label :
demo=# SELECT * FROM cypher ('demo', $$
demo$# MATCH (v:Book)
demo$# RETURN v
demo$# $$) as (vertex agtype);
vertex
---------------------------------------------------------------------------------------------------------------------------------------
{"id": 1125899906842625, "label": "Book", "properties": {"title": "The Hobbit"}}::vertex
{"id": 1125899906842626, "label": "Book", "properties": {"title": "SPQR: A History of Ancient Rome", "author": "Mary Beard"}}::vertex
(2 rows)
Here is the way that I’m creating the materialized view :
demo=# CREATE MATERIALIZED VIEW book_view AS SELECT * FROM cypher ('demo', $$
MATCH (v:Book)
RETURN v.author, v.title
$$) as (author agtype, title agtype);
ERROR: unhandled cypher(cstring) function call
DETAIL: demo
2
Answers
You can try a workaround by first creating a temporary table and populating it with the results of your Cypher query. Then, create a materialized view using the data from the temporary table. Use the following steps:
Unfortunately, PostgreSQL doesn’t support creating materialized views directly from the Cypher queries in this manner.
As Humza mentioned, you can achieve a similar result by creating a temporary table and inserting the data from the Cypher query into the table. Then, you can create indexes on the columns of that table. Here’s how you can do it:
1- Create a table to store the data:
2- Insert the data from the Cypher query into the table:
3- Create indexes on the columns of the table:
Now, you have a table called book_view that contains the data from your Cypher query, and indexes have been created on the author and title columns. You can query this table like any other PostgreSQL table.
IMPORTANT!! Keep in mind that the table will not be automatically updated when the graph data changes. You will need to update the table manually or create triggers to keep it in sync with the graph data.