How to make ag_labels() function in AgensGraph

AgensGraph is a Graph DB of Labeled Property Graph model. It has label data for presenting vertices, edges. If you want to see labels list, you should type ‘\dGl’ on AgensGraph console. (article quick-guide-2-1)

how to see labels list on AgensGraph console

Meta Command

  • \dG[+] [PATTERN] list graphs
  • \dGe[+] [PATTERN] list graph edge labels
  • \dGl[+] [PATTERN] list graph labels
  • \dGv[+] [PATTERN] list graph vertex labels
  • \dGi[+] [PATTERN] list graph property indexes

Since #AgensGraph is built on #Postgresql, you can use postgresql extensions like #plpgsql. Let’s make plpgsql function to show labels list.

-- DROP FUNCTION if exists ag_labels();

  RETURNS table (
    la_oid integer, la_name varchar, la_type varchar, la_owner varchar, la_desc text, la_volm varchar
  )AS $$ 
  graph_path text;
  labels record;
  -- get graph_path from user environment
  SELECT setting into graph_path FROM pg_settings WHERE name='graph_path';
  RAISE NOTICE 'graph_path => %', graph_path;
  -- if graph_path does not exists, return null 
  if graph_path is null then 
    return query SELECT WHERE false;
    -- get labels of graph_path                       
    return query
    SELECT l.labid as la_oid,
      cast(l.labname as varchar) as la_name,
    cast(case when l.labkind='e' then 'edge' else 'node' end as varchar) as la_type,
      cast(pg_catalog.pg_get_userbyid(c.relowner) as varchar) as la_owner,
      coalesce(null, pg_catalog.obj_description(l.oid, 'ag_label'), '') as la_desc,
      cast(pg_size_pretty(pg_total_relation_size( l.labname::varchar )) as varchar) as la_volm
    FROM pg_catalog.ag_label l
      INNER JOIN pg_catalog.ag_graph g ON g.oid = l.graphid
      LEFT OUTER JOIN pg_catalog.pg_class c ON c.oid = l.relid
      LEFT OUTER JOIN pg_stat_user_tables u on u.relid = l.relid
    WHERE g.graphname = graph_path and l.labname not in ('ag_vertex', 'ag_edge')
    ORDER BY l.labid;
  end if;
END ; 
$$ LANGUAGE plpgsql;

Then try to execute this.

select * from ag_labels();

Then you will see result like this.

results of ag_labels() function

Have a good day.

댓글 1개

댓글 남기기

아래 항목을 채우거나 오른쪽 아이콘 중 하나를 클릭하여 로그 인 하세요: 로고

WordPress.com의 계정을 사용하여 댓글을 남깁니다. 로그아웃 /  변경 )

Google photo

Google의 계정을 사용하여 댓글을 남깁니다. 로그아웃 /  변경 )

Twitter 사진

Twitter의 계정을 사용하여 댓글을 남깁니다. 로그아웃 /  변경 )

Facebook 사진

Facebook의 계정을 사용하여 댓글을 남깁니다. 로그아웃 /  변경 )

%s에 연결하는 중