<?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; postgresql sql databases</title> <atom:link href="http://sniptools.com/tag/postgresql-sql-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>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> </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 4/12 queries in 0.002 seconds using disk: basic
Object Caching 517/536 objects using disk: basic

Served from: sniptools.com @ 2012-05-21 19:10:52 -->
