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 1hSASc-0006cb-5e for pgsql-sql@arkaria.postgresql.org; Sun, 19 May 2019 01:17:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hSASY-00059T-6U for pgsql-sql@arkaria.postgresql.org; Sun, 19 May 2019 01:17:54 +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 1hSASX-00059M-Ly for pgsql-sql@lists.postgresql.org; Sun, 19 May 2019 01:17:53 +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 1hSASV-0001ZY-1K for pgsql-sql@postgresql.org; Sun, 19 May 2019 01:17:52 +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 x4J1HZVE029957; Sat, 18 May 2019 21:17:35 -0400 From: Tom Lane To: Larry Rosenman cc: pgsql-sql@postgresql.org Subject: Re: create index on a jsonb timestamp field? In-reply-to: <78a5d1232bb5ef5797a8ae6e1f23543f@lerctr.org> References: <78a5d1232bb5ef5797a8ae6e1f23543f@lerctr.org> Comments: In-reply-to Larry Rosenman message dated "Sat, 18 May 2019 17:53:35 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <29955.1558228655.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sat, 18 May 2019 21:17:35 -0400 Message-ID: <29956.1558228655@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Larry Rosenman writes: > when I try to create an index on the query_time field of the json = > structure I get: > ler=3D# create index dns_query_time_idx on dns_query(((data -> 'message'= = > ->> 'query_time')::text::timestamptz)); > ERROR: functions in index expression must be marked IMMUTABLE Yeah, because the timestamptz input function has dependencies on both the datestyle and timezone GUCs. Given that your input is ISO-format with explicit time zone, you don't really care about either of those things, but the mutability check doesn't know that. > Is there any easy way to do this? Or, what would the experts recommend = > here? The sanest way to deal with this IMO is to make a column containing the extracted timestamp, which you could maintain with a trigger, and then index that. You could alternatively make a custom function that you (mis?)label as immutable, but your queries would have to use that same function in order to get matched to the index, so I dunno about that being a user-friendly approach. BTW, I'd had the idea that the GENERATED option in PG v13 would allow setting up this sort of case without bothering with a handwritten trigger, but it seems not: regression=3D# create table foo(data jsonb, ts timestamptz GENERATED ALWAY= S AS ((data->>'ts')::timestamptz) stored); psql: ERROR: generation expression is not immutable I wonder if that's really necessary to insist on? regards, tom lane