Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i9z9f-0003jt-SW for pgsql-general@arkaria.postgresql.org; Mon, 16 Sep 2019 22:07:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i9z9e-00065N-Pf for pgsql-general@arkaria.postgresql.org; Mon, 16 Sep 2019 22:07:30 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1i9z9d-00065C-K9 for pgsql-general@lists.postgresql.org; Mon, 16 Sep 2019 22:07:30 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i9z9V-0004va-Oi for pgsql-general@postgresql.org; Mon, 16 Sep 2019 22:07:29 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 826B921C47; Mon, 16 Sep 2019 18:07:19 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Mon, 16 Sep 2019 18:07:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=H QA/yPjCeJGG9fpaRgM6on2ZBqbo9LuygD0TR3Y4K2Y=; b=5jNFcrGsVkaL7WJKK 4et8kB/Occ3I4CTEZShoYhRMHtYHpaj9nZr8eCgZlr8Zle5UUn7ANKSq1b9V0p91 Xuby/mhh+BrP9987buGsQNV9F+33E0kEPSaMmS2yyd5BPyEbrvxrEGcKvrZwOpYt 9wVZY6+VEpPtnZiO0ymR/xgxwlIespfiIUfK1iIMJmfZ3oqAsFrIjQDW9zbR6wjj ZkFMsULOb0QMfy2sN5FBCIzvBIZ1mDzBrGXqXoLplFxGT0+/J8twNiMZc28zZ8lJ PGZar3BlVVXujPeUAe4m2UsE6pQvR/wxHG04G6OLpAtm0qnBn17Lgb9cBe/i1Ee0 zJ6JQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=HQA/yPjCeJGG9fpaRgM6on2ZBqbo9LuygD0TR3Y4K 2Y=; b=EAJIuP7IWl/w/iU6/QPg5ijLLsTakX4eZZLIKu/kxkp6rfC/UUWrtKuPr b4qgHXzYIhYVi75J0hsLD7jdPnF4/JLAWk4EyI/35nuHjP5phNeXxvjZ5AZeHQqO JntJ+hdascHXEvKpOW44QFGlBPOYsDrHcnn1l7FmZY/i9zsLCQ3509ifUyrx3p3k 8U+j4UiMeSWWeuIm2sDDtGqIzd7cls3IOIYFlCSzq9spXdCIgQZQCTMaytP94kIl XCBlZV0eBzmwf2dLctEvJ8es3yLaNPuZ5WWsorOz8pGenUAkO56b3WQX78K8qQXA fltjWMdP6cyEmQHhcdSl9fXqKdBGA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrudeggddtiecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuffhomhgrihhnpehophgvnhhvihhsthgrshdrnhgvthdpphhoshhtghhrvghsqhhl rdhorhhgnecukfhppeehtddrudeljedrkedtrddvnecurfgrrhgrmhepmhgrihhlfhhroh hmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmnecuvehluhhsthgv rhfuihiivgeptd X-ME-Proxy: Received: from ranger.suse (50-197-80-2-static.hfc.comcastbusiness.net [50.197.80.2]) by mail.messagingengine.com (Postfix) with ESMTPA id C22DBD60065; Mon, 16 Sep 2019 18:07:18 -0400 (EDT) Subject: Re: Dubugging an intermittent foreign key insert error with csvlog To: Jeff Ross , PostgreSQL References: From: Adrian Klaver Message-ID: <750a7274-384c-b52f-1b9f-d1c13bfe112b@aklaver.com> Date: Mon, 16 Sep 2019 15:07:17 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 9/16/19 1:46 PM, Jeff Ross wrote: > Hi all, > > I've been debugging an intermittent foreign key insert error on our > single database / multi-tenant server.  To help isolate traffic by > tenant, I've switched to using csvlog and for the duration and have set > log_min_duration_statement to 0 to get *everything*.  Fortunately, daily > 80G csvlogs compress nicely. > > For lack of a readable way to paste in the 12 lines of relevant csvlog > into an e-mail, I've uploaded a very small 3K csv file to my web server at > >     https://openvistas.net/hansens_error.csv > > The bare bones of the issue involve inserting a row into a table named > load_det, then getting the serial sequence of that table, getting the > last_value of that sequence and then inserting into another table named > cargo_det using that retrieved last_value as the foreign key that ties > this row to the load_det table.  The vast majority of these succeed > without issue but not all. > > The csvlog snip shows what I believe are 2 simultaneous but separate > sessions and the session that attempts to insert into the cargo_det > table is not the same session that inserted into the load_det table. > That's what my hunch is but what is unclear to me is if those separate > sessions are also in separate transactions. To me it looks like the INSERT into load_det and into cargo_det are occurring in the same transaction(934281062). The part that would concern me is that: select last_value from load_det_id_seq occurs in different transactions and sessions. From here: https://www.postgresql.org/docs/11/sql-createsequence.html "Also, last_value will reflect the latest value reserved by any session, whether or not it has yet been returned by nextval." Especially as the error is coming from a different transaction(934281063) and session then the INSERTs. I'm guessing that there is cross talk on the sequence number fetch and application to cargo_det. > > csvlog has a couple of columns that I'm unclear about: > session_start_time, virtual_transaction_id.  Is session_start_time the > time inside a transaction block, as in beginning with a begin but before > a commit or rollback?  Or is it maybe just how long this pgbouncer > session has been connected?  virtual_transaction_id is defined in the > docs as backendID/localXID--do separate backendIDs also represent > separate transactions?  Is there a better way to determine separate > transactions within csvlog? > > Also, the app code that does this is legacy perl using DBD::Pg but the > original code was written for Informix.  We've been in the process of > moving off informix for a while now and should be done within the month. >  I intend to re-work this to use returning id (available in postgres > since 8.2!) instead of the serial sequence / last_value hack but not > quite there yet. > > Thanks, > > Jeff Ross > > -- Adrian Klaver adrian.klaver@aklaver.com