LCOV - code coverage report
Current view: top level - src/backend/commands - propgraphcmds.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 94.7 % 702 665
Test Date: 2026-07-03 19:57:34 Functions: 100.0 % 22 22
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 76.8 % 482 370

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * propgraphcmds.c
       4                 :             :  *    property graph manipulation
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :             :  *
       9                 :             :  * src/backend/commands/propgraphcmds.c
      10                 :             :  *
      11                 :             :  *-------------------------------------------------------------------------
      12                 :             :  */
      13                 :             : #include "postgres.h"
      14                 :             : 
      15                 :             : #include "access/genam.h"
      16                 :             : #include "access/htup_details.h"
      17                 :             : #include "access/nbtree.h"
      18                 :             : #include "access/table.h"
      19                 :             : #include "access/xact.h"
      20                 :             : #include "catalog/catalog.h"
      21                 :             : #include "catalog/indexing.h"
      22                 :             : #include "catalog/namespace.h"
      23                 :             : #include "catalog/pg_class.h"
      24                 :             : #include "catalog/pg_collation_d.h"
      25                 :             : #include "catalog/pg_operator_d.h"
      26                 :             : #include "catalog/pg_propgraph_element.h"
      27                 :             : #include "catalog/pg_propgraph_element_label.h"
      28                 :             : #include "catalog/pg_propgraph_label.h"
      29                 :             : #include "catalog/pg_propgraph_label_property.h"
      30                 :             : #include "catalog/pg_propgraph_property.h"
      31                 :             : #include "commands/defrem.h"
      32                 :             : #include "commands/propgraphcmds.h"
      33                 :             : #include "commands/tablecmds.h"
      34                 :             : #include "nodes/nodeFuncs.h"
      35                 :             : #include "parser/parse_coerce.h"
      36                 :             : #include "parser/parse_collate.h"
      37                 :             : #include "parser/parse_oper.h"
      38                 :             : #include "parser/parse_relation.h"
      39                 :             : #include "parser/parse_target.h"
      40                 :             : #include "utils/array.h"
      41                 :             : #include "utils/builtins.h"
      42                 :             : #include "utils/fmgroids.h"
      43                 :             : #include "utils/inval.h"
      44                 :             : #include "utils/lsyscache.h"
      45                 :             : #include "utils/rel.h"
      46                 :             : #include "utils/ruleutils.h"
      47                 :             : #include "utils/syscache.h"
      48                 :             : 
      49                 :             : 
      50                 :             : struct element_info
      51                 :             : {
      52                 :             :     Oid         elementid;
      53                 :             :     char        kind;
      54                 :             :     Oid         relid;
      55                 :             :     char       *aliasname;
      56                 :             :     ArrayType  *key;
      57                 :             : 
      58                 :             :     char       *srcvertex;
      59                 :             :     Oid         srcvertexid;
      60                 :             :     Oid         srcrelid;
      61                 :             :     ArrayType  *srckey;
      62                 :             :     ArrayType  *srcref;
      63                 :             :     ArrayType  *srceqop;
      64                 :             : 
      65                 :             :     char       *destvertex;
      66                 :             :     Oid         destvertexid;
      67                 :             :     Oid         destrelid;
      68                 :             :     ArrayType  *destkey;
      69                 :             :     ArrayType  *destref;
      70                 :             :     ArrayType  *desteqop;
      71                 :             : 
      72                 :             :     List       *labels;
      73                 :             : };
      74                 :             : 
      75                 :             : 
      76                 :             : static ArrayType *propgraph_element_get_key(ParseState *pstate, const List *key_clause, Relation element_rel,
      77                 :             :                                             const char *aliasname, int location);
      78                 :             : static void propgraph_edge_get_ref_keys(ParseState *pstate, const List *keycols, const List *refcols,
      79                 :             :                                         Relation edge_rel, Relation ref_rel,
      80                 :             :                                         const char *aliasname, int location, const char *type,
      81                 :             :                                         ArrayType **outkey, ArrayType **outref, ArrayType **outeqop);
      82                 :             : static AttrNumber *array_from_column_list(ParseState *pstate, const List *colnames, int location, Relation element_rel);
      83                 :             : static ArrayType *array_from_attnums(int numattrs, const AttrNumber *attnums);
      84                 :             : static Oid  insert_element_record(ObjectAddress pgaddress, struct element_info *einfo);
      85                 :             : static Oid  insert_label_record(Oid graphid, Oid peoid, const char *label);
      86                 :             : static void insert_property_records(Oid graphid, Oid ellabeloid, Oid pgerelid, const PropGraphProperties *properties);
      87                 :             : static void insert_property_record(Oid graphid, Oid ellabeloid, Oid pgerelid, const char *propname, const Expr *expr);
      88                 :             : static void check_element_properties(Oid peoid);
      89                 :             : static void check_element_label_properties(Oid ellabeloid);
      90                 :             : static void check_all_labels_properties(Oid pgrelid);
      91                 :             : static Oid  get_vertex_oid(ParseState *pstate, Oid pgrelid, const char *alias, int location);
      92                 :             : static Oid  get_edge_oid(ParseState *pstate, Oid pgrelid, const char *alias, int location);
      93                 :             : static Oid  get_element_relid(Oid peid);
      94                 :             : static List *get_graph_label_ids(Oid graphid);
      95                 :             : static List *get_label_element_label_ids(Oid labelid);
      96                 :             : static List *get_element_label_property_names(Oid ellabeloid);
      97                 :             : static List *get_graph_property_ids(Oid graphid);
      98                 :             : 
      99                 :             : 
     100                 :             : /*
     101                 :             :  * CREATE PROPERTY GRAPH
     102                 :             :  */
     103                 :             : ObjectAddress
     104                 :         205 : CreatePropGraph(ParseState *pstate, const CreatePropGraphStmt *stmt)
     105                 :             : {
     106                 :         205 :     CreateStmt *cstmt = makeNode(CreateStmt);
     107                 :             :     char        components_persistence;
     108                 :             :     ListCell   *lc;
     109                 :             :     ObjectAddress pgaddress;
     110                 :         205 :     List       *vertex_infos = NIL;
     111                 :         205 :     List       *edge_infos = NIL;
     112                 :         205 :     List       *element_aliases = NIL;
     113                 :         205 :     List       *element_oids = NIL;
     114                 :             : 
     115         [ +  + ]:         205 :     if (stmt->pgname->relpersistence == RELPERSISTENCE_UNLOGGED)
     116         [ +  - ]:           4 :         ereport(ERROR,
     117                 :             :                 (errcode(ERRCODE_SYNTAX_ERROR),
     118                 :             :                  errmsg("property graphs cannot be unlogged because they do not have storage")));
     119                 :             : 
     120                 :         201 :     components_persistence = RELPERSISTENCE_PERMANENT;
     121                 :             : 
     122   [ +  +  +  +  :         551 :     foreach(lc, stmt->vertex_tables)
                   +  + ]
     123                 :             :     {
     124                 :         358 :         PropGraphVertex *vertex = lfirst_node(PropGraphVertex, lc);
     125                 :             :         struct element_info *vinfo;
     126                 :             :         Relation    rel;
     127                 :             : 
     128                 :         358 :         vinfo = palloc0_object(struct element_info);
     129                 :         358 :         vinfo->kind = PGEKIND_VERTEX;
     130                 :             : 
     131                 :         358 :         vinfo->relid = RangeVarGetRelidExtended(vertex->vtable, AccessShareLock, 0, RangeVarCallbackOwnsRelation, NULL);
     132                 :             : 
     133                 :         354 :         rel = table_open(vinfo->relid, NoLock);
     134                 :             : 
     135         [ +  + ]:         354 :         if (rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
     136                 :           8 :             components_persistence = RELPERSISTENCE_TEMP;
     137                 :             : 
     138         [ +  + ]:         354 :         if (vertex->vtable->alias)
     139                 :           8 :             vinfo->aliasname = vertex->vtable->alias->aliasname;
     140                 :             :         else
     141                 :         346 :             vinfo->aliasname = vertex->vtable->relname;
     142                 :             : 
     143         [ +  + ]:         354 :         if (list_member(element_aliases, makeString(vinfo->aliasname)))
     144         [ +  - ]:           4 :             ereport(ERROR,
     145                 :             :                     (errcode(ERRCODE_DUPLICATE_TABLE),
     146                 :             :                      errmsg("alias \"%s\" used more than once as element table", vinfo->aliasname),
     147                 :             :                      parser_errposition(pstate, vertex->location)));
     148                 :             : 
     149                 :         350 :         vinfo->key = propgraph_element_get_key(pstate, vertex->vkey, rel, vinfo->aliasname, vertex->location);
     150                 :             : 
     151                 :         350 :         vinfo->labels = vertex->labels;
     152                 :             : 
     153                 :         350 :         table_close(rel, NoLock);
     154                 :             : 
     155                 :         350 :         vertex_infos = lappend(vertex_infos, vinfo);
     156                 :             : 
     157                 :         350 :         element_aliases = lappend(element_aliases, makeString(vinfo->aliasname));
     158                 :             :     }
     159                 :             : 
     160   [ +  +  +  +  :         354 :     foreach(lc, stmt->edge_tables)
                   +  + ]
     161                 :             :     {
     162                 :         181 :         PropGraphEdge *edge = lfirst_node(PropGraphEdge, lc);
     163                 :             :         struct element_info *einfo;
     164                 :             :         Relation    rel;
     165                 :             :         ListCell   *lc2;
     166                 :             :         Oid         srcrelid;
     167                 :             :         Oid         destrelid;
     168                 :             :         Relation    srcrel;
     169                 :             :         Relation    destrel;
     170                 :             : 
     171                 :         181 :         einfo = palloc0_object(struct element_info);
     172                 :         181 :         einfo->kind = PGEKIND_EDGE;
     173                 :             : 
     174                 :         181 :         einfo->relid = RangeVarGetRelidExtended(edge->etable, AccessShareLock, 0, RangeVarCallbackOwnsRelation, NULL);
     175                 :             : 
     176                 :         181 :         rel = table_open(einfo->relid, NoLock);
     177                 :             : 
     178         [ -  + ]:         181 :         if (rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
     179                 :           0 :             components_persistence = RELPERSISTENCE_TEMP;
     180                 :             : 
     181         [ -  + ]:         181 :         if (edge->etable->alias)
     182                 :           0 :             einfo->aliasname = edge->etable->alias->aliasname;
     183                 :             :         else
     184                 :         181 :             einfo->aliasname = edge->etable->relname;
     185                 :             : 
     186         [ -  + ]:         181 :         if (list_member(element_aliases, makeString(einfo->aliasname)))
     187         [ #  # ]:           0 :             ereport(ERROR,
     188                 :             :                     (errcode(ERRCODE_DUPLICATE_TABLE),
     189                 :             :                      errmsg("alias \"%s\" used more than once as element table", einfo->aliasname),
     190                 :             :                      parser_errposition(pstate, edge->location)));
     191                 :             : 
     192                 :         181 :         einfo->key = propgraph_element_get_key(pstate, edge->ekey, rel, einfo->aliasname, edge->location);
     193                 :             : 
     194                 :         181 :         einfo->srcvertex = edge->esrcvertex;
     195                 :         181 :         einfo->destvertex = edge->edestvertex;
     196                 :             : 
     197                 :         181 :         srcrelid = 0;
     198                 :         181 :         destrelid = 0;
     199   [ +  -  +  +  :         431 :         foreach(lc2, vertex_infos)
                   +  + ]
     200                 :             :         {
     201                 :         423 :             struct element_info *vinfo = lfirst(lc2);
     202                 :             : 
     203         [ +  + ]:         423 :             if (strcmp(vinfo->aliasname, edge->esrcvertex) == 0)
     204                 :         177 :                 srcrelid = vinfo->relid;
     205                 :             : 
     206         [ +  + ]:         423 :             if (strcmp(vinfo->aliasname, edge->edestvertex) == 0)
     207                 :         177 :                 destrelid = vinfo->relid;
     208                 :             : 
     209   [ +  +  +  + ]:         423 :             if (srcrelid && destrelid)
     210                 :         173 :                 break;
     211                 :             :         }
     212         [ +  + ]:         181 :         if (!srcrelid)
     213         [ +  - ]:           4 :             ereport(ERROR,
     214                 :             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     215                 :             :                      errmsg("source vertex \"%s\" of edge \"%s\" does not exist",
     216                 :             :                             edge->esrcvertex, einfo->aliasname),
     217                 :             :                      parser_errposition(pstate, edge->location)));
     218         [ +  + ]:         177 :         if (!destrelid)
     219         [ +  - ]:           4 :             ereport(ERROR,
     220                 :             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     221                 :             :                      errmsg("destination vertex \"%s\" of edge \"%s\" does not exist",
     222                 :             :                             edge->edestvertex, einfo->aliasname),
     223                 :             :                      parser_errposition(pstate, edge->location)));
     224                 :             : 
     225                 :         173 :         srcrel = table_open(srcrelid, NoLock);
     226                 :         173 :         destrel = table_open(destrelid, NoLock);
     227                 :             : 
     228                 :         173 :         propgraph_edge_get_ref_keys(pstate, edge->esrckey, edge->esrcvertexcols, rel, srcrel,
     229                 :         173 :                                     einfo->aliasname, edge->location, "SOURCE",
     230                 :             :                                     &einfo->srckey, &einfo->srcref, &einfo->srceqop);
     231                 :         165 :         propgraph_edge_get_ref_keys(pstate, edge->edestkey, edge->edestvertexcols, rel, destrel,
     232                 :         165 :                                     einfo->aliasname, edge->location, "DESTINATION",
     233                 :             :                                     &einfo->destkey, &einfo->destref, &einfo->desteqop);
     234                 :             : 
     235                 :         161 :         einfo->labels = edge->labels;
     236                 :             : 
     237                 :         161 :         table_close(destrel, NoLock);
     238                 :         161 :         table_close(srcrel, NoLock);
     239                 :             : 
     240                 :         161 :         table_close(rel, NoLock);
     241                 :             : 
     242                 :         161 :         edge_infos = lappend(edge_infos, einfo);
     243                 :             : 
     244                 :         161 :         element_aliases = lappend(element_aliases, makeString(einfo->aliasname));
     245                 :             :     }
     246                 :             : 
     247                 :         173 :     cstmt->relation = stmt->pgname;
     248                 :         173 :     cstmt->oncommit = ONCOMMIT_NOOP;
     249                 :             : 
     250                 :             :     /*
     251                 :             :      * Automatically make it temporary if any component tables are temporary
     252                 :             :      * (see also DefineView()).
     253                 :             :      */
     254         [ +  + ]:         173 :     if (stmt->pgname->relpersistence == RELPERSISTENCE_PERMANENT
     255         [ +  + ]:         161 :         && components_persistence == RELPERSISTENCE_TEMP)
     256                 :             :     {
     257                 :           4 :         cstmt->relation = copyObject(cstmt->relation);
     258                 :           4 :         cstmt->relation->relpersistence = RELPERSISTENCE_TEMP;
     259         [ +  - ]:           4 :         ereport(NOTICE,
     260                 :             :                 (errmsg("property graph \"%s\" will be temporary",
     261                 :             :                         stmt->pgname->relname)));
     262                 :             :     }
     263                 :             : 
     264                 :         173 :     pgaddress = DefineRelation(cstmt, RELKIND_PROPGRAPH, InvalidOid, NULL, NULL);
     265                 :             : 
     266   [ +  +  +  +  :         455 :     foreach(lc, vertex_infos)
                   +  + ]
     267                 :             :     {
     268                 :         302 :         struct element_info *vinfo = lfirst(lc);
     269                 :             :         Oid         peoid;
     270                 :             : 
     271                 :         302 :         peoid = insert_element_record(pgaddress, vinfo);
     272                 :         286 :         element_oids = lappend_oid(element_oids, peoid);
     273                 :             :     }
     274                 :             : 
     275   [ +  +  +  +  :         310 :     foreach(lc, edge_infos)
                   +  + ]
     276                 :             :     {
     277                 :         161 :         struct element_info *einfo = lfirst(lc);
     278                 :             :         Oid         peoid;
     279                 :             :         ListCell   *lc2;
     280                 :             : 
     281                 :             :         /*
     282                 :             :          * Look up the vertices again.  Now the vertices have OIDs assigned,
     283                 :             :          * which we need.
     284                 :             :          */
     285   [ +  -  +  -  :         383 :         foreach(lc2, vertex_infos)
                   +  - ]
     286                 :             :         {
     287                 :         383 :             struct element_info *vinfo = lfirst(lc2);
     288                 :             : 
     289         [ +  + ]:         383 :             if (strcmp(vinfo->aliasname, einfo->srcvertex) == 0)
     290                 :             :             {
     291                 :         161 :                 einfo->srcvertexid = vinfo->elementid;
     292                 :         161 :                 einfo->srcrelid = vinfo->relid;
     293                 :             :             }
     294         [ +  + ]:         383 :             if (strcmp(vinfo->aliasname, einfo->destvertex) == 0)
     295                 :             :             {
     296                 :         161 :                 einfo->destvertexid = vinfo->elementid;
     297                 :         161 :                 einfo->destrelid = vinfo->relid;
     298                 :             :             }
     299   [ +  +  +  + ]:         383 :             if (einfo->srcvertexid && einfo->destvertexid)
     300                 :         161 :                 break;
     301                 :             :         }
     302                 :             :         Assert(einfo->srcvertexid);
     303                 :             :         Assert(einfo->destvertexid);
     304                 :             :         Assert(einfo->srcrelid);
     305                 :             :         Assert(einfo->destrelid);
     306                 :         161 :         peoid = insert_element_record(pgaddress, einfo);
     307                 :         157 :         element_oids = lappend_oid(element_oids, peoid);
     308                 :             :     }
     309                 :             : 
     310                 :         149 :     CommandCounterIncrement();
     311                 :             : 
     312   [ +  +  +  +  :         701 :     foreach_oid(peoid, element_oids)
                   +  + ]
     313                 :         411 :         check_element_properties(peoid);
     314                 :         145 :     check_all_labels_properties(pgaddress.objectId);
     315                 :             : 
     316                 :         133 :     return pgaddress;
     317                 :             : }
     318                 :             : 
     319                 :             : /*
     320                 :             :  * Process the key clause specified for an element.  If key_clause is non-NIL,
     321                 :             :  * then it is a list of column names.  Otherwise, the primary key of the
     322                 :             :  * relation is used.  The return value is an array of column numbers.
     323                 :             :  */
     324                 :             : static ArrayType *
     325                 :         595 : propgraph_element_get_key(ParseState *pstate, const List *key_clause, Relation element_rel, const char *aliasname, int location)
     326                 :             : {
     327                 :             :     ArrayType  *a;
     328                 :             : 
     329         [ +  + ]:         595 :     if (key_clause == NIL)
     330                 :             :     {
     331                 :         128 :         Oid         pkidx = RelationGetPrimaryKeyIndex(element_rel, false);
     332                 :             : 
     333         [ -  + ]:         128 :         if (!pkidx)
     334         [ #  # ]:           0 :             ereport(ERROR,
     335                 :             :                     errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     336                 :             :                     errmsg("no key specified and no suitable primary key exists for definition of element \"%s\"", aliasname),
     337                 :             :                     parser_errposition(pstate, location));
     338                 :             :         else
     339                 :             :         {
     340                 :             :             Relation    indexDesc;
     341                 :             : 
     342                 :         128 :             indexDesc = index_open(pkidx, AccessShareLock);
     343                 :         128 :             a = array_from_attnums(indexDesc->rd_index->indkey.dim1, indexDesc->rd_index->indkey.values);
     344                 :         128 :             index_close(indexDesc, NoLock);
     345                 :             :         }
     346                 :             :     }
     347                 :             :     else
     348                 :             :     {
     349                 :         467 :         a = array_from_attnums(list_length(key_clause),
     350                 :         467 :                                array_from_column_list(pstate, key_clause, location, element_rel));
     351                 :             :     }
     352                 :             : 
     353                 :         595 :     return a;
     354                 :             : }
     355                 :             : 
     356                 :             : /*
     357                 :             :  * Process the source or destination link of an edge.
     358                 :             :  *
     359                 :             :  * keycols and refcols are column names representing the local and referenced
     360                 :             :  * (vertex) columns.  If they are both NIL, a matching foreign key is looked
     361                 :             :  * up.
     362                 :             :  *
     363                 :             :  * edge_rel and ref_rel are the local and referenced element tables.
     364                 :             :  *
     365                 :             :  * aliasname, location, and type are for error messages.  type is either
     366                 :             :  * "SOURCE" or "DESTINATION".
     367                 :             :  *
     368                 :             :  * The outputs are arrays of column numbers in outkey and outref.
     369                 :             :  */
     370                 :             : static void
     371                 :         410 : propgraph_edge_get_ref_keys(ParseState *pstate, const List *keycols, const List *refcols,
     372                 :             :                             Relation edge_rel, Relation ref_rel,
     373                 :             :                             const char *aliasname, int location, const char *type,
     374                 :             :                             ArrayType **outkey, ArrayType **outref, ArrayType **outeqop)
     375                 :             : {
     376                 :             :     int         nkeys;
     377                 :             :     AttrNumber *keyattnums;
     378                 :             :     AttrNumber *refattnums;
     379                 :             :     Oid        *keyeqops;
     380                 :             :     Datum      *datums;
     381                 :             : 
     382                 :             :     Assert((keycols && refcols) || (!keycols && !refcols));
     383                 :             : 
     384         [ +  + ]:         410 :     if (keycols)
     385                 :             :     {
     386         [ -  + ]:         390 :         if (list_length(keycols) != list_length(refcols))
     387         [ #  # ]:           0 :             ereport(ERROR,
     388                 :             :                     errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     389                 :             :                     errmsg("mismatching number of columns in %s vertex definition of edge \"%s\"", type, aliasname),
     390                 :             :                     parser_errposition(pstate, location));
     391                 :             : 
     392                 :         390 :         nkeys = list_length(keycols);
     393                 :         390 :         keyattnums = array_from_column_list(pstate, keycols, location, edge_rel);
     394                 :         390 :         refattnums = array_from_column_list(pstate, refcols, location, ref_rel);
     395                 :         390 :         keyeqops = palloc_array(Oid, nkeys);
     396                 :             : 
     397         [ +  + ]:         820 :         for (int i = 0; i < nkeys; i++)
     398                 :             :         {
     399                 :             :             Oid         keytype;
     400                 :             :             int32       keytypmod;
     401                 :             :             Oid         keycoll;
     402                 :             :             Oid         reftype;
     403                 :             :             int32       reftypmod;
     404                 :             :             Oid         refcoll;
     405                 :             :             Oid         opc;
     406                 :             :             Oid         opf;
     407                 :             :             StrategyNumber strategy;
     408                 :             : 
     409                 :             :             /*
     410                 :             :              * Lookup equality operator to be used for edge and vertex key.
     411                 :             :              * Vertex key is equivalent to primary key and edge key is similar
     412                 :             :              * to foreign key since edge key references vertex key. Hence
     413                 :             :              * vertex key is used as left operand and edge key is used as
     414                 :             :              * right operand. The method used to find the equality operators
     415                 :             :              * is similar to the method used to find equality operators for
     416                 :             :              * FK/PK comparison in ATAddForeignKeyConstraint() except that
     417                 :             :              * opclass of the vertex key type is used as a starting point.
     418                 :             :              * Since we need only equality operators we use both BT and HASH
     419                 :             :              * strategies.
     420                 :             :              *
     421                 :             :              * If the required operators do not exist, we can not construct
     422                 :             :              * quals linking an edge to its adjacent vertices.
     423                 :             :              */
     424                 :         438 :             get_atttypetypmodcoll(RelationGetRelid(edge_rel), keyattnums[i], &keytype, &keytypmod, &keycoll);
     425                 :         438 :             get_atttypetypmodcoll(RelationGetRelid(ref_rel), refattnums[i], &reftype, &reftypmod, &refcoll);
     426                 :         438 :             keyeqops[i] = InvalidOid;
     427                 :         438 :             strategy = BTEqualStrategyNumber;
     428                 :         438 :             opc = GetDefaultOpClass(reftype, BTREE_AM_OID);
     429         [ -  + ]:         438 :             if (!OidIsValid(opc))
     430                 :             :             {
     431                 :           0 :                 opc = GetDefaultOpClass(reftype, HASH_AM_OID);
     432                 :           0 :                 strategy = HTEqualStrategyNumber;
     433                 :             :             }
     434         [ +  - ]:         438 :             if (OidIsValid(opc))
     435                 :             :             {
     436                 :         438 :                 opf = get_opclass_family(opc);
     437         [ +  - ]:         438 :                 if (OidIsValid(opf))
     438                 :             :                 {
     439                 :         438 :                     keyeqops[i] = get_opfamily_member(opf, reftype, keytype, strategy);
     440         [ +  + ]:         438 :                     if (!OidIsValid(keyeqops[i]))
     441                 :             :                     {
     442                 :             :                         /* Last resort, implicit cast. */
     443         [ -  + ]:           4 :                         if (can_coerce_type(1, &keytype, &reftype, COERCION_IMPLICIT))
     444                 :           0 :                             keyeqops[i] = get_opfamily_member(opf, reftype, reftype, strategy);
     445                 :             :                     }
     446                 :             :                 }
     447                 :             :             }
     448                 :             : 
     449         [ +  + ]:         438 :             if (!OidIsValid(keyeqops[i]))
     450         [ +  - ]:           4 :                 ereport(ERROR,
     451                 :             :                         errcode(ERRCODE_SYNTAX_ERROR),
     452                 :             :                         errmsg("no equality operator exists for %s key comparison of edge \"%s\"",
     453                 :             :                                type, aliasname),
     454                 :             :                         parser_errposition(pstate, location));
     455                 :             : 
     456                 :             :             /*
     457                 :             :              * If collations of key attribute and referenced attribute are
     458                 :             :              * different, an edge may end up being adjacent to undesired
     459                 :             :              * vertices.  Prohibit such a case.
     460                 :             :              *
     461                 :             :              * PK/FK allows different collations as long as they are
     462                 :             :              * deterministic for backward compatibility. But we can be a bit
     463                 :             :              * stricter here and follow SQL standard.
     464                 :             :              */
     465         [ +  + ]:         434 :             if (keycoll != refcoll &&
     466   [ +  -  +  - ]:           4 :                 keycoll != DEFAULT_COLLATION_OID && refcoll != DEFAULT_COLLATION_OID &&
     467   [ +  -  +  - ]:           4 :                 OidIsValid(keycoll) && OidIsValid(refcoll))
     468         [ +  - ]:           4 :                 ereport(ERROR,
     469                 :             :                         errcode(ERRCODE_SYNTAX_ERROR),
     470                 :             :                         errmsg("collation mismatch in %s key of edge \"%s\": %s vs. %s",
     471                 :             :                                type, aliasname,
     472                 :             :                                get_collation_name(keycoll), get_collation_name(refcoll)),
     473                 :             :                         parser_errposition(pstate, location));
     474                 :             :         }
     475                 :             :     }
     476                 :             :     else
     477                 :             :     {
     478                 :          20 :         ForeignKeyCacheInfo *fk = NULL;
     479                 :             : 
     480   [ +  +  +  +  :          72 :         foreach_node(ForeignKeyCacheInfo, tmp, RelationGetFKeyList(edge_rel))
                   +  + ]
     481                 :             :         {
     482         [ +  + ]:          32 :             if (tmp->confrelid == RelationGetRelid(ref_rel))
     483                 :             :             {
     484         [ -  + ]:          16 :                 if (fk)
     485         [ #  # ]:           0 :                     ereport(ERROR,
     486                 :             :                             errcode(ERRCODE_SYNTAX_ERROR),
     487                 :             :                             errmsg("more than one suitable foreign key exists for %s key of edge \"%s\"", type, aliasname),
     488                 :             :                             parser_errposition(pstate, location));
     489                 :          16 :                 fk = tmp;
     490                 :             :             }
     491                 :             :         }
     492                 :             : 
     493         [ +  + ]:          20 :         if (!fk)
     494         [ +  - ]:           4 :             ereport(ERROR,
     495                 :             :                     errcode(ERRCODE_SYNTAX_ERROR),
     496                 :             :                     errmsg("no %s key specified and no suitable foreign key exists for definition of edge \"%s\"", type, aliasname),
     497                 :             :                     parser_errposition(pstate, location));
     498                 :             : 
     499                 :          16 :         nkeys = fk->nkeys;
     500                 :          16 :         keyattnums = fk->conkey;
     501                 :          16 :         refattnums = fk->confkey;
     502                 :          16 :         keyeqops = fk->conpfeqop;
     503                 :             :     }
     504                 :             : 
     505                 :         398 :     *outkey = array_from_attnums(nkeys, keyattnums);
     506                 :         398 :     *outref = array_from_attnums(nkeys, refattnums);
     507                 :         398 :     datums = palloc_array(Datum, nkeys);
     508         [ +  + ]:         844 :     for (int i = 0; i < nkeys; i++)
     509                 :         446 :         datums[i] = ObjectIdGetDatum(keyeqops[i]);
     510                 :         398 :     *outeqop = construct_array_builtin(datums, nkeys, OIDOID);
     511                 :         398 : }
     512                 :             : 
     513                 :             : /*
     514                 :             :  * Convert list of column names in the specified relation into an array of
     515                 :             :  * column numbers.
     516                 :             :  */
     517                 :             : static AttrNumber *
     518                 :        1247 : array_from_column_list(ParseState *pstate, const List *colnames, int location, Relation element_rel)
     519                 :             : {
     520                 :             :     int         numattrs;
     521                 :             :     AttrNumber *attnums;
     522                 :             :     int         i;
     523                 :             :     ListCell   *lc;
     524                 :             : 
     525                 :        1247 :     numattrs = list_length(colnames);
     526                 :        1247 :     attnums = palloc_array(AttrNumber, numattrs);
     527                 :             : 
     528                 :        1247 :     i = 0;
     529   [ +  -  +  +  :        2751 :     foreach(lc, colnames)
                   +  + ]
     530                 :             :     {
     531                 :        1504 :         char       *colname = strVal(lfirst(lc));
     532                 :        1504 :         Oid         relid = RelationGetRelid(element_rel);
     533                 :             :         AttrNumber  attnum;
     534                 :             : 
     535                 :        1504 :         attnum = get_attnum(relid, colname);
     536         [ -  + ]:        1504 :         if (!attnum)
     537         [ #  # ]:           0 :             ereport(ERROR,
     538                 :             :                     (errcode(ERRCODE_UNDEFINED_COLUMN),
     539                 :             :                      errmsg("column \"%s\" of relation \"%s\" does not exist",
     540                 :             :                             colname, get_rel_name(relid)),
     541                 :             :                      parser_errposition(pstate, location)));
     542                 :        1504 :         attnums[i++] = attnum;
     543                 :             :     }
     544                 :             : 
     545         [ +  + ]:        2751 :     for (int j = 0; j < numattrs; j++)
     546                 :             :     {
     547         [ +  + ]:        1795 :         for (int k = j + 1; k < numattrs; k++)
     548                 :             :         {
     549         [ -  + ]:         291 :             if (attnums[j] == attnums[k])
     550         [ #  # ]:           0 :                 ereport(ERROR,
     551                 :             :                         (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     552                 :             :                          errmsg("graph key columns list must not contain duplicates"),
     553                 :             :                          parser_errposition(pstate, location)));
     554                 :             :         }
     555                 :             :     }
     556                 :             : 
     557                 :        1247 :     return attnums;
     558                 :             : }
     559                 :             : 
     560                 :             : static ArrayType *
     561                 :        1391 : array_from_attnums(int numattrs, const AttrNumber *attnums)
     562                 :             : {
     563                 :             :     Datum      *attnumsd;
     564                 :             : 
     565                 :        1391 :     attnumsd = palloc_array(Datum, numattrs);
     566                 :             : 
     567         [ +  + ]:        3071 :     for (int i = 0; i < numattrs; i++)
     568                 :        1680 :         attnumsd[i] = Int16GetDatum(attnums[i]);
     569                 :             : 
     570                 :        1391 :     return construct_array_builtin(attnumsd, numattrs, INT2OID);
     571                 :             : }
     572                 :             : 
     573                 :             : static void
     574                 :        1311 : array_of_attnums_to_objectaddrs(Oid relid, ArrayType *arr, ObjectAddresses *addrs)
     575                 :             : {
     576                 :             :     Datum      *attnumsd;
     577                 :             :     int         numattrs;
     578                 :             : 
     579                 :        1311 :     deconstruct_array_builtin(arr, INT2OID, &attnumsd, NULL, &numattrs);
     580                 :             : 
     581         [ +  + ]:        2891 :     for (int i = 0; i < numattrs; i++)
     582                 :             :     {
     583                 :             :         ObjectAddress referenced;
     584                 :             : 
     585                 :        1580 :         ObjectAddressSubSet(referenced, RelationRelationId, relid, DatumGetInt16(attnumsd[i]));
     586                 :        1580 :         add_exact_object_address(&referenced, addrs);
     587                 :             :     }
     588                 :        1311 : }
     589                 :             : 
     590                 :             : static void
     591                 :         394 : array_of_opers_to_objectaddrs(ArrayType *arr, ObjectAddresses *addrs)
     592                 :             : {
     593                 :             :     Datum      *opersd;
     594                 :             :     int         numopers;
     595                 :             : 
     596                 :         394 :     deconstruct_array_builtin(arr, OIDOID, &opersd, NULL, &numopers);
     597                 :             : 
     598         [ +  + ]:         836 :     for (int i = 0; i < numopers; i++)
     599                 :             :     {
     600                 :             :         ObjectAddress referenced;
     601                 :             : 
     602                 :         442 :         ObjectAddressSet(referenced, OperatorRelationId, DatumGetObjectId(opersd[i]));
     603                 :         442 :         add_exact_object_address(&referenced, addrs);
     604                 :             :     }
     605                 :         394 : }
     606                 :             : 
     607                 :             : /*
     608                 :             :  * Insert a record for an element into the pg_propgraph_element catalog.  Also
     609                 :             :  * inserts labels and properties into their respective catalogs.
     610                 :             :  */
     611                 :             : static Oid
     612                 :         523 : insert_element_record(ObjectAddress pgaddress, struct element_info *einfo)
     613                 :             : {
     614                 :         523 :     Oid         graphid = pgaddress.objectId;
     615                 :             :     Relation    rel;
     616                 :             :     NameData    aliasname;
     617                 :             :     Oid         peoid;
     618                 :         523 :     Datum       values[Natts_pg_propgraph_element] = {0};
     619                 :         523 :     bool        nulls[Natts_pg_propgraph_element] = {0};
     620                 :             :     HeapTuple   tup;
     621                 :             :     ObjectAddress myself;
     622                 :             :     ObjectAddress referenced;
     623                 :             :     ObjectAddresses *addrs;
     624                 :             : 
     625                 :         523 :     rel = table_open(PropgraphElementRelationId, RowExclusiveLock);
     626                 :             : 
     627                 :         523 :     peoid = GetNewOidWithIndex(rel, PropgraphElementObjectIndexId, Anum_pg_propgraph_element_oid);
     628                 :         523 :     einfo->elementid = peoid;
     629                 :         523 :     values[Anum_pg_propgraph_element_oid - 1] = ObjectIdGetDatum(peoid);
     630                 :         523 :     values[Anum_pg_propgraph_element_pgepgid - 1] = ObjectIdGetDatum(graphid);
     631                 :         523 :     values[Anum_pg_propgraph_element_pgerelid - 1] = ObjectIdGetDatum(einfo->relid);
     632                 :         523 :     namestrcpy(&aliasname, einfo->aliasname);
     633                 :         523 :     values[Anum_pg_propgraph_element_pgealias - 1] = NameGetDatum(&aliasname);
     634                 :         523 :     values[Anum_pg_propgraph_element_pgekind - 1] = CharGetDatum(einfo->kind);
     635                 :         523 :     values[Anum_pg_propgraph_element_pgesrcvertexid - 1] = ObjectIdGetDatum(einfo->srcvertexid);
     636                 :         523 :     values[Anum_pg_propgraph_element_pgedestvertexid - 1] = ObjectIdGetDatum(einfo->destvertexid);
     637                 :         523 :     values[Anum_pg_propgraph_element_pgekey - 1] = PointerGetDatum(einfo->key);
     638                 :             : 
     639         [ +  + ]:         523 :     if (einfo->srckey)
     640                 :         197 :         values[Anum_pg_propgraph_element_pgesrckey - 1] = PointerGetDatum(einfo->srckey);
     641                 :             :     else
     642                 :         326 :         nulls[Anum_pg_propgraph_element_pgesrckey - 1] = true;
     643         [ +  + ]:         523 :     if (einfo->srcref)
     644                 :         197 :         values[Anum_pg_propgraph_element_pgesrcref - 1] = PointerGetDatum(einfo->srcref);
     645                 :             :     else
     646                 :         326 :         nulls[Anum_pg_propgraph_element_pgesrcref - 1] = true;
     647         [ +  + ]:         523 :     if (einfo->srceqop)
     648                 :         197 :         values[Anum_pg_propgraph_element_pgesrceqop - 1] = PointerGetDatum(einfo->srceqop);
     649                 :             :     else
     650                 :         326 :         nulls[Anum_pg_propgraph_element_pgesrceqop - 1] = true;
     651         [ +  + ]:         523 :     if (einfo->destkey)
     652                 :         197 :         values[Anum_pg_propgraph_element_pgedestkey - 1] = PointerGetDatum(einfo->destkey);
     653                 :             :     else
     654                 :         326 :         nulls[Anum_pg_propgraph_element_pgedestkey - 1] = true;
     655         [ +  + ]:         523 :     if (einfo->destref)
     656                 :         197 :         values[Anum_pg_propgraph_element_pgedestref - 1] = PointerGetDatum(einfo->destref);
     657                 :             :     else
     658                 :         326 :         nulls[Anum_pg_propgraph_element_pgedestref - 1] = true;
     659         [ +  + ]:         523 :     if (einfo->desteqop)
     660                 :         197 :         values[Anum_pg_propgraph_element_pgedesteqop - 1] = PointerGetDatum(einfo->desteqop);
     661                 :             :     else
     662                 :         326 :         nulls[Anum_pg_propgraph_element_pgedesteqop - 1] = true;
     663                 :             : 
     664                 :         523 :     tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
     665                 :         523 :     CatalogTupleInsert(rel, tup);
     666                 :         523 :     heap_freetuple(tup);
     667                 :             : 
     668                 :         523 :     ObjectAddressSet(myself, PropgraphElementRelationId, peoid);
     669                 :             : 
     670                 :             :     /* Add dependency on the property graph */
     671                 :         523 :     recordDependencyOn(&myself, &pgaddress, DEPENDENCY_AUTO);
     672                 :             : 
     673                 :         523 :     addrs = new_object_addresses();
     674                 :             : 
     675                 :             :     /* Add dependency on the relation */
     676                 :         523 :     ObjectAddressSet(referenced, RelationRelationId, einfo->relid);
     677                 :         523 :     add_exact_object_address(&referenced, addrs);
     678                 :         523 :     array_of_attnums_to_objectaddrs(einfo->relid, einfo->key, addrs);
     679                 :             : 
     680                 :             :     /*
     681                 :             :      * Add dependencies on vertices and equality operators used for key
     682                 :             :      * comparison.
     683                 :             :      */
     684         [ +  + ]:         523 :     if (einfo->srcvertexid)
     685                 :             :     {
     686                 :         197 :         ObjectAddressSet(referenced, PropgraphElementRelationId, einfo->srcvertexid);
     687                 :         197 :         add_exact_object_address(&referenced, addrs);
     688                 :         197 :         array_of_attnums_to_objectaddrs(einfo->relid, einfo->srckey, addrs);
     689                 :         197 :         array_of_attnums_to_objectaddrs(einfo->srcrelid, einfo->srcref, addrs);
     690                 :         197 :         array_of_opers_to_objectaddrs(einfo->srceqop, addrs);
     691                 :             :     }
     692         [ +  + ]:         523 :     if (einfo->destvertexid)
     693                 :             :     {
     694                 :         197 :         ObjectAddressSet(referenced, PropgraphElementRelationId, einfo->destvertexid);
     695                 :         197 :         add_exact_object_address(&referenced, addrs);
     696                 :         197 :         array_of_attnums_to_objectaddrs(einfo->relid, einfo->destkey, addrs);
     697                 :         197 :         array_of_attnums_to_objectaddrs(einfo->destrelid, einfo->destref, addrs);
     698                 :         197 :         array_of_opers_to_objectaddrs(einfo->desteqop, addrs);
     699                 :             :     }
     700                 :             : 
     701                 :         523 :     record_object_address_dependencies(&myself, addrs, DEPENDENCY_NORMAL);
     702                 :             : 
     703                 :         523 :     table_close(rel, NoLock);
     704                 :             : 
     705         [ +  - ]:         523 :     if (einfo->labels)
     706                 :             :     {
     707                 :             :         ListCell   *lc;
     708                 :             : 
     709   [ +  -  +  +  :        1134 :         foreach(lc, einfo->labels)
                   +  + ]
     710                 :             :         {
     711                 :         639 :             PropGraphLabelAndProperties *lp = lfirst_node(PropGraphLabelAndProperties, lc);
     712                 :             :             Oid         ellabeloid;
     713                 :             : 
     714         [ +  + ]:         639 :             if (lp->label)
     715                 :         268 :                 ellabeloid = insert_label_record(graphid, peoid, lp->label);
     716                 :             :             else
     717                 :         371 :                 ellabeloid = insert_label_record(graphid, peoid, einfo->aliasname);
     718                 :         639 :             insert_property_records(graphid, ellabeloid, einfo->relid, lp->properties);
     719                 :             : 
     720                 :         611 :             CommandCounterIncrement();
     721                 :             :         }
     722                 :             :     }
     723                 :             :     else
     724                 :             :     {
     725                 :             :         Oid         ellabeloid;
     726                 :           0 :         PropGraphProperties *pr = makeNode(PropGraphProperties);
     727                 :             : 
     728                 :           0 :         pr->all = true;
     729                 :           0 :         pr->location = -1;
     730                 :             : 
     731                 :           0 :         ellabeloid = insert_label_record(graphid, peoid, einfo->aliasname);
     732                 :           0 :         insert_property_records(graphid, ellabeloid, einfo->relid, pr);
     733                 :             :     }
     734                 :             : 
     735                 :         495 :     return peoid;
     736                 :             : }
     737                 :             : 
     738                 :             : /*
     739                 :             :  * Insert records for a label into the pg_propgraph_label and
     740                 :             :  * pg_propgraph_element_label catalogs, and register dependencies.
     741                 :             :  *
     742                 :             :  * Returns the OID of the new pg_propgraph_element_label record.
     743                 :             :  */
     744                 :             : static Oid
     745                 :         671 : insert_label_record(Oid graphid, Oid peoid, const char *label)
     746                 :             : {
     747                 :             :     Oid         labeloid;
     748                 :             :     Oid         ellabeloid;
     749                 :             : 
     750                 :             :     /*
     751                 :             :      * Insert into pg_propgraph_label if not already existing.
     752                 :             :      */
     753                 :         671 :     labeloid = GetSysCacheOid2(PROPGRAPHLABELNAME, Anum_pg_propgraph_label_oid, ObjectIdGetDatum(graphid), CStringGetDatum(label));
     754         [ +  + ]:         671 :     if (!labeloid)
     755                 :             :     {
     756                 :             :         Relation    rel;
     757                 :         522 :         Datum       values[Natts_pg_propgraph_label] = {0};
     758                 :         522 :         bool        nulls[Natts_pg_propgraph_label] = {0};
     759                 :             :         NameData    labelname;
     760                 :             :         HeapTuple   tup;
     761                 :             :         ObjectAddress myself;
     762                 :             :         ObjectAddress referenced;
     763                 :             : 
     764                 :         522 :         rel = table_open(PropgraphLabelRelationId, RowExclusiveLock);
     765                 :             : 
     766                 :         522 :         labeloid = GetNewOidWithIndex(rel, PropgraphLabelObjectIndexId, Anum_pg_propgraph_label_oid);
     767                 :         522 :         values[Anum_pg_propgraph_label_oid - 1] = ObjectIdGetDatum(labeloid);
     768                 :         522 :         values[Anum_pg_propgraph_label_pglpgid - 1] = ObjectIdGetDatum(graphid);
     769                 :         522 :         namestrcpy(&labelname, label);
     770                 :         522 :         values[Anum_pg_propgraph_label_pgllabel - 1] = NameGetDatum(&labelname);
     771                 :             : 
     772                 :         522 :         tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
     773                 :         522 :         CatalogTupleInsert(rel, tup);
     774                 :         522 :         heap_freetuple(tup);
     775                 :             : 
     776                 :         522 :         ObjectAddressSet(myself, PropgraphLabelRelationId, labeloid);
     777                 :             : 
     778                 :         522 :         ObjectAddressSet(referenced, RelationRelationId, graphid);
     779                 :         522 :         recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
     780                 :             : 
     781                 :         522 :         table_close(rel, NoLock);
     782                 :             :     }
     783                 :             : 
     784                 :             :     /*
     785                 :             :      * Insert into pg_propgraph_element_label
     786                 :             :      */
     787                 :             :     {
     788                 :             :         Relation    rel;
     789                 :         671 :         Datum       values[Natts_pg_propgraph_element_label] = {0};
     790                 :         671 :         bool        nulls[Natts_pg_propgraph_element_label] = {0};
     791                 :             :         HeapTuple   tup;
     792                 :             :         ObjectAddress myself;
     793                 :             :         ObjectAddress referenced;
     794                 :             : 
     795                 :         671 :         rel = table_open(PropgraphElementLabelRelationId, RowExclusiveLock);
     796                 :             : 
     797                 :         671 :         ellabeloid = GetNewOidWithIndex(rel, PropgraphElementLabelObjectIndexId, Anum_pg_propgraph_element_label_oid);
     798                 :         671 :         values[Anum_pg_propgraph_element_label_oid - 1] = ObjectIdGetDatum(ellabeloid);
     799                 :         671 :         values[Anum_pg_propgraph_element_label_pgellabelid - 1] = ObjectIdGetDatum(labeloid);
     800                 :         671 :         values[Anum_pg_propgraph_element_label_pgelelid - 1] = ObjectIdGetDatum(peoid);
     801                 :             : 
     802                 :         671 :         tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
     803                 :         671 :         CatalogTupleInsert(rel, tup);
     804                 :         671 :         heap_freetuple(tup);
     805                 :             : 
     806                 :         671 :         ObjectAddressSet(myself, PropgraphElementLabelRelationId, ellabeloid);
     807                 :             : 
     808                 :         671 :         ObjectAddressSet(referenced, PropgraphLabelRelationId, labeloid);
     809                 :         671 :         recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
     810                 :         671 :         ObjectAddressSet(referenced, PropgraphElementRelationId, peoid);
     811                 :         671 :         recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
     812                 :             : 
     813                 :         671 :         table_close(rel, NoLock);
     814                 :             :     }
     815                 :             : 
     816                 :         671 :     return ellabeloid;
     817                 :             : }
     818                 :             : 
     819                 :             : /*
     820                 :             :  * Insert records for properties into the pg_propgraph_property catalog.
     821                 :             :  */
     822                 :             : static void
     823                 :         679 : insert_property_records(Oid graphid, Oid ellabeloid, Oid pgerelid, const PropGraphProperties *properties)
     824                 :             : {
     825                 :         679 :     List       *proplist = NIL;
     826                 :             :     ParseState *pstate;
     827                 :             :     ParseNamespaceItem *nsitem;
     828                 :             :     List       *tp;
     829                 :             :     Relation    rel;
     830                 :             :     ListCell   *lc;
     831                 :             : 
     832         [ +  + ]:         679 :     if (properties->all)
     833                 :             :     {
     834                 :             :         Relation    attRelation;
     835                 :             :         SysScanDesc scan;
     836                 :             :         ScanKeyData key[1];
     837                 :             :         HeapTuple   attributeTuple;
     838                 :             : 
     839                 :         290 :         attRelation = table_open(AttributeRelationId, RowShareLock);
     840                 :         290 :         ScanKeyInit(&key[0],
     841                 :             :                     Anum_pg_attribute_attrelid,
     842                 :             :                     BTEqualStrategyNumber, F_OIDEQ,
     843                 :             :                     ObjectIdGetDatum(pgerelid));
     844                 :         290 :         scan = systable_beginscan(attRelation, AttributeRelidNumIndexId,
     845                 :             :                                   true, NULL, 1, key);
     846         [ +  + ]:        2798 :         while (HeapTupleIsValid(attributeTuple = systable_getnext(scan)))
     847                 :             :         {
     848                 :        2508 :             Form_pg_attribute att = (Form_pg_attribute) GETSTRUCT(attributeTuple);
     849                 :             :             ColumnRef  *cr;
     850                 :             :             ResTarget  *rt;
     851                 :             : 
     852   [ +  +  +  + ]:        2508 :             if (att->attnum <= 0 || att->attisdropped)
     853                 :        1728 :                 continue;
     854                 :             : 
     855                 :         780 :             cr = makeNode(ColumnRef);
     856                 :         780 :             rt = makeNode(ResTarget);
     857                 :             : 
     858                 :         780 :             cr->fields = list_make1(makeString(pstrdup(NameStr(att->attname))));
     859                 :         780 :             cr->location = -1;
     860                 :             : 
     861                 :         780 :             rt->name = pstrdup(NameStr(att->attname));
     862                 :         780 :             rt->val = (Node *) cr;
     863                 :         780 :             rt->location = -1;
     864                 :             : 
     865                 :         780 :             proplist = lappend(proplist, rt);
     866                 :             :         }
     867                 :         290 :         systable_endscan(scan);
     868                 :         290 :         table_close(attRelation, RowShareLock);
     869                 :             :     }
     870                 :             :     else
     871                 :             :     {
     872                 :         389 :         proplist = properties->properties;
     873                 :             : 
     874   [ +  +  +  +  :        1035 :         foreach(lc, proplist)
                   +  + ]
     875                 :             :         {
     876                 :         646 :             ResTarget  *rt = lfirst_node(ResTarget, lc);
     877                 :             : 
     878   [ +  +  -  + ]:         646 :             if (!rt->name && !IsA(rt->val, ColumnRef))
     879         [ #  # ]:           0 :                 ereport(ERROR,
     880                 :             :                         errcode(ERRCODE_SYNTAX_ERROR),
     881                 :             :                         errmsg("property name required"),
     882                 :             :                         parser_errposition(NULL, rt->location));
     883                 :             :         }
     884                 :             :     }
     885                 :             : 
     886                 :         679 :     rel = table_open(pgerelid, AccessShareLock);
     887                 :             : 
     888                 :         679 :     pstate = make_parsestate(NULL);
     889                 :         679 :     nsitem = addRangeTableEntryForRelation(pstate,
     890                 :             :                                            rel,
     891                 :             :                                            AccessShareLock,
     892                 :             :                                            NULL,
     893                 :             :                                            false,
     894                 :             :                                            true);
     895                 :         679 :     addNSItemToQuery(pstate, nsitem, true, true, true);
     896                 :             : 
     897                 :         679 :     table_close(rel, NoLock);
     898                 :             : 
     899                 :         679 :     tp = transformTargetList(pstate, proplist, EXPR_KIND_PROPGRAPH_PROPERTY);
     900                 :         679 :     assign_expr_collations(pstate, (Node *) tp);
     901                 :             : 
     902   [ +  +  +  +  :        2073 :     foreach(lc, tp)
                   +  + ]
     903                 :             :     {
     904                 :        1426 :         TargetEntry *te = lfirst_node(TargetEntry, lc);
     905                 :             : 
     906                 :        1426 :         insert_property_record(graphid, ellabeloid, pgerelid, te->resname, te->expr);
     907                 :             :     }
     908                 :         647 : }
     909                 :             : 
     910                 :             : /*
     911                 :             :  * Insert records for a property into the pg_propgraph_property and
     912                 :             :  * pg_propgraph_label_property catalogs, and register dependencies.
     913                 :             :  */
     914                 :             : static void
     915                 :        1426 : insert_property_record(Oid graphid, Oid ellabeloid, Oid pgerelid, const char *propname, const Expr *expr)
     916                 :             : {
     917                 :             :     Oid         propoid;
     918                 :        1426 :     Oid         exprtypid = exprType((const Node *) expr);
     919                 :        1426 :     int32       exprtypmod = exprTypmod((const Node *) expr);
     920                 :        1426 :     Oid         exprcollation = exprCollation((const Node *) expr);
     921                 :             : 
     922                 :             :     /*
     923                 :             :      * Insert into pg_propgraph_property if not already existing.
     924                 :             :      */
     925                 :        1426 :     propoid = GetSysCacheOid2(PROPGRAPHPROPNAME, Anum_pg_propgraph_property_oid, ObjectIdGetDatum(graphid), CStringGetDatum(propname));
     926         [ +  + ]:        1426 :     if (!OidIsValid(propoid))
     927                 :             :     {
     928                 :             :         Relation    rel;
     929                 :             :         NameData    propnamedata;
     930                 :         763 :         Datum       values[Natts_pg_propgraph_property] = {0};
     931                 :         763 :         bool        nulls[Natts_pg_propgraph_property] = {0};
     932                 :             :         HeapTuple   tup;
     933                 :             :         ObjectAddress myself;
     934                 :             :         ObjectAddress referenced;
     935                 :             : 
     936                 :         763 :         rel = table_open(PropgraphPropertyRelationId, RowExclusiveLock);
     937                 :             : 
     938                 :         763 :         propoid = GetNewOidWithIndex(rel, PropgraphPropertyObjectIndexId, Anum_pg_propgraph_property_oid);
     939                 :         763 :         values[Anum_pg_propgraph_property_oid - 1] = ObjectIdGetDatum(propoid);
     940                 :         763 :         values[Anum_pg_propgraph_property_pgppgid - 1] = ObjectIdGetDatum(graphid);
     941                 :         763 :         namestrcpy(&propnamedata, propname);
     942                 :         763 :         values[Anum_pg_propgraph_property_pgpname - 1] = NameGetDatum(&propnamedata);
     943                 :         763 :         values[Anum_pg_propgraph_property_pgptypid - 1] = ObjectIdGetDatum(exprtypid);
     944                 :         763 :         values[Anum_pg_propgraph_property_pgptypmod - 1] = Int32GetDatum(exprtypmod);
     945                 :         763 :         values[Anum_pg_propgraph_property_pgpcollation - 1] = ObjectIdGetDatum(exprcollation);
     946                 :             : 
     947                 :         763 :         tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
     948                 :         763 :         CatalogTupleInsert(rel, tup);
     949                 :         763 :         heap_freetuple(tup);
     950                 :             : 
     951                 :         763 :         ObjectAddressSet(myself, PropgraphPropertyRelationId, propoid);
     952                 :             : 
     953                 :         763 :         ObjectAddressSet(referenced, RelationRelationId, graphid);
     954                 :         763 :         recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
     955                 :         763 :         ObjectAddressSet(referenced, TypeRelationId, exprtypid);
     956                 :         763 :         recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
     957   [ +  +  +  + ]:         763 :         if (OidIsValid(exprcollation) && exprcollation != DEFAULT_COLLATION_OID)
     958                 :             :         {
     959                 :          23 :             ObjectAddressSet(referenced, CollationRelationId, exprcollation);
     960                 :          23 :             recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
     961                 :             :         }
     962                 :             : 
     963                 :         763 :         table_close(rel, NoLock);
     964                 :             :     }
     965                 :             :     else
     966                 :             :     {
     967                 :         663 :         HeapTuple   pgptup = SearchSysCache1(PROPGRAPHPROPOID, ObjectIdGetDatum(propoid));
     968                 :         663 :         Form_pg_propgraph_property pgpform = (Form_pg_propgraph_property) GETSTRUCT(pgptup);
     969                 :         663 :         Oid         proptypid = pgpform->pgptypid;
     970                 :         663 :         int32       proptypmod = pgpform->pgptypmod;
     971                 :         663 :         Oid         propcollation = pgpform->pgpcollation;
     972                 :             : 
     973                 :         663 :         ReleaseSysCache(pgptup);
     974                 :             : 
     975                 :             :         /*
     976                 :             :          * Check that in the graph, all properties with the same name have the
     977                 :             :          * same type (independent of which label they are on).  (See SQL/PGQ
     978                 :             :          * subclause "Consistency check of a tabular property graph
     979                 :             :          * descriptor".)
     980                 :             :          */
     981   [ +  +  +  + ]:         663 :         if (proptypid != exprtypid || proptypmod != exprtypmod)
     982                 :             :         {
     983         [ +  - ]:          16 :             ereport(ERROR,
     984                 :             :                     errcode(ERRCODE_SYNTAX_ERROR),
     985                 :             :                     errmsg("property \"%s\" data type mismatch: %s vs. %s",
     986                 :             :                            propname, format_type_with_typemod(proptypid, proptypmod), format_type_with_typemod(exprtypid, exprtypmod)),
     987                 :             :                     errdetail("In a property graph, a property of the same name has to have the same data type in each label."));
     988                 :             :         }
     989                 :             : 
     990                 :             :         /* Similarly for collation */
     991         [ +  + ]:         647 :         if (propcollation != exprcollation)
     992                 :             :         {
     993         [ +  - ]:          16 :             ereport(ERROR,
     994                 :             :                     errcode(ERRCODE_SYNTAX_ERROR),
     995                 :             :                     errmsg("property \"%s\" collation mismatch: %s vs. %s",
     996                 :             :                            propname, get_collation_name(propcollation), get_collation_name(exprcollation)),
     997                 :             :                     errdetail("In a property graph, a property of the same name has to have the same collation in each label."));
     998                 :             :         }
     999                 :             :     }
    1000                 :             : 
    1001                 :             :     /*
    1002                 :             :      * Insert into pg_propgraph_label_property
    1003                 :             :      */
    1004                 :             :     {
    1005                 :             :         Relation    rel;
    1006                 :        1394 :         Datum       values[Natts_pg_propgraph_label_property] = {0};
    1007                 :        1394 :         bool        nulls[Natts_pg_propgraph_label_property] = {0};
    1008                 :             :         Oid         plpoid;
    1009                 :             :         HeapTuple   tup;
    1010                 :             :         ObjectAddress myself;
    1011                 :             :         ObjectAddress referenced;
    1012                 :             : 
    1013                 :        1394 :         rel = table_open(PropgraphLabelPropertyRelationId, RowExclusiveLock);
    1014                 :             : 
    1015                 :        1394 :         plpoid = GetNewOidWithIndex(rel, PropgraphLabelPropertyObjectIndexId, Anum_pg_propgraph_label_property_oid);
    1016                 :        1394 :         values[Anum_pg_propgraph_label_property_oid - 1] = ObjectIdGetDatum(plpoid);
    1017                 :        1394 :         values[Anum_pg_propgraph_label_property_plppropid - 1] = ObjectIdGetDatum(propoid);
    1018                 :        1394 :         values[Anum_pg_propgraph_label_property_plpellabelid - 1] = ObjectIdGetDatum(ellabeloid);
    1019                 :        1394 :         values[Anum_pg_propgraph_label_property_plpexpr - 1] = CStringGetTextDatum(nodeToString(expr));
    1020                 :             : 
    1021                 :        1394 :         tup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
    1022                 :        1394 :         CatalogTupleInsert(rel, tup);
    1023                 :        1394 :         heap_freetuple(tup);
    1024                 :             : 
    1025                 :        1394 :         ObjectAddressSet(myself, PropgraphLabelPropertyRelationId, plpoid);
    1026                 :             : 
    1027                 :        1394 :         ObjectAddressSet(referenced, PropgraphPropertyRelationId, propoid);
    1028                 :        1394 :         recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
    1029                 :             : 
    1030                 :        1394 :         ObjectAddressSet(referenced, PropgraphElementLabelRelationId, ellabeloid);
    1031                 :        1394 :         recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
    1032                 :             : 
    1033                 :        1394 :         recordDependencyOnSingleRelExpr(&myself, (Node *) copyObject(expr), pgerelid, DEPENDENCY_NORMAL, DEPENDENCY_NORMAL, false);
    1034                 :             : 
    1035                 :        1394 :         table_close(rel, NoLock);
    1036                 :             :     }
    1037                 :        1394 : }
    1038                 :             : 
    1039                 :             : /*
    1040                 :             :  * Check that for the given graph element, all properties with the same name
    1041                 :             :  * have the same expression for each label.  (See SQL/PGQ subclause "Creation
    1042                 :             :  * of an element table descriptor".)
    1043                 :             :  *
    1044                 :             :  * We check this after all the catalog records are already inserted.  This
    1045                 :             :  * makes it easier to share this code between CREATE PROPERTY GRAPH and ALTER
    1046                 :             :  * PROPERTY GRAPH.  We pass in the element OID so that ALTER PROPERTY GRAPH
    1047                 :             :  * only has to check the element it has just operated on.  CREATE PROPERTY
    1048                 :             :  * GRAPH checks all elements it has created.
    1049                 :             :  */
    1050                 :             : static void
    1051                 :         499 : check_element_properties(Oid peoid)
    1052                 :             : {
    1053                 :             :     Relation    rel1;
    1054                 :             :     ScanKeyData key1[1];
    1055                 :             :     SysScanDesc scan1;
    1056                 :             :     HeapTuple   tuple1;
    1057                 :         499 :     List       *propoids = NIL;
    1058                 :         499 :     List       *propexprs = NIL;
    1059                 :             : 
    1060                 :         499 :     rel1 = table_open(PropgraphElementLabelRelationId, AccessShareLock);
    1061                 :         499 :     ScanKeyInit(&key1[0],
    1062                 :             :                 Anum_pg_propgraph_element_label_pgelelid,
    1063                 :             :                 BTEqualStrategyNumber, F_OIDEQ,
    1064                 :             :                 ObjectIdGetDatum(peoid));
    1065                 :             : 
    1066                 :         499 :     scan1 = systable_beginscan(rel1, PropgraphElementLabelElementLabelIndexId, true, NULL, 1, key1);
    1067         [ +  + ]:        1138 :     while (HeapTupleIsValid(tuple1 = systable_getnext(scan1)))
    1068                 :             :     {
    1069                 :         647 :         Form_pg_propgraph_element_label ellabel = (Form_pg_propgraph_element_label) GETSTRUCT(tuple1);
    1070                 :             :         Relation    rel2;
    1071                 :             :         ScanKeyData key2[1];
    1072                 :             :         SysScanDesc scan2;
    1073                 :             :         HeapTuple   tuple2;
    1074                 :             : 
    1075                 :         647 :         rel2 = table_open(PropgraphLabelPropertyRelationId, AccessShareLock);
    1076                 :         647 :         ScanKeyInit(&key2[0],
    1077                 :             :                     Anum_pg_propgraph_label_property_plpellabelid,
    1078                 :             :                     BTEqualStrategyNumber, F_OIDEQ,
    1079                 :             :                     ObjectIdGetDatum(ellabel->oid));
    1080                 :             : 
    1081                 :         647 :         scan2 = systable_beginscan(rel2, PropgraphLabelPropertyLabelPropIndexId, true, NULL, 1, key2);
    1082         [ +  + ]:        2021 :         while (HeapTupleIsValid(tuple2 = systable_getnext(scan2)))
    1083                 :             :         {
    1084                 :        1382 :             Form_pg_propgraph_label_property lprop = (Form_pg_propgraph_label_property) GETSTRUCT(tuple2);
    1085                 :             :             Oid         propoid;
    1086                 :             :             Datum       datum;
    1087                 :             :             bool        isnull;
    1088                 :             :             char       *propexpr;
    1089                 :             :             ListCell   *lc1,
    1090                 :             :                        *lc2;
    1091                 :             :             bool        found;
    1092                 :             : 
    1093                 :        1382 :             propoid = lprop->plppropid;
    1094                 :        1382 :             datum = heap_getattr(tuple2, Anum_pg_propgraph_label_property_plpexpr, RelationGetDescr(rel2), &isnull);
    1095                 :             :             Assert(!isnull);
    1096                 :        1382 :             propexpr = TextDatumGetCString(datum);
    1097                 :             : 
    1098                 :        1382 :             found = false;
    1099   [ +  +  +  +  :        2703 :             forboth(lc1, propoids, lc2, propexprs)
          +  +  +  +  +  
             +  +  -  +  
                      + ]
    1100                 :             :             {
    1101         [ +  + ]:        1436 :                 if (propoid == lfirst_oid(lc1))
    1102                 :             :                 {
    1103                 :             :                     Node       *na,
    1104                 :             :                                *nb;
    1105                 :             : 
    1106                 :         115 :                     na = stringToNode(propexpr);
    1107                 :         115 :                     nb = stringToNode(lfirst(lc2));
    1108                 :             : 
    1109                 :         115 :                     found = true;
    1110                 :             : 
    1111         [ +  + ]:         115 :                     if (!equal(na, nb))
    1112                 :             :                     {
    1113                 :             :                         HeapTuple   tuple3;
    1114                 :             :                         Form_pg_propgraph_element elform;
    1115                 :             :                         List       *dpcontext;
    1116                 :             :                         char       *dpa,
    1117                 :             :                                    *dpb;
    1118                 :             : 
    1119                 :           8 :                         tuple3 = SearchSysCache1(PROPGRAPHELOID, ObjectIdGetDatum(peoid));
    1120         [ -  + ]:           8 :                         if (!tuple3)
    1121         [ #  # ]:           0 :                             elog(ERROR, "cache lookup failed for property graph element %u", peoid);
    1122                 :           8 :                         elform = (Form_pg_propgraph_element) GETSTRUCT(tuple3);
    1123                 :           8 :                         dpcontext = deparse_context_for(get_rel_name(elform->pgerelid), elform->pgerelid);
    1124                 :             : 
    1125                 :           8 :                         dpa = deparse_expression(na, dpcontext, false, false);
    1126                 :           8 :                         dpb = deparse_expression(nb, dpcontext, false, false);
    1127                 :             : 
    1128                 :             :                         /*
    1129                 :             :                          * show in sorted order to keep output independent of
    1130                 :             :                          * index order
    1131                 :             :                          */
    1132         [ -  + ]:           8 :                         if (strcmp(dpa, dpb) > 0)
    1133                 :             :                         {
    1134                 :             :                             char       *tmp;
    1135                 :             : 
    1136                 :           0 :                             tmp = dpa;
    1137                 :           0 :                             dpa = dpb;
    1138                 :           0 :                             dpb = tmp;
    1139                 :             :                         }
    1140                 :             : 
    1141         [ +  - ]:           8 :                         ereport(ERROR,
    1142                 :             :                                 errcode(ERRCODE_SYNTAX_ERROR),
    1143                 :             :                                 errmsg("element \"%s\" property \"%s\" expression mismatch: %s vs. %s",
    1144                 :             :                                        NameStr(elform->pgealias), get_propgraph_property_name(propoid), dpa, dpb),
    1145                 :             :                                 errdetail("In a property graph element, a property of the same name has to have the same expression in each label."));
    1146                 :             : 
    1147                 :             :                         ReleaseSysCache(tuple3);
    1148                 :             :                     }
    1149                 :             : 
    1150                 :         107 :                     break;
    1151                 :             :                 }
    1152                 :             :             }
    1153                 :             : 
    1154         [ +  + ]:        1374 :             if (!found)
    1155                 :             :             {
    1156                 :        1267 :                 propoids = lappend_oid(propoids, propoid);
    1157                 :        1267 :                 propexprs = lappend(propexprs, propexpr);
    1158                 :             :             }
    1159                 :             :         }
    1160                 :         639 :         systable_endscan(scan2);
    1161                 :         639 :         table_close(rel2, AccessShareLock);
    1162                 :             :     }
    1163                 :             : 
    1164                 :         491 :     systable_endscan(scan1);
    1165                 :         491 :     table_close(rel1, AccessShareLock);
    1166                 :         491 : }
    1167                 :             : 
    1168                 :             : /*
    1169                 :             :  * Check that for the given element label, all labels of the same name in the
    1170                 :             :  * graph have the same number and names of properties (independent of which
    1171                 :             :  * element they are on).  (See SQL/PGQ subclause "Consistency check of a
    1172                 :             :  * tabular property graph descriptor".)
    1173                 :             :  *
    1174                 :             :  * We check this after all the catalog records are already inserted.  This
    1175                 :             :  * makes it easier to share this code between CREATE PROPERTY GRAPH and ALTER
    1176                 :             :  * PROPERTY GRAPH.  We pass in the element label OID so that some variants of
    1177                 :             :  * ALTER PROPERTY GRAPH only have to check the element label it has just
    1178                 :             :  * operated on.  CREATE PROPERTY GRAPH and other ALTER PROPERTY GRAPH variants
    1179                 :             :  * check all labels.
    1180                 :             :  */
    1181                 :             : static void
    1182                 :         983 : check_element_label_properties(Oid ellabeloid)
    1183                 :             : {
    1184                 :             :     Relation    rel;
    1185                 :             :     SysScanDesc scan;
    1186                 :             :     ScanKeyData key[1];
    1187                 :             :     HeapTuple   tuple;
    1188                 :         983 :     Oid         labelid = InvalidOid;
    1189                 :         983 :     Oid         ref_ellabeloid = InvalidOid;
    1190                 :             :     List       *myprops,
    1191                 :             :                *refprops;
    1192                 :             :     List       *diff1,
    1193                 :             :                *diff2;
    1194                 :             : 
    1195                 :         983 :     rel = table_open(PropgraphElementLabelRelationId, AccessShareLock);
    1196                 :             : 
    1197                 :             :     /*
    1198                 :             :      * Get element label info
    1199                 :             :      */
    1200                 :         983 :     ScanKeyInit(&key[0],
    1201                 :             :                 Anum_pg_propgraph_element_label_oid,
    1202                 :             :                 BTEqualStrategyNumber,
    1203                 :             :                 F_OIDEQ, ObjectIdGetDatum(ellabeloid));
    1204                 :         983 :     scan = systable_beginscan(rel, PropgraphElementLabelObjectIndexId, true, NULL, 1, key);
    1205         [ +  - ]:         983 :     if (HeapTupleIsValid(tuple = systable_getnext(scan)))
    1206                 :             :     {
    1207                 :         983 :         Form_pg_propgraph_element_label ellabel = (Form_pg_propgraph_element_label) GETSTRUCT(tuple);
    1208                 :             : 
    1209                 :         983 :         labelid = ellabel->pgellabelid;
    1210                 :             :     }
    1211                 :         983 :     systable_endscan(scan);
    1212         [ -  + ]:         983 :     if (!labelid)
    1213         [ #  # ]:           0 :         elog(ERROR, "element label %u not found", ellabeloid);
    1214                 :             : 
    1215                 :             :     /*
    1216                 :             :      * Find a reference element label to fetch label properties.  The
    1217                 :             :      * reference element label has to have the same label OID as the one being
    1218                 :             :      * checked but a different element OID.
    1219                 :             :      */
    1220                 :         983 :     ScanKeyInit(&key[0],
    1221                 :             :                 Anum_pg_propgraph_element_label_pgellabelid,
    1222                 :             :                 BTEqualStrategyNumber,
    1223                 :             :                 F_OIDEQ, ObjectIdGetDatum(labelid));
    1224                 :         983 :     scan = systable_beginscan(rel, PropgraphElementLabelLabelIndexId, true, NULL, 1, key);
    1225         [ +  + ]:        1641 :     while (HeapTupleIsValid(tuple = systable_getnext(scan)))
    1226                 :             :     {
    1227                 :        1112 :         Form_pg_propgraph_element_label otherellabel = (Form_pg_propgraph_element_label) GETSTRUCT(tuple);
    1228                 :             : 
    1229         [ +  + ]:        1112 :         if (otherellabel->oid != ellabeloid)
    1230                 :             :         {
    1231                 :         454 :             ref_ellabeloid = otherellabel->oid;
    1232                 :         454 :             break;
    1233                 :             :         }
    1234                 :             :     }
    1235                 :         983 :     systable_endscan(scan);
    1236                 :             : 
    1237                 :         983 :     table_close(rel, AccessShareLock);
    1238                 :             : 
    1239                 :             :     /*
    1240                 :             :      * If there is no previous definition of this label, then we are done.
    1241                 :             :      */
    1242         [ +  + ]:         983 :     if (!ref_ellabeloid)
    1243                 :         529 :         return;
    1244                 :             : 
    1245                 :             :     /*
    1246                 :             :      * Now check number and names.
    1247                 :             :      *
    1248                 :             :      * XXX We could provide more detail in the error messages, but that would
    1249                 :             :      * probably only be useful for some ALTER commands, because otherwise it's
    1250                 :             :      * not really clear which label definition is the wrong one, and so you'd
    1251                 :             :      * have to construct a rather verbose report to be of any use.  Let's keep
    1252                 :             :      * it simple for now.
    1253                 :             :      */
    1254                 :             : 
    1255                 :         454 :     myprops = get_element_label_property_names(ellabeloid);
    1256                 :         454 :     refprops = get_element_label_property_names(ref_ellabeloid);
    1257                 :             : 
    1258         [ +  + ]:         454 :     if (list_length(refprops) != list_length(myprops))
    1259         [ +  - ]:          16 :         ereport(ERROR,
    1260                 :             :                 errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
    1261                 :             :                 errmsg("mismatching number of properties in definition of label \"%s\"", get_propgraph_label_name(labelid)));
    1262                 :             : 
    1263                 :         438 :     diff1 = list_difference(myprops, refprops);
    1264                 :         438 :     diff2 = list_difference(refprops, myprops);
    1265                 :             : 
    1266   [ +  +  -  + ]:         438 :     if (diff1 || diff2)
    1267         [ +  - ]:           8 :         ereport(ERROR,
    1268                 :             :                 errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
    1269                 :             :                 errmsg("mismatching property names in definition of label \"%s\"", get_propgraph_label_name(labelid)));
    1270                 :             : }
    1271                 :             : 
    1272                 :             : /*
    1273                 :             :  * As above, but check all labels of a graph.
    1274                 :             :  */
    1275                 :             : static void
    1276                 :         193 : check_all_labels_properties(Oid pgrelid)
    1277                 :             : {
    1278   [ +  +  +  +  :         980 :     foreach_oid(labeloid, get_graph_label_ids(pgrelid))
                   +  + ]
    1279                 :             :     {
    1280   [ +  -  +  +  :        2139 :         foreach_oid(ellabeloid, get_label_element_label_ids(labeloid))
                   +  + ]
    1281                 :             :         {
    1282                 :         927 :             check_element_label_properties(ellabeloid);
    1283                 :             :         }
    1284                 :             :     }
    1285                 :         181 : }
    1286                 :             : 
    1287                 :             : /*
    1288                 :             :  * ALTER PROPERTY GRAPH
    1289                 :             :  */
    1290                 :             : ObjectAddress
    1291                 :         156 : AlterPropGraph(ParseState *pstate, const AlterPropGraphStmt *stmt)
    1292                 :             : {
    1293                 :             :     Oid         pgrelid;
    1294                 :             :     ListCell   *lc;
    1295                 :             :     ObjectAddress pgaddress;
    1296                 :             : 
    1297                 :             :     /*
    1298                 :             :      * ShareRowExclusiveLock is required because this command runs some
    1299                 :             :      * graph-wide consistency checks that wouldn't work if more than one ALTER
    1300                 :             :      * PROPERTY GRAPH could operate on the same graph at once.
    1301                 :             :      */
    1302                 :         156 :     pgrelid = RangeVarGetRelidExtended(stmt->pgname,
    1303                 :             :                                        ShareRowExclusiveLock,
    1304                 :         156 :                                        stmt->missing_ok ? RVR_MISSING_OK : 0,
    1305                 :             :                                        RangeVarCallbackOwnsRelation,
    1306                 :             :                                        NULL);
    1307         [ -  + ]:         152 :     if (pgrelid == InvalidOid)
    1308                 :             :     {
    1309         [ #  # ]:           0 :         ereport(NOTICE,
    1310                 :             :                 (errmsg("relation \"%s\" does not exist, skipping",
    1311                 :             :                         stmt->pgname->relname)));
    1312                 :           0 :         return InvalidObjectAddress;
    1313                 :             :     }
    1314                 :             : 
    1315                 :         152 :     ObjectAddressSet(pgaddress, RelationRelationId, pgrelid);
    1316                 :             : 
    1317   [ +  +  +  +  :         168 :     foreach(lc, stmt->add_vertex_tables)
                   +  + ]
    1318                 :             :     {
    1319                 :          32 :         PropGraphVertex *vertex = lfirst_node(PropGraphVertex, lc);
    1320                 :             :         struct element_info *vinfo;
    1321                 :             :         Relation    rel;
    1322                 :             :         Oid         peoid;
    1323                 :             : 
    1324                 :          32 :         vinfo = palloc0_object(struct element_info);
    1325                 :          32 :         vinfo->kind = PGEKIND_VERTEX;
    1326                 :             : 
    1327                 :          32 :         vinfo->relid = RangeVarGetRelidExtended(vertex->vtable, AccessShareLock, 0, RangeVarCallbackOwnsRelation, NULL);
    1328                 :             : 
    1329                 :          32 :         rel = table_open(vinfo->relid, NoLock);
    1330                 :             : 
    1331   [ +  +  +  - ]:          32 :         if (rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP && get_rel_persistence(pgrelid) != RELPERSISTENCE_TEMP)
    1332         [ +  - ]:           4 :             ereport(ERROR,
    1333                 :             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
    1334                 :             :                      errmsg("cannot add temporary element table to non-temporary property graph"),
    1335                 :             :                      errdetail("Table \"%s\" is a temporary table.", get_rel_name(vinfo->relid)),
    1336                 :             :                      parser_errposition(pstate, vertex->vtable->location)));
    1337                 :             : 
    1338         [ +  + ]:          28 :         if (vertex->vtable->alias)
    1339                 :           4 :             vinfo->aliasname = vertex->vtable->alias->aliasname;
    1340                 :             :         else
    1341                 :          24 :             vinfo->aliasname = vertex->vtable->relname;
    1342                 :             : 
    1343                 :          28 :         vinfo->key = propgraph_element_get_key(pstate, vertex->vkey, rel, vinfo->aliasname, vertex->location);
    1344                 :             : 
    1345                 :          28 :         vinfo->labels = vertex->labels;
    1346                 :             : 
    1347                 :          28 :         table_close(rel, NoLock);
    1348                 :             : 
    1349         [ +  + ]:          28 :         if (SearchSysCacheExists2(PROPGRAPHELALIAS,
    1350                 :             :                                   ObjectIdGetDatum(pgrelid),
    1351                 :             :                                   CStringGetDatum(vinfo->aliasname)))
    1352         [ +  - ]:           4 :             ereport(ERROR,
    1353                 :             :                     errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
    1354                 :             :                     errmsg("alias \"%s\" already exists in property graph \"%s\"",
    1355                 :             :                            vinfo->aliasname, stmt->pgname->relname),
    1356                 :             :                     parser_errposition(pstate, vertex->vtable->location));
    1357                 :             : 
    1358                 :          24 :         peoid = insert_element_record(pgaddress, vinfo);
    1359                 :             : 
    1360                 :          20 :         CommandCounterIncrement();
    1361                 :          20 :         check_element_properties(peoid);
    1362                 :          16 :         check_all_labels_properties(pgrelid);
    1363                 :             :     }
    1364                 :             : 
    1365   [ +  +  +  +  :         168 :     foreach(lc, stmt->add_edge_tables)
                   +  + ]
    1366                 :             :     {
    1367                 :          36 :         PropGraphEdge *edge = lfirst_node(PropGraphEdge, lc);
    1368                 :             :         struct element_info *einfo;
    1369                 :             :         Relation    rel;
    1370                 :             :         Relation    srcrel;
    1371                 :             :         Relation    destrel;
    1372                 :             :         Oid         peoid;
    1373                 :             : 
    1374                 :          36 :         einfo = palloc0_object(struct element_info);
    1375                 :          36 :         einfo->kind = PGEKIND_EDGE;
    1376                 :             : 
    1377                 :          36 :         einfo->relid = RangeVarGetRelidExtended(edge->etable, AccessShareLock, 0, RangeVarCallbackOwnsRelation, NULL);
    1378                 :             : 
    1379                 :          36 :         rel = table_open(einfo->relid, NoLock);
    1380                 :             : 
    1381   [ -  +  -  - ]:          36 :         if (rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP && get_rel_persistence(pgrelid) != RELPERSISTENCE_TEMP)
    1382         [ #  # ]:           0 :             ereport(ERROR,
    1383                 :             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
    1384                 :             :                      errmsg("cannot add temporary element table to non-temporary property graph"),
    1385                 :             :                      errdetail("Table \"%s\" is a temporary table.", get_rel_name(einfo->relid)),
    1386                 :             :                      parser_errposition(pstate, edge->etable->location)));
    1387                 :             : 
    1388         [ -  + ]:          36 :         if (edge->etable->alias)
    1389                 :           0 :             einfo->aliasname = edge->etable->alias->aliasname;
    1390                 :             :         else
    1391                 :          36 :             einfo->aliasname = edge->etable->relname;
    1392                 :             : 
    1393                 :          36 :         einfo->key = propgraph_element_get_key(pstate, edge->ekey, rel, einfo->aliasname, edge->location);
    1394                 :             : 
    1395                 :          36 :         einfo->srcvertexid = get_vertex_oid(pstate, pgrelid, edge->esrcvertex, edge->location);
    1396                 :          36 :         einfo->destvertexid = get_vertex_oid(pstate, pgrelid, edge->edestvertex, edge->location);
    1397                 :             : 
    1398                 :          36 :         einfo->srcrelid = get_element_relid(einfo->srcvertexid);
    1399                 :          36 :         einfo->destrelid = get_element_relid(einfo->destvertexid);
    1400                 :             : 
    1401                 :          36 :         srcrel = table_open(einfo->srcrelid, AccessShareLock);
    1402                 :          36 :         destrel = table_open(einfo->destrelid, AccessShareLock);
    1403                 :             : 
    1404                 :          36 :         propgraph_edge_get_ref_keys(pstate, edge->esrckey, edge->esrcvertexcols, rel, srcrel,
    1405                 :          36 :                                     einfo->aliasname, edge->location, "SOURCE",
    1406                 :             :                                     &einfo->srckey, &einfo->srcref, &einfo->srceqop);
    1407                 :          36 :         propgraph_edge_get_ref_keys(pstate, edge->edestkey, edge->edestvertexcols, rel, destrel,
    1408                 :          36 :                                     einfo->aliasname, edge->location, "DESTINATION",
    1409                 :             :                                     &einfo->destkey, &einfo->destref, &einfo->desteqop);
    1410                 :             : 
    1411                 :          36 :         einfo->labels = edge->labels;
    1412                 :             : 
    1413                 :          36 :         table_close(destrel, NoLock);
    1414                 :          36 :         table_close(srcrel, NoLock);
    1415                 :             : 
    1416                 :          36 :         table_close(rel, NoLock);
    1417                 :             : 
    1418         [ -  + ]:          36 :         if (SearchSysCacheExists2(PROPGRAPHELALIAS,
    1419                 :             :                                   ObjectIdGetDatum(pgrelid),
    1420                 :             :                                   CStringGetDatum(einfo->aliasname)))
    1421         [ #  # ]:           0 :             ereport(ERROR,
    1422                 :             :                     errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
    1423                 :             :                     errmsg("alias \"%s\" already exists in property graph \"%s\"",
    1424                 :             :                            einfo->aliasname, stmt->pgname->relname),
    1425                 :             :                     parser_errposition(pstate, edge->etable->location));
    1426                 :             : 
    1427                 :          36 :         peoid = insert_element_record(pgaddress, einfo);
    1428                 :             : 
    1429                 :          32 :         CommandCounterIncrement();
    1430                 :          32 :         check_element_properties(peoid);
    1431                 :          32 :         check_all_labels_properties(pgrelid);
    1432                 :             :     }
    1433                 :             : 
    1434   [ +  +  +  +  :         136 :     foreach(lc, stmt->drop_vertex_tables)
                   +  + ]
    1435                 :             :     {
    1436                 :           8 :         char       *alias = strVal(lfirst(lc));
    1437                 :             :         Oid         peoid;
    1438                 :             :         ObjectAddress obj;
    1439                 :             : 
    1440                 :           8 :         peoid = get_vertex_oid(pstate, pgrelid, alias, -1);
    1441                 :           8 :         ObjectAddressSet(obj, PropgraphElementRelationId, peoid);
    1442                 :           8 :         performDeletion(&obj, stmt->drop_behavior, 0);
    1443                 :             :     }
    1444                 :             : 
    1445   [ +  +  +  +  :         140 :     foreach(lc, stmt->drop_edge_tables)
                   +  + ]
    1446                 :             :     {
    1447                 :          12 :         char       *alias = strVal(lfirst(lc));
    1448                 :             :         Oid         peoid;
    1449                 :             :         ObjectAddress obj;
    1450                 :             : 
    1451                 :          12 :         peoid = get_edge_oid(pstate, pgrelid, alias, -1);
    1452                 :          12 :         ObjectAddressSet(obj, PropgraphElementRelationId, peoid);
    1453                 :          12 :         performDeletion(&obj, stmt->drop_behavior, 0);
    1454                 :             :     }
    1455                 :             : 
    1456                 :             :     /* Remove any orphaned pg_propgraph_label entries */
    1457   [ +  +  +  + ]:         128 :     if (stmt->drop_vertex_tables || stmt->drop_edge_tables)
    1458                 :             :     {
    1459   [ +  -  +  +  :          88 :         foreach_oid(labeloid, get_graph_label_ids(pgrelid))
                   +  + ]
    1460                 :             :         {
    1461         [ +  + ]:          64 :             if (!get_label_element_label_ids(labeloid))
    1462                 :             :             {
    1463                 :             :                 ObjectAddress obj;
    1464                 :             : 
    1465                 :          12 :                 ObjectAddressSet(obj, PropgraphLabelRelationId, labeloid);
    1466                 :          12 :                 performDeletion(&obj, stmt->drop_behavior, 0);
    1467                 :             :             }
    1468                 :             :         }
    1469                 :             :     }
    1470                 :             : 
    1471   [ +  +  +  +  :         144 :     foreach(lc, stmt->add_labels)
                   +  + ]
    1472                 :             :     {
    1473                 :          32 :         PropGraphLabelAndProperties *lp = lfirst_node(PropGraphLabelAndProperties, lc);
    1474                 :             :         Oid         peoid;
    1475                 :             :         Oid         pgerelid;
    1476                 :             :         Oid         ellabeloid;
    1477                 :             : 
    1478                 :             :         Assert(lp->label);
    1479                 :             : 
    1480         [ +  - ]:          32 :         if (stmt->element_kind == PROPGRAPH_ELEMENT_KIND_VERTEX)
    1481                 :          32 :             peoid = get_vertex_oid(pstate, pgrelid, stmt->element_alias, -1);
    1482                 :             :         else
    1483                 :           0 :             peoid = get_edge_oid(pstate, pgrelid, stmt->element_alias, -1);
    1484                 :             : 
    1485                 :          32 :         pgerelid = get_element_relid(peoid);
    1486                 :             : 
    1487                 :          32 :         ellabeloid = insert_label_record(pgrelid, peoid, lp->label);
    1488                 :          32 :         insert_property_records(pgrelid, ellabeloid, pgerelid, lp->properties);
    1489                 :             : 
    1490                 :          28 :         CommandCounterIncrement();
    1491                 :          28 :         check_element_properties(peoid);
    1492                 :          28 :         check_element_label_properties(ellabeloid);
    1493                 :             :     }
    1494                 :             : 
    1495         [ +  + ]:         112 :     if (stmt->drop_label)
    1496                 :             :     {
    1497                 :             :         Oid         peoid;
    1498                 :             :         Oid         labeloid;
    1499                 :          24 :         Oid         ellabeloid = InvalidOid;
    1500                 :             :         ObjectAddress obj;
    1501                 :             :         Relation    ellabelrel;
    1502                 :             :         SysScanDesc ellabelscan;
    1503                 :             :         ScanKeyData ellabelkey[1];
    1504                 :             :         int         nlabels;
    1505                 :             :         HeapTuple   tuple;
    1506                 :             : 
    1507         [ +  - ]:          24 :         if (stmt->element_kind == PROPGRAPH_ELEMENT_KIND_VERTEX)
    1508                 :          24 :             peoid = get_vertex_oid(pstate, pgrelid, stmt->element_alias, -1);
    1509                 :             :         else
    1510                 :           0 :             peoid = get_edge_oid(pstate, pgrelid, stmt->element_alias, -1);
    1511                 :             : 
    1512                 :          24 :         labeloid = GetSysCacheOid2(PROPGRAPHLABELNAME,
    1513                 :             :                                    Anum_pg_propgraph_label_oid,
    1514                 :             :                                    ObjectIdGetDatum(pgrelid),
    1515                 :             :                                    CStringGetDatum(stmt->drop_label));
    1516         [ +  + ]:          24 :         if (!labeloid)
    1517         [ +  - ]:           4 :             ereport(ERROR,
    1518                 :             :                     errcode(ERRCODE_UNDEFINED_OBJECT),
    1519                 :             :                     errmsg("property graph \"%s\" element \"%s\" has no label \"%s\"",
    1520                 :             :                            get_rel_name(pgrelid), stmt->element_alias, stmt->drop_label),
    1521                 :             :                     parser_errposition(pstate, -1));
    1522                 :             : 
    1523                 :             :         /*
    1524                 :             :          * Is the given label associated with the element?  Is this the only
    1525                 :             :          * label associated with the element?  Scan the
    1526                 :             :          * pg_propgraph_element_label table to find answers to these
    1527                 :             :          * questions.  Stop scanning when we know both answers.
    1528                 :             :          */
    1529                 :          20 :         ellabelrel = table_open(PropgraphElementLabelRelationId, AccessShareLock);
    1530                 :          20 :         ScanKeyInit(&ellabelkey[0],
    1531                 :             :                     Anum_pg_propgraph_element_label_pgelelid,
    1532                 :             :                     BTEqualStrategyNumber, F_OIDEQ,
    1533                 :             :                     ObjectIdGetDatum(peoid));
    1534                 :          20 :         ellabelscan = systable_beginscan(ellabelrel, PropgraphElementLabelElementLabelIndexId,
    1535                 :             :                                          true, NULL, 1, ellabelkey);
    1536                 :          20 :         nlabels = 0;
    1537         [ +  + ]:          44 :         while (HeapTupleIsValid(tuple = systable_getnext(ellabelscan)))
    1538                 :             :         {
    1539                 :          40 :             Form_pg_propgraph_element_label ellabelform = (Form_pg_propgraph_element_label) GETSTRUCT(tuple);
    1540                 :             : 
    1541                 :          40 :             nlabels++;
    1542                 :             : 
    1543         [ +  + ]:          40 :             if (ellabelform->pgellabelid == labeloid)
    1544                 :          20 :                 ellabeloid = ellabelform->oid;
    1545                 :             : 
    1546   [ +  +  +  + ]:          40 :             if (nlabels > 1 && ellabeloid)
    1547                 :          16 :                 break;
    1548                 :             :         }
    1549                 :          20 :         systable_endscan(ellabelscan);
    1550                 :          20 :         table_close(ellabelrel, AccessShareLock);
    1551                 :             : 
    1552         [ -  + ]:          20 :         if (!ellabeloid)
    1553         [ #  # ]:           0 :             ereport(ERROR,
    1554                 :             :                     errcode(ERRCODE_UNDEFINED_OBJECT),
    1555                 :             :                     errmsg("property graph \"%s\" element \"%s\" has no label \"%s\"",
    1556                 :             :                            get_rel_name(pgrelid), stmt->element_alias, stmt->drop_label),
    1557                 :             :                     parser_errposition(pstate, -1));
    1558                 :             : 
    1559                 :             :         /*
    1560                 :             :          * Prevent dropping the last label from an element. Every element must
    1561                 :             :          * have at least one label associated with it.
    1562                 :             :          */
    1563         [ +  + ]:          20 :         if (nlabels == 1)
    1564         [ +  - ]:           4 :             ereport(ERROR,
    1565                 :             :                     (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    1566                 :             :                      errmsg("cannot drop the last label from element \"%s\"",
    1567                 :             :                             stmt->element_alias),
    1568                 :             :                      errhint("Every element must have at least one label.")));
    1569                 :             : 
    1570                 :          16 :         ObjectAddressSet(obj, PropgraphElementLabelRelationId, ellabeloid);
    1571                 :          16 :         performDeletion(&obj, stmt->drop_behavior, 0);
    1572                 :             : 
    1573                 :             :         /* Remove any orphaned pg_propgraph_label entries */
    1574         [ +  + ]:          16 :         if (!get_label_element_label_ids(labeloid))
    1575                 :             :         {
    1576                 :          12 :             ObjectAddressSet(obj, PropgraphLabelRelationId, labeloid);
    1577                 :          12 :             performDeletion(&obj, stmt->drop_behavior, 0);
    1578                 :             :         }
    1579                 :             :     }
    1580                 :             : 
    1581         [ +  + ]:         100 :     if (stmt->add_properties)
    1582                 :             :     {
    1583                 :             :         Oid         peoid;
    1584                 :             :         Oid         pgerelid;
    1585                 :             :         Oid         labeloid;
    1586                 :           8 :         Oid         ellabeloid = InvalidOid;
    1587                 :             : 
    1588         [ +  + ]:           8 :         if (stmt->element_kind == PROPGRAPH_ELEMENT_KIND_VERTEX)
    1589                 :           4 :             peoid = get_vertex_oid(pstate, pgrelid, stmt->element_alias, -1);
    1590                 :             :         else
    1591                 :           4 :             peoid = get_edge_oid(pstate, pgrelid, stmt->element_alias, -1);
    1592                 :             : 
    1593                 :           8 :         labeloid = GetSysCacheOid2(PROPGRAPHLABELNAME,
    1594                 :             :                                    Anum_pg_propgraph_label_oid,
    1595                 :             :                                    ObjectIdGetDatum(pgrelid),
    1596                 :             :                                    CStringGetDatum(stmt->alter_label));
    1597         [ +  - ]:           8 :         if (labeloid)
    1598                 :           8 :             ellabeloid = GetSysCacheOid2(PROPGRAPHELEMENTLABELELEMENTLABEL,
    1599                 :             :                                          Anum_pg_propgraph_element_label_oid,
    1600                 :             :                                          ObjectIdGetDatum(peoid),
    1601                 :             :                                          ObjectIdGetDatum(labeloid));
    1602         [ -  + ]:           8 :         if (!ellabeloid)
    1603         [ #  # ]:           0 :             ereport(ERROR,
    1604                 :             :                     errcode(ERRCODE_UNDEFINED_OBJECT),
    1605                 :             :                     errmsg("property graph \"%s\" element \"%s\" has no label \"%s\"",
    1606                 :             :                            get_rel_name(pgrelid), stmt->element_alias, stmt->alter_label),
    1607                 :             :                     parser_errposition(pstate, -1));
    1608                 :             : 
    1609                 :           8 :         pgerelid = get_element_relid(peoid);
    1610                 :             : 
    1611                 :           8 :         insert_property_records(pgrelid, ellabeloid, pgerelid, stmt->add_properties);
    1612                 :             : 
    1613                 :           8 :         CommandCounterIncrement();
    1614                 :           8 :         check_element_properties(peoid);
    1615                 :           8 :         check_element_label_properties(ellabeloid);
    1616                 :             :     }
    1617                 :             : 
    1618         [ +  + ]:         100 :     if (stmt->drop_properties)
    1619                 :             :     {
    1620                 :             :         Oid         peoid;
    1621                 :             :         Oid         labeloid;
    1622                 :          24 :         Oid         ellabeloid = InvalidOid;
    1623                 :             :         ObjectAddress obj;
    1624                 :             : 
    1625         [ +  + ]:          24 :         if (stmt->element_kind == PROPGRAPH_ELEMENT_KIND_VERTEX)
    1626                 :          16 :             peoid = get_vertex_oid(pstate, pgrelid, stmt->element_alias, -1);
    1627                 :             :         else
    1628                 :           8 :             peoid = get_edge_oid(pstate, pgrelid, stmt->element_alias, -1);
    1629                 :             : 
    1630                 :          24 :         labeloid = GetSysCacheOid2(PROPGRAPHLABELNAME,
    1631                 :             :                                    Anum_pg_propgraph_label_oid,
    1632                 :             :                                    ObjectIdGetDatum(pgrelid),
    1633                 :             :                                    CStringGetDatum(stmt->alter_label));
    1634         [ +  - ]:          24 :         if (labeloid)
    1635                 :          24 :             ellabeloid = GetSysCacheOid2(PROPGRAPHELEMENTLABELELEMENTLABEL,
    1636                 :             :                                          Anum_pg_propgraph_element_label_oid,
    1637                 :             :                                          ObjectIdGetDatum(peoid),
    1638                 :             :                                          ObjectIdGetDatum(labeloid));
    1639                 :             : 
    1640         [ -  + ]:          24 :         if (!ellabeloid)
    1641         [ #  # ]:           0 :             ereport(ERROR,
    1642                 :             :                     errcode(ERRCODE_UNDEFINED_OBJECT),
    1643                 :             :                     errmsg("property graph \"%s\" element \"%s\" has no label \"%s\"",
    1644                 :             :                            get_rel_name(pgrelid), stmt->element_alias, stmt->alter_label),
    1645                 :             :                     parser_errposition(pstate, -1));
    1646                 :             : 
    1647   [ +  -  +  +  :          44 :         foreach(lc, stmt->drop_properties)
                   +  + ]
    1648                 :             :         {
    1649                 :          24 :             char       *propname = strVal(lfirst(lc));
    1650                 :             :             Oid         propoid;
    1651                 :          24 :             Oid         plpoid = InvalidOid;
    1652                 :             : 
    1653                 :          24 :             propoid = GetSysCacheOid2(PROPGRAPHPROPNAME,
    1654                 :             :                                       Anum_pg_propgraph_property_oid,
    1655                 :             :                                       ObjectIdGetDatum(pgrelid),
    1656                 :             :                                       CStringGetDatum(propname));
    1657         [ +  - ]:          24 :             if (propoid)
    1658                 :          24 :                 plpoid = GetSysCacheOid2(PROPGRAPHLABELPROP,
    1659                 :             :                                          Anum_pg_propgraph_label_property_oid,
    1660                 :             :                                          ObjectIdGetDatum(ellabeloid),
    1661                 :             :                                          ObjectIdGetDatum(propoid));
    1662         [ +  + ]:          24 :             if (!plpoid)
    1663         [ +  - ]:           4 :                 ereport(ERROR,
    1664                 :             :                         errcode(ERRCODE_UNDEFINED_OBJECT),
    1665                 :             :                         errmsg("property graph \"%s\" element \"%s\" label \"%s\" has no property \"%s\"",
    1666                 :             :                                get_rel_name(pgrelid), stmt->element_alias, stmt->alter_label, propname),
    1667                 :             :                         parser_errposition(pstate, -1));
    1668                 :             : 
    1669                 :          20 :             ObjectAddressSet(obj, PropgraphLabelPropertyRelationId, plpoid);
    1670                 :          20 :             performDeletion(&obj, stmt->drop_behavior, 0);
    1671                 :             :         }
    1672                 :             : 
    1673                 :          20 :         check_element_label_properties(ellabeloid);
    1674                 :             :     }
    1675                 :             : 
    1676                 :             :     /* Remove any orphaned pg_propgraph_property entries */
    1677   [ +  +  +  +  :          96 :     if (stmt->drop_properties || stmt->drop_vertex_tables || stmt->drop_edge_tables)
                   +  + ]
    1678                 :             :     {
    1679   [ +  -  +  +  :         264 :         foreach_oid(propoid, get_graph_property_ids(pgrelid))
                   +  + ]
    1680                 :             :         {
    1681                 :             :             Relation    rel;
    1682                 :             :             SysScanDesc scan;
    1683                 :             :             ScanKeyData key[1];
    1684                 :             : 
    1685                 :         216 :             rel = table_open(PropgraphLabelPropertyRelationId, RowShareLock);
    1686                 :         216 :             ScanKeyInit(&key[0],
    1687                 :             :                         Anum_pg_propgraph_label_property_plppropid,
    1688                 :             :                         BTEqualStrategyNumber, F_OIDEQ,
    1689                 :             :                         ObjectIdGetDatum(propoid));
    1690                 :             :             /* XXX no suitable index */
    1691                 :         216 :             scan = systable_beginscan(rel, InvalidOid, true, NULL, 1, key);
    1692         [ +  + ]:         216 :             if (!systable_getnext(scan))
    1693                 :             :             {
    1694                 :             :                 ObjectAddress obj;
    1695                 :             : 
    1696                 :          28 :                 ObjectAddressSet(obj, PropgraphPropertyRelationId, propoid);
    1697                 :          28 :                 performDeletion(&obj, stmt->drop_behavior, 0);
    1698                 :             :             }
    1699                 :             : 
    1700                 :         208 :             systable_endscan(scan);
    1701                 :         208 :             table_close(rel, RowShareLock);
    1702                 :             :         }
    1703                 :             :     }
    1704                 :             : 
    1705                 :             :     /*
    1706                 :             :      * Invalidate relcache entry of the property graph so that the queries in
    1707                 :             :      * the cached plans referencing the property graph will be rewritten
    1708                 :             :      * considering changes to the property graph.
    1709                 :             :      */
    1710                 :          88 :     CacheInvalidateRelcacheByRelid(pgrelid);
    1711                 :             : 
    1712                 :          88 :     return pgaddress;
    1713                 :             : }
    1714                 :             : 
    1715                 :             : /*
    1716                 :             :  * Get OID of vertex from graph OID and element alias.  Element must be a
    1717                 :             :  * vertex, otherwise error.
    1718                 :             :  */
    1719                 :             : static Oid
    1720                 :         156 : get_vertex_oid(ParseState *pstate, Oid pgrelid, const char *alias, int location)
    1721                 :             : {
    1722                 :             :     HeapTuple   tuple;
    1723                 :             :     Oid         peoid;
    1724                 :             : 
    1725                 :         156 :     tuple = SearchSysCache2(PROPGRAPHELALIAS, ObjectIdGetDatum(pgrelid), CStringGetDatum(alias));
    1726         [ -  + ]:         156 :     if (!tuple)
    1727         [ #  # ]:           0 :         ereport(ERROR,
    1728                 :             :                 errcode(ERRCODE_UNDEFINED_OBJECT),
    1729                 :             :                 errmsg("property graph \"%s\" has no element with alias \"%s\"",
    1730                 :             :                        get_rel_name(pgrelid), alias),
    1731                 :             :                 parser_errposition(pstate, location));
    1732                 :             : 
    1733         [ -  + ]:         156 :     if (((Form_pg_propgraph_element) GETSTRUCT(tuple))->pgekind != PGEKIND_VERTEX)
    1734         [ #  # ]:           0 :         ereport(ERROR,
    1735                 :             :                 errcode(ERRCODE_SYNTAX_ERROR),
    1736                 :             :                 errmsg("element \"%s\" of property graph \"%s\" is not a vertex",
    1737                 :             :                        alias, get_rel_name(pgrelid)),
    1738                 :             :                 parser_errposition(pstate, location));
    1739                 :             : 
    1740                 :         156 :     peoid = ((Form_pg_propgraph_element) GETSTRUCT(tuple))->oid;
    1741                 :             : 
    1742                 :         156 :     ReleaseSysCache(tuple);
    1743                 :             : 
    1744                 :         156 :     return peoid;
    1745                 :             : }
    1746                 :             : 
    1747                 :             : /*
    1748                 :             :  * Get OID of edge from graph OID and element alias.  Element must be an edge,
    1749                 :             :  * otherwise error.
    1750                 :             :  */
    1751                 :             : static Oid
    1752                 :          24 : get_edge_oid(ParseState *pstate, Oid pgrelid, const char *alias, int location)
    1753                 :             : {
    1754                 :             :     HeapTuple   tuple;
    1755                 :             :     Oid         peoid;
    1756                 :             : 
    1757                 :          24 :     tuple = SearchSysCache2(PROPGRAPHELALIAS, ObjectIdGetDatum(pgrelid), CStringGetDatum(alias));
    1758         [ -  + ]:          24 :     if (!tuple)
    1759         [ #  # ]:           0 :         ereport(ERROR,
    1760                 :             :                 errcode(ERRCODE_UNDEFINED_OBJECT),
    1761                 :             :                 errmsg("property graph \"%s\" has no element with alias \"%s\"",
    1762                 :             :                        get_rel_name(pgrelid), alias),
    1763                 :             :                 parser_errposition(pstate, location));
    1764                 :             : 
    1765         [ -  + ]:          24 :     if (((Form_pg_propgraph_element) GETSTRUCT(tuple))->pgekind != PGEKIND_EDGE)
    1766         [ #  # ]:           0 :         ereport(ERROR,
    1767                 :             :                 errcode(ERRCODE_SYNTAX_ERROR),
    1768                 :             :                 errmsg("element \"%s\" of property graph \"%s\" is not an edge",
    1769                 :             :                        alias, get_rel_name(pgrelid)),
    1770                 :             :                 parser_errposition(pstate, location));
    1771                 :             : 
    1772                 :          24 :     peoid = ((Form_pg_propgraph_element) GETSTRUCT(tuple))->oid;
    1773                 :             : 
    1774                 :          24 :     ReleaseSysCache(tuple);
    1775                 :             : 
    1776                 :          24 :     return peoid;
    1777                 :             : }
    1778                 :             : 
    1779                 :             : /*
    1780                 :             :  * Get the element table relation OID from the OID of the element.
    1781                 :             :  */
    1782                 :             : static Oid
    1783                 :         112 : get_element_relid(Oid peid)
    1784                 :             : {
    1785                 :             :     HeapTuple   tuple;
    1786                 :             :     Oid         pgerelid;
    1787                 :             : 
    1788                 :         112 :     tuple = SearchSysCache1(PROPGRAPHELOID, ObjectIdGetDatum(peid));
    1789         [ -  + ]:         112 :     if (!tuple)
    1790         [ #  # ]:           0 :         elog(ERROR, "cache lookup failed for property graph element %u", peid);
    1791                 :             : 
    1792                 :         112 :     pgerelid = ((Form_pg_propgraph_element) GETSTRUCT(tuple))->pgerelid;
    1793                 :             : 
    1794                 :         112 :     ReleaseSysCache(tuple);
    1795                 :             : 
    1796                 :         112 :     return pgerelid;
    1797                 :             : }
    1798                 :             : 
    1799                 :             : /*
    1800                 :             :  * Get a list of all label OIDs of a graph.
    1801                 :             :  */
    1802                 :             : static List *
    1803                 :         205 : get_graph_label_ids(Oid graphid)
    1804                 :             : {
    1805                 :             :     Relation    rel;
    1806                 :             :     SysScanDesc scan;
    1807                 :             :     ScanKeyData key[1];
    1808                 :             :     HeapTuple   tuple;
    1809                 :         205 :     List       *result = NIL;
    1810                 :             : 
    1811                 :         205 :     rel = table_open(PropgraphLabelRelationId, AccessShareLock);
    1812                 :         205 :     ScanKeyInit(&key[0],
    1813                 :             :                 Anum_pg_propgraph_label_pglpgid,
    1814                 :             :                 BTEqualStrategyNumber,
    1815                 :             :                 F_OIDEQ, ObjectIdGetDatum(graphid));
    1816                 :         205 :     scan = systable_beginscan(rel, PropgraphLabelGraphNameIndexId, true, NULL, 1, key);
    1817         [ +  + ]:         887 :     while (HeapTupleIsValid(tuple = systable_getnext(scan)))
    1818                 :             :     {
    1819                 :         682 :         result = lappend_oid(result, ((Form_pg_propgraph_label) GETSTRUCT(tuple))->oid);
    1820                 :             :     }
    1821                 :         205 :     systable_endscan(scan);
    1822                 :         205 :     table_close(rel, AccessShareLock);
    1823                 :             : 
    1824                 :         205 :     return result;
    1825                 :             : }
    1826                 :             : 
    1827                 :             : /*
    1828                 :             :  * Get a list of all element label OIDs for a label.
    1829                 :             :  */
    1830                 :             : static List *
    1831                 :         698 : get_label_element_label_ids(Oid labelid)
    1832                 :             : {
    1833                 :             :     Relation    rel;
    1834                 :             :     SysScanDesc scan;
    1835                 :             :     ScanKeyData key[1];
    1836                 :             :     HeapTuple   tuple;
    1837                 :         698 :     List       *result = NIL;
    1838                 :             : 
    1839                 :         698 :     rel = table_open(PropgraphElementLabelRelationId, AccessShareLock);
    1840                 :         698 :     ScanKeyInit(&key[0],
    1841                 :             :                 Anum_pg_propgraph_element_label_pgellabelid,
    1842                 :             :                 BTEqualStrategyNumber,
    1843                 :             :                 F_OIDEQ, ObjectIdGetDatum(labelid));
    1844                 :         698 :     scan = systable_beginscan(rel, PropgraphElementLabelLabelIndexId, true, NULL, 1, key);
    1845         [ +  + ]:        1753 :     while (HeapTupleIsValid(tuple = systable_getnext(scan)))
    1846                 :             :     {
    1847                 :        1055 :         result = lappend_oid(result, ((Form_pg_propgraph_element_label) GETSTRUCT(tuple))->oid);
    1848                 :             :     }
    1849                 :         698 :     systable_endscan(scan);
    1850                 :         698 :     table_close(rel, AccessShareLock);
    1851                 :             : 
    1852                 :         698 :     return result;
    1853                 :             : }
    1854                 :             : 
    1855                 :             : /*
    1856                 :             :  * Get the names of properties associated with the given element label OID.
    1857                 :             :  *
    1858                 :             :  * The result is a list of String nodes (so we can use list functions to
    1859                 :             :  * detect differences).
    1860                 :             :  */
    1861                 :             : static List *
    1862                 :         908 : get_element_label_property_names(Oid ellabeloid)
    1863                 :             : {
    1864                 :             :     Relation    rel;
    1865                 :             :     SysScanDesc scan;
    1866                 :             :     ScanKeyData key[1];
    1867                 :             :     HeapTuple   tuple;
    1868                 :         908 :     List       *result = NIL;
    1869                 :             : 
    1870                 :         908 :     rel = table_open(PropgraphLabelPropertyRelationId, AccessShareLock);
    1871                 :             : 
    1872                 :         908 :     ScanKeyInit(&key[0],
    1873                 :             :                 Anum_pg_propgraph_label_property_plpellabelid,
    1874                 :             :                 BTEqualStrategyNumber, F_OIDEQ,
    1875                 :             :                 ObjectIdGetDatum(ellabeloid));
    1876                 :             : 
    1877                 :         908 :     scan = systable_beginscan(rel, PropgraphLabelPropertyLabelPropIndexId, true, NULL, 1, key);
    1878                 :             : 
    1879         [ +  + ]:        2350 :     while ((tuple = systable_getnext(scan)))
    1880                 :             :     {
    1881                 :        1442 :         Form_pg_propgraph_label_property plpform = (Form_pg_propgraph_label_property) GETSTRUCT(tuple);
    1882                 :             : 
    1883                 :        1442 :         result = lappend(result, makeString(get_propgraph_property_name(plpform->plppropid)));
    1884                 :             :     }
    1885                 :             : 
    1886                 :         908 :     systable_endscan(scan);
    1887                 :         908 :     table_close(rel, AccessShareLock);
    1888                 :             : 
    1889                 :         908 :     return result;
    1890                 :             : }
    1891                 :             : 
    1892                 :             : /*
    1893                 :             :  * Get a list of all property OIDs of a graph.
    1894                 :             :  */
    1895                 :             : static List *
    1896                 :          32 : get_graph_property_ids(Oid graphid)
    1897                 :             : {
    1898                 :             :     Relation    rel;
    1899                 :             :     SysScanDesc scan;
    1900                 :             :     ScanKeyData key[1];
    1901                 :             :     HeapTuple   tuple;
    1902                 :          32 :     List       *result = NIL;
    1903                 :             : 
    1904                 :          32 :     rel = table_open(PropgraphPropertyRelationId, AccessShareLock);
    1905                 :          32 :     ScanKeyInit(&key[0],
    1906                 :             :                 Anum_pg_propgraph_property_pgppgid,
    1907                 :             :                 BTEqualStrategyNumber,
    1908                 :             :                 F_OIDEQ, ObjectIdGetDatum(graphid));
    1909                 :          32 :     scan = systable_beginscan(rel, PropgraphPropertyNameIndexId, true, NULL, 1, key);
    1910         [ +  + ]:         332 :     while (HeapTupleIsValid(tuple = systable_getnext(scan)))
    1911                 :             :     {
    1912                 :         300 :         result = lappend_oid(result, ((Form_pg_propgraph_property) GETSTRUCT(tuple))->oid);
    1913                 :             :     }
    1914                 :          32 :     systable_endscan(scan);
    1915                 :          32 :     table_close(rel, AccessShareLock);
    1916                 :             : 
    1917                 :          32 :     return result;
    1918                 :             : }
        

Generated by: LCOV version 2.0-1