Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lHTLs-0007LG-1M for pgsql-general@arkaria.postgresql.org; Wed, 03 Mar 2021 15:23:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lHTLq-0006Ri-VJ for pgsql-general@arkaria.postgresql.org; Wed, 03 Mar 2021 15:23:50 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lHTLq-0006RL-NR for pgsql-general@lists.postgresql.org; Wed, 03 Mar 2021 15:23:50 +0000 Received: from portal-smtp-out.hot.ee ([194.126.101.125]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lHTLn-0002Cq-HY for pgsql-general@postgresql.org; Wed, 03 Mar 2021 15:23:49 +0000 Received: from localhost (localhost [127.0.0.1]) by vm-hot-relay10.estpak.ee (Postfix) with ESMTP id 7756116C4 for ; Wed, 3 Mar 2021 17:23:43 +0200 (EET) X-Virus-Scanned: Debian amavisd-new at vm-hot-relay10.estpak.ee Received: from portal-smtp-out.hot.ee ([127.0.0.1]) by localhost (vm-hot-relay10.estpak.ee [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id XhSj-2IA+ZQ3 for ; Wed, 3 Mar 2021 17:23:41 +0200 (EET) Received: from mail.hot.ee (hot-relayhost1.estpak.ee [213.180.2.79]) by vm-hot-relay10.estpak.ee (Postfix) with ESMTP id 336D21800 for ; Wed, 3 Mar 2021 17:23:41 +0200 (EET) To: pgsql-general@postgresql.org From: Andrus Subject: Duplicate key error Message-ID: Date: Wed, 3 Mar 2021 17:23:35 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.8.0 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="------------CCCED139404092781967E516" Content-Language: et List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------CCCED139404092781967E516 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Hi! Sometimes duplicate key error     duplicate key value violates unique constraint "session_pkey"     Key (workplace)=(WIN-N9BSKUNKBC8 ) already exists. occurs in script:     delete from session where workplace='WIN-N9BSKUNKBC8' ;     INSERT INTO session (workplace,ipaddress,logintime,loggeduser)     SELECT 'WIN-N9BSKUNKBC8' , inet_client_addr()::CHAR(14),current_timestamp::CHAR(28),CURRENT_USER WHERE NOT EXISTS (SELECT 1 FROM session WHERE workplace='WIN-N9BSKUNKBC8' ) Sript is running form windows task scheduler on every 10 minutes. Error occurs only sometimes. How to fix this ? Table is defined as     CREATE TABLE public.session     (         loggeduser character(10) ,         workplace character(16) NOT NULL,         ipaddress character(20) ,         logintime character(28) ,         activity timestamp with time zone,         CONSTRAINT session_pkey PRIMARY KEY (workplace)     ) Environment:     PostgreSQL 13.1, compiled by Visual C++ build 1914, 64-bit     Windows server 2019     psqlODBC driver 13.00.0000 I already asked this in this list many years ago. In this case Tom replies that is looks like index race condition bug which was fixed in later Postgres 9 version. However this issue still exists in Postgres 13.1 Andrus. --------------CCCED139404092781967E516 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

Hi!

Sometimes duplicate key error

    duplicate key value violates unique constraint "session_pkey"
    Key (workplace)=(WIN-N9BSKUNKBC8 ) already exists.

occurs in script:

    delete from session where workplace='WIN-N9BSKUNKBC8' ;
    INSERT INTO session (workplace,ipaddress,logintime,loggeduser)
    SELECT 'WIN-N9BSKUNKBC8' , inet_client_addr()::CHAR(14),current_timestamp::CHAR(28),CURRENT_USER WHERE NOT EXISTS (SELECT 1 FROM session WHERE workplace='WIN-N9BSKUNKBC8' )

Sript is running form windows task scheduler on every 10 minutes. Error occurs only sometimes.

How to fix this ?
Table is defined as

    CREATE TABLE public.session
    (
        loggeduser character(10) ,
        workplace character(16) NOT NULL,
        ipaddress character(20) ,
        logintime character(28) ,
        activity timestamp with time zone,
        CONSTRAINT session_pkey PRIMARY KEY (workplace)
    )


Environment:

    PostgreSQL 13.1, compiled by Visual C++ build 1914, 64-bit
    Windows server 2019
    psqlODBC driver 13.00.0000


I already asked this in this list many years ago. In this case Tom replies that is looks like index race condition bug which was fixed in later Postgres 9 version.

However this issue still exists in Postgres 13.1

Andrus.

--------------CCCED139404092781967E516--