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 1gz11w-00060q-HX for pgsql-admin@arkaria.postgresql.org; Wed, 27 Feb 2019 15:21:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gz11v-0001FM-9u for pgsql-admin@arkaria.postgresql.org; Wed, 27 Feb 2019 15:21:55 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gz11u-0001D4-UD for pgsql-admin@lists.postgresql.org; Wed, 27 Feb 2019 15:21:55 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gz11s-0006B6-9z for pgsql-admin@lists.postgresql.org; Wed, 27 Feb 2019 15:21:53 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x1RFLnTO014555; Wed, 27 Feb 2019 10:21:49 -0500 From: Tom Lane To: Achilleas Mantzios cc: pgsql-admin@lists.postgresql.org Subject: Re: Index ignored on pkid = curval('some_seq'), used with pkid = (select curval(''some_seq') ) In-reply-to: <0ee1a3a2-7394-d518-2e22-790a84ef558c@matrix.gatewaynet.com> References: <0ee1a3a2-7394-d518-2e22-790a84ef558c@matrix.gatewaynet.com> Comments: In-reply-to Achilleas Mantzios message dated "Wed, 27 Feb 2019 10:54:11 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <14553.1551280909.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 27 Feb 2019 10:21:49 -0500 Message-ID: <14554.1551280909@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Achilleas Mantzios writes: > dynacom=3D# explain analyze update itemshist set reason=3D'{foo,bar}' wh= ere pkid =3D currval(('public.itemshist_pkid_seq'::text)::regclass); currval() is marked volatile, so that's not a legal index qualification. (Perhaps there's an argument that it'd be more useful to consider it stable, but certainly if you used it in the same query as a nextval() on the same sequence, you'd have trouble.) > -- but if I compare against select currval it uses the index: > dynacom=3D# explain analyze update itemshist set reason=3D'{foo,bar}' wh= ere pkid =3D ( SELECT currval(('public.itemshist_pkid_seq'::text)::regclas= s)); Yeah, the planner does not consider uncorrelated scalar sub-selects to be volatile; they'll be evaluated only once per query, regardless of what they contain. So this is sort of a traditional hack for freezing a volatile function's result. (I have no idea whether other RDBMSes read the SQL spec the same way on this point.) regards, tom lane