<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shrikaran</title>
    <description>The latest articles on DEV Community by Shrikaran (@shrikarankanagaraj).</description>
    <link>https://dev.to/shrikarankanagaraj</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F749747%2F0fb1121d-1a83-4d94-b263-85fd4b3bf086.jpg</url>
      <title>DEV Community: Shrikaran</title>
      <link>https://dev.to/shrikarankanagaraj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shrikarankanagaraj"/>
    <language>en</language>
    <item>
      <title>PostgreSQL - Change The Type Of A Column, A Walkthrough With Examples</title>
      <dc:creator>Shrikaran</dc:creator>
      <pubDate>Tue, 10 Oct 2023 18:52:59 +0000</pubDate>
      <link>https://dev.to/shrikarankanagaraj/postgresql-change-the-type-of-a-column-a-walkthrough-with-examples-1fkf</link>
      <guid>https://dev.to/shrikarankanagaraj/postgresql-change-the-type-of-a-column-a-walkthrough-with-examples-1fkf</guid>
      <description>&lt;h3&gt;
  
  
  How to change a PostgreSQL column type without messing up your table, losing data, or taking down your database.
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vHAuvfkD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://raw.githubusercontent.com/Shrikaran-Kanagaraj/PrivateSpace/main/Change.jpg%3Ftoken%3DGHSAT0AAAAAACIVIX2UP2F42FYLUHHKA4EEZJFTHXA" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vHAuvfkD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://raw.githubusercontent.com/Shrikaran-Kanagaraj/PrivateSpace/main/Change.jpg%3Ftoken%3DGHSAT0AAAAAACIVIX2UP2F42FYLUHHKA4EEZJFTHXA" alt="Change Image" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"In the world of databases, evolution is key. Altering tables is the chisel that sculpts raw data into refined information, ensuring your applications stay agile and responsive to the demands of tomorrow."&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;When operating a PostgreSQL database, we might at some point need to change the data type of a column. For example, maybe changing a &lt;code&gt;TEXT&lt;/code&gt; type column into &lt;code&gt;VARCHAR&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;PostgreSQL provides a simple syntax to do this, which we’ll walk through in this article. If you’re operating a complex or large database there are some gotchas, also explored below.&lt;/p&gt;

&lt;h3&gt;
  
  
  Syntax for PostgreSQL Column Type Change &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;To change a column type we need to run an &lt;a href="https://www.postgresql.org/docs/9.1/sql-altertable.html"&gt;ALTER TABLE statement&lt;/a&gt;, specifying the column and the new data type. Unlike some other databases (eg. MySQL) you can change a column type without having to also change everything else, like nullability or the default value.&lt;/p&gt;

&lt;p&gt;Here’s a basic example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE tableName ALTER COLUMN columnName SET DATA TYPE newDataType;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;SET DATA TYPE&lt;/code&gt; can be shortened to simply &lt;code&gt;TYPE&lt;/code&gt;, which I prefer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE tableName ALTER COLUMN columnName TYPE newDataType;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  PostgreSQL Change Column Type Examples
&lt;/h3&gt;

&lt;p&gt;First, for demonstration purposes, let’s create a &lt;code&gt;books&lt;/code&gt; table and insert some data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE books (
    id serial PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    author VARCHAR(255) NOT NULL DEFAULT 'Unknown Author',
    pulished_date DATE NOT NULL
);

INSERT INTO books(id, title, author, pulished_date) 
VALUES(1001,'How to use PostgreSQL','Matthew Rathbone','1989-03-07'),(1001,'Sunshine is Lovely','Joe Bloggs','2020-02-05');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suppose we want to change the data type of the &lt;code&gt;title&lt;/code&gt; column from &lt;code&gt;VARCHAR&lt;/code&gt; to &lt;code&gt;TEXT&lt;/code&gt;. We can do this by executing the following PostgreSQL code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE book ALTER COLUMN title TYPE TEXT;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also change the types of more than one column at a time. This can be done in a single statement using commas for each column change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE book 
ALTER COLUMN title TYPE TEXT,
ALTER COLUMN author TYPE TEXT;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is straightforward, and it works because Postgres allows changes between compatible data types implicitly, that means it converts the values automatically. This isn’t always true, which we’ll see below.&lt;/p&gt;

&lt;h4&gt;
  
  
  Constraint Problems Caused By Changing Column Type in PostgreSQL
&lt;/h4&gt;

&lt;p&gt;If you are altering a column type, it may make some of your constraints incompatible. For example the &lt;code&gt;DEFAULT&lt;/code&gt; value for a column, or any type of foreign key relationship. It is recommended that you at least review your constraints before changing a column type.&lt;/p&gt;

&lt;h3&gt;
  
  
  Changing a PostgreSQL Column To An Incompatible Data Type
&lt;/h3&gt;

&lt;p&gt;PostgreSQL also allows us to change between incompatible data types while preserving the data. For example, suppose you want to change a &lt;code&gt;TEXT&lt;/code&gt; data type column to &lt;code&gt;INTEGER&lt;/code&gt;. You might think it was as simple as this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE book ALTER COLUMN title TYPE integer;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, When we execute the statement, it will return an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ERROR: column "title" cannot be cast automatically to type integer SQL state: 42804. 
Hint: Specify a USING expression to perform the conversion
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PostgreSQL doesn’t know how to cast the column data into an &lt;code&gt;INTEGER&lt;/code&gt;from a &lt;code&gt;VARCHAR&lt;/code&gt;. We’ll have to tell PostgreSQL how to do this explicitly with the &lt;code&gt;USING&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;The syntax looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE tableName
ALTER COLUMN columnName TYPE newDataType USING (expressionGoesHere);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using ‘USING’ When Changing a Column Type In PostgreSQL
&lt;/h3&gt;

