sql >> Base de Datos >  >> RDS >> PostgreSQL

El incremento automático de PostgreSQL aumenta en cada actualización

Una secuencia se incrementará cada vez que se intente una inserción, independientemente de su éxito. Una simple update (como en su ejemplo) no lo incrementará sino un insert on conflict update lo hará desde el insert se prueba antes de la update .

Una solución es cambiar el id a bigint . Otra es no usar una secuencia y administrarla usted mismo. Y otra es hacer un upsert manual:

with s as (
    select id
    from notifications
    where title = 'something'
), i as (
    insert into notifications (title, description)
    select 'something', 'whatever'
    where not exists (select 1 from s)
)
update notifications
set title = 'something else'
where id = (select id from s)

Esto supone title es único.