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 1iVFb3-0003J6-PD for pgsql-performance@arkaria.postgresql.org; Thu, 14 Nov 2019 13:55:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iVFb2-0007S5-IT for pgsql-performance@arkaria.postgresql.org; Thu, 14 Nov 2019 13:55:40 +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 1iVFb2-0007RW-4o for pgsql-performance@lists.postgresql.org; Thu, 14 Nov 2019 13:55:40 +0000 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120] helo=us-smtp-1.mimecast.com) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1iVFax-0002L1-RT for pgsql-performance@lists.postgresql.org; Thu, 14 Nov 2019 13:55:38 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1573739732; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=xigAQnYIuRZKR4iQXQWDfKitXVvEURdvYXvLAVrBB4U=; b=GoC8IF/baO5dTjKEQWTR8hNb+lNInHGm8DSQZufHkyXvJ+SWWvpc7uAmGaOAaARd0oX08m ctGEO/W+FwXjmxCSijhWJcVvmn279pBZzCYG5tc2UzS6jMgg/H0eekPL5vwIZ1nswJ2Hlb Lb8NTL0rjdipnVWWV5ZdN7wAuGpWhpY= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-164-6HFDj0YhON2_bLWedqauQw-1; Thu, 14 Nov 2019 08:55:31 -0500 Received: by mail-qk1-f199.google.com with SMTP id r2so4003043qkb.2 for ; Thu, 14 Nov 2019 05:55:30 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:reply-to:from:subject:organization:message-id :date:user-agent:mime-version:content-language :content-transfer-encoding; bh=emBbXLpK0tlOswEEnsH9rswq2JgC6pXr2Aajn3QrIqU=; b=JkGi2ESCFbkvtEVM+2voDkVhjGGXjqmaHKSTaKw3YBLj2BpLZ2H2iEwaz70pnuI7c6 uGV2bYgUlQjzsLfau4HbIcpR0prxU9N31yQqB56FWMghwwHBilYUlM7oiccCBBuXHcIu bahYfnswuPX7VEJnYb7qptXR2e+ZXiIqfWEli2jwg4ZRc4KQvS2+PuPvVyNrJwI1aIPz SBsOpqgBVF5gNheP1h9efm9969fowCL3eu+xHThhOSrqypxgYanuqXnwpqny9HBc9gJS djBBGC3NpYrfPDoJ4YUWIhbmw/1s3QHwCP8uY7IVOuOKy5AyHe1iuI4hlhjzW2S6ghIk InDw== X-Gm-Message-State: APjAAAUeGB8vpznFBFM25BPFjKuolZ1r97lUI1cPgjJcRR+Ky2JkLHL0 kQ+pmZMGCQSeV9JiPDOfUQeBy5VozmLAlwdVCUavgSF5m/B9H5twEdel43UL5CvNO4KbC1C95sA lPnB8SXFtluYs0G2G2ocFH+kku4HXJdPpDUa6dg== X-Received: by 2002:a05:620a:8da:: with SMTP id z26mr7326603qkz.499.1573739730261; Thu, 14 Nov 2019 05:55:30 -0800 (PST) X-Google-Smtp-Source: APXvYqwuomh/9xEjQpTj8OD/y8Yfff8HR2mUxFywOGWmYV93O5tlPsb5fnK7wLTv3IlZfweTHCnW5w== X-Received: by 2002:a05:620a:8da:: with SMTP id z26mr7326580qkz.499.1573739729940; Thu, 14 Nov 2019 05:55:29 -0800 (PST) Received: from localhost.localdomain (c-24-61-8-151.hsd1.ma.comcast.net. [24.61.8.151]) by smtp.gmail.com with ESMTPSA id w18sm2688443qkb.41.2019.11.14.05.55.28 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 14 Nov 2019 05:55:29 -0800 (PST) To: pgsql-performance@lists.postgresql.org Reply-To: jesper.pedersen@redhat.com From: Jesper Pedersen Subject: JSON path Organization: Red Hat Message-ID: <71a1d71e-5263-2980-117c-066a37e481bc@redhat.com> Date: Thu, 14 Nov 2019 08:55:28 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.1.1 MIME-Version: 1.0 Content-Language: en-US X-MC-Unique: 6HFDj0YhON2_bLWedqauQw-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, We have a table which has a jsonb column in it. Each row contains a lot=20 of data in that column, so TOASTed. We have to extract data from that column at different levels, so an=20 example query could look like select col1, col2,=20 jsonb_path_query_first(data,'$.lvl1.lvl2.lvl3.lvl4.lvl5.val1."text()"')=20 as val1, jsonb_path_query_first(data,'$.lvl1.lvl2.lvl3.lvl4.lvl5.val2."text()"')=20 as val2, jsonb_path_query_first(data,'$.lvl1.lvl2.lvl3.lvl4.lvl5.val3."text()"')=20 as val3 from tbl where id =3D 1; I tried to rewrite it to WITH foo AS (select id, col1, col2, jsonb_path_query_first(data,'$.lvl1.lvl2.lvl3.lvl4.lvl5') as jsondata, from tbl ) select col1, col2, jsondata->val1->'text()' as val1, jsondata->val2->'text()' as val2, jsondata->val3->'text()' as val3 from foo where id =3D 1; However, WITH has the same run-time profile - most of the time is spent=20 in pglz_decompress. Using the -> notation has the same profile. The more data I extract from the JSON object the slower the query gets. Of course, if I change the column to EXTERNAL we see a ~3.5 x speedup in=20 the queries but disk space requirements goes up by too much. (We need to use a jsonb column as the data is unstructured, and may=20 differ in structure between rows. Yes, yes, I know...) PostgreSQL 12.x on RHEL. If anybody has some good ideas it would be appreciated. Thanks in advance ! Best regards, Jesper