&lt;p&gt;Grammar aside, &lt;code&gt;USING&lt;/code&gt; allows us to explicitly convert column data when changing the column type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE book
ALTER COLUMN title TYPE INTEGER USING (char_length(title));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this simple example we’re converting the value of the &lt;code&gt;title&lt;/code&gt; column to be a character count for the original title using a built in PostgreSQL function.&lt;/p&gt;

&lt;p&gt;While this example is fabricated, this is a foundation for much more complex and useful transformations. For example we could create a custom function that performs a complex operation and use this instead.&lt;/p&gt;

&lt;h3&gt;
  
  
  USING Is Not Always Required For Column Type Changes
&lt;/h3&gt;

&lt;p&gt;PostgreSQL will implicitly cast the common values and the constraints to the new data type if you do not use the &lt;code&gt;USING&lt;/code&gt; clause, but this doesn’t work for all column type changes. When we use the &lt;code&gt;USING&lt;/code&gt; clause, we provide an explicit way to transform the values.&lt;/p&gt;

&lt;p&gt;If you do not use the &lt;code&gt;USING&lt;/code&gt; clause, castings can fail and throw errors, like we saw above.&lt;/p&gt;

&lt;h3&gt;
  
  
  WARNING: PostgreSQL Column Type Changes Will Lock The Table
&lt;/h3&gt;

&lt;p&gt;When changing the type of a column to a type that has a different internal representation, PostgreSQL will lock the table with an &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; lock in order to rewrite the whole table on disk. This is crazy slow and expensive, we do not want this to happen to a production database in the middle of the day!&lt;/p&gt;

&lt;p&gt;There are some type changes that do not require a lock, but there’s no single place where these are documented (they’re not documented on the &lt;a href="https://www.postgresql.org/docs/9.1/sql-altertable.html"&gt;&lt;code&gt;ALTER TABLE&lt;/code&gt;  docs&lt;/a&gt; for example), so it’s kind of hard to tell what sort of pain your change will cause.&lt;/p&gt;

&lt;p&gt;There are a few hints at exceptions in the PostgreSQL mailing lists, for example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.postgresql.org/message-id/flat/E1PoFlG-0005Et-J1%40gemulon.postgresql.org"&gt;This Commit&lt;/a&gt; for not locking for &lt;code&gt;VARCHAR&lt;/code&gt; -&amp;gt; &lt;code&gt;TEXT&lt;/code&gt; conversions, and any other conversions that are binary compatible.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.depesz.com/2012/02/14/waiting-for-9-2-more-rewrite-less-alter-table-alter-types/"&gt;These Commits&lt;/a&gt; for not locking when increasing or removing type lengths (eg going from &lt;code&gt;VARCHAR(100)&lt;/code&gt; to &lt;code&gt;VARCHAR(255)&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Honestly it’s hard to know for certain if a table lock will be required by just looking through documentation.&lt;/p&gt;

&lt;p&gt;Fear not, there is an alternative.&lt;/p&gt;

&lt;h3&gt;
  
  
  An Alternative to Changing Column Type in PostgreSQL - Adding a New Column Instead
&lt;/h3&gt;

&lt;p&gt;Changing a column type is fairly simple, but for a large database with millions of records and a lot of concurrent queries it is also very expensive because of the &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; table lock.&lt;/p&gt;

&lt;p&gt;If we are working with a large or busy database, or we just want to have some extra assurances that we will not lose data, I prefer simply adding a new column rather than changing the type of an existing one.&lt;/p&gt;

&lt;p&gt;This workflow looks something like:&lt;/p&gt;

&lt;p&gt;1.Create a new column, let’s call it title_length:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE books ADD COLUMN title_length INTEGER NOT NULL DEFAULT 0;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.Set up a create/update trigger to write to this new column automatically when writing to the old one, &lt;a href="https://stackoverflow.com/questions/16102188/postgresql-insert-trigger-to-set-value"&gt;something like this&lt;/a&gt;.&lt;br&gt;
3.Backfill the new column manually with values from the old column. In our case&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UPDATE books SET title_length = char_length(title);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4.Update all of your code to start using the new column.&lt;br&gt;
5.Either rename the old column to &lt;code&gt;deprecated_title&lt;/code&gt; and rename &lt;code&gt;title_length&lt;/code&gt; to title, or just drop the old title column (and the temporary trigger) entirely.&lt;/p&gt;

&lt;p&gt;Wow, that’s way more information than I thought we’d have to go through. It’s a lot to remember!&lt;/p&gt;

&lt;p&gt;Remember, always review your constraints before making any column type changes. This will help ensure a smooth transition without compromising data integrity. Happy coding!&lt;/p&gt;




</description>
      <category>postgres</category>
      <category>database</category>
      <category>popstgresql</category>
      <category>alter</category>
    </item>
  </channel>
</rss>
