Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gAhUY-0003ur-4q for pgsql-general@arkaria.postgresql.org; Thu, 11 Oct 2018 20:23:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gAhUW-0000mF-8j for pgsql-general@arkaria.postgresql.org; Thu, 11 Oct 2018 20:23:28 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gAhUV-0000m8-VA for pgsql-general@lists.postgresql.org; Thu, 11 Oct 2018 20:23:28 +0000 Received: from mail-io1-xd43.google.com ([2607:f8b0:4864:20::d43]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gAhUT-00077V-Tz for pgsql-general@lists.postgresql.org; Thu, 11 Oct 2018 20:23:27 +0000 Received: by mail-io1-xd43.google.com with SMTP id l25-v6so7627167ioj.0 for ; Thu, 11 Oct 2018 13:23:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=nNz+AR5w3ufvViZfY9iRcoWiXgZgU/S17YWgoLPPev4=; b=ld37ihgoGuESryTZZTtpoVF0bzZcp6WWU5eJ3s+F9DkZltopyfw5JonS8Ru2yEz/gv mq9/CcidkL5aOOxioq3MRqqIVYB/T2u4HmsGYUC5dq0VlyTR+yHQLB/d/JdxYm/UEBqi yjauEv90/B+D4OGgAvgDFkxcevbUcNjsrKTl7uEEYTzzbzUz6lGpISRgyf1XkKoBuyUy 7hkEH98wgeEnBQyDQE6D5rETFXyV6RhEI0ahv+RBXsSG7NLeFBlZI4vflhyebXum9QKJ xtF07U+mx4TPVmfAbgeT9SDX+ilAz0sIw6PLL+MzfB9rScxvdRG5vlIfP+cTIKIrVhG4 zvRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=nNz+AR5w3ufvViZfY9iRcoWiXgZgU/S17YWgoLPPev4=; b=tviHJv4l6K1lzu46BwnFvpX/R4bxy2c2XdzJRmCeXsbeM4GrP7FcTG+NPMsud1KKZL DMHhLFDFJqbnI5yMfK07EzPnFPOOyjc+cU2RXaQyYARYjgc993DOP8JmUPSrEOlYbOU3 Jfrp4QJYyvGV9+VMoibL7ZGOQ51JhJsKiYFtNJ5vfBtrbWJymO7h5ZpAFjLIw+SlejQp qkFiGIrwRUU8Gesvk2kJDgv/epDNhMTtnUtqerOjykHL942fVxq1+cFRabkLLMxJZaQy hQqQ4TCcaph/0BmE4JbLh6ZLweO/YAXUcCvXFbieARGviljYHR/FRnstjqjTBm5rD27W T+Ng== X-Gm-Message-State: ABuFfohus505htG25kuBaU0RSM/YXW+1Z+pdmPw/Hnfs9XLZtGGlnijv XZZwK36ScrY06nbTueZvzp+kl3l3 X-Google-Smtp-Source: ACcGV60A0RTujG25TdVjGWFoZYb0nCddCk4VKlRz8e4St1X0vm3T7TsLvVT8RAu1E0uLZiwXa0LXVA== X-Received: by 2002:a5e:c206:: with SMTP id v6-v6mr2148801iop.302.1539289403597; Thu, 11 Oct 2018 13:23:23 -0700 (PDT) Received: from [192.168.1.10] (ip70-171-116-89.no.no.cox.net. [70.171.116.89]) by smtp.googlemail.com with ESMTPSA id e78-v6sm10759928itc.4.2018.10.11.13.23.22 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Oct 2018 13:23:22 -0700 (PDT) Subject: Re: something weird happened - can select by column value although column value exist To: pgsql-general@lists.postgresql.org References: From: Ron Message-ID: <0e111ff0-3a39-f22b-f823-1934e9db0732@gmail.com> Date: Thu, 11 Oct 2018 15:23:21 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 10/11/2018 03:17 PM, Dmitry O Litvintsev wrote: > Hi, > > Today the following happened: > > Found this error in my production log: > > < 2018-10-11 13:31:52.587 CDT >ERROR: insert or update on table "file" violates foreign key constraint "$1" > < 2018-10-11 13:31:52.587 CDT >DETAIL: Key (volume)=(155303) is not present in table "volume". > < 2018-10-11 13:31:52.587 CDT >STATEMENT: > INSERT INTO file (sanity_crc,uid,pnfs_id,crc,deleted,cache_mod_time,drive,volume,sanity_size,cache_status,gid,location_cookie,cache_location,original_library,bfid,pnfs_path,size) VALUES ( > 4002229874,0,'',256655919,'u','2018-10-11 13:31:52','',(SELECT id FROM volume where label='A'),65536,'B',0,'C','D','E','F','',197243) RETURNING * > > file table references volume table on file.volume = volume.id and file.volume is FK to volume.id. I doctored the query for privacy replacing string values with 'A', 'B'. ... > > (Queries similar to the above quoted are executed by an application and run thousand of times every day for years) > > So, the problem: > > SELECT id FROM volume where label='A'; > id > -------- > 155303 > (1 row) > > BUT: > > select * from volume where id = 155303; > ... > (0 rows) > > ?! > > id is a sequence: > > id | integer | not null default nextval(('volume_seq'::text)::regclass) > > > This entry id = 155303 has existed for some time and has a lot of existing file entries holding > FK reference to volume id = 155303 > > I "fixed" the issue just by: > > update volume set id = 155303 where label='A'; > > BUT It did not work right away. Meaning I did this once: > > update volume set id = 155303 where label='A'; > > no effect. > > I did it again, I also did it; > > update volume set id = (select id from volume where label='A'); > > and then again > > update volume set id = 155303 where label='A'; > > eventually it worked. Now, > > > select count(*) from volume where label='A'; > count > ------- > 1 > (1 row) > > > What is this? Version 9.3.9,. running on Linux RH6. Index corruption?  Maybe rebuild the FK. -- Angular momentum makes the world go 'round.