url的postgresql唯一非顺序id
我看过一些在Stackoverflow上创建字母数字ID的方法,但是它们都有它们的弱点,一些没有检查冲突,而另一些使用的序列在使用逻辑复制时不是一个好的选择 在谷歌搜索之后,我发现了一个网站,上面有下面的脚本,可以检查冲突,并且不使用序列。但是,这是在表中插入行时作为触发器完成的url的postgresql唯一非顺序id,postgresql,postgresql-11,Postgresql,Postgresql 11,我看过一些在Stackoverflow上创建字母数字ID的方法,但是它们都有它们的弱点,一些没有检查冲突,而另一些使用的序列在使用逻辑复制时不是一个好的选择 在谷歌搜索之后,我发现了一个网站,上面有下面的脚本,可以检查冲突,并且不使用序列。但是,这是在表中插入行时作为触发器完成的 -- Create a trigger function that takes no arguments. -- Trigger functions automatically have OLD, NEW record
-- Create a trigger function that takes no arguments.
-- Trigger functions automatically have OLD, NEW records
-- and TG_TABLE_NAME as well as others.
CREATE OR REPLACE FUNCTION unique_short_id()
RETURNS TRIGGER AS $$
-- Declare the variables we'll be using.
DECLARE
key TEXT;
qry TEXT;
found TEXT;
BEGIN
-- generate the first part of a query as a string with safely
-- escaped table name, using || to concat the parts
qry := 'SELECT id FROM ' || quote_ident(TG_TABLE_NAME) || ' WHERE id=';
-- This loop will probably only run once per call until we've generated
-- millions of ids.
LOOP
-- Generate our string bytes and re-encode as a base64 string.
key := encode(gen_random_bytes(6), 'base64');
-- Base64 encoding contains 2 URL unsafe characters by default.
-- The URL-safe version has these replacements.
key := replace(key, '/', '_'); -- url safe replacement
key := replace(key, '+', '-'); -- url safe replacement
-- Concat the generated key (safely quoted) with the generated query
-- and run it.
-- SELECT id FROM "test" WHERE id='blahblah' INTO found
-- Now "found" will be the duplicated id or NULL.
EXECUTE qry || quote_literal(key) INTO found;
-- Check to see if found is NULL.
-- If we checked to see if found = NULL it would always be FALSE
-- because (NULL = NULL) is always FALSE.
IF found IS NULL THEN
-- If we didn't find a collision then leave the LOOP.
EXIT;
END IF;
-- We haven't EXITed yet, so return to the top of the LOOP
-- and try again.
END LOOP;
-- NEW and OLD are available in TRIGGER PROCEDURES.
-- NEW is the mutated row that will actually be INSERTed.
-- We're replacing id, regardless of what it was before
-- with our key variable.
NEW.id = key;
-- The RECORD returned here is what will actually be INSERTed,
-- or what the next trigger will get if there is one.
RETURN NEW;
END;
$$ language 'plpgsql';
我有一个已经包含数据的表,我添加了一个名为
pid
的新列。是否可以修改此列并使用函数调用作为默认值,以便我以前的所有数据都获得一个短id?假设您有一个表test
:
DROP TABLE IF EXISTS test;
CREATE TABLE test (foo text, bar int);
INSERT INTO test (foo, bar) VALUES ('A', 1), ('B', 2);
您可以向其中添加id
列:
ALTER TABLE test ADD COLUMN id text;
并扣上扳机:
DROP TRIGGER IF EXISTS unique_short_id_on_test ON test;
CREATE TRIGGER unique_short_id_on_test
BEFORE INSERT ON test
FOR EACH ROW EXECUTE PROCEDURE unique_short_id();
现在制作一个临时表,temp
,其结构与test相同(但没有数据):
将测试
倒入温度
:
INSERT INTO temp (foo, bar)
SELECT foo, bar
FROM test
RETURNING *
产生如下结果:
| foo | bar | id |
|------------+-----+----------|
| A | 1 | 9yt9XQwm |
| B | 2 | LCeiA-P8 |
如果其他表在test
表上有外键引用,或者如果test
必须保持联机,
可能无法删除test
并将temp
重命名为test
。
相反,使用temp
中的id
s更新test
更安全
假设test
有一个主键(为了具体起见,我们称之为testid
),那么
您可以使用temp
中的id
s更新test
:
UPDATE test
SET id = temp.id
FROM temp
WHERE test.testid = temp.testid;
然后您可以删除temp
表:
DROP TABLE temp;
您只需使用相同的密钥生成更新现有行。总和是
replace(replace(编码(gen_random_bytes(6),'base64'),'/',''''''.','+','-'))
DROP TABLE temp;