<?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>Sniptools &#187; Databases</title> <atom:link href="http://sniptools.com/category/databases/feed/" rel="self" type="application/rss+xml" /><link>http://sniptools.com</link> <description>Design &#38; Technology Observations</description> <lastBuildDate>Tue, 15 May 2012 09:23:41 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" /> <item><title>Finding non-UTF8 values in PostgreSQL</title><link>http://sniptools.com/databases/finding-non-utf8-values-in-postgresql</link> <comments>http://sniptools.com/databases/finding-non-utf8-values-in-postgresql#comments</comments> <pubDate>Thu, 23 Jul 2009 01:56:02 +0000</pubDate> <dc:creator>Shanx</dc:creator> <category><![CDATA[Databases]]></category> <category><![CDATA[postgresql]]></category> <category><![CDATA[sql]]></category><guid
isPermaLink="false">http://sniptools.com/?p=528</guid> <description><![CDATA[This regexp worked for me. 1 2 3 4 5 6 7 8 9 10 11 12 SELECT * FROM TABLE WHERE NOT COLUMN ~ &#40; '^('&#124;&#124; $$&#91;\09\0A\0D\x20-\x7E&#93;&#124;$$&#124;&#124; -- ASCII $$&#91;\xC2-\xDF&#93;&#91;\x80-\xBF&#93;&#124;$$&#124;&#124; -- non-overlong 2-byte $$\xE0&#91;\xA0-\xBF&#93;&#91;\x80-\xBF&#93;&#124;$$&#124;&#124; -- excluding overlongs $$&#91;\xE1-\xEC\xEE\xEF&#93;&#91;\x80-\xBF&#93;&#123;2&#125;&#124;$$&#124;&#124; -- straight 3-byte $$\xED&#91;\x80-\x9F&#93;&#91;\x80-\xBF&#93;&#124;$$&#124;&#124; -- excluding surrogates $$\xF0&#91;\x90-\xBF&#93;&#91;\x80-\xBF&#93;&#123;2&#125;&#124;$$&#124;&#124; -- planes 1-3 $$&#91;\xF1-\xF3&#93;&#91;\x80-\xBF&#93;&#123;3&#125;&#124;$$&#124;&#124; -- planes 4-15 $$\xF4&#91;\x80-\x8F&#93;&#91;\x80-\xBF&#93;&#123;2&#125;$$&#124;&#124; [...]]]></description> <content:encoded><![CDATA[<p>This regexp worked for me.</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td
class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #993333; font-weight: bold;">TABLE</span>
<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">COLUMN</span> ~ <span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'^('</span><span style="color: #66cc66;">||</span>
 $$<span style="color: #66cc66;">&#91;</span>\09\0A\0D\x20<span style="color: #66cc66;">-</span>\x7E<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">|</span>$$<span style="color: #66cc66;">||</span>               <span style="color: #808080; font-style: italic;">-- ASCII</span>
 $$<span style="color: #66cc66;">&#91;</span>\xC2<span style="color: #66cc66;">-</span>\xDF<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>\x80<span style="color: #66cc66;">-</span>\xBF<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">|</span>$$<span style="color: #66cc66;">||</span>             <span style="color: #808080; font-style: italic;">-- non-overlong 2-byte</span>
  $$\xE0<span style="color: #66cc66;">&#91;</span>\xA0<span style="color: #66cc66;">-</span>\xBF<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>\x80<span style="color: #66cc66;">-</span>\xBF<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">|</span>$$<span style="color: #66cc66;">||</span>        <span style="color: #808080; font-style: italic;">-- excluding overlongs</span>
 $$<span style="color: #66cc66;">&#91;</span>\xE1<span style="color: #66cc66;">-</span>\xEC\xEE\xEF<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>\x80<span style="color: #66cc66;">-</span>\xBF<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">|</span>$$<span style="color: #66cc66;">||</span>  <span style="color: #808080; font-style: italic;">-- straight 3-byte</span>
  $$\xED<span style="color: #66cc66;">&#91;</span>\x80<span style="color: #66cc66;">-</span>\x9F<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>\x80<span style="color: #66cc66;">-</span>\xBF<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">|</span>$$<span style="color: #66cc66;">||</span>        <span style="color: #808080; font-style: italic;">-- excluding surrogates</span>
  $$\xF0<span style="color: #66cc66;">&#91;</span>\x90<span style="color: #66cc66;">-</span>\xBF<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>\x80<span style="color: #66cc66;">-</span>\xBF<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">|</span>$$<span style="color: #66cc66;">||</span>     <span style="color: #808080; font-style: italic;">-- planes 1-3</span>
 $$<span style="color: #66cc66;">&#91;</span>\xF1<span style="color: #66cc66;">-</span>\xF3<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>\x80<span style="color: #66cc66;">-</span>\xBF<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">|</span>$$<span style="color: #66cc66;">||</span>          <span style="color: #808080; font-style: italic;">-- planes 4-15</span>
  $$\xF4<span style="color: #66cc66;">&#91;</span>\x80<span style="color: #66cc66;">-</span>\x8F<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>\x80<span style="color: #66cc66;">-</span>\xBF<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#125;</span>$$<span style="color: #66cc66;">||</span>      <span style="color: #808080; font-style: italic;">-- plane 16</span>
 <span style="color: #ff0000;">')*$'</span> <span style="color: #66cc66;">&#41;</span>
