Postgres:了解主键序列

问题描述 投票:0回答:1

我的数据库不同步,这使我想到了这个问题:How to reset postgres' primary key sequence when it falls out of sync?(在下面复制)

但是,我在这里不太明白:'your_table_id_seq'是什么?我不知道在哪里找到这个。进行挖掘时,我发现了一个名为pg_sequence的表在pg_catalog中。与此有关吗?不过,我看不到任何将数据关联回表的方法。

-- Login to psql and run the following

-- What is the result?
SELECT MAX(id) FROM your_table;

-- Then run...
-- This should be higher than the last result.
SELECT nextval('your_table_id_seq');

-- If it's not higher... run this set the sequence last to your highest id. 
-- (wise to run a quick pg_dump first...)

BEGIN;
-- protect against concurrent inserts while you update the counter
LOCK TABLE your_table IN EXCLUSIVE MODE;
-- Update the sequence
SELECT setval('your_table_id_seq', COALESCE((SELECT MAX(id)+1 FROM your_table), 1), false);
COMMIT;
postgresql primary-key
1个回答
0
投票

以下查询给出所有序列的名称。

SELECT c.relname 
FROM pg_class c 
WHERE c.relkind = 'S';

通常将序列命名为$ {table} _id_seq。

我找到了这个问题的答案:List all sequences in a Postgres db 8.1 with SQL

© www.soinside.com 2019 - 2024. All rights reserved.