CentOS 6.2 64bit Server PostgreSQL 8.4.9 installed via yum
I am seeing errors from time to time in PostgreSQL's logs about a table called events. The events table is used to stores information generated by triggers in Zabbix everytime a specific event occurs on a server that is being monitored.
Zabbix is the only application using the database server.
Here is the error:
INSTRUCTION : insert into events (eventid,source,object,objectid,clock,ns,value,value_changed) values (6393428,2,3,38,1353354000,0,1,0)
ERROR: duplicate key violates unique constraint « events_pkey »
There is no sequence associated with the table events
Here is the table structure for events
Table « public.events »
Colonne | Type | Modificateurs | Stockage | Description
---------------+---------+----------------------------------+----------+-------------
eventid | bigint | non NULL | plain |
source | integer | non NULL Par défaut, 0 | plain |
object | integer | non NULL Par défaut, 0 | plain |
objectid | bigint | non NULL Par défaut, (0)::bigint | plain |
clock | integer | non NULL Par défaut, 0 | plain |
value | integer | non NULL Par défaut, 0 | plain |
acknowledged | integer | non NULL Par défaut, 0 | plain |
ns | integer | non NULL Par défaut, 0 | plain |
value_changed | integer | non NULL Par défaut, 0 | plain |
Index :
"events_pkey" PRIMARY KEY, btree (eventid)
"events_1" btree (object, objectid, eventid)
"events_2" btree (clock)
Référencé par :
TABLE "acknowledges" CONSTRAINT "c_acknowledges_2" FOREIGN KEY (eventid) REFERENCES events(eventid) ON DELETE CASCADE
TABLE "alerts" CONSTRAINT "c_alerts_2" FOREIGN KEY (eventid) REFERENCES events(eventid) ON DELETE CASCADE
Question
What steps can I take to resolve this error?
Thank you
Updated 11/26
I tried to run the ALTER TABLE ALTER COLUMN statement that dezso sent me, but I get an error about the bigserial type.
zabbix=# ALTER TABLE events ALTER COLUMN eventid TYPE bigserial;
ERREUR: le type « bigserial » n'existe pas
I can however create a new table with that data type
zabbix=# \connect postgres
psql (8.4.13)
Vous êtes maintenant connecté à la base de données « postgres ».
postgres=# CREATE TABLE test(x bigserial);
NOTICE: CREATE TABLE créera des séquences implicites « test_x_seq » pour la colonne serial « test.x »
CREATE TABLE
postgres=# \ds
Liste des relations
Schéma | Nom | Type | Propriétaire
--------+------------+----------+--------------
public | test_x_seq | séquence | postgres
(1 ligne)
How should I go about changing eventid to bigserial?
Update 11/27
I have tried another method to add the sequence to the column eventid
create sequence eventid_seq;
alter table events alter column eventid set default nextval('eventid_seq');
select (max(eventid) +1) as nexteventid from events;
SELECT setval('eventid_seq', 6645754);
That worked, but when I turned Zabbix back on, the eventid value continued to increase but the sequence didn't. What am I doing incorrectly?
SELECT * FROM events WHERE eventid = 6393428
return? I'm sure it will return exactly one row, which will show that somehow theeventid
generation gives back the wrong id. Generally it is wiser to leave this to a sequence. Another much used but quite bad option is to getmax(eventid) + 1
. In this latter case you have to be sure that betweeneventid
generation and the insert no other inserts will succeed.max(eventid) + 1
is a horrible idea to generate primary keys.ALTER TABLE events ALTER COLUMN eventid TYPE bigserial;
After that you have to change your application code to either completely omiteventid
field from the insert or get anextval()
before. And you must set the starting value of the resulting sequence appropriately.