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 1g59yc-0006k7-6m for pgsql-general@arkaria.postgresql.org; Wed, 26 Sep 2018 13:35:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g59ya-0004r8-LM for pgsql-general@arkaria.postgresql.org; Wed, 26 Sep 2018 13:35:36 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g59yZ-0004qv-E5 for pgsql-general@lists.postgresql.org; Wed, 26 Sep 2018 13:35:36 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g59yV-0001HB-3S for pgsql-general@postgresql.org; Wed, 26 Sep 2018 13:35:33 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 5AB2021D45; Wed, 26 Sep 2018 09:35:28 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Wed, 26 Sep 2018 09:35:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; bh=lshBlARqzaqAY6QPvMjlO9SH0TimF 20keRYPNVGXDYg=; b=aqJBUqNbVReIsdRkTJiDAOyDdK8orNyX8XmJi69bQBFjh 4wSJ4vN3oOv3XyBytYR5RmdxPTmXswDpvE3k4G5jGiOTjJuU0oWoOIfzzqEd3h6+ 2XU8gMW7Rd4EBGoxqTBr2jHtKdzkCpMDQE6mFiMOufKahKwTc3Pp6wzPjOzuzN6l eTmLxxqAAIVM/MipyZr9IoMBoTS5FJISnboZSAnw1W1LZsdh83ZFapxQRpg3O0N9 PSJ7xTsdtNMxJpGGKDBBuhwpj0pg+86934gPpoEIJZjlJ09AxG62Pr1HQA1MFzZo vECw000YGEGiAbxMMs2x6H50JVyAR2FBbHCDd4bKA== 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-sender:x-me-sender:x-sasl-enc; s=fm3; bh=lshBlA RqzaqAY6QPvMjlO9SH0TimF20keRYPNVGXDYg=; b=O+6fPbxw/C89/tKZY2fg2+ vp8jy6gCpEcCI8/XFcWz2V5r9+N8O4zVKSfERgEOeXx90IJLgPb0Zx2BgwVp1WpP KJo7s5EmUTpRxWEY9ftvqCFR9XGjBjgOJpvmO5cy+5rp95VY6okv2nGrEobAeg3V FBK/CWnIQWfjnZ06R4NEHdQ4UJi+I9F4oARKaLRTfEzg1WN78xoR6VlA5vKImlCe W5kOuZeci1KOISkjTuIQnlW6783ZVeD09SsZ2s7jbYbVlx+xQAxDluRajECUjdRB MXucbNieNdaYgXyyNXFENv6r7Czz+Cnoel0DTaq9d910/FMiFevj6hi8xsNRCc6g == X-ME-Proxy: X-ME-Sender: Received: from [192.168.1.3] (unknown [71.212.134.3]) by mail.messagingengine.com (Postfix) with ESMTPA id B9273E49C5; Wed, 26 Sep 2018 09:35:27 -0400 (EDT) Subject: Re: using the nextval('sequence_name') in sql, the result maybe is not right To: Wanglin , pgsql-general References: <58540bf0.1474c.16615c4ec95.Coremail.jluwln@163.com> From: Adrian Klaver Message-ID: Date: Wed, 26 Sep 2018 06:35:26 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.0 MIME-Version: 1.0 In-Reply-To: <58540bf0.1474c.16615c4ec95.Coremail.jluwln@163.com> 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/26/18 5:05 AM, Wanglin wrote: > Hi, all: >     PostgreSQL version : 10.3.  I use "nextval" in the sql , but I > think the result is not right, maybe it is a bug. > *The test case as bellow:* > create sequence seq1; > select nextval('seq1'); > create table tx1(id1 int, id2 int); > insert into tx1 select generate_series(1,100), random()*102; > explain verbose select * from tx1 where id2 = nextval('seq1');; > select * from tx1 where id2 = nextval('seq1'); > postgres=# explain verbose select * from tx1 where id2 = > nextval('seq1');; QUERY PLAN > ------------------------------------------------------------ Seq Scan on > public.tx1 (cost=0.00..43.90 rows=11 width=8) Output: id1, id2 Filter: > (tx1.id2 = nextval('seq1'::regclass)) (3 rows) > > postgres=# select * from tx1 where id2 = nextval('seq1'); *-- here, > **may be the result is not right* id1 | id2 -----+----- 56 | 57 (1 row) > > :: I think "nextval('seq1')" equal 2, so "select * from tx1 where id2 = > nextval('seq1')" equals "select * from tx1 where id2 = 2", is it ? As Alban pointed out calling nextval() increments the sequence. As your EXPLAIN shows Postgres your SELECT is doing a sequence scan. Using your test code here I get: select * from tx1; id1 | id2 -----+----- 1 | 27 2 | 42 3 | 93 4 | 2 5 | 85 So going in sequence Postgres is going to compare 27 to nextval()(which is 2), not find it move to 42 = nextval()(=3) not find it and so on. If I do: select * from tx1 order by id2; I get: id1 | id2 -----+----- 20 | 0 2 | 42 17 | 43 63 | 45 88 | 45 27 | 46 52 | 47 47 | 47 alter sequence seq1 restart; select * from tx1 where id2 = nextval('seq1') order by id2; id1 | id2 -----+----- 47 | 47 The sequence catches up with the values because there are duplicate 47 values in id2. > > Thanks, >     Wanglin > > > -- Adrian Klaver adrian.klaver@aklaver.com