Data Model

AgensGraph is a multi-model database. AgensGraph simultaneously supports both the property graph model and the relational model.

Property Graph Model

Labeled Property Graph Model

The property graph model contains connected entities, which can have any number of attributes. In AgensGraph, an entity is known as a vertex. Vertices can have an arbitrary number of attributes and can be categorized with labels. Labels are used to group vertices in order to represent some categories of vertices; i.e. representing the role of a person.

Edges are directed connections between two vertices. Edges can also have attributes and categorized labels like vertices. In AgensGraph, an edge always has a start vertex and an end vertex. If a query tries to delete a vertex, it must delete all its edges first. Broken edges cannot exist in AgensGraph.

Properties of edges and vertices are represented in the JSON format. JSON is a text format for the serialization of semi-structured data. JSONs are comprised of six data types: strings, numbers, booleans, null, objects and arrays. AgensGraph objects take full advantage of the JSON format by storing information as unordered collections of zero or more name/value pairs. A name is a string and a value can be any aforementioned type, including nested JSON types. AgensGraph specifically uses the JSONB format. Since JSONB is a decomposed binary format, it is processed much faster than regular JSON, but at the cost of a slightly slower input time.

Several graphs can be created in a database. In order to specify which graph is to be used, the session parameter graph_path is used.

To show the current graph path, use the following command.

SHOW graph_path;        

When a graph is created using CREATE GRAPH, graph_path is set to the created graph if graph_path is not set. You can create multiple graphs and change graph_path to another graph using the following command:

SET graph_path = graphname;        

The graph_path is a session variable, so every client must set the graph_path before querying the graph. Only one graph name can be specified for graph_path. Querying over multiple graphs is not allowed.

If you set the graph_path for each user or database using the ALTER ROLE or DATABASE statement, you do not need to run the SET graph_path statement whenever you connect the database.

ALTER ROLE user1 IN DATABASE gdb SET graph_path TO graphname;                         
ALTER DATABASE gdb SET graph_path TO graphname;                         

DROP

DROP GRAPH graphname CASCADE;        

A graph has initial labels for vertices and edges. These labels cannot be deleted. To drop the graph, users must do so with the CASCADE option. If current graph_path is the deleted graph, then graph_path is reset to null.

Labels

CREATE

CREATE VLABEL person;
CREATE VLABEL friend inherits (person);

CREATE ELABEL knows;
CREATE ELABEL live_together;
CREATE ELABEL room_mate inherits (knows, live_together);

The keywords VLABEL and ELABEL are used for identifying vertices and edges respectively. CREATE VLABEL will create a vertex label. VLABEL can inherit VLABEL only (in other words, VLABEL cannot inherit ELABEL). inherits is an option to inherit a parent label. If not specified, the system sets the initial label as a parent label. Multiple inheritance is possible for creating complex labels.

DROP

DROP VLABEL friend;
DROP VLABEL person;

VLABEL friend inherits person, so VLABEL person cannot be dropped directly. VLABEL friend should be dropped first.

If a label is bound to any vertex or edge, you cannot delete this label by using DROP VLABEL or DROP ELABEL.

DROP VLABEL person;
ERROR:  cannot drop person because it is not empty.

You can use DROP ... CASCADE to delete a label regardless of whether it is bound to or not, and in this case, all data object that has this label is deleted along with it.

DROP VLABEL person CASCADE;
NOTICE:  drop cascades to vlabel friend
DROP VLABEL

DROP ELABEL knows CASCADE;

Detailed Description

GRAPH

CREATE GRAPH

CREATE GRAPH [ IF NOT EXISTS ] graph_name [AUTHORIZATION role_name];        
  • IF NOT EXISTS

    • Do nothing if the same name already exists

  • AUTHORIZATION role_name

    • The role name of the user who will own the new graph

ALTER GRAPH

ALTER GRAPH graph_name RENAME TO new_name;                 
ALTER GRAPH graph_name OWNER TO { new_owner | CURRENT_USER | SESSION_USER };        
  • graph_name

    • The name of an existing graph.

  • RENAME TO new_name

    • This form changes the name of a graph to new_name.

  • OWNER TO new_owner

    • This form changes the owner of the graph.

LABEL

CREATE LABEL

The synopsis of both VLABEL and ELABEL is identical.

CREATE [ UNLOGGED ] VLABEL [ IF NOT EXISTS ] label_name [DISABLE INDEX]                       
       [ INHERITS ( parent_label_name [, ...] ) ]                                          
       [ WITH (storage_parameter)]                             
       [ TABLESPACE tablespace_name ];                                               
  • UNLOGGED

    • Data written to an unlogged label is not recorded to the write-ahead log, which makes unlogged labels considerably faster than logged labels. However, unlogged labels are not crash-safe.

  • IF NOT EXISTS

    • Do nothing if the same name already exists.

  • label_name

    • The name of the vertex/edge label to be created.

  • DISABLE INDEX

    • Create label with invalid index. The invalid indexes can not be used for searching or inserting until reindexed.

  • INHERITS ( parent_label [, …] )

    • The optional INHERITS clause specifies a list of vertex/edge labels. If it is empty, the new label inherits the initial label. Use of INHERITS creates a persistent edge between the new child label and its parent label(s). The data of the child label is included in scans of the parent(s) by default.

  • TABLESPACE tablespace_name

    • The new label will be created in the tablespace whose name is tablespace_name.

