create table if not exists auto_increase_id
(
id serial,
name varchar(10)
);
-- alter table auto_increase_id
drop function if exists func_trigger_auto_increase_id;
create or replace function func_trigger_auto_increase_id()
returns trigger as
$$
begin
raise notice 'new_id:%',new.id;
-- insert into auto_increase_id values (new.*);
return new;
end;
$$
language plpgsql
volatile;
create trigger trigger_insert_auto_increase_id
before insert
on auto_increase_id
for each row
execute procedure func_trigger_auto_increase_id();
insert into auto_increase_id (name)
values (generate_series(1,10000));
自动分表的逻辑我没有写,主要是想验证下自增的字段能否在触发器函数中获取
执行结果表名是可以做到的
[2020-07-21 09:49:12] [00000] new_id:20252
[2020-07-21 09:49:12] [00000] new_id:20253
[2020-07-21 09:49:12] [00000] new_id:20254
[2020-07-21 09:49:12] [00000] new_id:20255
[2020-07-21 09:49:12] [00000] new_id:20256
[2020-07-21 09:49:12] [00000] new_id:20257
自动创建分区表参考: