码迷,mamicode.com
首页 > 数据库 > 详细

sql脚本函数编写postgresql数据库

时间:2019-09-11 22:21:12      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:too   clock   postgres   timestamp   自增   cloc   create   pgsql   编写   

postgresql创建Long自增函数

CREATE SEQUENCE global_id_sequence;

CREATE OR REPLACE FUNCTION seq_id(OUT result bigint) AS $$
DECLARE
    our_epoch bigint := 1314220021721;
    seq_id bigint;
    now_millis bigint;
    -- the id of this DB shard, must be set for each
    -- schema shard you have - you could pass this as a parameter too
    shard_id int := 1;
BEGIN
    SELECT nextval('global_id_sequence') % 1024 INTO seq_id;

    SELECT FLOOR(EXTRACT(EPOCH FROM clock_timestamp()) * 1000) INTO now_millis;
    result := (now_millis - our_epoch) << 23;
    result := result | (shard_id << 10);
    result := result | (seq_id);
END;
$$ LANGUAGE PLPGSQL;

<!--alter function seq_id(out bigint) owner to postgres;-->

创建sql脚本postgresql

do language plpgsql
$$
begin
update grade set grade = 90 where sno = '20161003';
end
$$

sql脚本函数编写postgresql数据库

标签:too   clock   postgres   timestamp   自增   cloc   create   pgsql   编写   

原文地址:https://www.cnblogs.com/ltspring/p/11508636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!