<?xml version="1.0" encoding="UTF-8" standalone="yes"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-us"><id>https://blog.pgxn.org/tags/directory-structure/</id><title>Directory Structure</title><updated>2010-10-19T22:28:00Z</updated><link rel="self" type="application/atom+xml" href="https://blog.pgxn.org/tags/directory-structure/feed.xml"/><link rel="alternate" type="text/html" href="https://blog.pgxn.org/tags/directory-structure/"/><author><name>The PGXN Maintainers</name></author><generator uri="https://gohugo.io/" version="0.167.0">Hugo</generator><entry><id>https://blog.pgxn.org/post/1353947079</id><title type="html">Creating Distributions</title><link rel="alternate" type="text/html" href="https://blog.pgxn.org/2010/creating-distributions/"/><updated>2026-10-07T16:13:48Z</updated><published>2010-10-19T22:28:00Z</published><author><name>David E. Wheeler</name></author><category scheme="https://blog.pgxn.org/tags" term="distribution" label="Distribution"/><category scheme="https://blog.pgxn.org/tags" term="makefile" label="Makefile"/><category scheme="https://blog.pgxn.org/tags" term="pgxn" label="Pgxn"/><category scheme="https://blog.pgxn.org/tags" term="pgxs" label="Pgxs"/><category scheme="https://blog.pgxn.org/tags" term="pg_config" label="pg_config"/><category scheme="https://blog.pgxn.org/tags" term="pg_regress" label="pg_regress"/><category scheme="https://blog.pgxn.org/tags" term="directory-structure" label="Directory Structure"/><summary type="html"><![CDATA[<p>Following the <a href="https://blog.pgxn.org/post/1352326020/first-upload">upload of <code>pair</code></a> to PGXN, I wanted to take a few minutes to
write about how to structure a PGXN distribution.</p>
<h2 id="omg-distribution-wtf">OMG Distribution WTF?</h2>
<p>First of all, what is a &ldquo;distribution&rdquo; in the PGXN sense? Basically, it&rsquo;s a
collection of one or more PostgreSQL extensions. That&rsquo;s it.</p>
<p>So why allow more than one extension? Maybe no PGXN distribution will ever
have more than one extension. After all, the goal should be many focused,
minimalist tools that folks can combine in their apps. But sometimes it
doesn&rsquo;t work out that way.</p>]]></summary><content type="html" xml:base="https://blog.pgxn.org/" xml:space="preserve"><![CDATA[<p>Following the <a href="https://blog.pgxn.org/post/1352326020/first-upload">upload of <code>pair</code></a> to PGXN, I wanted to take a few minutes to
write about how to structure a PGXN distribution.</p>
<h2 id="omg-distribution-wtf">OMG Distribution WTF?</h2>
<p>First of all, what is a &ldquo;distribution&rdquo; in the PGXN sense? Basically, it&rsquo;s a
collection of one or more PostgreSQL extensions. That&rsquo;s it.</p>
<p>So why allow more than one extension? Maybe no PGXN distribution will ever
have more than one extension. After all, the goal should be many focused,
minimalist tools that folks can combine in their apps. But sometimes it
doesn&rsquo;t work out that way.</p>
<p>As an example, I&rsquo;ve been planning to break <a href="https://pgtap.org/">pgTAP</a> up into two parts for a
while: one for scalar and relational testing, the other for schema testing.
Often one needs only the scalar and relational testing, while the schema
testing is more often needed only for testing replication and whatnot. Whether
or not I choose to distribute both parts in one package I have yet to
determine, but it could well make sense to keep them in one distribution.</p>
<p>Besides, I&rsquo;ve tried to write <a href="https://github.com/theory/pgxn-manager">PGXN::Manager</a> in such a way that it&rsquo;s not
specific to PostgreSQL. So that if someone wanted to create a Drupal XN with
it or something, they could. Or PyXN. Or, hell, even if <a href="https://cpan/org/">CPAN</a> wanted to
switch someday, they could. (Note that I&rsquo;ve registered myxn.org for fun. I may
or may not do anything with that, but see <a href="https://theory.github.com/mytap/">MyTAP</a>. Yes, I am insane.)</p>
<h2 id="thats-so-meta">That&rsquo;s So Meta</h2>
<p>Anyway, back to the structure of distributions. At its simplest, the only
thing PGXN requires is a single file, <code>META.json</code>, which describes the
package. This is (currently) the only file that PGXN Manager uses to index a
distribution, so it&rsquo;s important to get it right. The <a href="https://pgxn.org/meta/spec.html">PGXN Meta Spec</a> has a
rather complete example of a hypothetical pgTAP distribution <code>META.json</code>.</p>
<p>If you have only one .sql file for your extension and it&rsquo;s the same name as
the distribution (and I expect this would be common), then you can make it
pretty simple. For example, the <code>pair</code> distribution has only one SQL file. So
the <code>META.json</code> could be:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-json" data-lang="json"><span class="line"><span class="cl"><span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;name&#34;</span><span class="p">:</span> <span class="s2">&#34;pair&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;abstract&#34;</span><span class="p">:</span> <span class="s2">&#34;A key/value pair data type&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;version&#34;</span><span class="p">:</span> <span class="s2">&#34;0.1.0&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;maintainer&#34;</span><span class="p">:</span> <span class="s2">&#34;David E. Wheeler &lt;david@justatheory.com&gt;&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;license&#34;</span><span class="p">:</span> <span class="s2">&#34;postgresql&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;meta-spec&#34;</span><span class="p">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">      <span class="nt">&#34;version&#34;</span><span class="p">:</span> <span class="s2">&#34;1.0.0&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">      <span class="nt">&#34;url&#34;</span><span class="p">:</span> <span class="s2">&#34;https://pgxn.org/meta/spec.txt&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="p">},</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>That&rsquo;s it. The only thing that may not be obvious from this example is that
all version numbers in a <code>META.json</code> <em>must</em> be <a href="https://semver.org/">semantic versions</a>. If they&rsquo;re
not, PGXN will make them so. So &ldquo;1.2&rdquo; will become &ldquo;1.2.0&rdquo;, and so would
&ldquo;1.02&rdquo;. So do try to use semantic versions and not worry about it.</p>
<p>In the short run, you won&rsquo;t need anything more in your <code>META.json</code> file. But
once I get to creating the search site and the command-line client for PGXN,
you&rsquo;re probably going to want to do more. Other useful keys to include are:</p>
<ul>
<li><a href="https://pgxn.org/meta/spec.html#tags"><code>tags</code></a>: An array of tags to associate with a distribution. Will help
with searching.</li>
<li><a href="https://pgxn.org/meta/spec.html#prereqs"><code>prereqs</code></a>: A list of prerequisite extensions or PostgreSQL contrib
moules (or PostgreSQL itself).</li>
<li><a href="https://pgxn.org/meta/spec.html#provides"><code>provides</code></a>: A list of included extensions. Useful if you have more than
one or the one has a different name that the distribution (silly, but it
happens). It also will index such extension names such that you are the
owner, if you&rsquo;re the first to update one with that name.</li>
<li><a href="https://pgxn.org/meta/spec.html#release_status"><code>release_status</code></a>: To label a distribution as &ldquo;stable,&rdquo; &ldquo;unstable,&rdquo; or
&ldquo;testing.&rdquo; Useful for uploading distributions for people to test but that
clients won&rsquo;t install by default.</li>
<li><a href="https://pgxn.org/meta/spec.html#resources"><code>resources</code></a>: A list of related links, such as to an SCM repository or
bug tracker. The search site will output these links.</li>
</ul>
<p>Have a look at <a href="https://github.com/theory/kv-pair/blob/master/META.json">the <code>META.json</code> in the <code>pair</code> distribution</a> for a more
extended example.</p>
<h2 id="new-order">New Order</h2>
<p>For PGXN, the general idea is that you&rsquo;ll use <a href="https://www.postgresql.org/docs/9/static/xfunc-c.html#XFUNC-C-PGXS">PGXS</a> to create your PostgreSQL
extensions. I&rsquo;m hoping to encourage a slight modification of the directory
layout for PGXN distributions, but as I hope I&rsquo;ve made clear so far, PGXN
itself doesn&rsquo;t really care how you structure things, or if you use PGXS. That
said, the proposed <a href="https://wiki.postgresql.org/wiki/PGXN#PGXN_Client">download and installation client</a> will assume the use of
PGXS (unless and until the PostgreSQL core adds some other kind of
extension-building support), so it&rsquo;s probably the best choice.</p>
<p>Most PGXS-powered distributions have the code files in the main directory,
with documentation in a <code>README.extension_name</code> file. What I&rsquo;d like to see
instead, and will encourage via the forthcoming <a href="https://wiki.postgresql.org/wiki/PGXN#Search_Site">search site</a>, is that things
be organized into subdirectories:</p>
<ul>
<li><code>src</code> for any C source code files</li>
<li><code>sql</code> for SQL source files. These usually are responsible for installing an
extension into a database</li>
<li><code>doc</code> for documentation files (the search site will likely look there for
Markdown, Textile, HTML, and other document formats)</li>
<li><code>test</code> for tests</li>
</ul>
<p>I&rsquo;ve tried to make the <code>pair</code> distribution a good <a href="https://github.com/theory/kv-pair/blob/">example of this</a>. To make
it all work, The <a href="https://github.com/theory/kv-pair/blob/master/Makefile">Makefile</a> is written like so:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-makefile" data-lang="makefile"><span class="line"><span class="cl"><span class="nv">DATA</span> <span class="o">=</span> sql/pair.sql sql/uninstall_pair.sql
</span></span><span class="line"><span class="cl"><span class="nv">TESTS</span> <span class="o">=</span> <span class="k">$(</span>wildcard test/sql/*.sql<span class="k">)</span>
</span></span><span class="line"><span class="cl"><span class="nv">REGRESS</span> <span class="o">=</span> <span class="k">$(</span>patsubst test/sql/%.sql,%,<span class="k">$(</span>TESTS<span class="k">))</span>
</span></span><span class="line"><span class="cl"><span class="nv">REGRESS_OPTS</span> <span class="o">=</span> --inputdir<span class="o">=</span><span class="nb">test</span>
</span></span><span class="line"><span class="cl"><span class="nv">DOCS</span> <span class="o">=</span> doc/pair.txt
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="err">ifdef</span> <span class="err">NO_PGXS</span>
</span></span><span class="line"><span class="cl"><span class="nv">top_builddir</span> <span class="o">=</span> ../..
</span></span><span class="line"><span class="cl"><span class="err">include</span> <span class="k">$(</span><span class="nv">top_builddir</span><span class="k">)</span><span class="err">/src/Makefile.global</span>
</span></span><span class="line"><span class="cl"><span class="err">include</span> <span class="k">$(</span><span class="nv">top_srcdir</span><span class="k">)</span><span class="err">/contrib/contrib-global.mk</span>
</span></span><span class="line"><span class="cl"><span class="err">else</span>
</span></span><span class="line"><span class="cl"><span class="nv">PG_CONFIG</span> <span class="o">=</span> pg_config
</span></span><span class="line"><span class="cl"><span class="nv">PGXS</span> <span class="o">:=</span> <span class="k">$(</span>shell <span class="k">$(</span>PG_CONFIG<span class="k">)</span> --pgxs<span class="k">)</span>
</span></span><span class="line"><span class="cl"><span class="err">include</span> <span class="k">$(</span><span class="nv">PGXS</span><span class="k">)</span>
</span></span><span class="line"><span class="cl"><span class="err">endif</span>
</span></span></code></pre></div><p>The <code>DATA</code> variable identifies the files containing the extension, while
<code>TESTS</code> loads a list of all the tests, which are in the <code>test/sql</code> directory.
Note that I&rsquo;m using <code>pg_regress</code> for tests. It expects that tests be named and
that there be corresponding &ldquo;expected&rdquo; files to compare against. With the
<code>REGRESS_OPTS = --inputdir=test</code> line, I&rsquo;m telling <code>pg_regess</code> to find the
test files in <a href="https://github.com/theory/kv-pair/tree/master/test/sql/"><code>test/sql</code></a> and the expected output files in <a href="https://github.com/theory/kv-pair/tree/master/test/expected/"><code>test/expected</code></a>.
And finally, the <code>DOCS</code> variable points to a single file with the
documentation, <a href="https://github.com/theory/kv-pair/blob/master/doc/pair.txt"><code>doc/pair.txt</code></a>. If this extension had required any C code
(like <a href="https://pgtap.org/">pgTAP</a> or <a href="https://postgis.org/">PostGIS</a> do), I would have pointed the <code>MODULES</code> variable at
files in a <code>src</code> directory.</p>
<p>After that we just have build instructions. If called with <code>make NO_PGXS=1</code>,
it assumes that the unzipped distribution directory has been put in the
&ldquo;contrib&rdquo; directory of the PostgreSQL source tree used to build PostgreSQL.
That&rsquo;s probably only important if one is installing on PostgreSQL 8.1 or
lower. Otherwise, it assumes a plain <code>make</code> and uses the <a href="https://www.postgresql.org/docs/current/static/app-pgconfig.html"><code>pg_config</code></a> in your
path to find PGXS to do the build.</p>
<p>For more on PostgreSQL extension building support, please consult <a href="https://www.postgresql.org/docs/9/static/xfunc-c.html#XFUNC-C-PGXS">the
documentation</a>.</p>
<h2 id="zip-me-up">Zip Me Up</h2>
<p>Once you&rsquo;ve got your extension developed and well-tested, and your
distribution just right and the <code>META.json</code> file all proof-read and solid,
it&rsquo;s time to upload the distribution to PGXN. What you want to do is to zip it
up to create a distribution archive. Here&rsquo;s what I did for <code>pair</code>, exporting
it from Git:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">git checkout-index -af --prefix ~/Desktop/pair-0.1.0/
</span></span><span class="line"><span class="cl"><span class="nb">cd</span> ~/Desktop/
</span></span><span class="line"><span class="cl">rm pair-0.1.0/.gitignore
</span></span><span class="line"><span class="cl">zip -r pair-0.1.0.zip pair-0.1.0
</span></span></code></pre></div><p>Then the <code>pair-0.1.0.zip</code> file was ready to upload. Simple, eh?</p>
<p>Now, one can upload any kind of archive file to PGXN, including a tarball, or
bzip2&hellip;um&hellip;ball? Basically, any kind of archive format recognized by
<a href="https://search.cpan.org/perldoc?Archive::Extract">Archive::Extract</a>. You can upload a <code>.pgz</code> if you like, in which case PGXN
will assume that it&rsquo;s a zip file. A zip file is best because then
PGXN::Manager won&rsquo;t have to rewrite it. It&rsquo;s also preferable that everything
be unpacked from an archive into a directory with the name
<code>$distribution-$version</code>. If not, PGXN will rewrite it to do so. But it saves
the server some effort if all it has to do is move a .zip file that&rsquo;s properly
formatted, so it would be appreciated if you would upload stuff that&rsquo;s already
nicely formatted for distribution in a zip archive.</p>
<h2 id="release-it">Release It!</h2>
<p>And that&rsquo;s it! Not too bad, eh? Just please do be very careful cutting and
pasting examples; I initially uploaded the <code>pair</code> distribution thinking that
it contained pgTAP. It was kind of a PITA to fix. Hopefully we&rsquo;ll be able to
build things up to the point where a lot of this stuff can be automated
(especially the creation of the <code>META.json</code>), but for now it&rsquo;s done by hand.
So be careful out there, and good luck!</p>
<p>Oh, and if you have an extension that you&rsquo;d like to release on PGXN now, I am
running a limited beta for interested extension developers. Please hit the
<a href="https://groups.google.com/group/pgxn-users/">mail list</a> for the details to be posted shortly.</p>
]]></content></entry></feed>