pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: ON.KG <skyer@on.kg>
To: pgsql-general@postgresql.org
Subject: duplicate key violates unique constraint
Date: Mon, 13 Jun 2005 20:22:39 +0400
Message-ID: <10813889593.20050613202239@on.kg> (raw)
In-Reply-To: <20050610095655.0215752956@svr1.postgresql.org>
References: <42A95936.3060002@archonet.com>
	<20050610095655.0215752956@svr1.postgresql.org>

Hi All!

I have table:

CREATE TABLE table1 (
   ip char(15) NOT NULL,
   hits integer NOT NULL default '1',
   PRIMARY KEY (ip)
);

So it's counting hits per each IP for current day and every day
trancated by cron:
TRUNCATE TABLE table1;

before inserting or updating this table there're some checkings,
logs, etc., so I'm using PL/PgSQL for that

after all checkings and logs I have:

      UPDATE table1
      SET hits = hits + 1
      WHERE ip = some_ip;

      IF NOT FOUND THEN
         INSERT INTO table1
            (ip)
         VALUES
            (some_ip);
      END IF;

when IP is not found in table it inserts new record into table
but in logs i see error
ERROR:  duplicate key violates unique constraint "table1"
CONTEXT:  PL/pgSQL function "insert_table1" line 68 at SQL statement

But record is inserted into table

what may be the problem?

i also tried before:
      SELECT INTO cnt hits
      FROM table1
      WHERE ip = some_ip;

      IF FOUND THEN
         UPDATE table1
         SET hits = hits + 1
         WHERE ip = some_ip;
      ELSE
         INSERT INTO table1
            (ip)
         VALUES
            (some_ip);
      END IF;

But same error still appears

Thank You




view thread (17+ messages)  latest in thread

Message-ID: <10813889593.20050613202239@on.kg>
Permalink:  ../10813889593.20050613202239@on.kg/
Also on:    postgresql.org/message-id/10813889593.20050613202239@on.kg

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: skyer@on.kg, pgsql-general@postgresql.org
  Subject: Re: duplicate key violates unique constraint
  In-Reply-To: <10813889593.20050613202239@on.kg>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox