Postgresql 插入前的触发器按2递增序列

Postgresql 插入前的触发器按2递增序列,postgresql,Postgresql,我正在使用PostgreSQL 9.6。我有以下顺序和表格: CREATE SEQUENCE measure\u id\u seq 增量1 最小值1 最大值9223372036854775807 开始1 缓存1; 创建表度量( id integer非空默认值nextval('measure_id_seq'::regclass), 时区不为空的time_stp时间戳, val双精度不为空, 污染物字符变化(255)不为空, 桩号字符变化(255)不为空, 约束度量_pkey主键(id) ) 与(

我正在使用PostgreSQL 9.6。我有以下顺序和表格:

CREATE SEQUENCE measure\u id\u seq
增量1
最小值1
最大值9223372036854775807
开始1
缓存1;
创建表度量(
id integer非空默认值nextval('measure_id_seq'::regclass),
时区不为空的time_stp时间戳,
val双精度不为空,
污染物字符变化(255)不为空,
桩号字符变化(255)不为空,
约束度量_pkey主键(id)
)
与(
OID=错误
);
“测量”表根据“污染物”和“站点”列的值划分为不同的表

为此,我有一个触发器,在将新数据插入表之前执行:

CREATE OR REPLACE FUNCTION before_insert_on_measure()
RETURNS TRIGGER LANGUAGE plpgsql as $$

DECLARE format_station TEXT := lower(replace(new.station, ' ', ''));
DECLARE format_pollutant TEXT := lower(replace(new.pollutant, ' ', ''));
BEGIN
    EXECUTE format(
        $f$
            CREATE TABLE IF NOT EXISTS %I (
            check (station = %L AND pollutant = %L)
            ) INHERITS (measure)
        $f$, 
        concat('partition_measure_', format_station, '_', format_pollutant), 
        new.station,
        new.pollutant);
    EXECUTE format(
        $f$
            INSERT INTO %I (time_stp, val, pollutant, station)
            VALUES (%L, %L, %L, %L)
        $f$, 
        concat('partition_measure_', format_station, '_', format_pollutant), 
        new.time_stp,
        new.val,
        new.pollutant,
        new.station
        );
    RETURN NULL;
END $$;

CREATE TRIGGER before_insert_on_measure
    BEFORE INSERT ON measure
    FOR EACH ROW EXECUTE PROCEDURE before_insert_on_measure();
将新数据插入表“measures”将创建正确的子表。例如,当我插入以下数据时:

INSERT INTO measure (time_stp, val, pollutant, station)
    VALUES ('2015-01-01 01:00:00', 32.4, 'O3', 'Station 1');
INSERT INTO measure (time_stp, val, pollutant, station)
    VALUES ('2016-01-01 01:00:00', 54, 'NO2', 'Station 2');
它创建以下表格:

  • 分区测量站1
  • 分区测量站2号
我的问题是我的“id”列增加了2

id time_stp                 val  pollutant station
2  "2015-01-01 01:00:00+04" 32.4 "O3"      "Station 1"
4  "2016-01-01 01:00:00+04" 54   "NO2"     "Station 2"
相反,我希望:

id ...
1 ...
2 ...
似乎每个插入都会执行两次nextval(“度量值id”)

有什么建议吗

编辑:JGH提供的答案是正确的。以下是已编辑的触发器,以供参考:

CREATE OR REPLACE FUNCTION before_insert_on_measure()
RETURNS TRIGGER LANGUAGE plpgsql as $$

DECLARE format_station TEXT := lower(replace(new.station, ' ', ''));
DECLARE format_pollutant TEXT := lower(replace(new.pollutant, ' ', ''));
BEGIN
    EXECUTE format(
        $f$
            CREATE TABLE IF NOT EXISTS %I (
            check (station = %L AND pollutant = %L)
            ) INHERITS (measure)
        $f$, 
        concat('partition_measure_', format_station, '_', format_pollutant), 
        new.station,
        new.pollutant);
    EXECUTE format(
        $f$
            INSERT INTO %I
            VALUES (%L, %L, %L, %L, %L)
        $f$, 
        concat('partition_measure_', format_station, '_', format_pollutant), 
        new.id,
        new.time_stp,
        new.val,
        new.pollutant,
        new.station
        );
    RETURN NULL;
END $$;

CREATE TRIGGER before_insert_on_measure
    BEFORE INSERT ON measure
    FOR EACH ROW EXECUTE PROCEDURE before_insert_on_measure();

在触发器中,对分区表执行insert语句时,必须传递new.id值。如果没有,则使用默认值,即对序列生成器的第二次调用