Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kmVx0-0007hj-HM for pgsql-general@arkaria.postgresql.org; Tue, 08 Dec 2020 05:54:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kmVwy-000139-4d for pgsql-general@arkaria.postgresql.org; Tue, 08 Dec 2020 05:54:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kmVwx-000131-TK for pgsql-general@lists.postgresql.org; Tue, 08 Dec 2020 05:54:11 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kmVwv-0005vU-DE for pgsql-general@lists.postgresql.org; Tue, 08 Dec 2020 05:54:11 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 0B85s6v43214469; Tue, 8 Dec 2020 00:54:06 -0500 From: Tom Lane To: Ken Tanzer cc: Steve Baldwin , "David G. Johnston" , Rob Sargent , "pgsql-generallists.postgresql.org" Subject: Re: Extracting data from jsonb array? In-reply-to: References: <72f54672-ad66-0370-9128-206e49389b60@gmail.com> <3210187.1607400978@sss.pgh.pa.us> <3211450.1607402699@sss.pgh.pa.us> Comments: In-reply-to Ken Tanzer message dated "Mon, 07 Dec 2020 21:38:21 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3214467.1607406846.1@sss.pgh.pa.us> Date: Tue, 08 Dec 2020 00:54:06 -0500 Message-ID: <3214468.1607406846@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Ken Tanzer writes: > There's one last piece of this query I'm clearly not getting though. Where > it says: > from foo as f, jsonb_to_recordset(js) as t(key2 text) > what is actually going on there? I keep reading this as a table foo (f) > cross-joined to a table created by jsonb_to_recordset (t). But that > doesn't seem right, because rows from t are only joining with matching rows > from f, rather than all of them. Is there some unspoken implicit logic > going on here, or something else entirely that is going over my head? There's an implicit LATERAL there: ... from foo as f, lateral jsonb_to_recordset(js) as t(key2 text) ie, for each row of foo, extract the foo.js column and evaluate jsonb_to_recordset(js) --- which, in this case, produces multiple rows that are joined to the original foo row. This is again a SQL-ism. I don't particularly care for their choice to allow LATERAL to be implicit for function-call-like FROM items, because it seems pretty confusing; but the spec is the spec. [ thinks for a bit... ] Again, I'm too lazy to go digging in the spec's dense verbiage at this hour, but I'm vaguely recalling that they may only require this behavior for the one case of the function being UNNEST(). I think it was our choice to allow it to work like that for any set-returning function. regards, tom lane