ALTER LABEL

ALTER [ IF EXISTS ] VLABEL label_name RENAME TO new_name;        
ALTER [ IF EXISTS ] VLABEL label_name OWNER TO new_owner;        
ALTER [ IF EXISTS ] VLABEL label_name SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN };        
ALTER [ IF EXISTS ] VLABEL label_name SET TABLESPACE new_tablespace;        
ALTER [ IF EXISTS ] VLABEL label_name CLUSTER ON idxname;        
ALTER [ IF EXISTS ] VLABEL label_name SET WITHOUT CLUSTER;        
ALTER [ IF EXISTS ] VLABEL label_name SET LOGGED;        
ALTER [ IF EXISTS ] VLABEL label_name SET UNLOGGED;        
ALTER [ IF EXISTS ] VLABEL label_name INHERIT parent_label;        
ALTER [ IF EXISTS ] VLABEL label_name NO INHERIT parent_label;        
ALTER [ IF EXISTS ] VLABEL label_name DISABLE INDEX;        
  • IF EXISTS

    • Do not throw an error if the label does not exist.

  • label_name

    • The name of an existing vertex/edge label.

  • RENAME TO new_name

    • Changes the name of a label to new_name.

  • OWNER TO new_owner

    • Changes the owner of the label.

  • SET STORAGE

    • This form sets the storage mode for property. This controls whether property is held inline or in a secondary TOAST table, and whether the data should be compressed or not.

    • PLAIN must be used for fixed-length, inline and uncompressed values.

    • MAIN is inline and compressed.

    • EXTERNAL is external and uncompressed.

    • EXTENDED is external and compressed.

  • SET TABLESPACE

    • This form changes the label’s tablespace to the specified tablespace and moves the data files to the new tablespace. Indexes on the label are not moved.

  • CLUSTER/WITHOUT CLUSTER

    • This form select/remove the default index for future cluster operation. See this.

  • SET LOGGED/UNLOGGED

    • This form changes the label from unlogged to logged or vice-versa.

  • INHERIT/NO INHERIT

    • This form adds/removes the target label to/from the parent label’s children list.

  • DISABLE INDEX

    • This form changes all indexes of the label to invalid index. The invalid indexes can not be used for searching or inserting until reindexed.

Property Index

The property index provides a method to build indexes on the property values. Users can also create an index for an expression.

CREATE INDEX

CREATE [UNIQUE] PROPERTY INDEX [CONCURRENTLY] [IF NOT EXIST] indexname      
ON labelname [USING method]         
( attribute_expr | (expr) [COLLATE collation] [opclass] [ASC | DESC]         
[NULLS {FIRST | LAST}]  )        
[WITH] (storage_parameter = value [,...])         
[TABLESPACE tablespacename]         
[WHERE predicate];                                 
  • UNIQUE

    • Causes the system to check for duplicate values in the table when the index is created if the data already exists and each time data is added. Attempts to insert or update data which would result in duplicate entries will generate an error.

  • CONCURRENTLY

    • With this option, the index will be built without taking any locks that prevent concurrent CREATE, DELETE(DETACH), SET on the label. There are several caveats to be aware of when using this option. See this.

  • IF NOT EXIST

    • Do nothing if the same name already exists

  • indexname

    • The name of the index to be created. If it is omitted, AgensGraph chooses an adequate name.

  • labelname

    • The name of the label to be indexed.

  • USING METHOD

    • The name of the index method to be used. They are btree, hash, gist, spgist, gin and brin. The default method is btree.

  • attribute_expr

    • An expression which is pointing the attribute of property.

  • COLLATE collation

    • The name of the collation to use for the index. By default, the index uses the collation declared for the column to be indexed or the result collation of the expression to be indexed. Indexes with non-default collations can be useful for queries that involve expressions using non-default collations.

  • opclass

    • The name of an operator class.

  • ASC | DESC

    • Specified ascending/descending sort order. ASC is the default.

  • NULLS { FIRST | LAST }

    • Specifies that nulls sort before/after non-nulls. LAST is default when ASC is specified.

  • WITH storage_parameter

  • TABLESPACE tablespacename

    • The tablespace in which to create the index.

  • WHERE predicate

    • The constraint expression for a partial index.

DROP INDEX

DROP PROPERTY INDEX indexname [CASCADE | RESTRICT];        
  • indexname

    • The indexname can be checked with ag_property_indexes or \dGi.

  • CASCADE

    • Automatically drop objects that depend on the graph.

  • RESTRICT

    • Refuse to drop the index if any objects depend on it. This is the default.

Constraints

This section provides information about how to use constraints on properties. Users can create two types of constraints; UNIQUE constraint and CHECK constraint.

CREATE CONSTRAINT constraint_name ON label_name ASSERT field_expr IS UNIQUE;        
CREATE CONSTRAINT constraint_name ON label_name ASSERT check_expr;        
DROP CONSTRAINT constraint_name ON label_name;        
  • field_expr

    • This form represents a JSON expression. This JSON can be a nested JSON.

  • UNIQUE

    • This form specified that json_expression can contain only unique values.

  • check_expr

    • The check expression returns a boolean result for new or updated properties. The result must be TRUE or UNKNOWN for an insert or update to succeed. Should any property of an insert or update operation produce a FALSE result, an error exception is raised and the insert or update will rolled back.