Instragram的UUID创建失败
我想实现的Instagram的UUID,他们在这篇文章中谈到:http://instagram-engineering.tumblr.com/post/10853187575/sharding-ids-at-instagramInstragram的UUID创建失败
我的实现看起来是这样的:
CREATE OR REPLACE FUNCTION engagement.next_id(OUT result bigint) AS $$ DECLARE
our_epoch bigint := 1314220021721;
seq_id bigint;
now_millis bigint;
shard_id int := 5;
BEGIN
SELECT nextval('engagement.table_id_seq') %% 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;
但我不断收到此错误:
Warning: pg_execute(): Query failed: ERROR: relation "engagement.table_id_seq" does not exist LINE 1: SELECT nextval('engagement.table_id_seq') %% 1024^QUERY: SELECT nextval('engagement.table_id_seq') %% 1024 CONTEXT: PL/pgSQL function next_id() line 8
我实际上是否想创建一个名为table_id_seq或不同的表?
回答:
只需创建序列
create sequence engagement.table_id_seq
如果你不具备的Instagram的64位UUID尺寸限制,你可以使用更简单的PostgreSQL的uuid_generate_v1mc()
。 postgresql类型是128位长。
create table t (id serial, uid uuid); insert into t (uid)
select uuid_generate_v1mc()
from generate_series(1, 100000);
它是由创建时间排序:
select * from (
select
*,
row_number() over(order by uid) rn
from t
order by id
) s
where id != rn;
回答:
我给了一个完整的写上去,实现域的,和辅助功能,在你上面举的博客文章扩大。这应该可以帮助你实现Instagram模式。 I highly suggest checking it out over at Database Administrators.
以上是 Instragram的UUID创建失败 的全部内容, 来源链接: utcz.com/qa/258013.html