;</pre></td></tr></table></div><p>Or, if you have iconv on your system, as most UNIX variants do, you can pg_dumpall a database and run the following command on it:</p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">@</span>iconv<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$input</span>, <span style="color: #ff0000;">'UTF-8'</span>, <span style="color: #ff0000;">'UTF-8'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> == <span style="color: #007800;">$input</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Good UTF-8!&quot;</span>; <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Nope.&quot;</span></pre></div></div><p>Then take a plain text pg_dump of the database in UTF-8. Let's call it "db.orig.dmp". Strip all problem characters:</p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;">iconv <span style="color: #660033;">-f</span> UTF-<span style="color: #000000;">8</span> <span style="color: #660033;">-t</span> UTF-<span style="color: #000000;">8</span> <span style="color: #660033;">-c</span> <span style="color: #000000; font-weight: bold;">&lt;</span>db .orig.dmp <span style="color: #000000; font-weight: bold;">&gt;</span>db.stripped.sql
<span style="color: #000000; font-weight: bold;">&lt;/</span>db<span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div><p>Compare both files:</p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">diff</span> <span style="color: #660033;">-u</span> db.orig.dmp db.stripped.sql</pre></div></div><p>That output will show all lines containing a problem character. Now the tedious part: Use "db.orig.dmp" to find out which tables they belong to, locate the records in the database by primary key and fix them. Alternatively, you can use iconv's auto-repair if you know which bytes give you a problem.</p><p>For example, if you know that the trouble stems only from 0x80 bytes that should be Euro symbols, you could:</p><div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;">iconv <span style="color: #660033;">-f</span> UTF-<span style="color: #000000;">8</span> <span style="color: #660033;">-t</span> UTF-<span style="color: #000000;">8</span> <span style="color: #660033;">--byte-subst</span>=<span style="color: #ff0000;">&quot;&amp;lt;0x%x&gt;&quot;</span> <span style="color: #000000; font-weight: bold;">&lt;</span> db.orig.sql <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/&amp;lt;0x80&gt;/EUR/g'</span> <span style="color: #000000; font-weight: bold;">&gt;</span>db.fixed.sql</pre></div></div><p>The resulting "db.fixed.sql" could then be loaded into the new database.</p> ]]></content:encoded> <wfw:commentRss>http://sniptools.com/databases/finding-non-utf8-values-in-postgresql/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Resize a column in a PostgreSQL table without changing data</title><link>http://sniptools.com/databases/resize-a-column-in-a-postgresql-table-without-changing-data</link> <comments>http://sniptools.com/databases/resize-a-column-in-a-postgresql-table-without-changing-data#comments</comments> <pubDate>Tue, 13 Jan 2009 05:49:21 +0000</pubDate> <dc:creator>Shanx</dc:creator> <category><![CDATA[Databases]]></category> <category><![CDATA[postgresql sql databases]]></category><guid
isPermaLink="false">http://sniptools.com/?p=432</guid> <description><![CDATA[You use PostgreSQL. You find that a column you have in a table is of a smaller length than you now wish. In my case, this was a varchar(20) that I now wished to make varchar(35). Nothing else. I just want to change the size, keeping the data intact. The ALTER TABLE ...ALTER COLUMN...TYPE... command [...]]]></description> <content:encoded><![CDATA[<p>You use PostgreSQL. You find that a column you have in a table is of a smaller length than you now wish. In my case, this was a <code>varchar(20)</code> that I now wished to make <code>varchar(35)</code>. Nothing else. I just want to change the size, keeping the data intact.</p><p>The <code>ALTER TABLE ...ALTER COLUMN...TYPE...</code> <a
href="http://www.postgresql.org/docs/current/interactive/sql-altertable.html">command</a> is useful only if you want to alter the data somehow, or change the data type. Otherwise, it'll be an aeon before this finishes even inside a transaction on a database of any meaningful size.</p><p>Until now, I was not familiar with any sensible mechanism to simply change the size in PG. But yesterday, Tom Lane himself suggested something ubercool in the list.</p><p>Let's assume for the sake of simplicity that your table is called "<code>TABLE1</code>" and your column is "COL1". You can find the size of your "<code>COL1</code>" column by issuing the following query on the system tables:</p><div
class="wp_syntax"><div
class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> atttypmod <span style="color: #993333; font-weight: bold;">FROM</span> pg_attribute
<span style="color: #993333; font-weight: bold;">WHERE</span> attrelid <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'TABLE1'</span>::regclass
<span style="color: #993333; font-weight: bold;">AND</span> attname <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'COL1'</span>;
&nbsp;
atttypmod
<span style="color: #808080; font-style: italic;">-----------</span>
<span style="color: #cc66cc;">24</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">ROW</span><span style="color: #66cc66;">&#41;</span></pre></div></div><p>This means that the size is 20 (4 is added for legacy reasons, we're told). You can now conveniently change this to a <code>varchar(35)</code> size by issuing this command:</p><div
class="wp_syntax"><div
class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">UPDATE</span> pg_attribute <span style="color: #993333; font-weight: bold;">SET</span> atttypmod <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">35</span><span style="color: #66cc66;">+</span><span style="color: #cc66cc;">4</span>
<span style="color: #993333; font-weight: bold;">WHERE</span> attrelid <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'TABLE1'</span>::regclass
<span style="color: #993333; font-weight: bold;">AND</span> attname <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'COL1'</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #cc66cc;">1</span></pre></div></div><p>Note that I manually added the 4 to the desired size of 35..again, for some legacy reasons inside PG. Done. That's it. Should we check?</p><div
class="wp_syntax"><div
class="code"><pre class="sql" style="font-family:monospace;">d TABLE1
&nbsp;
<span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">&quot;public.TABLE1&quot;</span>
<span style="color: #993333; font-weight: bold;">COLUMN</span>  <span style="color: #66cc66;">|</span>  <span style="color: #993333; font-weight: bold;">TYPE</span>                 <span style="color: #66cc66;">|</span> Modifiers
<span style="color: #808080; font-style: italic;">--------+-----------------------+-----------</span>
COL1    <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">VARYING</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">35</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span></pre></div></div><p>Such a simple yet effective trick. Of course it'd be nicer if this is somehow included in a more proper way in the database, but this does the job.</p> ]]></content:encoded> <wfw:commentRss>http://sniptools.com/databases/resize-a-column-in-a-postgresql-table-without-changing-data/feed/</wfw:commentRss> <slash:comments>8</slash:comments> </item> <item><title>Upgrade PostgreSQL within the same &quot;minor version&quot; (8.2.3 -&gt; 8.2.9)</title><link>http://sniptools.com/databases/upgrade-postgresql-within-the-same-minor-version</link> <comments>http://sniptools.com/databases/upgrade-postgresql-within-the-same-minor-version#comments</comments> <pubDate>Mon, 28 Apr 2008 04:30:58 +0000</pubDate> <dc:creator>Shanx</dc:creator> <category><![CDATA[Databases]]></category> <category><![CDATA[Linux]]></category> <category><![CDATA[postgresql]]></category><guid
isPermaLink="false">http://sniptools.com/?p=377</guid> <description><![CDATA[PostgreSQL is a full-fledged enterprise-grade database brought into the public domain. Many homemade webmasters now have to deal with what is a very viable — if not an outright better — alternative to the raging popularity of a significantly simpler MySQL. Yet, for all its merits and addictive features, PostgreSQL is a beast to upgrade [...]]]></description> <content:encoded><![CDATA[<p>PostgreSQL is a full-fledged enterprise-grade database brought into the public domain. Many homemade webmasters now have to deal with what is a very viable — if not an outright better — alternative to the raging popularity of a significantly simpler MySQL.</p><p>Yet, for all its merits and addictive features, PostgreSQL is a <a
href="http://www.devissues.com/upgrading-postgresql-new-major-version">beast to upgrade</a> between major version releases.</p><p>If you were going from 8.2.x to the latest stable at the time of this writing, 8.3.x, then the move from the 8.2 series to 8.3 would require you to dump/restore your entire database. Not a simple chore if you have a 100 GB worth of critical data, but you have no choice as the innards of PG typically change between "major upgrades". For this, replication technologies such as <a
href="http://slony.info">Slony</a> are a useful option as you can install the new database separately, replicate the data over, and then make a switch when the two databases are mirrored — thereby avoiding the downtime of the hassle (here's an <a
href="http://www.pgcon.org/2007/schedule/events/20.en.html">informative PGCON presentation</a>).</p><h2>Fortunately, minor version upgrades are much simpler.</h2><p>No dump/restore is needed. If you were to go from, say 8.2.<span
style="color: #cc0000;">3</span> to 8.2.<span
style="color: #cc0000;">9</span>, all you would have to do is to upgrade the RPMs. I'm assuming Linux here but the same would work for other platforms.</p><ol><li>Check what version you have currently installed:<div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;">rpm <span style="color: #660033;">-qa</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> postgres <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tee</span> <span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span>rpm_pgsql</pre></div></div></li><li>Stop your postgres server<div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>service postgresql stop</pre></div></div></li><li>We will now simply execute the RPM upgrade command, which of course assumes that you have the latest <a
href="http://www.postgresql.org/download/">RPMs downloaded</a> from the PG website. Notice below that they're all lumped into the same command to prevent any (unlikely) possibility of dependency issues, so make sure all of this appears on one line — the backslash is included here for formatting (so it won't break on the command line).<div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;">rpm <span style="color: #660033;">-Uvh</span> 
  postgresql-8.2.9-1PGDG.rhel4.i386.rpm 
  postgresql-contrib-8.2.9-1PGDG.rhel4.i386.rpm 
  postgresql-devel-8.2.9-1PGDG.rhel4.i386.rpm 
  postgresql-libs-8.2.9-1PGDG.rhel4.i386.rpm 
  postgresql-server-8.2.9-1PGDG.rhel4.i386.rpm</pre></div></div></li><li>Hopefully that went without a hitch. Now restart the postgresql server:<div
class="wp_syntax"><div
class="code"><pre class="bash" style="font-family:monospace;">service postgresql restart</pre></div></div></li></ol><p>That's it. That should do it. It is highly unlikely that you'll see errors. And if you're running an older 8.2.x, it's highly recommended you upgrade to 8.2.9 anyway due to performance increases and some bug fixes.</p> ]]></content:encoded> <wfw:commentRss>http://sniptools.com/databases/upgrade-postgresql-within-the-same-minor-version/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)
Database Caching 5/8 queries in 0.002 seconds using disk: basic
Object Caching 688/691 objects using disk: basic

Served from: sniptools.com @ 2012-05-17 19:13:43 -->
