<?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>Schadenfreude &#187; postgres</title>
	<atom:link href="http://www.ralree.com/tag/postgres/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ralree.com</link>
	<description>Malicious enjoyment derived from observing someone else's misfortune</description>
	<lastBuildDate>Thu, 09 Feb 2012 01:49:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Reading compressed files with postgres using named pipes</title>
		<link>http://www.ralree.com/2009/09/04/reading-compressed-files-with-postgres-using-named-pipes/</link>
		<comments>http://www.ralree.com/2009/09/04/reading-compressed-files-with-postgres-using-named-pipes/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 06:38:55 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[awesome]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[databases]]></category>
		<category><![CDATA[dba]]></category>
		<category><![CDATA[dump]]></category>
		<category><![CDATA[postgres]]></category>

		<guid isPermaLink="false">http://www.ralree.com/?p=22661</guid>
		<description><![CDATA[Postgres has the same type of ability MySQL has to read in files, yet much nicer syntax. LOAD DATA INFILE from MySQL is just COPY in postgres. I decided to try having it read from a named pipe today, and it worked out nicely. I started out making a test db and making a nice little schema: postgres@tardis:~$ createdb test postgres@tardis:~$ psql test psql (8.4.0) Type "help" for help. test=# CREATE TYPE rank AS ENUM ('general', 'sergeant', 'private'); CREATE TYPE [...]]]></description>
			<content:encoded><![CDATA[<p>Postgres has the same type of ability MySQL has to read in files, yet much nicer syntax.  <code>LOAD DATA INFILE</code> from MySQL is just <code>COPY</code> in postgres.  I decided to try having it read from a named pipe today, and it worked out nicely.<br />
<span id="more-22661"></span><br />
I started out making a test db and making a nice little schema:</p>
<pre><code>
postgres@tardis:~$ createdb test
postgres@tardis:~$ psql test
psql (8.4.0)
Type "help" for help.

test=# CREATE TYPE rank AS ENUM ('general', 'sergeant', 'private');
CREATE TYPE
test=# CREATE TABLE military (id SERIAL PRIMARY KEY,
test(#   name VARCHAR(128),
test(#   rank rank);
NOTICE:  CREATE TABLE will create implicit sequence "military_id_seq" for serial column "military.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "military_pkey" for table "military"
CREATE TABLE
</code></pre>
<p>Notice the use of <code>SERIAL</code>?  That&#8217;s postgres&#8217; <code>AUTO_INCREMENT</code>, basically.  I like it better.  Next, it&#8217;s time to make a text file with some data and compress it.  Here&#8217;s what I put in the file (note that the spaces between the words are <code>TAB</code> characters):</p>
<pre><code>
general Lee
sergeant  Hartman
private Pyle
</code></pre>
<p>And compress it with <code>gzip</code>, making a nice little file:</p>
<pre><code>
hank@tardis:/tmp$ gzip file
hank@tardis:/tmp$ zcat file.gz
general	Lee
sergeant	Hartman
private	Pyle
</code></pre>
<p>Now let&#8217;s actually make a named pipe for postgres to read from:</p>
<pre><code>
hank@tardis:/tmp$ mkfifo namedpipe
</code></pre>
<p>Now that we have our named pipe, let&#8217;s start reading from it:</p>
<pre><code>
test=# COPY military (rank, name) FROM '/tmp/namedpipe' WITH DELIMITER E'\t';
</code></pre>
<p>The <code>E'\t'</code> part means to escape characters inside the single-quoted string, turning this into an actual tab character.  All that we have to do now is use zcat:</p>
<pre><code>
hank@tardis:/tmp$ zcat file.gz > namedpipe
</code></pre>
<p>Immediately, there&#8217;s some output in the psql session:</p>
<pre><code>
COPY 3
</code></pre>
<p>So, postgres says it got 3 records successfully.  Yay!  Now, let&#8217;s display them:</p>
<pre><code>
test=# select * from military;
 id |  name   |   rank
----+---------+----------
  1 | Lee     | general
  2 | Hartman | sergeant
  3 | Pyle    | private
(3 rows)
</code></pre>
<p>So, this is a pretty good method to read in compressed files with postgres.  I&#8217;ve seen many articles that use similar methods with postgres dump files, but it&#8217;s useful for bulk delimited data loading as well, as many times it&#8217;s prudent to compress bulk data files and not extract them before loading them.  See the postgres <a href="http://www.postgresql.org/docs/8.4/interactive/sql-copy.html">COPY</a> page for more information about this awesome function.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ralree.com/2009/09/04/reading-compressed-files-with-postgres-using-named-pipes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>OSCON 2008 Tutorials: A perspective</title>
		<link>http://www.ralree.com/2008/07/22/oscon-2008-tutorials-a-perspective/</link>
		<comments>http://www.ralree.com/2008/07/22/oscon-2008-tutorials-a-perspective/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 18:10:00 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[conference]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[intel]]></category>
		<category><![CDATA[oscon]]></category>
		<category><![CDATA[oscon2008]]></category>
		<category><![CDATA[postgres]]></category>

		<guid isPermaLink="false">http://www.ralree.info/2008/07/22/oscon-2008-tutorials-a-perspective</guid>
		<description><![CDATA[I&#8217;ve been at OSCON 2008, and I&#8217;ve been keeping pretty good notes on the tutorials I&#8217;ve attended. GitHub has a great feature if you take notes in rdoc, making nice HTML when it&#8217;s viewed online. Here&#8217;s some links to my notes: Pro PostgreSQL This talk was alright, but it wasn&#8217;t really tailored for people who hadn&#8217;t used Postgres extensively. Yet, I&#8217;m sure the notes will come in handy in the future. Introduction to Django This was an awesome talk. He [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been at OSCON 2008, and I&#8217;ve been keeping pretty good notes on <a href="http://github.com/hank/life/tree/master/oscon/2008/tutorials">the tutorials I&#8217;ve attended</a>.  GitHub has a great feature if you take notes in rdoc, making nice HTML when it&#8217;s viewed online.  Here&#8217;s some links to my notes:</p>
<h2><a href="http://github.com/hank/life/tree/master/oscon/2008/tutorials/ProPostgres.rdoc">Pro PostgreSQL</a></h2>
<p>This talk was alright, but it wasn&#8217;t really tailored for people who hadn&#8217;t used Postgres extensively.  Yet, I&#8217;m sure the notes will come in handy in the future.</p>
<h2><a href="http://github.com/hank/life/tree/master/oscon/2008/tutorials/Django.rdoc">Introduction to Django</a></h2>
<p>This was an awesome talk.  He went through designing an actual working application using Django.  It turns out to be incredibly easy to quickly create a really cool application.  I need to research this because it looks to me to be better than Rails.</p>
<h2><a href="http://github.com/hank/life/tree/master/oscon/2008/tutorials/Ubiquitous.Multithreading.rdoc">Ubiquitous Multithreading</a></h2>
<p>This talk was alright if you wanted to get an idea about the challenges of multi-threading and how Intel&#8217;s TBB solves some of them.  It was interesting, and I learned a few things, but it would have been a better session than a tutorial (LONG!)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ralree.com/2008/07/22/oscon-2008-tutorials-a-perspective/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PL/Ruby on CentOS 5</title>
		<link>http://www.ralree.com/2007/08/01/pl-ruby-on-centos-5/</link>
		<comments>http://www.ralree.com/2007/08/01/pl-ruby-on-centos-5/#comments</comments>
		<pubDate>Wed, 01 Aug 2007 20:24:00 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[centos]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[make]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.ralree.info/2007/10/13/pl-ruby-on-centos-5</guid>
		<description><![CDATA[I had some trouble compiling PL/Ruby for PostgreSQL today on Modzer0. I solved it with some clever extconf.rb switches: ruby extconf.rb --with-pgsql-include=/usr/include/pgsql/ --with-pgsql-version=81 Now I avoid the problems I was having before: [root@modzer0 plruby-0.5.1]# make make[1]: Entering directory #... In function ‘perm_fmgr_info’:plruby.c:116: error: ‘TopMemoryContext’ undeclared (first use in this function) plruby.c:116: error: (Each undeclared identifier is reported only once plruby.c:116: error: for each function it appears in.) plruby.c: In function ‘plruby_call_handler’: plruby.c:706: warning: unused variable ‘result’ plruby.c: In function ‘pl_compile’: [...]]]></description>
			<content:encoded><![CDATA[<p>I had some trouble compiling <a href="http://moulon.inra.fr/ruby/plruby.html">PL/Ruby</a> for PostgreSQL today on Modzer0.  I solved it with some clever <code>extconf.rb</code> switches:</p>
<pre><code>
ruby extconf.rb --with-pgsql-include=/usr/include/pgsql/ --with-pgsql-version=81
</code></pre>
<p>Now I avoid the problems I was having before:</p>
<pre><code>
[root@modzer0 plruby-0.5.1]# make
make[1]: Entering directory
#...
In function ‘perm_fmgr_info’:plruby.c:116: error: ‘TopMemoryContext’ undeclared (first use in this function)
plruby.c:116: error: (Each undeclared identifier is reported only once
plruby.c:116: error: for each function it appears in.)
plruby.c: In function ‘plruby_call_handler’:
plruby.c:706: warning: unused variable ‘result’
plruby.c: In function ‘pl_compile’:
plruby.c:875: error: subscripted value is neither array nor pointer
plruby.c:876: error: subscripted value is neither array nor pointer
plruby.c:879: error: subscripted value is neither array nor pointer
plruby.c:889: error: subscripted value is neither array nor pointer
plruby.c:937: error: ‘TopMemoryContext’ undeclared (first use in this function)
make[1]: *** [plruby.o] Error 1
</code></pre>
<p>Now I just have to create the language in Postgres.  I&#8217;m making it trusted since I don&#8217;t want anyone doing anything nasty.  I might build the untrusted version later like Robby did <a href="http://www.robbyonrails.com/articles/2005/08/22/installing-untrusted-pl-ruby-for-postgresql">here</a>.</p>
<pre><code>
CREATE FUNCTION plruby_call_handler() RETURNS language_handler
  AS '/usr/lib/ruby/site_ruby/1.8/i386-linux/plruby.so'
  LANGUAGE C;

CREATE TRUSTED LANGUAGE 'plruby'
  HANDLER plruby_call_handler
  LANCOMPILER 'PL/Ruby';
</code></pre>
<h2>Voila!</h2>
<pre><code>
Procedural Languages
  Name  | Trusted?
--------+----------
 plperl | yes
 plruby | yes
</code></pre>
<p><img src="http://ralree.info/assets/2007/8/1/o-face.jpg" alt="OH OH"/></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ralree.com/2007/08/01/pl-ruby-on-centos-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

