<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Fred Yankowski on Medium]]></title>
        <description><![CDATA[Stories by Fred Yankowski on Medium]]></description>
        <link>https://medium.com/@fredcy?source=rss-62640f23e9f8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*HoGV_K190vmwN4cC.jpg</url>
            <title>Stories by Fred Yankowski on Medium</title>
            <link>https://medium.com/@fredcy?source=rss-62640f23e9f8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 18:46:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@fredcy/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Follow the chain: recursive Postgres query]]></title>
            <link>https://medium.com/@fredcy/follow-the-chain-recursive-postgres-query-6547c2cddcc0?source=rss-62640f23e9f8------2</link>
            <guid isPermaLink="false">https://medium.com/p/6547c2cddcc0</guid>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[tezos]]></category>
            <category><![CDATA[blockchain]]></category>
            <dc:creator><![CDATA[Fred Yankowski]]></dc:creator>
            <pubDate>Tue, 18 Sep 2018 21:23:01 GMT</pubDate>
            <atom:updated>2018-09-19T17:35:18.465Z</atom:updated>
            <content:encoded><![CDATA[<p>While writing a blockchain explorer for the <a href="https://gitlab.com/tezos/tezos">Tezos</a> project, using PostgreSQL to hold data obtained from a Tezos node, I had several cases where I wanted to traverse the blockchain. That is, I wanted to start from a given block in the chain and follow the predecessor links to get the sequence of blocks starting from that one. Here is a Postgres function that, given the hash of a head block and the maximum number of blocks to return, returns a table of blocks found by walking the chain starting from that head.</p><pre>CREATE OR REPLACE FUNCTION chain(head TEXT, length INTEGER)<br>RETURNS SETOF block LANGUAGE sql AS $$</pre><pre>WITH RECURSIVE walk(hash) AS (<br>          SELECT *<br>          FROM block<br>          WHERE hash = head<br>        UNION ALL<br>          SELECT block.*<br>          FROM walk, block<br>          WHERE walk.predecessor = block.hash<br>          AND walk.predecessor != walk.hash<br>)<br>SELECT * FROM walk LIMIT length</pre><pre>$$</pre><p>This example assumes that the block table is keyed by the hash column and has a predecessor column linking to the previous block. It also assumes that the genesis (oldest) block links to itself as predecessor.</p><p>The recursive query can of course be used on its own without a wrapper function. My actual case involved a join of the block table with a parallel table and it was helpful to wrap it all in a reusable function.</p><p>It’s important to have the LIMIT in the SELECT statement that drives the recursive query (if we want to limit at all). If we didn’t have it there and rather applied the limit to the output of the function</p><pre>select * from chain(some_head_hash) limit 10;</pre><p>then the chain function would walk and return the entire chain, which is much slower if only a limited segment is wanted.</p><p>For more information on recursive queries, see <a href="https://www.postgresql.org/docs/current/static/queries-with.html">https://www.postgresql.org/docs/current/static/queries-with.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6547c2cddcc0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>