     drop table if exists pagerank;
     drop table if exists pagerank_tmp;
     drop table if exists out_degree;
+    drop table if exists trans;
 
     -- init pagerank
     -- first insert all nodes into a tmp table allow duplicate, then clean up

CREATE OR REPLACE FUNCTION pagerank() RETURNS VOID AS
$$
DECLARE
    currentIndex integer;
    diff real;
    num_of_nodes integer;
    damper real;
    new_rank real;
    old_rank real;
    src_rank real;
    src_degree integer;
    nid integer;
    sid integer;

    create table pagerank(node_id int primary key, rank real);    
    insert into pagerank_tmp(node_id, rank) select src_id, 1.0 from edge;
    insert into pagerank_tmp(node_id, rank) select dst_id, 1.0 from edge;
    select count(*) into num_of_nodes from pagerank_tmp group by node_id;
    insert into pagerank(node_id, rank) select node_id, 1.0/num_of_nodes from pagerank_tmp group by node_id;
    delete from pagerank_tmp;
    insert into pagerank_tmp(node_id, rank) select node_id,rank from pagerank;

