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 1hSRdK-0001m5-Dk for pgsql-sql@arkaria.postgresql.org; Sun, 19 May 2019 19:38:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hSRdH-0004uq-BY for pgsql-sql@arkaria.postgresql.org; Sun, 19 May 2019 19:38:07 +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 1hSRdG-0004ug-Mw for pgsql-sql@lists.postgresql.org; Sun, 19 May 2019 19:38:07 +0000 Received: from ns-b.lerctr.org ([2001:470:1f0f:3ad::53:2] helo=thebighonker.lerctr.org) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hSRdA-0008FN-FL for pgsql-sql@postgresql.org; Sun, 19 May 2019 19:38:03 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lerctr.org; s=ler2019; h=Message-ID:References:In-Reply-To:Subject:Cc:To:From:Date: Content-Transfer-Encoding:Content-Type:MIME-Version:Sender:Reply-To: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=vge9gdkWtnxFLcOePgBmIpP44Jz507FJUy9N5K9Qjow=; b=mBdsb5mh5+BRk7yByegepyNdzH N9BFPeXWjI8ZpCsVRsEUjKyOYgIoZYx1/gNXGlS35U4sW8mXkOZK16uSeQlYSFacOGomvwsOzGpTu FTwbDNnIQtz/jk8rWtEV+8nyYtqbYYVnKZqnsAHpGXAcQM3d4956TwowNjblhIjFIaon9g1rniBZo lEsI+nqlzn9k7BlvShIPf7Hh6b7oGiunlnDl2QGekELehgo1a5+baW6t975+OVRB39fjuaThBm42W NtITI4WJGI4k5/08iGLuA6HOGj2wPdDewU7Pu5EZ1bIaZ0yvwN3QNFk/tsXT/N+jbRbb9o7rq6iHq riKnFkWA==; Received: from thebighonker.lerctr.org ([2001:470:1f0f:3ad:bb:dcff:fe50:d900]:11873 helo=webmail.lerctr.org) by thebighonker.lerctr.org with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92 (FreeBSD)) (envelope-from ) id 1hSRd5-0008ZZ-IH; Sun, 19 May 2019 14:37:55 -0500 Received: from 2600:1700:210:b180:91d0:3d34:47b2:2268 by webmail.lerctr.org with HTTP (HTTP/1.1 POST); Sun, 19 May 2019 14:37:55 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Sun, 19 May 2019 14:37:55 -0500 From: Larry Rosenman To: Tom Lane Cc: pgsql-sql@postgresql.org Subject: Re: create index on a jsonb timestamp field? In-Reply-To: <29956.1558228655@sss.pgh.pa.us> References: <78a5d1232bb5ef5797a8ae6e1f23543f@lerctr.org> <29956.1558228655@sss.pgh.pa.us> Message-ID: <8b5563be3bf7f0ae9826b223b61fa22d@lerctr.org> X-Sender: ler@lerctr.org User-Agent: Roundcube Webmail/1.3.9 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 05/18/2019 8:17 pm, Tom Lane wrote: > Larry Rosenman writes: >> when I try to create an index on the query_time field of the json >> structure I get: >> ler=# 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. This is what I wound up figuring out between my original post and yours, and it works great. > > 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=# create table foo(data jsonb, ts timestamptz GENERATED > ALWAYS AS ((data->>'ts')::timestamptz) stored); > psql: ERROR: generation expression is not immutable > > I wonder if that's really necessary to insist on? Good question. Is that something the project is going to look into? > > regards, tom lane -- Larry Rosenman http://www.lerctr.org/~ler Phone: +1 214-642-9640 E-Mail: ler@lerctr.org US Mail: 5708 Sabbia Dr, Round Rock, TX 78665-2106