<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PostgreSQL JSON Lateral Recursive &#8211; Web Scraping Service</title>
	<atom:link href="https://webrobots.io/tag/postgresql-json-lateral-recursive/feed/" rel="self" type="application/rss+xml" />
	<link>https://webrobots.io</link>
	<description>We do web scraping service better!</description>
	<lastBuildDate>Mon, 04 Mar 2019 14:19:29 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.14</generator>
	<item>
		<title>PostgreSQL 9.4 JSON Queries</title>
		<link>https://webrobots.io/postgresql-json-queries/</link>
					<comments>https://webrobots.io/postgresql-json-queries/#respond</comments>
		
		<dc:creator><![CDATA[nicerobot]]></dc:creator>
		<pubDate>Tue, 17 Feb 2015 09:24:43 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[postgreSQL]]></category>
		<category><![CDATA[PostgreSQL JSON Lateral Recursive]]></category>
		<guid isPermaLink="false">http://webrobots.io/?p=5106</guid>

					<description><![CDATA[Intro Querying JSON with SQL is extremely powerful and convenient.  Some great things about: Use SQL to query unstructured data Join relational and JSON tables Convert between JSON and relational schema But query writing can be difficult and non obvious at first.  Official documentation doesn't have many samples. Many useful queries need other great but [...]]]></description>
										<content:encoded><![CDATA[<p><div class="fusion-fullwidth fullwidth-box fusion-builder-row-1 nonhundred-percent-fullwidth non-hundred-percent-height-scrolling"  style='background-color: #ffffff;background-position: center center;background-repeat: no-repeat;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px;border-bottom-width:0px;border-color:#eae9e9;border-top-style:solid;border-bottom-style:solid;'><div class="fusion-builder-row fusion-row "><div  class="fusion-layout-column fusion_builder_column fusion_builder_column_1_1 fusion-builder-column-0 fusion-one-full fusion-column-first fusion-column-last 1_1"  style='margin-top:0px;margin-bottom:0px;'><div class="fusion-column-wrapper" style="padding: 0px 0px 0px 0px;background-position:left top;background-repeat:no-repeat;-webkit-background-size:cover;-moz-background-size:cover;-o-background-size:cover;background-size:cover;"   data-bg-url=""><div class="fusion-text"><h1>Intro</h1>
<p>Querying JSON with SQL is extremely powerful and convenient.  Some great things about:</p>
<ul>
<li>Use SQL to query unstructured data</li>
<li>Join relational and JSON tables</li>
<li>Convert between JSON and relational schema</li>
</ul>
<p>But query writing can be difficult and non obvious at first.  Official documentation doesn&#8217;t have many samples. Many useful queries need other great but not widely known features of PosgresSQL like LATERAL joins and recursive queries.</p>
<p>This tutorial has some real world examples.</p>
<h1>Get some data</h1>
<p>Lets use GitHub Archive as source for large JSON with complex structure:</p>
<pre>wget http://data.githubarchive.org/2015-01-01-15.json.gz
gzip -d 2015-01-01-15.json.gz</pre>
<pre></pre>
<h1>Load JSON to PostgreSQL</h1>
<p>Super easy:</p>
<pre>COPY github FROM 'c:\temp\2015-01-01-15.json'
WITH (format csv, quote e'\x01', delimiter e'\x02', escape e'\x01')</pre>
<p>Query returned successfully: 11351 rows affected, 1025 ms execution time.</p>
<h1>Do some simple queries</h1>
<p>Still straightforward:</p>
<pre>select js-&gt;&gt;'type', count(*) from github group by 1;</pre>
<pre>select js-&gt;'actor'-&gt;'login' from github where js-&gt;&gt;'type' = 'IssuesEvent';</pre>
<h1>Explore structure with LATERAL joins</h1>
<p>Many Postgress functions return rowsets and are invoked like:</p>
<pre>select * from jsonb_each(jsonb)</pre>
<p>If your json is in many rows like in our GitHub sample then we need LATERAL joins:</p>
<pre>Get top level keys from all:
 select
 key,
 max(length(value::text)),
 json_agg(distinct jsonb_typeof(value))
 from github a, lateral jsonb_each(a.js) kv
 group by key

</pre>
<h1>Recursive queries</h1>
<p>JSON is tree like and recursive queries are often necessary.<br />
Here we enumerate all possible paths that exist in the documents:</p>
<pre>with recursive tree(lvl, key, path, jstype) as (
select distinct 
 0 as lvl,
 kv.key as key, 
 array</p>
</div><div class="fusion-clearfix"></div></div></div></div></div><div class="fusion-fullwidth fullwidth-box fusion-builder-row-2 hundred-percent-fullwidth non-hundred-percent-height-scrolling"  style='background-color: #ffffff;background-position: center center;background-repeat: no-repeat;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;border-top-width:0px;border-bottom-width:0px;border-color:#eae9e9;border-top-style:solid;border-bottom-style:solid;'><div class="fusion-builder-row fusion-row "><div  class="fusion-layout-column fusion_builder_column fusion_builder_column_1_1 fusion-builder-column-1 fusion-one-full fusion-column-first fusion-column-last fusion-column-no-min-height 1_1"  style='margin-top:0px;margin-bottom:0px;'><div class="fusion-column-wrapper" style="padding: 0px 0px 0px 0px;background-position:left top;background-repeat:no-repeat;-webkit-background-size:cover;-moz-background-size:cover;-o-background-size:cover;background-size:cover;"   data-bg-url=""><div class="fusion-text"><p>[kv.key] as path, 
 jsonb_typeof(value) as jstype 
from github a, lateral jsonb_each(a.js) kv
union all 
select distinct 
 tree.lvl + 1 as lvl,
 kv.key,
 array_append(tree.path, kv.key) as path, 
 jsonb_typeof(value) as jstype 
from tree, github a, lateral jsonb_each(a.js #&gt; tree.path) kv
where jsonb_typeof(a.js #&gt; tree.path) = 'object'
)
select path, jstype from tree
order by path;</p>
<h1>Further reading and references</h1>
<p>Others did tests showing that PostgresSQL is about 3x faster and files occupy about 3x less space than MongoDB.</p>
<p><a href="https://vibhorkumar.wordpress.com/2014/05/15/write-operation-mongodb-vs-postgresql-9-3-json/">https://vibhorkumar.wordpress.com/2014/05/15/write-operation-mongodb-vs-postgresql-9-3-json/<br />
</a><a href="http://blogs.enterprisedb.com/2014/09/24/postgres-outperforms-mongodb-and-ushers-in-new-developer-reality/">http://blogs.enterprisedb.com/2014/09/24/postgres-outperforms-mongodb-and-ushers-in-new-developer-reality/</a></p>
<p>Official operator and function reference:<br />
<a href="http://www.postgresql.org/docs/9.4/static/functions-json.html">http://www.postgresql.org/docs/9.4/static/functions-json.html</a><br />
<a href="http://www.postgresql.org/docs/9.4/static/functions-aggregate.html">http://www.postgresql.org/docs/9.4/static/functions-aggregate.html</a></p>
<pre></pre>
</div><div class="fusion-clearfix"></div></div></div></div></div></p>
]]></content:encoded>
					
					<wfw:commentRss>https://webrobots.io/postgresql-json-queries/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/

Page Caching using Disk: Enhanced 
Minified using Disk
Database Caching 16/59 queries in 0.062 seconds using Disk

Served from: webrobots.io @ 2026-11-18 12:55:32 by W3 Total Cache
-->