<?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: Son Tran</title>
    <description>The latest articles on DEV Community by Son Tran (@tbson87).</description>
    <link>https://dev.to/tbson87</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F79995%2F6de9d37a-5521-4b7a-a835-9787e74caf0c.jpg</url>
      <title>DEV Community: Son Tran</title>
      <link>https://dev.to/tbson87</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tbson87"/>
    <language>en</language>
    <item>
      <title>Which Tables Reference This Table? Finding Incoming Foreign Keys in PostgreSQL, MySQL, SQL Server and SQLite</title>
      <dc:creator>Son Tran</dc:creator>
      <pubDate>Mon, 30 Nov 2026 02:50:19 +0000</pubDate>
      <link>https://dev.to/tbson87/which-tables-reference-this-table-finding-incoming-foreign-keys-in-postgresql-mysql-sql-server-2pgg</link>
      <guid>https://dev.to/tbson87/which-tables-reference-this-table-finding-incoming-foreign-keys-in-postgresql-mysql-sql-server-2pgg</guid>
      <description>&lt;p&gt;&lt;em&gt;Disclosure: I build &lt;a href="https://schemity.com" rel="noopener noreferrer"&gt;Schemity&lt;/a&gt;, a desktop ERD tool - this post is from our blog and uses it for the examples.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; Ask the catalogue for foreign keys whose referenced table is yours: &lt;code&gt;pg_constraint.confrelid&lt;/code&gt; in PostgreSQL, &lt;code&gt;REFERENCED_TABLE_NAME&lt;/code&gt; in MySQL, &lt;code&gt;sys.foreign_keys.referenced_object_id&lt;/code&gt; in SQL Server, and &lt;code&gt;pragma_foreign_key_list&lt;/code&gt; across every table in SQLite. Then check two things the list does not tell you: whether a key appears once per partition, and whether it still enforces anything, because disabled and unvalidated keys are listed like the rest.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To find the tables that reference a table, ask the database's catalogue for foreign keys whose referenced table is yours. Every engine records that, but each stores it differently, and the answer it gives back has two traps: some engines list one foreign key several times, and most engines can list keys that enforce nothing. A third gap is a reference nobody declared, which no catalogue can show.&lt;/p&gt;

&lt;p&gt;Everything below was run in throwaway containers on PostgreSQL 18.3, MySQL 8.4.11 and SQL Server 2022 (CU26), and with the sqlite3 3.54.0 shell, against a &lt;code&gt;customers&lt;/code&gt; table with &lt;code&gt;orders&lt;/code&gt;, &lt;code&gt;addresses&lt;/code&gt; and a &lt;code&gt;payments&lt;/code&gt; table in a second schema pointing at it.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to find which tables reference a table
&lt;/h2&gt;

&lt;p&gt;Query the foreign keys whose target is your table: one catalogue lookup per engine, with SQLite as the exception that has to scan every table.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Database&lt;/th&gt;
&lt;th&gt;Where incoming keys are recorded&lt;/th&gt;
&lt;th&gt;The &lt;code&gt;ON DELETE&lt;/code&gt; action&lt;/th&gt;
&lt;th&gt;Quick look&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;PostgreSQL&lt;/td&gt;
&lt;td&gt;&lt;code&gt;pg_constraint.confrelid&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;confdeltype&lt;/code&gt;, or &lt;code&gt;pg_get_constraintdef&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;psql &lt;code&gt;\d customers&lt;/code&gt;, under Referenced by&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MySQL&lt;/td&gt;
&lt;td&gt;&lt;code&gt;information_schema.KEY_COLUMN_USAGE.REFERENCED_TABLE_NAME&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;REFERENTIAL_CONSTRAINTS.DELETE_RULE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;none: &lt;code&gt;SHOW CREATE TABLE&lt;/code&gt; shows only outgoing keys&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SQL Server&lt;/td&gt;
&lt;td&gt;&lt;code&gt;sys.foreign_keys.referenced_object_id&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;delete_referential_action_desc&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;EXEC sp_fkeys @pktable_name = 'customers'&lt;/code&gt;, with the rule as a number (0 is &lt;code&gt;CASCADE&lt;/code&gt;) and no disabled flag&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SQLite&lt;/td&gt;
&lt;td&gt;nowhere: each table lists only its own keys&lt;/td&gt;
&lt;td&gt;&lt;code&gt;pragma_foreign_key_list(...).on_delete&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The queries, one per engine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- PostgreSQL&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;conrelid&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;referencing_table&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;conname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nf"&gt;pg_get_constraintdef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;definition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;convalidated&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_constraint&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;contype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'f'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;confrelid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'customers'&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;conparentid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- MySQL&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;TABLE_SCHEMA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;TABLE_NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;CONSTRAINT_NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;COLUMN_NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;DELETE_RULE&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;information_schema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;KEY_COLUMN_USAGE&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;information_schema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;REFERENTIAL_CONSTRAINTS&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;
  &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;CONSTRAINT_SCHEMA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;CONSTRAINT_SCHEMA&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;CONSTRAINT_NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;CONSTRAINT_NAME&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;REFERENCED_TABLE_SCHEMA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'shop'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;REFERENCED_TABLE_NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'customers'&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;TABLE_SCHEMA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;TABLE_NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;ORDINAL_POSITION&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- SQL Server&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="no"&gt;OBJECT_SCHEMA_NAME&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent_object_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;referencing_schema&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="no"&gt;OBJECT_NAME&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parent_object_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;referencing_table&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;delete_referential_action_desc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;is_disabled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;is_not_trusted&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;foreign_keys&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;referenced_object_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;OBJECT_ID&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'dbo.customers'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All three found the key from &lt;code&gt;payments&lt;/code&gt; in the other schema, because they filter on the referenced table, not on the referencing one. On MySQL that is a second database, &lt;code&gt;billing.payments&lt;/code&gt;, listed with &lt;code&gt;DELETE_RULE&lt;/code&gt; &lt;code&gt;NO ACTION&lt;/code&gt;, the rule MySQL records when none is written. A column declared with only an inline &lt;code&gt;customer_id BIGINT REFERENCES customers (id)&lt;/code&gt; did not appear at all, because MySQL 8.4 accepts that clause and creates no key, as the post on &lt;a href="https://schemity.com/blog/check-database-migrations-in-ci-without-a-database/" rel="noopener noreferrer"&gt;checking migrations in CI&lt;/a&gt; measured.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does the same foreign key appear three times in PostgreSQL?
&lt;/h2&gt;

&lt;p&gt;Because PostgreSQL copies a foreign key onto every partition of a partitioned table, and the copies are rows in &lt;code&gt;pg_constraint&lt;/code&gt; too.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;orders&lt;/code&gt; partitioned by year into &lt;code&gt;orders_2025&lt;/code&gt; and &lt;code&gt;orders_2026&lt;/code&gt;, the same lookup without &lt;code&gt;conparentid = 0&lt;/code&gt;, selecting &lt;code&gt;confdeltype&lt;/code&gt; for the action, returned five rows for &lt;code&gt;customers&lt;/code&gt; where there are three keys:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; referencing      | conname                    | confdeltype
------------------+----------------------------+-------------
 orders           | orders_customer_id_fkey    | r
 orders_2025      | orders_customer_id_fkey    | r
 orders_2026      | orders_customer_id_fkey    | r
 addresses        | addresses_customer_id_fkey | c
 billing.payments | payments_customer_id_fkey  | a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A key into a partitioned table is copied too, the other way round. The key from &lt;code&gt;invoices&lt;/code&gt; into &lt;code&gt;orders&lt;/code&gt; is stored three times, once pointing at &lt;code&gt;orders&lt;/code&gt; and once at each partition, so the incoming list for &lt;code&gt;orders&lt;/code&gt; shows it once, but &lt;code&gt;invoices&lt;/code&gt; shows three outgoing keys and each partition shows one incoming key. Each copy names its original in &lt;code&gt;conparentid&lt;/code&gt;, so keeping only &lt;code&gt;conparentid = 0&lt;/code&gt; returns one row per key in every direction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;orders_customer_id_fkey  (orders -&amp;gt; customers)
├── copy on orders_2025
└── copy on orders_2026
invoices_order_id_order_created_at_fkey  (invoices -&amp;gt; orders)
├── ..._fkey_1, pointing at orders_2025
└── ..._fkey_2, pointing at orders_2026
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;psql's &lt;code&gt;\d customers&lt;/code&gt; already hides them: its Referenced by section listed &lt;code&gt;addresses&lt;/code&gt;, &lt;code&gt;orders&lt;/code&gt; and &lt;code&gt;billing.payments&lt;/code&gt; once each, and marked the unvalidated key &lt;code&gt;NOT VALID&lt;/code&gt;. The &lt;code&gt;'customers'::regclass&lt;/code&gt; cast resolves the name through &lt;code&gt;search_path&lt;/code&gt;, so qualify it (&lt;code&gt;'public.customers'::regclass&lt;/code&gt;) when another schema has a table of the same name.&lt;/p&gt;

&lt;h2&gt;
  
  
  SQLite: why is there no reverse lookup?
&lt;/h2&gt;

&lt;p&gt;SQLite stores each foreign key only in the &lt;code&gt;CREATE TABLE&lt;/code&gt; of the table that declares it, so finding the incoming ones means reading every table's list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;referencing_table&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="s2"&gt;"from"&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;column_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="s2"&gt;"to"&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;referenced_column&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;on_delete&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sqlite_schema&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="nf"&gt;pragma_foreign_key_list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'table'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="s2"&gt;"table"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'customers'&lt;/span&gt; &lt;span class="k"&gt;COLLATE&lt;/span&gt; &lt;span class="no"&gt;NOCASE&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;COLLATE NOCASE&lt;/code&gt; matters. The referenced name is stored as written, and &lt;code&gt;addresses&lt;/code&gt; had been declared with &lt;code&gt;REFERENCES Customers (id)&lt;/code&gt;; a plain &lt;code&gt;= 'customers'&lt;/code&gt; returned only &lt;code&gt;orders&lt;/code&gt;. A key pointing at a table that does not exist, such as a typo &lt;code&gt;REFERENCES customer (id)&lt;/code&gt;, is accepted by &lt;code&gt;CREATE TABLE&lt;/code&gt; and simply never matches. The &lt;a href="https://www.sqlite.org/foreignkeys.html" rel="noopener noreferrer"&gt;SQLite foreign key documentation&lt;/a&gt; explains the other catch: enforcement is off unless each connection runs &lt;code&gt;PRAGMA foreign_keys = ON&lt;/code&gt;, and with it off an &lt;code&gt;orders&lt;/code&gt; row pointing at customer 42, who did not exist, went in without a complaint.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which listed foreign keys actually enforce anything?
&lt;/h2&gt;

&lt;p&gt;Not all of them: PostgreSQL lists unvalidated keys, SQL Server lists disabled and untrusted keys, and MySQL and SQLite keep no record of rows written with checks off, so read the state columns before trusting the list.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Database&lt;/th&gt;
&lt;th&gt;State to check&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;th&gt;Measured&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;PostgreSQL&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;convalidated = false&lt;/code&gt; (added &lt;code&gt;NOT VALID&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;New rows are checked; existing rows never were&lt;/td&gt;
&lt;td&gt;An address pointing at customer 99 stayed after the key was added&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PostgreSQL 18&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;conenforced = false&lt;/code&gt; (&lt;code&gt;NOT ENFORCED&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Nothing is checked&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SQL Server&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;is_disabled = 1&lt;/code&gt; (&lt;code&gt;NOCHECK CONSTRAINT&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;No check and no action&lt;/td&gt;
&lt;td&gt;A disabled &lt;code&gt;CASCADE&lt;/code&gt; key left the address behind when its customer was deleted&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SQL Server&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;is_not_trusted = 1&lt;/code&gt; (added &lt;code&gt;WITH NOCHECK&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Checked from now on; rows already there never were&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;payments&lt;/code&gt; kept a row pointing at customer 99&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MySQL&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;foreign_key_checks = 0&lt;/code&gt; on the session, with no flag in the catalogue&lt;/td&gt;
&lt;td&gt;Rows written while it was off were never checked&lt;/td&gt;
&lt;td&gt;An orphan address went in while &lt;code&gt;TABLE_CONSTRAINTS.ENFORCED&lt;/code&gt; still said &lt;code&gt;YES&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SQLite&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;PRAGMA foreign_keys&lt;/code&gt; on the connection&lt;/td&gt;
&lt;td&gt;Off by default, per connection&lt;/td&gt;
&lt;td&gt;An orphan order inserted with it off&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The SQL Server case is the one that misleads most. &lt;code&gt;sys.foreign_keys&lt;/code&gt; reported the disabled key on &lt;code&gt;addresses&lt;/code&gt; with &lt;code&gt;delete_referential_action_desc&lt;/code&gt; &lt;code&gt;CASCADE&lt;/code&gt; (and &lt;code&gt;is_not_trusted = 1&lt;/code&gt;, as every disabled key has). Deleting customer 2 succeeded and left address 6 holding &lt;code&gt;customer_id = 2&lt;/code&gt;, a customer that no longer existed. Anyone reading the catalogue to answer "what does deleting a customer take with it" would have been told the addresses go too.&lt;/p&gt;

&lt;p&gt;There is also what no catalogue records: a reference nobody declared. A Rails or Django app that never added the constraint still has &lt;code&gt;orders.customer_id&lt;/code&gt; meaning a customer, and the queries above return nothing for it. Finding those is a different job, covered in &lt;a href="https://schemity.com/blog/should-you-use-foreign-key-constraints/" rel="noopener noreferrer"&gt;whether to use foreign key constraints&lt;/a&gt;, which also shows how to count the orphans before declaring one.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Schemity show the keys pointing at a table?
&lt;/h2&gt;

&lt;p&gt;Schemity lists the foreign keys in the diagram that point at a table in that table's Keys tab, once each, with their columns and their &lt;code&gt;ON DELETE&lt;/code&gt; and &lt;code&gt;ON UPDATE&lt;/code&gt; actions.&lt;/p&gt;

&lt;p&gt;Schemity is database design software that reads your live database, shows the impact of every schema change before it runs, and keeps the diagram as a file in Git. Open a table and the read-only Keys tab of its drawer shows up to three lists: the primary key, the foreign keys the table declares, and Referenced by, the keys in other tables that point at it. A key the database does not enforce is tagged &lt;code&gt;NOT ENFORCED&lt;/code&gt;, and a relation you drew yourself for a reference nobody declared is tagged &lt;code&gt;virtual&lt;/code&gt; (see &lt;a href="https://schemity.com/doc/virtual-relations/" rel="noopener noreferrer"&gt;virtual relations&lt;/a&gt;). Selecting a table on the canvas draws every relation touching it at double weight, which is the same answer as a picture. That is the point of reading a live database to understand your schema: the list comes from the catalogue, not from someone's memory of it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fn54thpspyu1354w38qux.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fn54thpspyu1354w38qux.webp" alt="Schemity's Keys tab for the customers table on PostgreSQL 18.3: the primary key customers_pkey on id, and under Referenced by two foreign keys, addresses_customer_id_fkey on addresses(customer_id) with CASCADE on delete and NO ACTION on update, and orders_customer_id_fkey on orders(customer_id) with RESTRICT and NO ACTION, each listed once although orders is partitioned; the footer counts 4 entities and 3 relations." width="800" height="597"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Checking this post's partitioned &lt;code&gt;orders&lt;/code&gt; table found a bug. Schemity read only ordinary tables, so the partitioned parent was missing from the diagram, each partition showed up as a table of its own, and every key was drawn once per partition, the same repetition as the naive query above. In the next release a partitioned table is read as one table: its columns, keys, checks and indexes once, its row count and size summed across its partitions, and the views that read it listed as its dependents for &lt;a href="https://schemity.com/doc/impact-analysis/" rel="noopener noreferrer"&gt;impact analysis&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F77hbi9g8ig46jxvd2zhb.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F77hbi9g8ig46jxvd2zhb.webp" alt="Schemity's canvas on PostgreSQL 18.3 with the customers table selected: the partitioned orders table appears once, with created_at, id and customer_id and no orders_2025 or orders_2026, next to invoices and addresses; the relations from customers to orders and to addresses are drawn at double weight, and the footer counts 4 entities and 3 relations." width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What the Keys tab does not show matters as much. It lists keys between tables of the diagram's schema; a key from another schema, like &lt;code&gt;billing.payments&lt;/code&gt;, is read as a dependent for impact analysis but does not appear under Referenced by. A PostgreSQL key added &lt;code&gt;NOT VALID&lt;/code&gt; and a SQL Server key that is disabled or untrusted are shown as ordinary keys, so the disabled &lt;code&gt;CASCADE&lt;/code&gt; above would read as a cascade. Schemity does not yet know a table's partition key, so it does not warn that PostgreSQL refuses a &lt;code&gt;UNIQUE&lt;/code&gt; constraint on a partitioned table without it. And a reference nobody declared shows only if you draw it. Impact analysis reads the database, not your application, so an empty Referenced by list is not proof that nothing reads the table.&lt;/p&gt;

&lt;h2&gt;
  
  
  Before you rely on the list
&lt;/h2&gt;

&lt;p&gt;Run the catalogue query, then read the state of each key it returns, then ask what it cannot contain.&lt;/p&gt;

&lt;p&gt;On PostgreSQL filter &lt;code&gt;conparentid = 0&lt;/code&gt; and look at &lt;code&gt;convalidated&lt;/code&gt;; on SQL Server look at &lt;code&gt;is_disabled&lt;/code&gt; and &lt;code&gt;is_not_trusted&lt;/code&gt;; on SQLite compare names without case and remember enforcement is per connection; on MySQL filter on the referenced schema so keys from other databases come back too. Then look for the references nobody declared, because the catalogue only knows what was written down. For what happens when one of those keys fires, see &lt;a href="https://schemity.com/blog/on-delete-restrict-vs-no-action/" rel="noopener noreferrer"&gt;ON DELETE RESTRICT vs NO ACTION&lt;/a&gt; and &lt;a href="https://schemity.com/blog/postgres-drop-table-cascade-what-it-drops/" rel="noopener noreferrer"&gt;what DROP TABLE CASCADE drops in PostgreSQL&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>postgres</category>
      <category>mysql</category>
    </item>
    <item>
      <title>MySQL DATETIME vs TIMESTAMP: Which to Use, and What Converting a Column Does to Its Values</title>
      <dc:creator>Son Tran</dc:creator>
      <pubDate>Mon, 30 Nov 2026 02:49:59 +0000</pubDate>
      <link>https://dev.to/tbson87/mysql-datetime-vs-timestamp-which-to-use-and-what-converting-a-column-does-to-its-values-41bd</link>
      <guid>https://dev.to/tbson87/mysql-datetime-vs-timestamp-which-to-use-and-what-converting-a-column-does-to-its-values-41bd</guid>
      <description>&lt;p&gt;&lt;em&gt;Disclosure: I build &lt;a href="https://schemity.com" rel="noopener noreferrer"&gt;Schemity&lt;/a&gt;, a desktop ERD tool - this post is from our blog and uses it for the examples.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; Use &lt;code&gt;DATETIME&lt;/code&gt; for MySQL timestamps and have the application write UTC: it holds years 1000 to 9999 and stores exactly what it is given. &lt;code&gt;TIMESTAMP&lt;/code&gt; converts through the session time zone and refuses anything after 2038-01-19 03:14:07 UTC, which already rules it out for expiry dates and contracts. Converting a column between the two rewrites the table, and in either direction the session time zone decides what the values become.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A MySQL &lt;code&gt;TIMESTAMP&lt;/code&gt; column cannot hold a date after January 2038 and changes meaning with each connection's time zone, so &lt;code&gt;DATETIME&lt;/code&gt; filled with UTC by the application is the safer default. &lt;code&gt;TIMESTAMP&lt;/code&gt; looks like the more correct type, because MySQL converts it to UTC for you, but it ends in January 2038, it shifts every value with the connection's time zone, and it refuses some local times outright. The choice matters most on a table that already has data, because converting a column between the two can fail on rows you did not know about or quietly change what every value means.&lt;/p&gt;

&lt;p&gt;Everything below ran on MySQL 9.7.4 in a throwaway container, with the default strict &lt;code&gt;sql_mode&lt;/code&gt; and the container's time zone set to UTC.&lt;/p&gt;

&lt;h2&gt;
  
  
  MySQL DATETIME vs TIMESTAMP: which should you use?
&lt;/h2&gt;

&lt;p&gt;Use &lt;code&gt;DATETIME&lt;/code&gt; with the application writing UTC; pick &lt;code&gt;TIMESTAMP&lt;/code&gt; only if you want MySQL to convert through each session's time zone and every date will stay before 2038.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://dev.mysql.com/doc/refman/9.7/en/datetime.html" rel="noopener noreferrer"&gt;MySQL manual&lt;/a&gt; states both differences in two lines: "MySQL converts &lt;code&gt;TIMESTAMP&lt;/code&gt; values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as &lt;code&gt;DATETIME&lt;/code&gt;.)" and "&lt;code&gt;TIMESTAMP&lt;/code&gt; has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC."&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;DATETIME&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;TIMESTAMP&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Range&lt;/td&gt;
&lt;td&gt;1000-01-01 to 9999-12-31&lt;/td&gt;
&lt;td&gt;1970-01-01 00:00:01 to 2038-01-19 03:14:07 UTC&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stores&lt;/td&gt;
&lt;td&gt;The value as written&lt;/td&gt;
&lt;td&gt;UTC, converted from the session time zone&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Read back in another session time zone&lt;/td&gt;
&lt;td&gt;Unchanged&lt;/td&gt;
&lt;td&gt;Converted to that zone&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A local time that does not exist (spring-forward gap)&lt;/td&gt;
&lt;td&gt;Stored&lt;/td&gt;
&lt;td&gt;Refused (error 1292)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Storage&lt;/td&gt;
&lt;td&gt;5 bytes, plus up to 3 for fractional seconds&lt;/td&gt;
&lt;td&gt;4 bytes, plus up to 3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;DEFAULT CURRENT_TIMESTAMP&lt;/code&gt; and &lt;code&gt;ON UPDATE CURRENT_TIMESTAMP&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Allowed&lt;/td&gt;
&lt;td&gt;Allowed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Created by&lt;/td&gt;
&lt;td&gt;Django &lt;code&gt;DateTimeField&lt;/code&gt; (&lt;code&gt;datetime(6)&lt;/code&gt;), Rails &lt;code&gt;datetime&lt;/code&gt; and timestamps, Laravel &lt;code&gt;dateTime()&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Laravel &lt;code&gt;timestamps()&lt;/code&gt; and &lt;code&gt;timestamp()&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The storage figures are from the manual's &lt;a href="https://dev.mysql.com/doc/refman/9.7/en/storage-requirements.html" rel="noopener noreferrer"&gt;storage requirements page&lt;/a&gt;: one byte per column per row is the whole saving. The framework column is from each project's own source: Django's MySQL backend maps &lt;code&gt;DateTimeField&lt;/code&gt; to &lt;code&gt;datetime(6)&lt;/code&gt;, Rails' MySQL adapter maps its &lt;code&gt;datetime&lt;/code&gt; type to &lt;code&gt;datetime&lt;/code&gt;, and Laravel's documentation says &lt;code&gt;timestamps()&lt;/code&gt; creates "&lt;code&gt;TIMESTAMP&lt;/code&gt; equivalent columns". So a Laravel app on MySQL holds its &lt;code&gt;created_at&lt;/code&gt; in the type that ends in 2038, and a Django or Rails app does not.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does TIMESTAMP do with time zones that DATETIME does not?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;TIMESTAMP&lt;/code&gt; converts on the way in and on the way out using the session's &lt;code&gt;time_zone&lt;/code&gt;, so the same row reads differently from two connections, while &lt;code&gt;DATETIME&lt;/code&gt; returns exactly what was written.&lt;/p&gt;

&lt;p&gt;Both columns of one row were set to &lt;code&gt;2026-11-29 09:00:00&lt;/code&gt; from a session at &lt;code&gt;+07:00&lt;/code&gt;, then read back from a session at &lt;code&gt;+00:00&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;ev&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ts&lt;/span&gt; &lt;span class="kt"&gt;TIMESTAMP&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dt&lt;/span&gt; &lt;span class="kt"&gt;DATETIME&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;time_zone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'+07:00'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;ev&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'2026-11-29 09:00:00'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'2026-11-29 09:00:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;time_zone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'+00:00'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dt&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ev&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- ts: 2026-11-29 02:00:00   dt: 2026-11-29 09:00:00&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That conversion is the appeal of &lt;code&gt;TIMESTAMP&lt;/code&gt;, and also its catch: the meaning of every write depends on a setting each connection carries. A connection pool configured with &lt;code&gt;time_zone = 'SYSTEM'&lt;/code&gt; on a server whose system zone is not UTC, and &lt;code&gt;+00:00&lt;/code&gt; on another, writes different instants for the same text. With a named zone loaded, daylight saving adds two more surprises. In &lt;code&gt;America/New_York&lt;/code&gt;, &lt;code&gt;2026-03-08 02:30:00&lt;/code&gt; does not exist, and inserting it into the &lt;code&gt;TIMESTAMP&lt;/code&gt; failed with &lt;code&gt;Incorrect datetime value&lt;/code&gt; while the &lt;code&gt;DATETIME&lt;/code&gt; column took it. &lt;code&gt;2026-11-01 01:30:00&lt;/code&gt; happens twice, and MySQL stored the first of the two, &lt;code&gt;05:30&lt;/code&gt; UTC, without saying which it picked.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;DATETIME&lt;/code&gt; that the application always writes in UTC has none of these problems. The rule moves into the application, which already knows what zone its clock is in. If you need a value's original zone, store it in its own column; neither type keeps it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens to a TIMESTAMP column after 2038?
&lt;/h2&gt;

&lt;p&gt;It cannot hold the value: anything after '2038-01-19 03:14:07' UTC, or before 1970, fails the statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;ev&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'2038-01-19 03:14:07'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;-- stored&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;ev&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'2038-01-19 03:14:08'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- ERROR 1292 (22007): Incorrect datetime value: '2038-01-19 03:14:08' for column 'ts'&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;ev&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'1965-06-01 00:00:00'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- ERROR 1292 (22007): Incorrect datetime value: '1965-06-01 00:00:00' for column 'ts'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is about eleven years away for &lt;code&gt;created_at&lt;/code&gt;, but already here for any column that holds a future date: &lt;code&gt;expires_at&lt;/code&gt;, &lt;code&gt;valid_until&lt;/code&gt;, a contract end, a loan's last payment. A 15-year subscription sold today ends in 2041 and cannot be stored. Birth dates and historical records fail at the other end. &lt;code&gt;DATETIME&lt;/code&gt; takes all of them.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does converting between TIMESTAMP and DATETIME change?
&lt;/h2&gt;

&lt;p&gt;It rewrites the table either way, and the session time zone decides the result: to &lt;code&gt;DATETIME&lt;/code&gt; each stored instant becomes that session's wall-clock time, and to &lt;code&gt;TIMESTAMP&lt;/code&gt; every value outside 1970 to 2038 fails the statement.&lt;/p&gt;

&lt;p&gt;Two identical tables held &lt;code&gt;02:00&lt;/code&gt; UTC in a &lt;code&gt;TIMESTAMP&lt;/code&gt; column. Each was converted with the same statement, from sessions in different zones:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;time_zone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'+00:00'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="no"&gt;MODIFY&lt;/span&gt; &lt;span class="n"&gt;ts&lt;/span&gt; &lt;span class="kt"&gt;DATETIME&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;-- a.ts: 2026-11-29 02:00:00&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;time_zone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'+07:00'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="no"&gt;MODIFY&lt;/span&gt; &lt;span class="n"&gt;ts&lt;/span&gt; &lt;span class="kt"&gt;DATETIME&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;-- b.ts: 2026-11-29 09:00:00&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing failed and nothing warned. If the migration tool's connection uses the server's &lt;code&gt;SYSTEM&lt;/code&gt; zone and the server is not in UTC, every converted value is shifted by that offset, and only a reader who compares them with another column notices. Set the session to UTC before the &lt;code&gt;ALTER&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The other direction fails instead of shifting. Converting a &lt;code&gt;DATETIME&lt;/code&gt; column that held &lt;code&gt;2040-01-01&lt;/code&gt; to &lt;code&gt;TIMESTAMP&lt;/code&gt; stopped with error 1292 on that row, and MySQL rolled the &lt;code&gt;ALTER&lt;/code&gt; back, leaving the column a &lt;code&gt;DATETIME&lt;/code&gt;. Either way it is a full table copy: &lt;code&gt;ALGORITHM=INPLACE&lt;/code&gt; was refused ("Cannot change column type INPLACE") and so was &lt;code&gt;ALGORITHM=INSTANT&lt;/code&gt; ("Need to rebuild the table to change column type"), and a copy holds back writes for as long as it runs, as the post on &lt;a href="https://schemity.com/blog/mysql-alter-table-lock-instant-inplace-copy/" rel="noopener noreferrer"&gt;which &lt;code&gt;ALTER TABLE&lt;/code&gt; changes lock the table&lt;/a&gt; measured.&lt;/p&gt;

&lt;p&gt;Before converting to &lt;code&gt;TIMESTAMP&lt;/code&gt;, count the rows that will fail, with the session at &lt;code&gt;+00:00&lt;/code&gt; as the conversion should be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*) FROM subscriptions
WHERE expires_at &amp;lt; '1970-01-01 00:00:01' OR expires_at &amp;gt;= '2038-01-19 03:14:08';
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How does Schemity show what a DATETIME or TIMESTAMP change costs?
&lt;/h2&gt;

&lt;p&gt;Schemity's impact analysis reports a conversion to &lt;code&gt;TIMESTAMP&lt;/code&gt; as a change that fails if a value does not fit, counts the rows that will not, and plans the conversion in a UTC session.&lt;/p&gt;

&lt;p&gt;Schemity is database design software that reads your live database, shows the impact of every schema change before it runs, and keeps the diagram as a file in Git. Connected to the test database, changing &lt;code&gt;subscriptions.expires_at&lt;/code&gt; from &lt;code&gt;DATETIME&lt;/code&gt; to &lt;code&gt;TIMESTAMP&lt;/code&gt; on the diagram produced the planned SQL and two findings in the change preview, before anything ran:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;time_zone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'+00:00'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="sb"&gt;`subscriptions`&lt;/span&gt; &lt;span class="no"&gt;MODIFY&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="sb"&gt;`expires_at`&lt;/span&gt; &lt;span class="kt"&gt;TIMESTAMP&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://schemity.com/doc/impact-analysis/" rel="noopener noreferrer"&gt;Impact analysis&lt;/a&gt; read "Changes the type of subscriptions.expires_at, fails if a value does not fit: ~3 rows" and "Rewrites subscriptions to change expires_at: ~3 rows, 16 KB on disk", the 3 being the table's estimated row count. Its exact count used to count every non-&lt;code&gt;NULL&lt;/code&gt; value, here all 3. In the next release it counts only the values outside &lt;code&gt;TIMESTAMP&lt;/code&gt;'s range, and on that table it found the 2 subscriptions expiring in 2041 and 2126, the two rows that made the &lt;code&gt;ALTER&lt;/code&gt; fail when it ran. That is what it means to see the impact of every change: the failing rows are named before the copy starts. The &lt;code&gt;SET time_zone&lt;/code&gt; line is there because a change between the two types converts values through the session's zone; Schemity puts the connection's own zone back afterwards. The field editor offers &lt;code&gt;CURRENT_TIMESTAMP&lt;/code&gt; as a default on both types.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frw4ogqnbylsnf2k1diwk.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frw4ogqnbylsnf2k1diwk.webp" alt="Schemity's change preview on MySQL 9.7.4 after changing subscriptions.expires_at from DATETIME to TIMESTAMP: the planned migration SET time_zone = '+00:00'; then ALTER TABLE subscriptions MODIFY COLUMN expires_at TIMESTAMP NOT NULL, one planned change marked altered, and two impact findings, Changes the type of subscriptions.expires_at, fails if a value does not fit: ~3 rows, and Rewrites subscriptions to change expires_at: ~3 rows, 16 KB on disk." width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4t8pt2knwlaihl6p3tu4.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4t8pt2knwlaihl6p3tu4.webp" alt="Schemity's Impact drawer on MySQL 9.7.4 for the same change: under Can fail on existing data, Changes the type of subscriptions.expires_at, fails if a value does not fit: ~3 rows, with Exactly 2 rows beside Show on canvas after Count exactly was pressed; under Rewrites the table, Rewrites subscriptions to change expires_at: ~3 rows, 16 KB on disk." width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Three limits. Schemity does not warn that a &lt;code&gt;TIMESTAMP&lt;/code&gt; column will need to hold a date after 2038; it only counts the rows that already do when you convert. The count compares stored values against the range, so a &lt;code&gt;DATE&lt;/code&gt; or &lt;code&gt;DATETIME&lt;/code&gt; written in local time is counted as if it were UTC. And impact analysis reads the database, not your application: a migration written by Laravel or Django and run outside Schemity sets whatever session time zone its own connection has, which Schemity cannot see.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which type to choose, and how to switch
&lt;/h2&gt;

&lt;p&gt;Choose &lt;code&gt;DATETIME&lt;/code&gt; and write UTC; if a table already uses &lt;code&gt;TIMESTAMP&lt;/code&gt;, convert it in a UTC session after counting what would not fit.&lt;/p&gt;

&lt;p&gt;For an existing Laravel schema, &lt;code&gt;timestamps()&lt;/code&gt; columns can stay as they are until something needs a date past 2038, but columns that hold future dates belong in &lt;code&gt;DATETIME&lt;/code&gt; now (&lt;code&gt;$table-&amp;gt;dateTime('expires_at')&lt;/code&gt;). When converting a busy table in production, the usual route avoids the long copy altogether. Add a new &lt;code&gt;created_at_utc DATETIME NULL&lt;/code&gt; column, write both from the application, backfill in batches with &lt;code&gt;SET time_zone = '+00:00'&lt;/code&gt; on the backfill connection, switch reads, and a release later drop the old column and rename the new one to &lt;code&gt;created_at&lt;/code&gt;. On 9.7.4 the add, the drop and the rename each ran with &lt;code&gt;ALGORITHM=INSTANT&lt;/code&gt;, so only the batched backfill touches rows; without the &lt;code&gt;SET time_zone&lt;/code&gt;, a backfill run at &lt;code&gt;+07:00&lt;/code&gt; wrote &lt;code&gt;09:00&lt;/code&gt; for a &lt;code&gt;02:00&lt;/code&gt; UTC value. gh-ost and pt-online-schema-change are the other usual route, and their connections need the same session zone check. On PostgreSQL the same choice has the names the other way round; see &lt;a href="https://schemity.com/blog/postgres-timestamp-vs-timestamptz/" rel="noopener noreferrer"&gt;&lt;code&gt;timestamp&lt;/code&gt; vs &lt;code&gt;timestamptz&lt;/code&gt;&lt;/a&gt;, and &lt;a href="https://schemity.com/blog/mysql-to-postgresql-schema-conversion/" rel="noopener noreferrer"&gt;the MySQL to PostgreSQL conversion&lt;/a&gt; for which MySQL type becomes which.&lt;/p&gt;

</description>
      <category>mysql</category>
      <category>database</category>
      <category>sql</category>
      <category>webdev</category>
    </item>
    <item>
      <title>PostgreSQL json vs jsonb: Which to Use, and What Converting a Live Column Costs</title>
      <dc:creator>Son Tran</dc:creator>
      <pubDate>Mon, 30 Nov 2026 02:49:27 +0000</pubDate>
      <link>https://dev.to/tbson87/postgresql-json-vs-jsonb-which-to-use-and-what-converting-a-live-column-costs-2ok0</link>
      <guid>https://dev.to/tbson87/postgresql-json-vs-jsonb-which-to-use-and-what-converting-a-live-column-costs-2ok0</guid>
      <description>&lt;p&gt;&lt;em&gt;Disclosure: I build &lt;a href="https://schemity.com" rel="noopener noreferrer"&gt;Schemity&lt;/a&gt;, a desktop ERD tool - this post is from our blog and uses it for the examples.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; Use &lt;code&gt;jsonb&lt;/code&gt; unless you need the exact text you were sent: &lt;code&gt;json&lt;/code&gt; keeps whitespace, key order and duplicate keys, but the column itself cannot be indexed, compared, grouped or deduplicated, and every read parses it again. Converting a live &lt;code&gt;json&lt;/code&gt; column to &lt;code&gt;jsonb&lt;/code&gt; rewrites the table under a lock that blocks reads, fails outright on a &lt;code&gt;\\u0000&lt;/code&gt; escape or a lone surrogate, and silently keeps only the last of a duplicated key, so find those rows before the migration runs.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Use &lt;code&gt;jsonb&lt;/code&gt; for a JSON column in PostgreSQL unless your application needs back the exact text it stored. &lt;code&gt;json&lt;/code&gt; keeps the input as text, with its whitespace, key order and duplicate keys, and pays for that on every read and every query that compares values. The decision is easy for a new column. It is harder for an old &lt;code&gt;json&lt;/code&gt; column on a live table, because converting it rewrites every row, can fail on data you have never looked at, and changes some values without saying so.&lt;/p&gt;

&lt;p&gt;Everything below ran on PostgreSQL 18.3 in a throwaway container, in a UTF-8 database. The test table holds 1M order events of about 150 characters each, inserted from the same compact text into a &lt;code&gt;json&lt;/code&gt; table and a &lt;code&gt;jsonb&lt;/code&gt; table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;events_json&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="no"&gt;IDENTITY&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="kt"&gt;json&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;events_jsonb&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="no"&gt;IDENTITY&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="kt"&gt;jsonb&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- each payload looks like:&lt;/span&gt;
&lt;span class="c1"&gt;-- {"meta":{"ip":"10.0.7.7","source":"web"},"type":"order.refunded","items":[{"qty":2,"sku":"SKU-7"}],"amount":9.59,"currency":"EUR","customer_id":7}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Postgres json vs jsonb: which should you use?
&lt;/h2&gt;

&lt;p&gt;Use &lt;code&gt;jsonb&lt;/code&gt;: it was 6x faster to read a field from, it is the only one of the two whose column can be indexed or compared, and it costs some extra space and insert time.&lt;/p&gt;

&lt;p&gt;The PostgreSQL manual's &lt;a href="https://www.postgresql.org/docs/18/datatype-json.html" rel="noopener noreferrer"&gt;JSON types page&lt;/a&gt; gives the reason in one sentence: &lt;code&gt;json&lt;/code&gt; "stores an exact copy of the input text, which processing functions must reparse on each execution", while &lt;code&gt;jsonb&lt;/code&gt; is "stored in a decomposed binary format that makes it slightly slower to input due to added conversion overhead, but significantly faster to process". Its recommendation is just as direct: "most applications should prefer to store JSON data as &lt;code&gt;jsonb&lt;/code&gt;, unless there are quite specialized needs, such as legacy assumptions about ordering of object keys."&lt;/p&gt;

&lt;p&gt;On the 1M-row tables, with no index other than the primary key unless the row says so, each query repeated until the timing settled:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;json&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;jsonb&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Table size&lt;/td&gt;
&lt;td&gt;192 MB&lt;/td&gt;
&lt;td&gt;261 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Average value size&lt;/td&gt;
&lt;td&gt;159 bytes&lt;/td&gt;
&lt;td&gt;229 bytes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Insert 1M rows&lt;/td&gt;
&lt;td&gt;3.8 s&lt;/td&gt;
&lt;td&gt;6.0 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;WHERE payload-&amp;gt;&amp;gt;'type' = 'order.refunded'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;425 ms&lt;/td&gt;
&lt;td&gt;72 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;WHERE (payload-&amp;gt;&amp;gt;'customer_id')::int = 4242&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;424 ms&lt;/td&gt;
&lt;td&gt;131 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;WHERE payload @&amp;gt; '{"customer_id": 4242}'&lt;/code&gt; with a GIN index&lt;/td&gt;
&lt;td&gt;Not possible&lt;/td&gt;
&lt;td&gt;0.4 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;UNIQUE&lt;/code&gt;, btree or GIN index on the column&lt;/td&gt;
&lt;td&gt;Refused&lt;/td&gt;
&lt;td&gt;Allowed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Keeps whitespace, key order and duplicate keys&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;jsonb&lt;/code&gt; was 36% larger here. It drops the quotes and commas, but it stores an offset entry for every key and value and keeps numbers as &lt;code&gt;numeric&lt;/code&gt;, which costs more on documents made of many short fields like these. The GIN index (&lt;code&gt;jsonb_path_ops&lt;/code&gt;) added 35 MB. What you get for that is reading a field without parsing the whole document, and one index that serves containment queries on any key.&lt;/p&gt;

&lt;p&gt;An expression index works on either type: &lt;code&gt;CREATE INDEX ON events_json ((payload-&amp;gt;&amp;gt;'type'))&lt;/code&gt; is accepted, and it makes that one filter fast on &lt;code&gt;json&lt;/code&gt; too. The difference is that you need one per field you filter on, while a GIN index on a &lt;code&gt;jsonb&lt;/code&gt; column covers containment on all of them.&lt;/p&gt;

&lt;h2&gt;
  
  
  What can you not do with a json column?
&lt;/h2&gt;

&lt;p&gt;You cannot compare &lt;code&gt;json&lt;/code&gt; values at all, so anything that needs equality fails: &lt;code&gt;DISTINCT&lt;/code&gt;, &lt;code&gt;GROUP BY&lt;/code&gt;, &lt;code&gt;UNION&lt;/code&gt;, &lt;code&gt;=&lt;/code&gt;, a &lt;code&gt;UNIQUE&lt;/code&gt; constraint, and every btree or GIN index on the column.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- ERROR:  could not identify an equality operator for type json&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FROM e GROUP BY payload;
-- ERROR:  could not identify an equality operator for type json
SELECT * FROM e WHERE payload = '{"a":1}';
-- ERROR:  operator does not exist: json = unknown
ALTER TABLE e ADD CONSTRAINT e_payload_key UNIQUE (payload);
-- ERROR:  data type json has no default operator class for access method "btree"
CREATE INDEX ON e USING gin (payload);
-- ERROR:  data type json has no default operator class for access method "gin"
SELECT * FROM e WHERE payload @&amp;gt; '{"a":1}';
-- ERROR:  operator does not exist: json @&amp;gt; unknown
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;UNION&lt;/code&gt; failure is the one that tends to show up late, in a reporting query written months after the column. Field access with &lt;code&gt;-&amp;gt;&lt;/code&gt; and &lt;code&gt;-&amp;gt;&amp;gt;&lt;/code&gt; works on both types, which is why a &lt;code&gt;json&lt;/code&gt; column can go years without anyone noticing it is the wrong one: every read works, just slower than it should, and the first &lt;code&gt;GROUP BY payload&lt;/code&gt; or containment query fails.&lt;/p&gt;

&lt;h2&gt;
  
  
  When is json the right choice?
&lt;/h2&gt;

&lt;p&gt;Choose &lt;code&gt;json&lt;/code&gt; when the column is a record of exactly what you received, written once and read back whole, and nothing queries inside it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;json&lt;/code&gt; keeps the input as typed. &lt;code&gt;jsonb&lt;/code&gt; normalizes it, so the same input reads back differently:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="s1"&gt;'{"b": 1,  "a": 2, "a": 3}'&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;json&lt;/span&gt;  &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;as_json&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="s1"&gt;'{"b": 1,  "a": 2, "a": 3}'&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;jsonb&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;as_jsonb&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;--           as_json          |     as_jsonb&lt;/span&gt;
&lt;span class="c1"&gt;-- ---------------------------+------------------&lt;/span&gt;
&lt;span class="c1"&gt;--  {"b": 1,  "a": 2, "a": 3} | {"a": 3, "b": 1}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That makes &lt;code&gt;json&lt;/code&gt; a fair type for a raw webhook body or an audit log entry, where the point is to keep the text as it arrived. If a body has to be kept even when it is not valid JSON, or byte for byte, for example to check a signature computed over it, &lt;code&gt;text&lt;/code&gt; or &lt;code&gt;bytea&lt;/code&gt; is the safer column, because &lt;code&gt;json&lt;/code&gt; refuses malformed input. &lt;code&gt;json&lt;/code&gt; was also 26% smaller and inserted 1.6x faster in the test above, which matters for an append-only log nobody reads. As soon as a query filters, groups or joins on the contents, the column wants to be &lt;code&gt;jsonb&lt;/code&gt;. A common split is both: the raw text for the record, and a &lt;code&gt;jsonb&lt;/code&gt; column, or the fields you query as plain columns, for everything else.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does converting a json column to jsonb cost?
&lt;/h2&gt;

&lt;p&gt;A full table rewrite under a lock that blocks reads, and it can fail partway through that rewrite or change values without an error.&lt;/p&gt;

&lt;p&gt;The conversion is one statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;webhooks&lt;/span&gt; &lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="no"&gt;TYPE&lt;/span&gt; &lt;span class="kt"&gt;jsonb&lt;/span&gt; &lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;jsonb&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On a &lt;code&gt;webhooks&lt;/code&gt; table with 1M rows, three things happened in order.&lt;/p&gt;

&lt;p&gt;First, a view that read the column stopped it before it started:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ERROR:  cannot alter type of a column used by a view or rule
DETAIL:  rule _RETURN on view paid_webhooks depends on column "payload"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The view has to be dropped and recreated around the change, as with &lt;a href="https://schemity.com/blog/postgres-alter-column-type-used-by-view/" rel="noopener noreferrer"&gt;any type change under a view&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Second, with the view gone, one row out of a million failed the whole statement after 3.2 seconds of rewriting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ERROR:  unsupported Unicode escape sequence
DETAIL:  \u0000 cannot be converted to text.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;json&lt;/code&gt; accepts a &lt;code&gt;\u0000&lt;/code&gt; escape, because it only checks the syntax. &lt;code&gt;jsonb&lt;/code&gt; rejects it, "because that cannot be represented in PostgreSQL's &lt;code&gt;text&lt;/code&gt; type", in the manual's words. It also rejects a lone UTF-16 surrogate such as &lt;code&gt;"\ud800"&lt;/code&gt;, which &lt;code&gt;json&lt;/code&gt; stores without complaint and which JavaScript's &lt;code&gt;JSON.stringify&lt;/code&gt; writes for half of a broken emoji. That one fails as &lt;code&gt;invalid input syntax for type json&lt;/code&gt;, with &lt;code&gt;Unicode low surrogate must follow a high surrogate&lt;/code&gt;. Nothing was converted, and the 3.2 seconds were spent holding the lock.&lt;/p&gt;

&lt;p&gt;Third, after that row was deleted, the conversion took 3.6 seconds, wrote the table to a new file (&lt;code&gt;pg_relation_filenode&lt;/code&gt; changed), and kept only the last value of a duplicated key without a warning: &lt;code&gt;{"amount":10,"amount":12}&lt;/code&gt; became &lt;code&gt;{"amount": 12}&lt;/code&gt;. Every row not already in &lt;code&gt;jsonb&lt;/code&gt;'s own key order and spacing changed too, which here was all of them, and matters if anything compares the stored text or returns it to a client as-is.&lt;/p&gt;

&lt;p&gt;During those seconds the statement holds an &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; lock. With the conversion open in one session, a plain &lt;code&gt;SELECT&lt;/code&gt; on the table in another waited and hit its &lt;code&gt;lock_timeout&lt;/code&gt;. On a busy table, every query queued behind it waits too.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you find the rows before converting?
&lt;/h2&gt;

&lt;p&gt;Run one query for the values &lt;code&gt;jsonb&lt;/code&gt; refuses, which fail the statement, and one for the duplicate keys it will silently collapse.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- values jsonb refuses (PostgreSQL 16 and later)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;webhooks&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="nf"&gt;pg_input_is_valid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'jsonb'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- the same on any version: escaped backslashes out of the way first,&lt;/span&gt;
&lt;span class="c1"&gt;-- then valid surrogate pairs, then any \u0000 or surrogate left over&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;webhooks&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nf"&gt;regexp_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;regexp_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'\\\\'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'_'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'g'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'\\u[dD][89abAB][0-9a-fA-F]{2}\\u[dD][c-fC-F][0-9a-fA-F]{2}'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'_'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'g'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="o"&gt;~&lt;/span&gt; &lt;span class="s1"&gt;'\\u(0000|[dD][89a-fA-F][0-9a-fA-F]{2})'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- objects whose top-level keys repeat&lt;/span&gt;
&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;valid&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;MATERIALIZED&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;webhooks&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nf"&gt;pg_input_is_valid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'jsonb'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nf"&gt;json_typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'object'&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;valid&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FROM json_object_keys(payload))
   &amp;lt;&amp;gt; (SELECT count(*)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nf"&gt;jsonb_object_keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;jsonb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The version-independent query gave the same answer as &lt;code&gt;pg_input_is_valid&lt;/code&gt; on 200,007 generated values, valid and invalid escapes mixed. The duplicate-key query filters first, in a materialized CTE, because &lt;code&gt;json_object_keys&lt;/code&gt; fails on a refused value and on an array or a scalar, and a plain &lt;code&gt;AND&lt;/code&gt; does not guarantee the order the conditions run in. It only sees top-level keys: a repeat inside a nested object such as &lt;code&gt;{"a":{"x":1,"x":2}}&lt;/code&gt; is not found. All three scan the whole table, so run them on a replica or outside peak hours.&lt;/p&gt;

&lt;p&gt;On a large table in production, most teams avoid the long lock by not converting in place, and every step of this ran on the same 1M-row table. Add a nullable &lt;code&gt;jsonb&lt;/code&gt; column (10 ms) and a trigger that fills it on every insert and update before the backfill starts, so no row written during it is missed. Backfill in batches (100,000 rows took about 1 second) and fix or remove the refused rows as their batches fail. Add &lt;code&gt;CHECK (payload_jsonb IS NOT NULL) NOT VALID&lt;/code&gt; and validate it, which scans without blocking writes. Then swap in one short transaction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="k"&gt;LOCAL&lt;/span&gt; &lt;span class="n"&gt;lock_timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'2s'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;webhooks&lt;/span&gt; &lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;payload_jsonb&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;-- uses the validated check, no scan&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;webhooks&lt;/span&gt; &lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="n"&gt;webhooks_payload_jsonb_not_null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;TRIGGER&lt;/span&gt; &lt;span class="n"&gt;webhooks_payload_jsonb&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;webhooks&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;webhooks&lt;/span&gt; &lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;webhooks&lt;/span&gt; &lt;span class="no"&gt;RENAME&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;payload_json&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;webhooks&lt;/span&gt; &lt;span class="no"&gt;RENAME&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;payload_jsonb&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;paid_webhooks&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;paid_webhooks&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;'amount'&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;webhooks&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;'type'&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'order.paid'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The whole transaction took about 20 ms. The view has to be recreated inside it, because a view holds its columns by number, not name: without that step it keeps reading &lt;code&gt;payload_json&lt;/code&gt;. The old column loses &lt;code&gt;NOT NULL&lt;/code&gt; so new inserts, which no longer fill it, still work, and it stays until a later release drops it. Indexes and constraints on the old column do not move either and are rebuilt on the new one. The &lt;code&gt;lock_timeout&lt;/code&gt; makes a swap that cannot get its lock give up and be retried instead of queueing every query behind it, and the backfill leaves about a table's worth of dead rows for &lt;code&gt;VACUUM&lt;/code&gt;. It does not avoid the duplicate-key collapse: that still happens, one batch at a time.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Schemity show this before the migration runs?
&lt;/h2&gt;

&lt;p&gt;Schemity's lint flags a key PostgreSQL will refuse, and its impact analysis reports the rewrite, the lock and the blocking view, and in the next release counts the rows &lt;code&gt;jsonb&lt;/code&gt; will refuse.&lt;/p&gt;

&lt;p&gt;Schemity is database design software that reads your live database, shows the impact of every schema change before it runs, and keeps the diagram as a file in Git. When you design a table, &lt;a href="https://schemity.com/doc/schema-lint/" rel="noopener noreferrer"&gt;schema lint&lt;/a&gt; (Schemity Pro) reports a &lt;code&gt;UNIQUE&lt;/code&gt;, primary key or index on a &lt;code&gt;json&lt;/code&gt; column as "Unique key on a column type the database cannot index, so creating it fails", with the reason "PostgreSQL has no btree operator class for JSON", and a GIN index on one with "PostgreSQL has no GIN operator class for json; jsonb has one." A GIN index on a &lt;code&gt;jsonb&lt;/code&gt; column passes. In the test, PostgreSQL refused the same two statements lint flagged: the &lt;code&gt;UNIQUE&lt;/code&gt; on the &lt;code&gt;json&lt;/code&gt; column and its GIN index.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fa7mi0bsywv1co2orw49u.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fa7mi0bsywv1co2orw49u.webp" alt="Schemity's lint drawer on PostgreSQL 18.3 showing two Fails at runtime findings for a draft table audit_events with payload JSON (unique) and body JSONB: audit_events_payload_key, Unique key on a column type the database cannot index, so creating it fails, PostgreSQL has no btree operator class for JSON; and audit_events_payload_idx, Index on a column type the database cannot index, so creating it fails, PostgreSQL has no GIN operator class for json; jsonb has one. The GIN index on body is not flagged." width="800" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The change preview for the same draft shows the two statements PostgreSQL refuses, line 3's &lt;code&gt;UNIQUE&lt;/code&gt; on the &lt;code&gt;json&lt;/code&gt; column and line 6's GIN index on it, while line 7's GIN index on the &lt;code&gt;jsonb&lt;/code&gt; column is accepted:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fim4eu8w1pzict830lmu0.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fim4eu8w1pzict830lmu0.webp" alt="Schemity's change preview for creating audit_events on PostgreSQL 18.3: the planned migration CREATE TABLE &amp;quot;audit_events&amp;quot; with &amp;quot;id&amp;quot; BIGINT GENERATED ALWAYS AS IDENTITY NOT NULL PRIMARY KEY, &amp;quot;payload&amp;quot; JSON NOT NULL UNIQUE and &amp;quot;body&amp;quot; JSONB NOT NULL, then CREATE INDEX &amp;quot;audit_events_payload_idx&amp;quot; ON &amp;quot;audit_events&amp;quot; USING gin (&amp;quot;payload&amp;quot;) and CREATE INDEX &amp;quot;audit_events_body_idx&amp;quot; ON &amp;quot;audit_events&amp;quot; USING gin (&amp;quot;body&amp;quot;), one planned change, Table audit_events created, and Lint: 2 naming the unique key and the index on audit_events.payload." width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the conversion, &lt;a href="https://schemity.com/doc/impact-analysis/" rel="noopener noreferrer"&gt;impact analysis&lt;/a&gt; (Schemity Pro) reads the same change, made on the diagram or written as that &lt;code&gt;ALTER TABLE&lt;/code&gt; in a migration file, and reports that it rewrites &lt;code&gt;webhooks&lt;/code&gt; (~1M rows, 194 MB), blocks reads and writes while it runs, and is refused by the view &lt;code&gt;paid_webhooks&lt;/code&gt;. In the next release it also says the change fails if a value does not fit, with an exact count of the values holding a &lt;code&gt;\u0000&lt;/code&gt; escape or a lone surrogate, and it warns that values may not survive the cast, for the duplicate keys, key order and whitespace &lt;code&gt;jsonb&lt;/code&gt; does not keep. That is the point of being able to see the impact of every change: the row that would have failed the rewrite is counted before the lock is taken.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgikgsx2hqnkad718b4zv.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgikgsx2hqnkad718b4zv.webp" alt="Schemity's change preview for webhooks.payload changed from JSON to JSONB on PostgreSQL 18.3: the planned migration ALTER TABLE &amp;quot;webhooks&amp;quot; ALTER COLUMN &amp;quot;payload&amp;quot; TYPE JSONB USING &amp;quot;payload&amp;quot;::JSONB, one planned change, Column webhooks.payload altered, lint with no findings, and five impact findings: values may not survive the cast (~1M rows), fails if a value does not fit (~1M rows), rewrites webhooks to change payload (~1M rows, 194 MB on disk), holds back reads and writes to webhooks while it reads ~1M rows, and 1 object depends on webhooks.payload and the database refuses the change, the view paid_webhooks." width="799" height="527"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The preview shows the estimate. The exact count is in the impact drawer: on a copy of the table with a &lt;code&gt;\u0000&lt;/code&gt; row, a lone surrogate and an escaped backslash followed by &lt;code&gt;u0000&lt;/code&gt; added, Count exactly on the fails-if-a-value-does-not-fit line returned 2, the first two.&lt;/p&gt;

&lt;p&gt;What it does not see is your application. Impact analysis reads the database, not your code, so it cannot tell you that an API returns the stored text and its clients will see the keys in a new order, or that a test compares the payload as a string. It does not look for duplicate keys at all: its warning covers every value, and the query above is how to find the rows. In a database whose encoding is not UTF-8, &lt;code&gt;jsonb&lt;/code&gt; also refuses escapes for characters that encoding lacks, which the count does not include. Search the codebase for code that reads the column as text before converting it, and let the test suite cover the rest.&lt;/p&gt;

&lt;p&gt;The same kind of choice for strings, where the default is the less restrictive type and a limit is a rule you opt into, is in &lt;a href="https://schemity.com/blog/postgres-varchar-vs-text/" rel="noopener noreferrer"&gt;varchar vs text&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>sql</category>
      <category>json</category>
    </item>
    <item>
      <title>SQLite STRICT Tables: Should You Use Them, and What Converting an Existing Table Breaks</title>
      <dc:creator>Son Tran</dc:creator>
      <pubDate>Mon, 30 Nov 2026 02:49:07 +0000</pubDate>
      <link>https://dev.to/tbson87/sqlite-strict-tables-should-you-use-them-and-what-converting-an-existing-table-breaks-55n4</link>
      <guid>https://dev.to/tbson87/sqlite-strict-tables-should-you-use-them-and-what-converting-an-existing-table-breaks-55n4</guid>
      <description>&lt;p&gt;&lt;em&gt;Disclosure: I build &lt;a href="https://schemity.com" rel="noopener noreferrer"&gt;Schemity&lt;/a&gt;, a desktop ERD tool - this post is from our blog and uses it for the examples.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; Use &lt;code&gt;STRICT&lt;/code&gt; for new SQLite tables your own code writes to: it refuses &lt;code&gt;'abc'&lt;/code&gt; in an &lt;code&gt;INTEGER&lt;/code&gt; column, which an ordinary table stores without a word, and it cost about a quarter more insert time in a 1M-row test. Skip it when a framework declares types like &lt;code&gt;varchar&lt;/code&gt; or &lt;code&gt;datetime&lt;/code&gt;, which a &lt;code&gt;STRICT&lt;/code&gt; table refuses, or when anything older than SQLite 3.37.0 must open the file. There is no &lt;code&gt;ALTER TABLE&lt;/code&gt; to switch an existing table, so converting is a rebuild that stops at the first bad row.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A SQLite &lt;code&gt;STRICT&lt;/code&gt; table checks the type of every value written to it, so an &lt;code&gt;INTEGER&lt;/code&gt; column refuses &lt;code&gt;'abc'&lt;/code&gt; instead of storing it as text the way an ordinary SQLite table does. That makes it the right default for a new table your own code owns. It is the wrong choice where a framework writes the schema, because it accepts only six type names, and converting an existing table is not a switch: it is a full rebuild that fails on whatever bad data the table already holds.&lt;/p&gt;

&lt;p&gt;Everything below was run with the &lt;code&gt;sqlite3&lt;/code&gt; shell that ships with macOS, Apple's build of SQLite, which reports version 3.54.0. Each claim was reproduced in a scratch database file, and the timings are from a 1M-row table on an Apple silicon laptop.&lt;/p&gt;

&lt;h2&gt;
  
  
  Should I use strict tables in SQLite?
&lt;/h2&gt;

&lt;p&gt;Use &lt;code&gt;STRICT&lt;/code&gt; for a new table when your code declares the schema and every writer goes through it; keep an ordinary table when a framework declares the column types or an old SQLite has to read the file.&lt;/p&gt;

&lt;p&gt;SQLite's default is the opposite of most other databases: a column's declared type is a preference, called its affinity, not a rule. The &lt;a href="https://www.sqlite.org/datatype3.html" rel="noopener noreferrer"&gt;datatype documentation&lt;/a&gt; says SQLite converts a value when it can and otherwise stores it as given, so &lt;code&gt;'abc'&lt;/code&gt; goes into an &lt;code&gt;INTEGER&lt;/code&gt; column as text and stays there until a query that adds it up returns the wrong total. The &lt;a href="https://www.sqlite.org/stricttables.html" rel="noopener noreferrer"&gt;STRICT tables page&lt;/a&gt; added the other behaviour "as of version 3.37.0 (2021-11-27)", one table at a time.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Ordinary table&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;STRICT&lt;/code&gt; table&lt;/th&gt;
&lt;th&gt;Ordinary table with a &lt;code&gt;CHECK&lt;/code&gt; per column&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;'abc'&lt;/code&gt; into an &lt;code&gt;INTEGER&lt;/code&gt; column&lt;/td&gt;
&lt;td&gt;Stored as text&lt;/td&gt;
&lt;td&gt;Refused&lt;/td&gt;
&lt;td&gt;Refused&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;'42'&lt;/code&gt; into an &lt;code&gt;INTEGER&lt;/code&gt; column&lt;/td&gt;
&lt;td&gt;Stored as integer 42&lt;/td&gt;
&lt;td&gt;Stored as integer 42&lt;/td&gt;
&lt;td&gt;Stored as integer 42&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;NULL&lt;/code&gt; into a nullable column&lt;/td&gt;
&lt;td&gt;Stored&lt;/td&gt;
&lt;td&gt;Stored&lt;/td&gt;
&lt;td&gt;Refused, unless the check lists &lt;code&gt;'null'&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Declared types allowed&lt;/td&gt;
&lt;td&gt;Anything, &lt;code&gt;varchar(255)&lt;/code&gt; and &lt;code&gt;datetime(6)&lt;/code&gt; included&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;INT&lt;/code&gt;, &lt;code&gt;INTEGER&lt;/code&gt;, &lt;code&gt;REAL&lt;/code&gt;, &lt;code&gt;TEXT&lt;/code&gt;, &lt;code&gt;BLOB&lt;/code&gt;, &lt;code&gt;ANY&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Anything&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Readable by SQLite before 3.37.0&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Reports an error on open&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can be added to an existing table&lt;/td&gt;
&lt;td&gt;Nothing to add&lt;/td&gt;
&lt;td&gt;Only by rebuilding the table&lt;/td&gt;
&lt;td&gt;Rebuild, or &lt;code&gt;ADD CONSTRAINT&lt;/code&gt; on 3.53.0 and later&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Copy of 1M rows&lt;/td&gt;
&lt;td&gt;0.41-0.46 s&lt;/td&gt;
&lt;td&gt;0.52-0.54 s&lt;/td&gt;
&lt;td&gt;Not measured&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The insert cost is real but small: copying the same 1M rows of a five-column table (two integers, a real and two text columns) took 0.41-0.46 s into an ordinary table and 0.52-0.54 s into a &lt;code&gt;STRICT&lt;/code&gt; one, over four runs in alternating order, so roughly a quarter more. For a table written row by row from an application, that difference disappears into everything else the write does.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does a STRICT table refuse that an ordinary table stores?
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;STRICT&lt;/code&gt; table refuses any value it cannot convert to the column's type without loss, and stores the rest converted, while an ordinary table stores both.&lt;/p&gt;

&lt;p&gt;The same five inserts into an &lt;code&gt;INTEGER&lt;/code&gt; column of each kind of table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qty&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;STRICT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;qty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'abc'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;-- Error: cannot store TEXT value in INTEGER column s.qty&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;qty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1.5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;-- Error: cannot store REAL value in INTEGER column s.qty&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;qty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'123'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;-- stored as integer 123&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;qty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'1e3'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;-- stored as integer 1000&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;qty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;2.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;-- stored as integer 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On an ordinary table with the same column, &lt;code&gt;'abc'&lt;/code&gt; is stored as the text &lt;code&gt;abc&lt;/code&gt; and &lt;code&gt;1.5&lt;/code&gt; as the real number &lt;code&gt;1.5&lt;/code&gt;, both without an error. &lt;code&gt;STRICT&lt;/code&gt; still converts: &lt;code&gt;'123'&lt;/code&gt;, &lt;code&gt;' 123'&lt;/code&gt; with a leading space, &lt;code&gt;'1e3'&lt;/code&gt; and &lt;code&gt;2.0&lt;/code&gt; all arrive as integers, because each converts exactly. The error SQLite raises for the others is &lt;code&gt;SQLITE_CONSTRAINT_DATATYPE&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The less obvious difference is &lt;code&gt;ANY&lt;/code&gt;. In a &lt;code&gt;STRICT&lt;/code&gt; table, an &lt;code&gt;ANY&lt;/code&gt; column keeps a value exactly as it was sent, so &lt;code&gt;'0123'&lt;/code&gt; stays the text &lt;code&gt;0123&lt;/code&gt;. In an ordinary table, &lt;code&gt;ANY&lt;/code&gt; is not a type name SQLite recognises, so the column gets numeric affinity and &lt;code&gt;'0123'&lt;/code&gt; is stored as the integer &lt;code&gt;123&lt;/code&gt;, losing the leading zero. A column declared &lt;code&gt;STRING&lt;/code&gt; does the same in an ordinary table, which is why a product code or a postcode should be declared &lt;code&gt;TEXT&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which column types does a STRICT table accept?
&lt;/h2&gt;

&lt;p&gt;Six: &lt;code&gt;INT&lt;/code&gt;, &lt;code&gt;INTEGER&lt;/code&gt;, &lt;code&gt;REAL&lt;/code&gt;, &lt;code&gt;TEXT&lt;/code&gt;, &lt;code&gt;BLOB&lt;/code&gt; and &lt;code&gt;ANY&lt;/code&gt;, written without a length or precision.&lt;/p&gt;

&lt;p&gt;Every other declared type fails when the table is created:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="kt"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;STRICT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- Parse error: unknown datatype for a.name: "VARCHAR(255)"&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="kt"&gt;DATETIME&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;STRICT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- Parse error: unknown datatype for b.created_at: "DATETIME"&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flag&lt;/span&gt; &lt;span class="kt"&gt;BOOLEAN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;STRICT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- Parse error: unknown datatype for c.flag: "BOOLEAN"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;TEXT(100)&lt;/code&gt; and &lt;code&gt;INTEGER(10)&lt;/code&gt; fail the same way, and so does a column with no type at all ("missing datatype"). The same check runs on &lt;code&gt;ALTER TABLE ... ADD COLUMN&lt;/code&gt;: adding &lt;code&gt;email VARCHAR(100)&lt;/code&gt; to a &lt;code&gt;STRICT&lt;/code&gt; table fails with &lt;code&gt;error in table st after add column: unknown datatype for st.email: "VARCHAR(100)"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is where frameworks collide with &lt;code&gt;STRICT&lt;/code&gt;. Rails, for one, writes &lt;code&gt;varchar&lt;/code&gt;, &lt;code&gt;datetime(6)&lt;/code&gt; and &lt;code&gt;json&lt;/code&gt; into a SQLite schema and reads the type back from that declared text, which the &lt;a href="https://schemity.com/blog/sqlite-round-trip-campfire-jellyfin/" rel="noopener noreferrer"&gt;Campfire round trip&lt;/a&gt; measured: &lt;code&gt;datetime(6)&lt;/code&gt; came back as a datetime with precision 6, while a column declared &lt;code&gt;TEXT&lt;/code&gt; came back as text. A &lt;code&gt;STRICT&lt;/code&gt; table has to declare that timestamp &lt;code&gt;TEXT&lt;/code&gt;, which by the same mapping Rails would read as text unless the model overrides it, for example with &lt;code&gt;attribute :created_at, :datetime&lt;/code&gt;. Before switching a framework-managed table, check that the framework can declare the six names and still map them to the types your code expects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Can you convert an existing SQLite table to STRICT?
&lt;/h2&gt;

&lt;p&gt;Only by rebuilding it, and the rebuild stops at the first row whose value does not fit the new type.&lt;/p&gt;

&lt;p&gt;There is no &lt;code&gt;ALTER TABLE&lt;/code&gt; form for it: &lt;code&gt;ALTER TABLE orders STRICT&lt;/code&gt; is a syntax error. The conversion is the same four-statement rebuild SQLite uses for a &lt;a href="https://schemity.com/blog/sqlite-change-column-type-table-rebuild/" rel="noopener noreferrer"&gt;column type change&lt;/a&gt;, with the declared types rewritten into the six names:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- sqlite3 shell: stop at the first error (a comment on the next line breaks it)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bail&lt;/span&gt; &lt;span class="k"&gt;on&lt;/span&gt;
&lt;span class="no"&gt;PRAGMA&lt;/span&gt; &lt;span class="n"&gt;foreign_keys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;OFF&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;-- before BEGIN: inside a transaction it does nothing&lt;/span&gt;
&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders__new&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;qty&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="kt"&gt;REAL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;note&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;          &lt;span class="c1"&gt;-- was VARCHAR(200)&lt;/span&gt;
    &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt;     &lt;span class="c1"&gt;-- was DATETIME&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;STRICT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;orders__new&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qty&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;note&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qty&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;note&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders__new&lt;/span&gt; &lt;span class="no"&gt;RENAME&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;orders_created_at&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="no"&gt;PRAGMA&lt;/span&gt; &lt;span class="n"&gt;foreign_key_check&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="no"&gt;PRAGMA&lt;/span&gt; &lt;span class="n"&gt;foreign_keys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;.bail on&lt;/code&gt; line is not decoration. Run without it, as &lt;code&gt;sqlite3 app.db &amp;lt; convert.sql&lt;/code&gt;, the shell reports the failed &lt;code&gt;INSERT&lt;/code&gt; and carries on: it dropped the old table, renamed the empty new one into its place and committed, and all three rows of the test table were gone. From application code, roll back on the first error for the same reason.&lt;/p&gt;

&lt;p&gt;On a table holding one &lt;code&gt;'n/a'&lt;/code&gt; in &lt;code&gt;qty&lt;/code&gt;, the &lt;code&gt;INSERT&lt;/code&gt; failed with &lt;code&gt;cannot store TEXT value in INTEGER column orders__new.qty&lt;/code&gt; and copied nothing. It names the column but not the row, and it stops at the first bad value, so a table with three bad rows takes three failed attempts to discover them one at a time. Run a finder query first, with one condition per column, listing the storage classes the new type accepts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qty&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;qty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nf"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;qty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'integer'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'null'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="nf"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'real'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'integer'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'null'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the test table it returned all three bad rows in one pass: &lt;code&gt;'n/a'&lt;/code&gt; in &lt;code&gt;qty&lt;/code&gt;, &lt;code&gt;'free'&lt;/code&gt; in &lt;code&gt;total&lt;/code&gt;, and &lt;code&gt;1.5&lt;/code&gt; in &lt;code&gt;qty&lt;/code&gt;, which an ordinary &lt;code&gt;INTEGER&lt;/code&gt; column had stored as a real number. Where the conversion changes a column's affinity, such as a &lt;code&gt;VARCHAR&lt;/code&gt; column becoming &lt;code&gt;INTEGER&lt;/code&gt;, a text value that converts exactly, like &lt;code&gt;'42'&lt;/code&gt;, is listed too but would be accepted by the copy, so treat the list as the rows to look at, not all rows to delete.&lt;/p&gt;

&lt;p&gt;The rebuild also drops the old table's indexes and triggers, which is why the script recreates &lt;code&gt;orders_created_at&lt;/code&gt;; save their SQL from &lt;code&gt;sqlite_schema&lt;/code&gt; before you start. On 1M clean rows the whole transaction took about 1.2 s, half of it rebuilding the index, and &lt;code&gt;PRAGMA integrity_check&lt;/code&gt; returned &lt;code&gt;ok&lt;/code&gt; afterwards. SQLite allows one writer at a time, so every other connection that wants to write waits for that transaction if it set a busy timeout, and fails with &lt;code&gt;database is locked&lt;/code&gt; if it did not.&lt;/p&gt;

&lt;p&gt;In production, the safer release has three steps: deploy application code that validates the types so no new bad rows arrive, run the finder query and fix the rows it lists, then rebuild in a later release after taking a copy of the file with &lt;code&gt;VACUUM INTO 'backup.db'&lt;/code&gt; (0.36 s for the 77 MB test database). If the rebuild fails and the script stops there, nothing changed, and if something reads the converted table wrongly afterwards, the copy is the way back.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Schemity flag a type a STRICT table will refuse?
&lt;/h2&gt;

&lt;p&gt;Schemity reports a column type that a &lt;code&gt;STRICT&lt;/code&gt; table refuses on the diagram and in the migration's impact findings, before the schema migration runs.&lt;/p&gt;

&lt;p&gt;Schemity is database design software that reads your live database, shows the impact of every schema change before it runs, and keeps the diagram as a file in Git. When it reads a SQLite database, a SQL script or a migration file, each table keeps its &lt;code&gt;STRICT&lt;/code&gt; and &lt;code&gt;WITHOUT ROWID&lt;/code&gt; options, and the table rebuild Schemity plans for a type or &lt;code&gt;NOT NULL&lt;/code&gt; change writes them back, so a rebuilt &lt;code&gt;STRICT&lt;/code&gt; table stays &lt;code&gt;STRICT&lt;/code&gt; and an &lt;code&gt;ANY&lt;/code&gt; column keeps &lt;code&gt;'0123'&lt;/code&gt; as text.&lt;/p&gt;

&lt;p&gt;The field editor offers SQLite's declared types, &lt;code&gt;VARCHAR&lt;/code&gt; and &lt;code&gt;DATETIME&lt;/code&gt; among them, on every table. Picking one on a &lt;code&gt;STRICT&lt;/code&gt; table used to produce a migration SQLite refused at apply time. In the next release, &lt;a href="https://schemity.com/doc/schema-lint/" rel="noopener noreferrer"&gt;schema lint&lt;/a&gt; has a &lt;code&gt;sqlite-strict-type-refused&lt;/code&gt; rule in its Runtime group that marks the column on the canvas: "orders is a STRICT table, which refuses the type of coupon_code." &lt;a href="https://schemity.com/doc/impact-analysis/" rel="noopener noreferrer"&gt;Impact analysis&lt;/a&gt; reports the same column for the planned statement, "Gives orders.coupon_code a type that SQLite refuses: orders is STRICT, which takes only INT, INTEGER, REAL, TEXT, BLOB or ANY", so you see the failure where you read the rest of the migration's cost and can change the type to &lt;code&gt;TEXT&lt;/code&gt; before applying. That is what it takes to see the impact of every change: the refusal shows up in the plan, not as an error when you apply it. Both checks are part of Schemity Pro, and &lt;a href="https://schemity.com/doc/migration-check-ci/" rel="noopener noreferrer"&gt;Migration Check&lt;/a&gt; runs the same two on a pull request, reading which tables are &lt;code&gt;STRICT&lt;/code&gt; from the migration history.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F83i6ehqddp5ipwv9auov.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F83i6ehqddp5ipwv9auov.webp" alt="Schemity's Lint drawer with one Fails at runtime finding on orders.coupon_code: orders is a STRICT table, which refuses the type of coupon_code, beside the orders table on the canvas where coupon_code is declared VARCHAR(20) and marked in red in the margin." width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ft4jngppki13gsnquceqm.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ft4jngppki13gsnquceqm.webp" alt="Schemity's change preview on SQLite with two planned changes, Column customers.email altered to VARCHAR(200) and Column orders.coupon_code added as VARCHAR(20), and three impact findings: Gives orders.coupon_code a type that SQLite refuses, Gives customers.email a type that SQLite refuses, both naming the STRICT table and its six allowed types, and Rewrites customers to change email." width="800" height="616"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What these checks do not see matters as much. They read the schema, not the rows, so the values that would fail a conversion are found by the finder query, not by Schemity. Schemity cannot make a table &lt;code&gt;STRICT&lt;/code&gt;: the option is kept as read, not set from the diagram, so the conversion above is still a script you write. And nothing here knows what your framework expects back from a column, so a timestamp redeclared &lt;code&gt;TEXT&lt;/code&gt; passes every check and can still change what your framework hands your code.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to use STRICT, and when a CHECK is enough
&lt;/h2&gt;

&lt;p&gt;Make new tables you own &lt;code&gt;STRICT&lt;/code&gt;, and protect a framework-managed table with a per-column &lt;code&gt;CHECK&lt;/code&gt; instead.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;CHECK (typeof(qty) IN ('integer', 'null'))&lt;/code&gt; on an ordinary table gives the same refusal for one column without restricting the declared type: &lt;code&gt;'abc'&lt;/code&gt; fails with &lt;code&gt;CHECK constraint failed&lt;/code&gt;, &lt;code&gt;'42'&lt;/code&gt; passes because SQLite converts it to an integer before the check runs, and &lt;code&gt;NULL&lt;/code&gt; passes because the list names it, which a plain &lt;code&gt;typeof(qty) = 'integer'&lt;/code&gt; would not. It works on any SQLite version and leaves &lt;code&gt;datetime(6)&lt;/code&gt; declared as the framework wants it. Its cost is that you write one per column and nothing reminds you to add it to the next one.&lt;/p&gt;

&lt;p&gt;To decide, ask three questions about the table: does anything you do not control declare its types, does anything older than 3.37.0 open the file, and does the table already hold rows the finder query lists. Three answers of no make &lt;code&gt;STRICT&lt;/code&gt; the better table. For the other choices SQLite leaves to you, see &lt;a href="https://schemity.com/blog/rename-column-what-breaks-postgres-mysql-sql-server-sqlite/" rel="noopener noreferrer"&gt;what a column rename breaks&lt;/a&gt; and why a migration that &lt;a href="https://schemity.com/blog/transactional-ddl-postgres-mysql-cockroachdb/" rel="noopener noreferrer"&gt;fails halfway&lt;/a&gt; behaves differently on each database.&lt;/p&gt;

</description>
      <category>sqlite</category>
      <category>database</category>
      <category>sql</category>
      <category>webdev</category>
    </item>
    <item>
      <title>MySQL Generated Columns: VIRTUAL vs STORED, and Which One to Index</title>
      <dc:creator>Son Tran</dc:creator>
      <pubDate>Sun, 29 Nov 2026 05:31:20 +0000</pubDate>
      <link>https://dev.to/tbson87/mysql-generated-columns-virtual-vs-stored-and-which-one-to-index-58oi</link>
      <guid>https://dev.to/tbson87/mysql-generated-columns-virtual-vs-stored-and-which-one-to-index-58oi</guid>
      <description>&lt;p&gt;&lt;em&gt;Disclosure: I build &lt;a href="https://schemity.com" rel="noopener noreferrer"&gt;Schemity&lt;/a&gt;, a desktop ERD tool - this post is from our blog and uses it for the examples.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; Make a MySQL generated column &lt;code&gt;VIRTUAL&lt;/code&gt; unless it has to be a primary key, a foreign key, or carry a &lt;code&gt;FULLTEXT&lt;/code&gt; or &lt;code&gt;SPATIAL&lt;/code&gt; index, which only &lt;code&gt;STORED&lt;/code&gt; allows. Adding a &lt;code&gt;VIRTUAL&lt;/code&gt; column to 1,000,000 rows was instant and an index on it worked as well as one on a &lt;code&gt;STORED&lt;/code&gt; column, while adding the &lt;code&gt;STORED&lt;/code&gt; one copied the table for 4.8 seconds with writes blocked. The trap is changing a &lt;code&gt;VIRTUAL&lt;/code&gt; column's expression: &lt;code&gt;MODIFY&lt;/code&gt; copies the table too, and dropping and re-adding an unindexed column does not.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A MySQL generated column should be &lt;code&gt;VIRTUAL&lt;/code&gt; unless you need it as a primary key, a foreign key, or under a &lt;code&gt;FULLTEXT&lt;/code&gt; or &lt;code&gt;SPATIAL&lt;/code&gt; index, because a &lt;code&gt;VIRTUAL&lt;/code&gt; column costs nothing to add, takes no space in the row, and can still be indexed. &lt;code&gt;STORED&lt;/code&gt; buys cheaper reads of the unindexed value and those key and index types, and you pay for it with a full table copy when the column is added and again whenever its expression changes.&lt;/p&gt;

&lt;p&gt;Everything below ran on MySQL 8.4.11 in a throwaway container with default settings, on this table holding 1,000,000 rows (99 MB of data, 40 MB of indexes):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;order_lines&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="no"&gt;AUTO_INCREMENT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;unit_price&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;quantity&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;attrs&lt;/span&gt; &lt;span class="kt"&gt;json&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="kt"&gt;datetime&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="kc"&gt;CURRENT_TIMESTAMP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="n"&gt;order_lines_order_id_idx&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  MySQL generated column virtual vs stored: what is the difference?
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;VIRTUAL&lt;/code&gt; column is computed when a row is read and a &lt;code&gt;STORED&lt;/code&gt; column is computed when a row is written and kept in the row.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;VIRTUAL&lt;/code&gt; is what you get when you write neither keyword. The &lt;a href="https://dev.mysql.com/doc/refman/8.4/en/create-table-generated-columns.html" rel="noopener noreferrer"&gt;MySQL manual&lt;/a&gt; says "The default is &lt;code&gt;VIRTUAL&lt;/code&gt; if neither keyword is specified" and "&lt;code&gt;InnoDB&lt;/code&gt; supports secondary indexes on virtual columns". The rest of this table is measured:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;VIRTUAL&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;STORED&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Add to 1,000,000 rows&lt;/td&gt;
&lt;td&gt;Instant (&lt;code&gt;ALGORITHM=INSTANT&lt;/code&gt; accepted)&lt;/td&gt;
&lt;td&gt;4.8 s table copy, writes blocked&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Space in the table&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;td&gt;7.3 MB for a &lt;code&gt;decimal(12,2)&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;SUM()&lt;/code&gt; over all rows, no index&lt;/td&gt;
&lt;td&gt;0.66 s&lt;/td&gt;
&lt;td&gt;0.36 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Secondary or &lt;code&gt;UNIQUE&lt;/code&gt; index&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Primary key&lt;/td&gt;
&lt;td&gt;Refused, error 3106&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;FULLTEXT&lt;/code&gt; or &lt;code&gt;SPATIAL&lt;/code&gt; index&lt;/td&gt;
&lt;td&gt;Refused, error 3106&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Foreign key on it, or to it&lt;/td&gt;
&lt;td&gt;Refused, error 3733&lt;/td&gt;
&lt;td&gt;Yes; as the child column, no &lt;code&gt;ON UPDATE CASCADE&lt;/code&gt; or &lt;code&gt;SET NULL&lt;/code&gt; and no &lt;code&gt;ON DELETE SET NULL&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Foreign key on a column it reads&lt;/td&gt;
&lt;td&gt;Any action&lt;/td&gt;
&lt;td&gt;No &lt;code&gt;CASCADE&lt;/code&gt; or &lt;code&gt;SET NULL&lt;/code&gt; action (error 1215)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Change the expression&lt;/td&gt;
&lt;td&gt;Table copy with &lt;code&gt;MODIFY&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Table copy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Switch to the other kind&lt;/td&gt;
&lt;td&gt;Refused, error 3106&lt;/td&gt;
&lt;td&gt;Refused, error 3106&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Both kinds share the rules about what the expression may contain. &lt;code&gt;NOW()&lt;/code&gt; is refused (error 3763, "contains a disallowed function"), a generated column may only refer to generated columns defined before it (error 3107), an &lt;code&gt;INSERT&lt;/code&gt; may only give it &lt;code&gt;DEFAULT&lt;/code&gt; (error 3105), and a column it reads cannot be dropped (error 3108).&lt;/p&gt;

&lt;h2&gt;
  
  
  What does adding each kind cost on a large table?
&lt;/h2&gt;

&lt;p&gt;Adding a &lt;code&gt;VIRTUAL&lt;/code&gt; column changes only metadata, and adding a &lt;code&gt;STORED&lt;/code&gt; column copies every row while writes wait.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;order_lines&lt;/span&gt;
  &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;line_total&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unit_price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;VIRTUAL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ALGORITHM&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;INSTANT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- Query OK, 0 rows affected (0.01 sec)&lt;/span&gt;

&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;order_lines&lt;/span&gt;
  &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;line_total_stored&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unit_price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;STORED&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- Query OK, 1000000 rows affected (4.76 sec)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;MySQL would not do the &lt;code&gt;STORED&lt;/code&gt; add any cheaper way. &lt;code&gt;ALGORITHM=INSTANT&lt;/code&gt; and &lt;code&gt;ALGORITHM=INPLACE&lt;/code&gt; were both refused with error 1845, and &lt;code&gt;ALGORITHM=COPY, LOCK=NONE&lt;/code&gt; with error 1846: "COPY algorithm requires a lock. Try LOCK=SHARED." So for those 4.8 seconds the table can be read and not written, and the time grows with the table. That is the same lock class as the changes in &lt;a href="https://schemity.com/blog/mysql-alter-table-lock-instant-inplace-copy/" rel="noopener noreferrer"&gt;does MySQL ALTER TABLE lock the table&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The table's data grew from 99.2 MB to 106.6 MB for the &lt;code&gt;STORED&lt;/code&gt; column and did not change for the &lt;code&gt;VIRTUAL&lt;/code&gt; one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Can you index a virtual generated column?
&lt;/h2&gt;

&lt;p&gt;Yes, and the index is as large and as usable as one on a &lt;code&gt;STORED&lt;/code&gt; column, because an index always stores the value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;order_lines&lt;/span&gt; &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;lt_v_idx&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line_total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ALGORITHM&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;INPLACE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;LOCK&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;NONE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;-- 1.70 s&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;order_lines&lt;/span&gt; &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;lt_s_idx&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line_total_stored&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ALGORITHM&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;INPLACE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;LOCK&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;NONE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;-- 1.52 s&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each index added about 24.7 MB. With the indexes in place the two columns read alike: &lt;code&gt;SUM()&lt;/code&gt; took 0.23 s on the &lt;code&gt;VIRTUAL&lt;/code&gt; column and 0.20 s on the &lt;code&gt;STORED&lt;/code&gt; one, both from the index. Writes cost the same too. Inserting 1,000,000 rows took 3.06 s with no generated column, 3.30 s with a &lt;code&gt;VIRTUAL&lt;/code&gt; one, 3.10 s with a &lt;code&gt;STORED&lt;/code&gt; one, and 5.60 s and 5.29 s once each was indexed.&lt;/p&gt;

&lt;p&gt;The optimizer also matches the expression itself. This query never names the column and still used its index:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FROM order_lines WHERE unit_price * quantity = 45.00;
-- type: ref, key: lt_v_idx, rows: 155
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The manual's condition is that "the expression must be identical and it must have the same result type", and it means it: the same query written with &lt;code&gt;quantity * unit_price&lt;/code&gt; scanned all 1,000,000 rows, and so did &lt;code&gt;unit_price * quantity = 45&lt;/code&gt;. The literal has to be a decimal, as in &lt;code&gt;45.00&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is the standard way to index one field of a JSON document:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;order_lines&lt;/span&gt;
  &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;sku&lt;/span&gt; &lt;span class="kt"&gt;varchar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;COLLATE&lt;/span&gt; &lt;span class="n"&gt;utf8mb4_bin&lt;/span&gt;
  &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attrs&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;'$.sku'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;VIRTUAL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ALGORITHM&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;INSTANT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;order_lines&lt;/span&gt;
  &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;order_lines_sku_idx&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sku&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ALGORITHM&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;INPLACE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;LOCK&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;NONE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A lookup by &lt;code&gt;attrs-&amp;gt;&amp;gt;'$.sku'&lt;/code&gt; went from 0.41 s scanning the table to under 0.01 s, and again the query can keep using the JSON expression.&lt;/p&gt;

&lt;p&gt;Two details in that statement matter. The &lt;code&gt;COLLATE utf8mb4_bin&lt;/code&gt; matches what the JSON expression returns. Without it the column takes the table's case-insensitive collation, and once the index existed &lt;code&gt;WHERE attrs-&amp;gt;&amp;gt;'$.sku' = 'sku-1'&lt;/code&gt; returned 50 rows where the same query without the index returned 0: adding the index changed the answer. And the column and the index are two statements, because the two in one &lt;code&gt;ALTER TABLE&lt;/code&gt; were refused &lt;code&gt;LOCK=NONE&lt;/code&gt; (error 1846) and built the index with writes waiting.&lt;/p&gt;

&lt;p&gt;A functional index, &lt;code&gt;ADD INDEX ((CAST(attrs-&amp;gt;&amp;gt;'$.color' AS CHAR(10))))&lt;/code&gt;, is the same thing with the &lt;code&gt;VIRTUAL&lt;/code&gt; column hidden from you, and a query has to repeat the &lt;code&gt;CAST&lt;/code&gt; exactly to use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  When does a generated column have to be STORED?
&lt;/h2&gt;

&lt;p&gt;Only when it must be a primary key, take part in a foreign key, or carry a &lt;code&gt;FULLTEXT&lt;/code&gt; or &lt;code&gt;SPATIAL&lt;/code&gt; index.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;VIRTUAL&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- ERROR 3106: 'Defining a virtual generated column as primary key' is not supported for generated columns.&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;child&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;VIRTUAL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;FOREIGN&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- ERROR 3733: Foreign key 'child_ibfk_1' uses virtual column 'pid' which is not supported.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;code&gt;STORED&lt;/code&gt; column works in both places, with limits. As the child column of a foreign key it was refused &lt;code&gt;ON DELETE SET NULL&lt;/code&gt; and &lt;code&gt;ON UPDATE CASCADE&lt;/code&gt; (error 3104), since MySQL cannot write into a generated column. A foreign key cannot point at a &lt;code&gt;VIRTUAL&lt;/code&gt; column either, even one with a &lt;code&gt;UNIQUE&lt;/code&gt; index (error 3733). A &lt;code&gt;FULLTEXT&lt;/code&gt; or &lt;code&gt;SPATIAL&lt;/code&gt; index on a &lt;code&gt;VIRTUAL&lt;/code&gt; column is refused with error 3106.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;STORED&lt;/code&gt; column also costs something a &lt;code&gt;VIRTUAL&lt;/code&gt; one does not. A foreign key on a plain column that a &lt;code&gt;STORED&lt;/code&gt; column reads was refused &lt;code&gt;ON UPDATE CASCADE&lt;/code&gt; and &lt;code&gt;ON DELETE SET NULL&lt;/code&gt; (error 1215, "Cannot add foreign key constraint"), and the same table with the column &lt;code&gt;VIRTUAL&lt;/code&gt; accepted both.&lt;/p&gt;

&lt;p&gt;The other reason to store is a value that is expensive to compute and read far more often than it is written, without an index. For plain arithmetic the read gap above was 0.66 s against 0.36 s per million rows, and an index removes it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens when you change a generated column later?
&lt;/h2&gt;

&lt;p&gt;Changing the expression copies the table for both kinds, and the kind itself cannot be changed at all.&lt;/p&gt;

&lt;p&gt;This is the surprise in the &lt;code&gt;VIRTUAL&lt;/code&gt; column. Nothing is stored, and &lt;code&gt;MODIFY&lt;/code&gt; still rewrote every row:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;order_lines&lt;/span&gt; &lt;span class="no"&gt;MODIFY&lt;/span&gt; &lt;span class="n"&gt;line_total&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unit_price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;VIRTUAL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- Query OK, 1000000 rows affected (3.55 sec)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ALGORITHM=INSTANT&lt;/code&gt; and &lt;code&gt;ALGORITHM=INPLACE&lt;/code&gt; were both refused for it (error 1845). Dropping the column and adding it back in one statement does the same job without the copy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;order_lines&lt;/span&gt;
  &lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;line_total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;line_total&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unit_price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;VIRTUAL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="no"&gt;ALGORITHM&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;INPLACE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;LOCK&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="no"&gt;NONE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- Query OK, 0 rows affected (0.01 sec)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That moves the column to the end of the table unless you add &lt;code&gt;AFTER&lt;/code&gt;, and it only works while the column has no index. With an index on it, the same statement was refused with error 1846 ("INPLACE ADD or DROP of virtual columns cannot be combined with other ALTER TABLE actions"), and without the &lt;code&gt;ALGORITHM&lt;/code&gt; clause MySQL fell back to copying the table, as &lt;code&gt;MODIFY&lt;/code&gt; does (9.2 s with the index in place). The way through is three statements, each &lt;code&gt;ALGORITHM=INPLACE, LOCK=NONE&lt;/code&gt;: drop the index, drop and re-add the column, add the index again (1.9 s), with queries on that column unindexed in between.&lt;/p&gt;

&lt;p&gt;Switching kind is refused in both directions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;order_lines&lt;/span&gt; &lt;span class="no"&gt;MODIFY&lt;/span&gt; &lt;span class="n"&gt;line_total&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unit_price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;STORED&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- ERROR 3106: 'Changing the STORED status' is not supported for generated columns.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;MODIFY&lt;/code&gt; cannot switch kind, but a drop and re-add in one statement can. &lt;code&gt;STORED&lt;/code&gt; to &lt;code&gt;VIRTUAL&lt;/code&gt; took 0.02 s, and &lt;code&gt;VIRTUAL&lt;/code&gt; to &lt;code&gt;STORED&lt;/code&gt; costs one table copy, the same copy you would have paid on day one. That is the reason to default to &lt;code&gt;VIRTUAL&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is NOT NULL checked on a virtual generated column?
&lt;/h2&gt;

&lt;p&gt;Not for rows that already exist: MySQL added a &lt;code&gt;NOT NULL&lt;/code&gt; &lt;code&gt;VIRTUAL&lt;/code&gt; column over a row where the expression is &lt;code&gt;NULL&lt;/code&gt;, and that row then reads as 0.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;VIRTUAL&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- Query OK, 0 rows affected, no warning&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- NULL | 0 | 0&lt;/span&gt;
&lt;span class="c1"&gt;--    1 | 2 | 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;New writes are checked: &lt;code&gt;INSERT INTO t (a) VALUES (NULL)&lt;/code&gt; failed with error 1048, "Column 'b' cannot be null". The old row surfaces later: adding an index on &lt;code&gt;b&lt;/code&gt; failed with the same error 1048, and so does any later &lt;code&gt;ALTER TABLE&lt;/code&gt; that copies the table. Adding the column &lt;code&gt;WITH VALIDATION&lt;/code&gt; checks the existing rows up front and was refused here with 1048. The &lt;code&gt;STORED&lt;/code&gt; version of the column was refused at the &lt;code&gt;ALTER TABLE&lt;/code&gt;, because the copy evaluates every row. This ran in the default strict SQL mode.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Schemity handle generated columns on MySQL?
&lt;/h2&gt;

&lt;p&gt;Schemity reads both kinds from the live database, shows which is which, and reports the table copy before a change runs.&lt;/p&gt;

&lt;p&gt;Schemity is database design software that reads your live database, shows the impact of every schema change before it runs, and keeps the diagram as a file in Git. On the canvas a generated column carries its expression after an equals sign, where a default would be, and the field's details say &lt;code&gt;Generated (virtual)&lt;/code&gt; or &lt;code&gt;Generated (stored)&lt;/code&gt;. The expression belongs to the database, so the field form leaves only the name and description editable, which is how you &lt;a href="https://schemity.com/doc/tables-and-fields/" rel="noopener noreferrer"&gt;design the next one&lt;/a&gt; without a diagram that disagrees with the server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvuxm1ps8q1e9yyfhst9h.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvuxm1ps8q1e9yyfhst9h.webp" alt="Schemity reading order_lines from MySQL 9.7.4: line_total and line_total_stored are DECIMAL(12,2) columns drawn with &amp;quot;=  raw `unit_price` endraw  *  raw `quantity` endraw &amp;quot;, and sku is a VARCHAR(20) drawn with &amp;quot;= json_unquote(json_extr...&amp;quot;. The line_total row is selected and the field details panel shows Nullable ticked, type DECIMAL(12,2) and Generated (virtual) with the expression  raw `unit_price` endraw  *  raw `quantity` endraw ." width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When an AI agent edits the diagram over the &lt;a href="https://schemity.com/doc/ai-assisted-design/" rel="noopener noreferrer"&gt;MCP server&lt;/a&gt;, a switch between &lt;code&gt;VIRTUAL&lt;/code&gt; and &lt;code&gt;STORED&lt;/code&gt; is refused with MySQL's own reason, error 3106, and the advice to delete and re-create the column. A changed expression is planned as &lt;code&gt;MODIFY COLUMN&lt;/code&gt;, and &lt;a href="https://schemity.com/doc/impact-analysis/" rel="noopener noreferrer"&gt;impact analysis&lt;/a&gt; (Schemity Pro) reports it for what it is on this table: a rewrite of about 1,000,000 rows that holds back writes. A new &lt;code&gt;STORED&lt;/code&gt; column is reported the same way, and a new &lt;code&gt;VIRTUAL&lt;/code&gt; one produces no finding.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvmmdlapvn5raecq5gelx.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvmmdlapvn5raecq5gelx.webp" alt="Schemity's change preview for a migration file on MySQL 9.7.4: the planned statement ALTER TABLE order_lines ADD COLUMN color varchar(10) GENERATED ALWAYS AS (json_unquote(json_extract(attrs, '$.color'))) STORED, the new color column highlighted green on order_lines, and two impact findings: &amp;quot;Rewrites order_lines to change color: ~994.9K rows, 167 MB on disk&amp;quot; and &amp;quot;Filling color for every row holds back writes to order_lines while it reads ~994.9K rows, 167 MB on disk&amp;quot;." width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The same question on PostgreSQL, where the alternative is a trigger, is measured in &lt;a href="https://schemity.com/blog/postgres-generated-column-vs-trigger/" rel="noopener noreferrer"&gt;generated column vs trigger&lt;/a&gt;, and what a rename does to an expression that reads the column is in &lt;a href="https://schemity.com/blog/rename-column-what-breaks-postgres-mysql-sql-server-sqlite/" rel="noopener noreferrer"&gt;renaming a column: what breaks&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>mysql</category>
      <category>database</category>
      <category>sql</category>
      <category>performance</category>
    </item>
    <item>
      <title>PostgreSQL DROP TABLE CASCADE: What It Drops, and What It Leaves Broken</title>
      <dc:creator>Son Tran</dc:creator>
      <pubDate>Sun, 29 Nov 2026 05:30:26 +0000</pubDate>
      <link>https://dev.to/tbson87/postgresql-drop-table-cascade-what-it-drops-and-what-it-leaves-broken-91e</link>
      <guid>https://dev.to/tbson87/postgresql-drop-table-cascade-what-it-drops-and-what-it-leaves-broken-91e</guid>
      <description>&lt;p&gt;&lt;em&gt;Disclosure: I build &lt;a href="https://schemity.com" rel="noopener noreferrer"&gt;Schemity&lt;/a&gt;, a desktop ERD tool - this post is from our blog and uses it for the examples.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; &lt;code&gt;DROP TABLE ... CASCADE&lt;/code&gt; does not delete the rows that reference the table. It drops the objects that depend on the table: foreign key constraints, views and views on those views, materialized views, policies, &lt;code&gt;BEGIN ATOMIC&lt;/code&gt; functions, tables that inherit from it, columns typed with its row, and defaults that use its sequences. PostgreSQL lists them only in a &lt;code&gt;NOTICE&lt;/code&gt;. Run the drop without &lt;code&gt;CASCADE&lt;/code&gt; first, or inside a transaction, and read the list.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;DROP TABLE ... CASCADE&lt;/code&gt; in PostgreSQL does not delete the rows in tables that reference it: it drops the database objects that depend on the table, and leaves those rows where they are. The danger is that the list of objects is longer than the foreign keys people expect, it can include another table and another table's column, and PostgreSQL reports it only as a &lt;code&gt;NOTICE&lt;/code&gt; after the fact.&lt;/p&gt;

&lt;p&gt;Everything below ran on PostgreSQL 18.3 in a throwaway container. The table being dropped is &lt;code&gt;customers&lt;/code&gt; (1,000 rows of its own), with one of each kind of thing that can depend on a table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="no"&gt;IDENTITY&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;tier&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="s1"&gt;'free'&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="no"&gt;IDENTITY&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;DELETE&lt;/span&gt; &lt;span class="k"&gt;CASCADE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="kt"&gt;numeric&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;order_items&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="no"&gt;IDENTITY&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;DELETE&lt;/span&gt; &lt;span class="k"&gt;CASCADE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;sku&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;customer_totals&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;big_spenders&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customer_totals&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;MATERIALIZED&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;customer_tiers&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;tier&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FROM customers GROUP BY tier;
CREATE FUNCTION customer_email(p_id bigint) RETURNS text LANGUAGE sql
    AS 'SELECT email FROM customers WHERE id = p_id';
CREATE FUNCTION customer_email_atomic(p_id bigint) RETURNS text LANGUAGE sql
    BEGIN ATOMIC SELECT email FROM customers WHERE id = p_id; END;
CREATE FUNCTION touch() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RETURN NEW; END $$;
CREATE TRIGGER customers_touch BEFORE UPDATE ON customers FOR EACH ROW EXECUTE FUNCTION touch();
CREATE TABLE vip_customers (discount int NOT NULL DEFAULT 10) INHERITS (customers);
CREATE TABLE customer_snapshots (taken_at timestamptz NOT NULL DEFAULT now(), snapshot customers);
CREATE SEQUENCE customer_ref_seq OWNED BY customers.email;
CREATE TABLE tickets (id bigint PRIMARY KEY DEFAULT nextval('customer_ref_seq'), subject text NOT NULL);
ALTER TABLE orders ENABLE ROW LEVEL SECURITY;
CREATE POLICY orders_paying ON orders USING (EXISTS (
    SELECT 1 FROM customers c WHERE c.id = customer_id AND c.tier &amp;lt;&amp;gt; 'free'));
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;orders&lt;/code&gt; and &lt;code&gt;order_items&lt;/code&gt; hold 10,000 rows each, &lt;code&gt;vip_customers&lt;/code&gt; holds one row and &lt;code&gt;customer_snapshots&lt;/code&gt; five. Half the customers are on a paid tier, so the policy shows 5,000 orders to a role &lt;code&gt;app&lt;/code&gt; that has &lt;code&gt;SELECT&lt;/code&gt; on &lt;code&gt;orders&lt;/code&gt; and &lt;code&gt;customers&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Postgres DROP TABLE CASCADE: what does it drop?
&lt;/h2&gt;

&lt;p&gt;It drops every object that depends on the table, and every object that depends on those, and here that was nine objects besides the table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BEGIN;
DROP TABLE customers CASCADE;
NOTICE:  drop cascades to 9 other objects
DETAIL:  drop cascades to column snapshot of table customer_snapshots
drop cascades to constraint orders_customer_id_fkey on table orders
drop cascades to view customer_totals
drop cascades to view big_spenders
drop cascades to materialized view customer_tiers
drop cascades to function customer_email_atomic(bigint)
drop cascades to table vip_customers
drop cascades to default value for column id of table tickets
drop cascades to policy orders_paying on table orders
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;NOTICE&lt;/code&gt; is a flat list, but the dependencies are not. Drawn as a tree, three of the nine are reached through something other than the table itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;customers
├── constraint orders_customer_id_fkey   (on orders)
├── view customer_totals
│   └── view big_spenders                (reads only customer_totals)
├── materialized view customer_tiers
├── function customer_email_atomic(bigint)
├── table vip_customers                  (INHERITS)
├── type customers                       (the table's row type)
│   └── column customer_snapshots.snapshot
├── sequence customer_ref_seq            (OWNED BY customers.email)
│   └── default of tickets.id
└── policy orders_paying                 (on orders, reads customers)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Dependent&lt;/th&gt;
&lt;th&gt;What &lt;code&gt;CASCADE&lt;/code&gt; did&lt;/th&gt;
&lt;th&gt;What you lose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Foreign key &lt;code&gt;orders_customer_id_fkey&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Dropped the constraint&lt;/td&gt;
&lt;td&gt;The check; &lt;code&gt;orders&lt;/code&gt; kept all 10,000 rows&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;View &lt;code&gt;customer_totals&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Dropped&lt;/td&gt;
&lt;td&gt;The view&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;View &lt;code&gt;big_spenders&lt;/code&gt;, which reads only the other view&lt;/td&gt;
&lt;td&gt;Dropped&lt;/td&gt;
&lt;td&gt;The view&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Materialized view &lt;code&gt;customer_tiers&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Dropped&lt;/td&gt;
&lt;td&gt;The view and its stored result&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Function with a &lt;code&gt;BEGIN ATOMIC&lt;/code&gt; body&lt;/td&gt;
&lt;td&gt;Dropped&lt;/td&gt;
&lt;td&gt;The function&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Function with a string body&lt;/td&gt;
&lt;td&gt;Kept&lt;/td&gt;
&lt;td&gt;Nothing yet: it fails on its next call&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Table &lt;code&gt;vip_customers&lt;/code&gt;, which inherits from &lt;code&gt;customers&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Dropped&lt;/td&gt;
&lt;td&gt;The table and its rows&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Column &lt;code&gt;customer_snapshots.snapshot&lt;/code&gt;, typed &lt;code&gt;customers&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Dropped&lt;/td&gt;
&lt;td&gt;The column and the 5 values in it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Default of &lt;code&gt;tickets.id&lt;/code&gt;, on a sequence &lt;code&gt;customers&lt;/code&gt; owns&lt;/td&gt;
&lt;td&gt;Dropped&lt;/td&gt;
&lt;td&gt;The default; the next insert fails&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Policy &lt;code&gt;orders_paying&lt;/code&gt; on &lt;code&gt;orders&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Dropped&lt;/td&gt;
&lt;td&gt;The policy; &lt;code&gt;orders&lt;/code&gt; reads as empty to every role but its owner&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Trigger &lt;code&gt;customers_touch&lt;/code&gt; on &lt;code&gt;customers&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Went with the table&lt;/td&gt;
&lt;td&gt;Nothing extra; its function &lt;code&gt;touch()&lt;/code&gt; stays&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Without &lt;code&gt;CASCADE&lt;/code&gt;, the same statement is refused and names the same nine objects in the error's &lt;code&gt;DETAIL&lt;/code&gt;. That refusal is the default, and writing &lt;code&gt;RESTRICT&lt;/code&gt; says it out loud.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does DROP TABLE CASCADE delete rows in other tables?
&lt;/h2&gt;

&lt;p&gt;No: it removes the foreign key constraint and leaves the referencing rows in place.&lt;/p&gt;

&lt;p&gt;After the drop, &lt;code&gt;orders&lt;/code&gt; and &lt;code&gt;order_items&lt;/code&gt; still held 10,000 rows each, and &lt;code&gt;orders.customer_id&lt;/code&gt; was a plain column full of ids that pointed at nothing. The &lt;code&gt;ON DELETE CASCADE&lt;/code&gt; on that key did not run, because no row was deleted: the table was removed.&lt;/p&gt;

&lt;p&gt;Three statements you might run against &lt;code&gt;customers&lt;/code&gt; carry the word cascade, and they do three different jobs:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Statement&lt;/th&gt;
&lt;th&gt;What cascades&lt;/th&gt;
&lt;th&gt;Result on &lt;code&gt;orders&lt;/code&gt; here&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;DROP TABLE customers CASCADE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Dependent objects&lt;/td&gt;
&lt;td&gt;Foreign key dropped, 10,000 rows kept&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;TRUNCATE customers CASCADE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The truncate, to referencing tables&lt;/td&gt;
&lt;td&gt;Emptied, and &lt;code&gt;order_items&lt;/code&gt; with it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;DELETE FROM customers&lt;/code&gt; with &lt;code&gt;ON DELETE CASCADE&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;The delete, row by row&lt;/td&gt;
&lt;td&gt;Matching rows deleted, here all of them&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A plain &lt;code&gt;TRUNCATE customers&lt;/code&gt; is refused while a foreign key references the table, with the hint &lt;code&gt;Truncate table "orders" at the same time, or use TRUNCATE ... CASCADE.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Three of the nine objects do take stored data with them, and none is a foreign key: the inheriting table &lt;code&gt;vip_customers&lt;/code&gt; with its rows, the &lt;code&gt;snapshot&lt;/code&gt; column with its values, and the materialized view with its stored result, the only one of the three you can rebuild.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is left broken after the drop?
&lt;/h2&gt;

&lt;p&gt;Three things kept existing and stopped working: a function, an insert, and a whole table's visibility.&lt;/p&gt;

&lt;p&gt;The string-bodied function is not in the &lt;code&gt;NOTICE&lt;/code&gt;, because PostgreSQL does not track what a string body reads. It fails when called:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nf"&gt;customer_email&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- ERROR:  relation "customers" does not exist&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;tickets.id&lt;/code&gt; lost its default, so the insert that never named the column now fails:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;tickets&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'after'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- ERROR:  null value in column "id" of relation "tickets" violates not-null constraint&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The sequence itself, &lt;code&gt;customer_ref_seq&lt;/code&gt;, is dropped as well and is not in the &lt;code&gt;NOTICE&lt;/code&gt;: only the default that used it is named. Putting the default back means a new sequence started above the highest &lt;code&gt;id&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The policy is the quiet one. &lt;code&gt;orders&lt;/code&gt; still has row-level security enabled, and with its only policy gone the default is to show nothing. A role that could read 5,000 orders before the drop read 0 after it, with no error.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you see the list before you run it?
&lt;/h2&gt;

&lt;p&gt;Run the drop without &lt;code&gt;CASCADE&lt;/code&gt;, or run it inside a transaction and roll back.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- ERROR:  cannot drop table customers because other objects depend on it&lt;/span&gt;
&lt;span class="c1"&gt;-- DETAIL:  column snapshot of table customer_snapshots depends on type customers&lt;/span&gt;
&lt;span class="c1"&gt;-- constraint orders_customer_id_fkey on table orders depends on table customers&lt;/span&gt;
&lt;span class="c1"&gt;-- ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The refusal lists what &lt;code&gt;CASCADE&lt;/code&gt; would take, with the reason for each, including objects reached through another object (&lt;code&gt;view big_spenders depends on view customer_totals&lt;/code&gt;). Because DDL is transactional in PostgreSQL, the other way is &lt;code&gt;BEGIN; DROP TABLE customers CASCADE;&lt;/code&gt;, read the &lt;code&gt;NOTICE&lt;/code&gt;, then &lt;code&gt;ROLLBACK&lt;/code&gt;. All 1,000 customers were back after the rollback. Note that the drop holds an &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; lock on the table and on every table it touches (here &lt;code&gt;orders&lt;/code&gt;, &lt;code&gt;tickets&lt;/code&gt;, &lt;code&gt;customer_snapshots&lt;/code&gt; and &lt;code&gt;vip_customers&lt;/code&gt;) until the transaction ends, so reads of &lt;code&gt;orders&lt;/code&gt; wait too. Do not leave it open on a live database.&lt;/p&gt;

&lt;p&gt;The client sees at most 100 dependents. Past that the message ends &lt;code&gt;and N other objects (see server log for list)&lt;/code&gt;, and the full list is in the server log.&lt;/p&gt;

&lt;p&gt;Neither list includes the string-bodied function. Find those by searching function bodies for the table name, and treat a match as a maybe.&lt;/p&gt;

&lt;p&gt;The cautious version of the migration drops each dependent by name, in order, and ends with a &lt;code&gt;DROP TABLE&lt;/code&gt; that has no &lt;code&gt;CASCADE&lt;/code&gt;. If something new has come to depend on the table since you wrote it, the last statement fails and tells you.&lt;/p&gt;

&lt;p&gt;On a production database, many teams split the drop over two releases: rename the table first (&lt;code&gt;ALTER TABLE customers RENAME TO customers_retired&lt;/code&gt;), and drop it a release later. Views, policies and foreign keys follow a rename, because PostgreSQL holds them by object id, not by name. Anything that names the table as text, the string-bodied function and the application's own queries, fails right away, and one more &lt;code&gt;RENAME&lt;/code&gt; undoes it. A &lt;code&gt;DROP&lt;/code&gt; cannot be undone that way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is DROP TABLE CASCADE the same on MySQL, SQL Server and SQLite?
&lt;/h2&gt;

&lt;p&gt;No, and none of them drops dependent objects the way PostgreSQL does.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;DROP TABLE t CASCADE&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;A foreign key into &lt;code&gt;t&lt;/code&gt;
&lt;/th&gt;
&lt;th&gt;A view on &lt;code&gt;t&lt;/code&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;PostgreSQL 18.3&lt;/td&gt;
&lt;td&gt;Drops dependents&lt;/td&gt;
&lt;td&gt;Constraint dropped&lt;/td&gt;
&lt;td&gt;Dropped&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MySQL 8.4.11&lt;/td&gt;
&lt;td&gt;Keyword accepted and ignored&lt;/td&gt;
&lt;td&gt;Drop refused, error 3730&lt;/td&gt;
&lt;td&gt;Left broken, error 1356 on &lt;code&gt;SELECT&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SQL Server 2022&lt;/td&gt;
&lt;td&gt;Syntax error, Msg 156&lt;/td&gt;
&lt;td&gt;Drop refused, Msg 3726&lt;/td&gt;
&lt;td&gt;Left broken, Msg 208, or refuses the drop if schema-bound (Msg 3729)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SQLite 3.54.0&lt;/td&gt;
&lt;td&gt;Syntax error&lt;/td&gt;
&lt;td&gt;See below&lt;/td&gt;
&lt;td&gt;Left broken, "no such table"&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;SQLite is the one that touches rows. With &lt;code&gt;PRAGMA foreign_keys = ON&lt;/code&gt;, a plain &lt;code&gt;DROP TABLE customers&lt;/code&gt; deletes the table's rows first, so the foreign key actions run: a child table with &lt;code&gt;ON DELETE CASCADE&lt;/code&gt; was emptied, and one with the default &lt;code&gt;NO ACTION&lt;/code&gt; and matching rows stopped the drop with "FOREIGN KEY constraint failed". That is the opposite of PostgreSQL, where the same key on the same tables kept every child row.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Schemity show what a table drop takes with it?
&lt;/h2&gt;

&lt;p&gt;Schemity lists each dependent of the table before the migration runs, with what PostgreSQL will do to it.&lt;/p&gt;

&lt;p&gt;Schemity is database design software that reads your live database, shows the impact of every schema change before it runs, and keeps the diagram as a file in Git. When you delete a table on a connected PostgreSQL diagram, the planned migration drops the foreign keys the diagram knows by name and then writes &lt;code&gt;DROP TABLE IF EXISTS "customers" CASCADE&lt;/code&gt;. So what the &lt;code&gt;CASCADE&lt;/code&gt; reaches is exactly the part worth reading, and &lt;a href="https://schemity.com/doc/impact-analysis/" rel="noopener noreferrer"&gt;impact analysis&lt;/a&gt; (Schemity Pro) puts it under Dependent objects: the views, including a view that reads the table only through another view, the materialized view, the policy, the &lt;code&gt;BEGIN ATOMIC&lt;/code&gt; function, the inheriting table, the typed column and the other table's default, each with "CASCADE drops it too, and the database reports that only as a notice." The string-bodied function is listed as one that mentions the table and may fail the next time it runs. This is the list you would otherwise get from the &lt;code&gt;NOTICE&lt;/code&gt;, read before the statement instead of after, which is the point of being able to see the impact of every change.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fexyjb2bqr7rr13e8d02s.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fexyjb2bqr7rr13e8d02s.webp" alt="Schemity's change preview for dropping customers on PostgreSQL 18.3: customers outlined in red with its line to orders, the planned migration ending in DROP TABLE IF EXISTS &amp;quot;customers&amp;quot; CASCADE, and the impact findings. &amp;quot;10 objects depend on customers&amp;quot; lists materialized view customer_tiers, view customer_totals, view big_spenders, function customer_email_atomic, inheriting table vip_customers, column customer_snapshots.snapshot, default of column tickets.id and policy orders_paying, each with &amp;quot;CASCADE drops it too, and the database reports that only as a notice&amp;quot;, plus trigger customers_touch, dropped with the table, and function customer_email, which mentions customers in its body." width="800" height="804"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Most of these objects never appear on an ERD. A diagram draws tables, columns and foreign keys, so the views, policies, functions and sequence defaults that &lt;code&gt;CASCADE&lt;/code&gt; reaches are exactly the part a diagram leaves out, the same way it leaves out &lt;a href="https://schemity.com/blog/on-delete-cascade-is-invisible-in-your-erd/" rel="noopener noreferrer"&gt;what ON DELETE CASCADE will delete&lt;/a&gt;. A foreign key from a table in &lt;a href="https://schemity.com/blog/postgres-schema-vs-database/" rel="noopener noreferrer"&gt;another schema&lt;/a&gt; is on the same list too, since it is outside the diagram and &lt;code&gt;CASCADE&lt;/code&gt; drops it as well. In a modular monolith that is often &lt;a href="https://schemity.com/blog/foreign-keys-between-modules-modular-monolith/" rel="noopener noreferrer"&gt;another module's foreign key&lt;/a&gt;, which the team dropping the table may never have seen.&lt;/p&gt;

&lt;p&gt;The list has limits, and they matter more than the list. It covers what the database records. The string-bodied function is found by matching the table's name as a whole word in the function's source, so a function that builds the name at run time, such as &lt;code&gt;EXECUTE format('SELECT ... FROM %I', 'cust' || 'omers')&lt;/code&gt;, is not found. Nothing outside the database is found at all: the application's queries, ORM models, reports, scheduled jobs and other services that read the table. Impact analysis reads the database, not your application, so an empty list means the database holds nothing else that depends on the table, not that nothing reads it. Searching the codebase, and the rename-first release above, cover the rest.&lt;/p&gt;

&lt;p&gt;The same question for one column, where PostgreSQL drops some dependents without any &lt;code&gt;CASCADE&lt;/code&gt; at all, is in &lt;a href="https://schemity.com/blog/postgres-drop-column-what-it-drops/" rel="noopener noreferrer"&gt;what DROP COLUMN takes with it&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>sql</category>
      <category>devops</category>
    </item>
    <item>
      <title>PostgreSQL Redundant Indexes: Is an Index on (a) Redundant When You Have (a, b)?</title>
      <dc:creator>Son Tran</dc:creator>
      <pubDate>Sun, 29 Nov 2026 05:29:32 +0000</pubDate>
      <link>https://dev.to/tbson87/postgresql-redundant-indexes-is-an-index-on-a-redundant-when-you-have-a-b-1ang</link>
      <guid>https://dev.to/tbson87/postgresql-redundant-indexes-is-an-index-on-a-redundant-when-you-have-a-b-1ang</guid>
      <description>&lt;p&gt;&lt;em&gt;Disclosure: I build &lt;a href="https://schemity.com" rel="noopener noreferrer"&gt;Schemity&lt;/a&gt;, a desktop ERD tool - this post is from our blog and uses it for the examples.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; A plain b-tree index on &lt;code&gt;(a)&lt;/code&gt; is redundant when another b-tree index starts with &lt;code&gt;a&lt;/code&gt;, such as &lt;code&gt;(a, b)&lt;/code&gt;: PostgreSQL 18.3 served the same lookup from the wider index with the same number of heap reads. Do not drop it on that rule if it is &lt;code&gt;UNIQUE&lt;/code&gt;, uses a different operator class or has &lt;code&gt;INCLUDE&lt;/code&gt; columns the wider one lacks, and judge a partial or hash index on its own. Usage counters will not tell you which one to drop.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can usually drop a PostgreSQL index on &lt;code&gt;(a)&lt;/code&gt; once a b-tree index on &lt;code&gt;(a, b)&lt;/code&gt; exists, because the wider index finds the same rows for every lookup on &lt;code&gt;a&lt;/code&gt;. The exceptions matter more than the rule: several kinds of index share a leading column with a wider one and cannot be judged from the column list, and dropping the wrong one breaks a query or a constraint.&lt;/p&gt;

&lt;p&gt;Everything below ran on PostgreSQL 18.3 in a throwaway container, on an &lt;code&gt;orders&lt;/code&gt; table with 1,000,000 rows (89 MB) and these indexes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="no"&gt;IDENTITY&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="kt"&gt;numeric&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;orders_customer_id_idx&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;orders_customer_id_created_at_idx&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;orders_email_key&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;orders_email_status_idx&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;orders_status_open_idx&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'open'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;orders_status_created_at_idx&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;orders_email_pattern_idx&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="n"&gt;text_pattern_ops&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;orders_customer_id_hash_idx&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="n"&gt;hash&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;orders_customer_id_total_idx&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;INCLUDE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;orders_id_idx&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;orders_created_at_desc_idx&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;orders_created_at_id_idx&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Postgres redundant index: is (a) redundant when (a, b) exists?
&lt;/h2&gt;

&lt;p&gt;Yes, for two plain b-tree indexes: the wider one found the same rows with the same heap reads.&lt;/p&gt;

&lt;p&gt;With only &lt;code&gt;orders_customer_id_idx&lt;/code&gt; available, &lt;code&gt;WHERE customer_id = 4242&lt;/code&gt; returned its 20 rows through a bitmap scan that read 20 heap blocks. With only &lt;code&gt;orders_customer_id_created_at_idx&lt;/code&gt; available, the plan was the same shape with the same 20 heap blocks. A b-tree is sorted by its first column first, so a search on that column descends either index the same way.&lt;/p&gt;

&lt;p&gt;With only these two present, the planner picked the wider one for the plain lookup, and it is the only one of the two that can also return a customer's orders already sorted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4242&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt; &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- Index Scan Backward using orders_customer_id_created_at_idx&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The narrow index is smaller, 7.4 MB against 30 MB, because b-tree deduplication packs the repeated &lt;code&gt;customer_id&lt;/code&gt; values and cannot pack the wider keys, which are all distinct. That is the one thing it has going for it, and it shows in one kind of query: a count over 10,000 customers read 199 buffers from the narrow index and 773 from the wide one. It is rarely enough to keep a second index that every write has to maintain.&lt;/p&gt;

&lt;p&gt;Three indexes on this table are redundant in that sense:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Index&lt;/th&gt;
&lt;th&gt;Covered by&lt;/th&gt;
&lt;th&gt;Why&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;orders_customer_id_idx (customer_id)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;orders_customer_id_created_at_idx&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Leading column of a wider index&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;orders_id_idx (id)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;orders_pkey&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Same column as the primary key's index&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;orders_created_at_desc_idx (created_at DESC)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;orders_created_at_id_idx&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A one-column index reads in either direction&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The last one surprises people. &lt;code&gt;ORDER BY created_at DESC LIMIT 10&lt;/code&gt; ran as &lt;code&gt;Index Scan Backward using orders_created_at_id_idx&lt;/code&gt; once the &lt;code&gt;DESC&lt;/code&gt; index was gone. Direction only matters in a multi-column index whose columns sort in different directions, or when an index puts &lt;code&gt;NULL&lt;/code&gt;s in the non-default place (&lt;code&gt;DESC NULLS LAST&lt;/code&gt;, &lt;code&gt;ASC NULLS FIRST&lt;/code&gt;). PostgreSQL also accepted two identical &lt;code&gt;CREATE INDEX&lt;/code&gt; statements under different names with no warning, so exact duplicates are easy to collect.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which indexes look redundant and are not?
&lt;/h2&gt;

&lt;p&gt;Five on this table share a leading column with a wider index and cannot be called redundant from the column list: three do something the wider one cannot, and two are a judgment call.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Index&lt;/th&gt;
&lt;th&gt;Looks covered by&lt;/th&gt;
&lt;th&gt;Why it is not simply redundant&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;orders_email_key&lt;/code&gt;, &lt;code&gt;UNIQUE (email)&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(email, status)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Refuses a second row with the same email&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;orders_status_open_idx&lt;/code&gt;, partial on &lt;code&gt;status = 'open'&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(status, created_at)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Judgment call: serves nothing new, but is 1.7 MB against 32 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;orders_email_pattern_idx&lt;/code&gt;, &lt;code&gt;text_pattern_ops&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(email, status)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Serves &lt;code&gt;LIKE 'user1234%'&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;orders_customer_id_total_idx&lt;/code&gt;, &lt;code&gt;INCLUDE (total)&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(customer_id, created_at)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Answers &lt;code&gt;sum(total)&lt;/code&gt; from the index alone&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;orders_customer_id_hash_idx&lt;/code&gt;, hash&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(customer_id, created_at)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Judgment call: another access method, not comparable by columns; here it serves nothing the b-tree does not&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;The unique index.&lt;/strong&gt; With &lt;code&gt;orders_email_key&lt;/code&gt; dropped, an insert of an email that already existed went through and the table held two rows for it. &lt;code&gt;(email, status)&lt;/code&gt; allows that, because the pair differs. Here the wider index is the one to question: with &lt;code&gt;email&lt;/code&gt; unique, a lookup by email finds at most one row either way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The pattern index.&lt;/strong&gt; This database uses the &lt;code&gt;en_US.utf8&lt;/code&gt; collation, where an ordinary b-tree cannot serve a &lt;code&gt;LIKE&lt;/code&gt; prefix. &lt;code&gt;WHERE email LIKE 'user1234%'&lt;/code&gt; used &lt;code&gt;orders_email_pattern_idx&lt;/code&gt;, and with that index dropped it became a parallel sequential scan of the whole table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The &lt;code&gt;INCLUDE&lt;/code&gt; index.&lt;/strong&gt; &lt;code&gt;SELECT sum(total) FROM orders WHERE customer_id = 4242&lt;/code&gt; ran as an index-only scan that read 5 buffers. Without the index it read 24, because it had to visit the table for &lt;code&gt;total&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The hash index.&lt;/strong&gt; With all twelve indexes present, the planner sent the plain &lt;code&gt;customer_id = 4242&lt;/code&gt; lookup to the hash index. It is 31 MB, larger than either b-tree on the column, and the b-trees answer the same lookup, so it is the one of the five most likely to go.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does an index on the second column help, and what about skip scan?
&lt;/h2&gt;

&lt;p&gt;An index on &lt;code&gt;(b)&lt;/code&gt; is not made redundant by &lt;code&gt;(a, b)&lt;/code&gt;, though PostgreSQL 18 can now use the wider index for it at a higher cost.&lt;/p&gt;

&lt;p&gt;Before PostgreSQL 18, a lookup by &lt;code&gt;created_at&lt;/code&gt; alone could not use &lt;code&gt;orders_status_created_at_idx (status, created_at)&lt;/code&gt; efficiently. PostgreSQL 18 added the b-tree skip scan (&lt;a href="https://www.postgresql.org/docs/18/release-18.html" rel="noopener noreferrer"&gt;release notes&lt;/a&gt;: "Allow skip scans of btree indexes"), which searches the index roughly once per distinct value of the leading column, plus the probes that find each next value. &lt;code&gt;status&lt;/code&gt; has four values here, and with both &lt;code&gt;created_at&lt;/code&gt; indexes dropped the lookup took nine searches:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Index Scan using orders_status_created_at_idx on orders
  Index Cond: (created_at = '2026-01-05 00:00:00+00'::timestamp with time zone)
  Index Searches: 9
  Buffers: shared hit=14 read=19
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is 33 buffers. The dedicated index did the same lookup in one search and 4 buffers. So the skip scan makes a missing index survivable when the leading column has a handful of values; it is not a reason to call an index on the second column redundant, and with a high-cardinality leading column the planner does not choose it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does a redundant index cost?
&lt;/h2&gt;

&lt;p&gt;Disk space and time on writes, and how much time depends on the order the rows arrive in.&lt;/p&gt;

&lt;p&gt;I inserted 200,000 rows three times with all twelve indexes and three times with the three redundant ones dropped:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Load&lt;/th&gt;
&lt;th&gt;All twelve indexes&lt;/th&gt;
&lt;th&gt;Without the three redundant&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Rows in increasing order of id and time&lt;/td&gt;
&lt;td&gt;7.2, 7.4, 7.5 s&lt;/td&gt;
&lt;td&gt;7.0, 7.0, 8.3 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Random &lt;code&gt;customer_id&lt;/code&gt; and &lt;code&gt;created_at&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;14.6, 16.1, 17.1 s&lt;/td&gt;
&lt;td&gt;11.5, 12.4, 13.0 s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;On the append-only load the difference is inside the noise. On the random load the three indexes cost 3 to 4 seconds every time, about a fifth of the load. Cutting the table to four indexes brought the first load down to 4.1 s. The three redundant indexes also hold 50 MB between them, more than half the size of the table.&lt;/p&gt;

&lt;p&gt;Dropping one does not change which rows any query finds. The only queries that read more afterwards are wide range or index-only scans on the leading column, like the count above.&lt;/p&gt;

&lt;p&gt;The cost is easy to miss because it never shows up as a slow query. It shows up as slower writes, more WAL, longer &lt;code&gt;VACUUM&lt;/code&gt; and a bigger backup.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you find redundant indexes in PostgreSQL?
&lt;/h2&gt;

&lt;p&gt;Compare the leading key columns of every pair of indexes on a table in &lt;code&gt;pg_index&lt;/code&gt;, and skip the kinds that are never redundant.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexrelid&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;redundant&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexrelid&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;covered_by&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_index&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;pg_index&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indrelid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indrelid&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexrelid&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexrelid&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;pg_class&lt;/span&gt; &lt;span class="n"&gt;ac&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;ac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexrelid&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;pg_class&lt;/span&gt; &lt;span class="n"&gt;bc&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;bc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexrelid&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indisunique&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indisvalid&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indisvalid&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indpred&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indpred&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexprs&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexprs&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;ac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;relam&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;relam&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indnkeyatts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indnatts&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indnkeyatts&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indnkeyatts&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indkey&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indnkeyatts&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indkey&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;int2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indnkeyatts&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indclass&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indnkeyatts&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indclass&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indnkeyatts&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indcollation&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indnkeyatts&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indcollation&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indnkeyatts&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indnkeyatts&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indnkeyatts&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indisunique&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexrelid&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexrelid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the example it returns exactly the three redundant indexes and none of the five look-alikes. It ignores sort direction and &lt;code&gt;NULL&lt;/code&gt; placement, which is right for a plain one-column index and needs a second look when an index mixes &lt;code&gt;ASC&lt;/code&gt; and &lt;code&gt;DESC&lt;/code&gt; or sets &lt;code&gt;NULLS FIRST&lt;/code&gt; or &lt;code&gt;NULLS LAST&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Do not use scan counts for this. After 1,000 runs of &lt;code&gt;SELECT count(*) FROM orders WHERE customer_id = $1&lt;/code&gt;, &lt;code&gt;pg_stat_user_indexes&lt;/code&gt; showed:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Index&lt;/th&gt;
&lt;th&gt;&lt;code&gt;idx_scan&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;orders_customer_id_total_idx&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;1000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;orders_customer_id_created_at_idx&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;orders_customer_id_idx&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;orders_customer_id_hash_idx&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The planner sent every lookup to one index, so the other three read as unused, including the one that the sorted query above needs. A count of 0 says the planner had a better choice for the queries that ran, not that the index does nothing.&lt;/p&gt;

&lt;p&gt;Before dropping one, check that nothing names it, such as a query hint from an extension or a &lt;code&gt;CLUSTER&lt;/code&gt;. Then drop it with &lt;code&gt;DROP INDEX CONCURRENTLY&lt;/code&gt; so writes are not blocked.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Schemity find redundant indexes?
&lt;/h2&gt;

&lt;p&gt;Schemity's schema lint has a &lt;code&gt;redundant-index&lt;/code&gt; rule that reports an index whose columns are a leading prefix of another index, a unique constraint or the primary key.&lt;/p&gt;

&lt;p&gt;Schemity is database design software that reads your live database, shows the impact of every schema change before it runs, and keeps the diagram as a file in Git. Read from this database, &lt;a href="https://schemity.com/doc/schema-lint/" rel="noopener noreferrer"&gt;schema lint&lt;/a&gt; (Schemity Pro) reports two findings on &lt;code&gt;orders&lt;/code&gt;: &lt;code&gt;orders_customer_id_idx&lt;/code&gt;, "a leading prefix of &lt;code&gt;orders_customer_id_created_at_idx&lt;/code&gt; (customer_id, created_at)", and &lt;code&gt;orders_id_idx&lt;/code&gt;, "the same as &lt;code&gt;orders_pkey&lt;/code&gt;". It leaves the five look-alikes alone: a unique index enforces something, an operator class and an &lt;code&gt;INCLUDE&lt;/code&gt; list serve queries the wider index does not, and a partial index or another access method is yours to judge. It also leaves the &lt;code&gt;DESC&lt;/code&gt; index alone, so that one takes the query above to find.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6hboz6q0lyinbevjo5dq.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6hboz6q0lyinbevjo5dq.webp" alt="Schemity's lint drawer beside the orders and customers tables read from PostgreSQL 18.3, with two findings under Costs. For orders_customer_id_idx: &amp;quot;Index duplicates the leading columns of a wider one. Its columns (customer_id) are a leading prefix of orders_customer_id_created_at_idx (customer_id, created_at), which already serves the same queries, and every insert, update and delete maintains both.&amp;quot; For orders_id_idx: &amp;quot;Index has the same columns as another one&amp;quot;, naming orders_pkey. Each finding has Ignore, Copy, Show on canvas and Fix, and the orders table shows idx: 12 in its footer." width="800" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each finding sits on the table in the diagram, can be searched, can be ignored (the choice is saved in the diagram file), and has a Fix button that removes the index as one undoable edit. The drop then goes through the same &lt;a href="https://schemity.com/doc/migration-sql-diff/" rel="noopener noreferrer"&gt;migration preview&lt;/a&gt; as any other change. That is the useful order for a schema you did not write: understand your schema first, including which of its indexes are only weight.&lt;/p&gt;

&lt;p&gt;Redundant indexes are one of the leftovers a long-lived database collects. Another is the sequence nothing uses any more, covered in &lt;a href="https://schemity.com/blog/postgres-find-unused-sequences/" rel="noopener noreferrer"&gt;finding unused sequences in PostgreSQL&lt;/a&gt;, and the index that must not be dropped is in &lt;a href="https://schemity.com/blog/mysql-cannot-drop-index-needed-in-foreign-key-constraint/" rel="noopener noreferrer"&gt;cannot drop index needed in a foreign key constraint&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>sql</category>
      <category>performance</category>
    </item>
    <item>
      <title>PostgreSQL Schema vs Database: Which Should Separate Your Modules or Tenants?</title>
      <dc:creator>Son Tran</dc:creator>
      <pubDate>Sun, 29 Nov 2026 05:28:38 +0000</pubDate>
      <link>https://dev.to/tbson87/postgresql-schema-vs-database-which-should-separate-your-modules-or-tenants-3p6h</link>
      <guid>https://dev.to/tbson87/postgresql-schema-vs-database-which-should-separate-your-modules-or-tenants-3p6h</guid>
      <description>&lt;p&gt;&lt;em&gt;Disclosure: I build &lt;a href="https://schemity.com" rel="noopener noreferrer"&gt;Schemity&lt;/a&gt;, a desktop ERD tool - this post is from our blog and uses it for the examples.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; Use schemas to separate modules that still reference each other: a foreign key, a join and a transaction all cross a schema and none crosses a database. Use a separate database only when two parts must never share a transaction, a connection or a backup. For tenants, a schema each works for tens or hundreds: 1,000 tenants of 10 empty tables took 427 MB and 50,415 catalog rows on PostgreSQL 18.3.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In PostgreSQL, put two modules in separate schemas of one database when they still reference each other, and in separate databases only when they must share nothing. A foreign key, a join and a transaction all cross a schema boundary, and none of them crosses a database boundary, so the choice is really a choice about what the database will still check for you.&lt;/p&gt;

&lt;p&gt;Everything below ran on PostgreSQL 18.3 in a throwaway container, with default settings, on this database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;SCHEMA&lt;/span&gt; &lt;span class="n"&gt;identity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;SCHEMA&lt;/span&gt; &lt;span class="n"&gt;billing&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;identity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="no"&gt;IDENTITY&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;identity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;audit_log&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="no"&gt;IDENTITY&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;identity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;action&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;billing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;invoices&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="no"&gt;IDENTITY&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;identity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="kt"&gt;numeric&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;invoices_user_id_idx&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;billing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;invoices&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;billing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;audit_log&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="no"&gt;IDENTITY&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;invoice_id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;billing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;invoices&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;action&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;billing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user_totals&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;identity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;billing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;invoices&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
    &lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;identity.users&lt;/code&gt; holds 1,000 rows and &lt;code&gt;billing.invoices&lt;/code&gt; holds 1,000,000 (87 MB with its indexes).&lt;/p&gt;

&lt;h2&gt;
  
  
  Postgres schema vs database: what is the difference?
&lt;/h2&gt;

&lt;p&gt;A schema is a namespace inside one database, and a database is a separate catalog that a connection cannot see out of.&lt;/p&gt;

&lt;p&gt;The PostgreSQL manual &lt;a href="https://www.postgresql.org/docs/18/ddl-schemas.html" rel="noopener noreferrer"&gt;puts it in two sentences&lt;/a&gt;: a client connection "can only access data in a single database", and "Unlike databases, schemas are not rigidly separated". Everything in this table follows from that:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Two schemas, one database&lt;/th&gt;
&lt;th&gt;Two databases, one server&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Foreign key between them&lt;/td&gt;
&lt;td&gt;Works&lt;/td&gt;
&lt;td&gt;Refused: &lt;code&gt;cross-database references are not implemented&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Join in one query&lt;/td&gt;
&lt;td&gt;Works&lt;/td&gt;
&lt;td&gt;Refused, same error (only through &lt;code&gt;postgres_fdw&lt;/code&gt; or &lt;code&gt;dblink&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;One transaction over both&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No: two connections, atomic only with two-phase commit&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Connection&lt;/td&gt;
&lt;td&gt;One&lt;/td&gt;
&lt;td&gt;One per database&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Roles&lt;/td&gt;
&lt;td&gt;Shared&lt;/td&gt;
&lt;td&gt;Shared (roles belong to the server)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Extensions&lt;/td&gt;
&lt;td&gt;Installed once&lt;/td&gt;
&lt;td&gt;Installed per database&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Empty cost&lt;/td&gt;
&lt;td&gt;2 ms to create, no files&lt;/td&gt;
&lt;td&gt;44 ms to create, 7.6 MB on disk&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Move a table across&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;ALTER TABLE ... SET SCHEMA&lt;/code&gt;, 2.6 ms&lt;/td&gt;
&lt;td&gt;Dump and restore&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dump or restore one alone&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;pg_dump -n&lt;/code&gt;, not self-contained&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;pg_dump&lt;/code&gt;, self-contained&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you come from MySQL, a PostgreSQL schema is what MySQL calls a database: there &lt;code&gt;CREATE SCHEMA&lt;/code&gt; is a synonym for &lt;code&gt;CREATE DATABASE&lt;/code&gt;, and two of them on one server can join and reference each other.&lt;/p&gt;

&lt;h2&gt;
  
  
  What can cross a schema boundary that cannot cross a database?
&lt;/h2&gt;

&lt;p&gt;A foreign key, a view, a join and a transaction cross a schema, and a database boundary refuses all four.&lt;/p&gt;

&lt;p&gt;The setup above already does it: &lt;code&gt;billing.invoices.user_id&lt;/code&gt; references &lt;code&gt;identity.users&lt;/code&gt;, and &lt;code&gt;billing.user_totals&lt;/code&gt; joins the two schemas. Against a second database on the same server, both are refused with the same message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;crm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;public&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;contacts&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- ERROR:  cross-database references are not implemented: "crm.public.contacts"&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;billing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;contact_id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;crm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;public&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;contacts&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- ERROR:  cross-database references are not implemented: "crm.public.contacts"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The usual workaround, &lt;code&gt;postgres_fdw&lt;/code&gt;, gives you the join back and not the key. A foreign table can be queried, but it cannot be the target of a foreign key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;billing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fdw_child&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;remote&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- ERROR:  referenced relation "users" is not a table&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So once two modules live in two databases, nothing stops an invoice from pointing at a user that was deleted. That is the same trade as &lt;a href="https://schemity.com/blog/foreign-keys-between-modules-modular-monolith/" rel="noopener noreferrer"&gt;dropping a foreign key between two modules&lt;/a&gt;, made for every key at once.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does a separate schema isolate a module?
&lt;/h2&gt;

&lt;p&gt;A schema isolates access and names, and it does not isolate dependencies.&lt;/p&gt;

&lt;p&gt;Access first. A role granted only the &lt;code&gt;billing&lt;/code&gt; tables cannot read &lt;code&gt;identity&lt;/code&gt; directly, and the foreign key into &lt;code&gt;identity&lt;/code&gt; is still enforced for it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="no"&gt;ROLE&lt;/span&gt; &lt;span class="n"&gt;billing_app&lt;/span&gt; &lt;span class="no"&gt;LOGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;GRANT&lt;/span&gt; &lt;span class="no"&gt;USAGE&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;SCHEMA&lt;/span&gt; &lt;span class="n"&gt;billing&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;billing_app&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;GRANT&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;billing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;invoices&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;billing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;audit_log&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;billing_app&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="no"&gt;ROLE&lt;/span&gt; &lt;span class="n"&gt;billing_app&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FROM identity.users;
-- ERROR:  permission denied for schema identity
INSERT INTO billing.invoices (user_id, total) VALUES (999999, 5);
-- ERROR:  insert or update on table "invoices" violates foreign key constraint "invoices_user_id_fkey"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is the property a module boundary wants: the billing code cannot query users, and it still cannot invent one. Mind the views, though. A view runs with its owner's privileges, so after &lt;code&gt;GRANT SELECT ON billing.user_totals TO billing_app&lt;/code&gt; the same role read &lt;code&gt;u1000@example.com&lt;/code&gt; out of &lt;code&gt;identity.users&lt;/code&gt; through it.&lt;/p&gt;

&lt;p&gt;Names second. Both schemas have an &lt;code&gt;audit_log&lt;/code&gt;, and which one an unqualified name means depends on the session's &lt;code&gt;search_path&lt;/code&gt;. With the default path (&lt;code&gt;"$user", public&lt;/code&gt;) the name finds neither and &lt;code&gt;SELECT count(*) FROM audit_log&lt;/code&gt; fails with &lt;code&gt;relation "audit_log" does not exist&lt;/code&gt;. With &lt;code&gt;SET search_path TO billing, identity&lt;/code&gt; it is &lt;code&gt;billing.audit_log&lt;/code&gt;; swap the order and the same query reads &lt;code&gt;identity.audit_log&lt;/code&gt;, and an unqualified &lt;code&gt;CREATE TABLE&lt;/code&gt; lands in whichever schema is first. Qualify every name in code that touches more than one schema.&lt;/p&gt;

&lt;p&gt;Dependencies are what a schema does not contain. &lt;code&gt;identity.users&lt;/code&gt; cannot be dropped, and neither can its &lt;code&gt;id&lt;/code&gt; column or its primary key, because of a key that lives in &lt;code&gt;billing&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;identity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- ERROR:  cannot drop table identity.users because other objects depend on it&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;DROP SCHEMA identity CASCADE&lt;/code&gt; does go through, and takes four objects with it, two of them in the other schema: the constraint &lt;code&gt;invoices_user_id_fkey&lt;/code&gt; on &lt;code&gt;billing.invoices&lt;/code&gt; and the view &lt;code&gt;billing.user_totals&lt;/code&gt;, reported only in a &lt;code&gt;NOTICE&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How hard is it to move a table later?
&lt;/h2&gt;

&lt;p&gt;Between schemas a move is a catalog update that took 2.6 ms for a million rows, and between databases it is a dump and a restore that loses every key crossing the boundary.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ALTER TABLE billing.invoices SET SCHEMA identity&lt;/code&gt; took 2.6 ms on the 1,000,000-row table. Its &lt;code&gt;relfilenode&lt;/code&gt; was 16420 before and after, so no data was rewritten, and the foreign key from &lt;code&gt;billing.audit_log&lt;/code&gt; followed it: its definition read &lt;code&gt;REFERENCES identity.invoices(id)&lt;/code&gt; afterwards. A view that reads the table by name in another schema keeps working too, because PostgreSQL tracks it by object id.&lt;/p&gt;

&lt;p&gt;Moving both schemas to a new database with &lt;code&gt;pg_dump | psql&lt;/code&gt; took 2.3 seconds for the same data, which is fast, and is not the hard part. The hard part is moving one. A dump of &lt;code&gt;billing&lt;/code&gt; alone, taken before the role above existed, is not self-contained:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pg_dump -n billing --schema-only shop | psql billing_db
ERROR:  relation "identity.users" does not exist
ERROR:  relation "billing.user_totals" does not exist
ERROR:  schema "identity" does not exist
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both tables arrive, the view does not, and the only foreign key left is the one inside the schema. &lt;code&gt;invoices_user_id_fkey&lt;/code&gt; is gone and cannot be put back, because its target is now in another database. Starting with schemas keeps the move to a database open; starting with databases makes the move back a data-cleaning project, since rows written without the key may already be orphans.&lt;/p&gt;

&lt;h2&gt;
  
  
  Should each tenant get its own schema or its own database?
&lt;/h2&gt;

&lt;p&gt;A schema per tenant works for tens or hundreds of tenants and gets expensive in the thousands, because every table costs catalog rows and files even when it is empty.&lt;/p&gt;

&lt;p&gt;I created 1,000 tenant schemas of 10 small tables each, with no rows in any of them:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Measured on PostgreSQL 18.3, default settings&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Creating all 1,000 in one transaction&lt;/td&gt;
&lt;td&gt;Failed at the 288th: &lt;code&gt;out of shared memory&lt;/code&gt;, hint to raise &lt;code&gt;max_locks_per_transaction&lt;/code&gt; (default 64)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Creating them one transaction per tenant&lt;/td&gt;
&lt;td&gt;Worked&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Rows in &lt;code&gt;pg_class&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;50,415 (a new database has 415)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Database size, zero rows of data&lt;/td&gt;
&lt;td&gt;427 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;pg_dump --schema-only&lt;/code&gt; of the database&lt;/td&gt;
&lt;td&gt;2.9 s, 7.1 MB of SQL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;pg_dump --schema-only -n tenant_500&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;0.54 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;DROP SCHEMA tenant_1000 CASCADE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;17 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Each empty table brings three 8 kB files: the index for its key, the sequence for its identity and the index of its TOAST table. That is 234 MB, and the rest is the catalog describing them: &lt;code&gt;pg_attribute&lt;/code&gt; alone was 91 MB, &lt;code&gt;pg_depend&lt;/code&gt; 33 MB and &lt;code&gt;pg_class&lt;/code&gt; 24 MB. A migration then has to run 1,000 times, and anything that touches many tenants in one transaction meets the lock limit that stopped the first attempt.&lt;/p&gt;

&lt;p&gt;A database per tenant costs more per tenant (7.6 MB and 44 ms each, empty) and buys the hard boundary: a tenant can be backed up, restored, moved to another server or deleted alone, and no query can join two of them. It also means one connection pool per tenant, since a connection belongs to one database.&lt;/p&gt;

&lt;p&gt;For most products the third option is the right default: one set of tables with a &lt;code&gt;tenant_id&lt;/code&gt; column, with &lt;a href="https://schemity.com/blog/postgres-composite-foreign-key-vs-row-level-security/" rel="noopener noreferrer"&gt;composite foreign keys or row-level security&lt;/a&gt; keeping tenants apart.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which should I use: a schema or a database?
&lt;/h2&gt;

&lt;p&gt;Choose a schema unless you can name the thing the two parts must never share.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Modules of one application&lt;/strong&gt; (billing, identity, catalog): schemas. You keep the keys between them, one migration, one backup, and a role per module if you want the access boundary.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A tenant that must be restored, moved or deleted alone&lt;/strong&gt;, or that has a contractual isolation requirement: a database.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Many small tenants&lt;/strong&gt;: neither, use a &lt;code&gt;tenant_id&lt;/code&gt; column.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Two applications with different release cycles and no shared rows&lt;/strong&gt;: databases.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Staging and production&lt;/strong&gt;: separate servers, not either of these.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How do you find the foreign keys that cross schemas?
&lt;/h2&gt;

&lt;p&gt;Compare the schema of each foreign key's table with the schema of the table it references in &lt;code&gt;pg_constraint&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;con&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;conname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;con&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;conrelid&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt;  &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;referencing_table&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;con&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;confrelid&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;referenced_table&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_constraint&lt;/span&gt; &lt;span class="n"&gt;con&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;pg_class&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;con&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;conrelid&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;pg_class&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;con&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;confrelid&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;con&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;contype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'f'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;relnamespace&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;relnamespace&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the example it returns one row: &lt;code&gt;invoices_user_id_fkey&lt;/code&gt;, from &lt;code&gt;billing.invoices&lt;/code&gt; to &lt;code&gt;identity.users&lt;/code&gt;. That list is the real coupling between your schemas, and it is the list of keys you would lose by splitting them into databases. Views are the other half: &lt;code&gt;billing.user_totals&lt;/code&gt; reads &lt;code&gt;identity.users&lt;/code&gt; and appears in no foreign key list.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Schemity read a database with several schemas?
&lt;/h2&gt;

&lt;p&gt;Schemity reads one schema per diagram and reports the keys and views from other schemas that depend on its tables.&lt;/p&gt;

&lt;p&gt;Schemity is database design software that reads your live database, shows the impact of every schema change before it runs, and keeps the diagram as a file in Git. For a PostgreSQL connection, the &lt;strong&gt;Schema&lt;/strong&gt; field in the &lt;a href="https://schemity.com/doc/connect-postgresql/" rel="noopener noreferrer"&gt;connection dialog&lt;/a&gt; loads the database's schemas so you pick one, and the diagram draws that schema's tables. A diagram of &lt;code&gt;billing&lt;/code&gt; shows &lt;code&gt;invoices&lt;/code&gt; and &lt;code&gt;audit_log&lt;/code&gt; with the key between them; &lt;code&gt;invoices.user_id&lt;/code&gt; keeps its foreign key marker with no line, because the table at the other end is in &lt;code&gt;identity&lt;/code&gt;, and the field's details name the target as &lt;code&gt;identity.users.id&lt;/code&gt;. A migration that has to add the key again writes &lt;code&gt;REFERENCES "identity"."users"&lt;/code&gt;, so it cannot land on a &lt;code&gt;users&lt;/code&gt; table of the current schema.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvywtn58xs928x4fyn13c.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvywtn58xs928x4fyn13c.webp" alt="Schemity reading the billing schema of a PostgreSQL 18.3 database: invoices and audit_log joined by a relationship line, the user_totals view beside them, and invoices.user_id selected with its foreign key marker and no line. The field details panel shows FK ticked, type BIGINT, References identity.users.id (ON DELETE NO ACTION, ON UPDATE NO ACTION) and the index invoices_user_id_idx (user_id)." width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open the &lt;code&gt;identity&lt;/code&gt; diagram and plan a change to &lt;code&gt;users.id&lt;/code&gt;, and &lt;a href="https://schemity.com/doc/impact-analysis/" rel="noopener noreferrer"&gt;impact analysis&lt;/a&gt; (Schemity Pro) lists what the diagram cannot draw: the foreign key &lt;code&gt;invoices_user_id_fkey on billing.invoices&lt;/code&gt; and the view &lt;code&gt;billing.user_totals&lt;/code&gt;, each with what PostgreSQL will do. Delete the &lt;code&gt;users&lt;/code&gt; table and the planned &lt;code&gt;DROP TABLE ... CASCADE&lt;/code&gt; would &lt;a href="https://schemity.com/blog/postgres-drop-table-cascade-what-it-drops/" rel="noopener noreferrer"&gt;take both with it&lt;/a&gt;, which PostgreSQL reports only in a &lt;code&gt;NOTICE&lt;/code&gt;, so the finding says they are dropped too. A drop of the &lt;code&gt;id&lt;/code&gt; column is refused while the key exists, a rename is followed, and a type change is refused unless the two columns can still be compared. On this example the view refuses a type change first (&lt;code&gt;cannot alter type of a column used by a view or rule&lt;/code&gt;). With the view, the identity and the key inside &lt;code&gt;identity&lt;/code&gt; out of the way, &lt;code&gt;bigint&lt;/code&gt; to &lt;code&gt;integer&lt;/code&gt; or &lt;code&gt;numeric&lt;/code&gt; passed, and to &lt;code&gt;text&lt;/code&gt; or &lt;code&gt;uuid&lt;/code&gt; failed with &lt;code&gt;foreign key constraint "invoices_user_id_fkey" cannot be implemented&lt;/code&gt;. That is how you understand your schema one module at a time without losing the edges between modules.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxmqkx33mc543kg534ckp.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxmqkx33mc543kg534ckp.webp" alt="Schemity's change preview for dropping identity.users on PostgreSQL 18.3: users outlined in red with its line to audit_log, the planned migration ending in DROP TABLE IF EXISTS &amp;quot;users&amp;quot; CASCADE, and three impact findings. The second says 2 objects depend on users: view billing.user_totals depends on users.email and foreign key invoices_user_id_fkey on billing.invoices depends on users.id, each with &amp;quot;CASCADE drops it too, and the database reports that only as a notice.&amp;quot;" width="800" height="563"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Import SQL does the same for a dump: when the file holds more than one schema it lists them to pick from, and it keeps two tables of the same name in different schemas apart, so neither &lt;code&gt;audit_log&lt;/code&gt; overwrites the other.&lt;/p&gt;

&lt;p&gt;If your modules share one schema instead, a &lt;a href="https://schemity.com/doc/context-views/" rel="noopener noreferrer"&gt;context view&lt;/a&gt; gives you the per-module picture without moving anything. A context view is a focused sub-diagram that displays a chosen subset of the main diagram while the main view remains the single source of truth.&lt;/p&gt;

&lt;p&gt;When a table turns out to sit in the wrong module, the cycle it creates is the usual symptom, covered in &lt;a href="https://schemity.com/blog/circular-dependency-between-modules-misplaced-table/" rel="noopener noreferrer"&gt;circular dependencies between modules&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>sql</category>
      <category>architecture</category>
    </item>
    <item>
      <title>SQL Server IDENTITY vs SEQUENCE: Which Should a Primary Key Use?</title>
      <dc:creator>Son Tran</dc:creator>
      <pubDate>Sat, 28 Nov 2026 07:11:04 +0000</pubDate>
      <link>https://dev.to/tbson87/sql-server-identity-vs-sequence-which-should-a-primary-key-use-4mfg</link>
      <guid>https://dev.to/tbson87/sql-server-identity-vs-sequence-which-should-a-primary-key-use-4mfg</guid>
      <description>&lt;p&gt;&lt;em&gt;Disclosure: I build &lt;a href="https://schemity.com" rel="noopener noreferrer"&gt;Schemity&lt;/a&gt;, a desktop ERD tool - this post is from our blog and uses it for the examples.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; Give a table's own key &lt;code&gt;IDENTITY&lt;/code&gt; unless you need what only a sequence does: one number space across several tables, a number before the row exists, or a column you can renumber later without rebuilding the table. A sequence default also lets an import write its own ids, which then collide with the sequence. A bare &lt;code&gt;CREATE SEQUENCE&lt;/code&gt; with no options starts at the most negative &lt;code&gt;bigint&lt;/code&gt;, so always state &lt;code&gt;AS&lt;/code&gt; and &lt;code&gt;START WITH&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A SQL Server primary key should use &lt;code&gt;IDENTITY&lt;/code&gt; when it belongs to one table and only the database fills it, and a &lt;code&gt;SEQUENCE&lt;/code&gt; when the number has to be shared, known before the insert, or changed later. The difference that decides most cases is not speed or gaps but what you can still do after the table exists: an &lt;code&gt;IDENTITY&lt;/code&gt; is fixed when the column is created, and SQL Server has no statement that adds one to a column, removes one, or changes its step.&lt;/p&gt;

&lt;p&gt;Everything below ran on SQL Server 2022 (RTM-CU26, 16.0.4265.3) in a throwaway container, with these two tables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders_i&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;IDENTITY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;note&lt;/span&gt; &lt;span class="nf"&gt;nvarchar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="no"&gt;SEQUENCE&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id_seq&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;START&lt;/span&gt; &lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="no"&gt;INCREMENT&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders_s&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="n"&gt;df_orders_s_id&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;NEXT&lt;/span&gt; &lt;span class="no"&gt;VALUE&lt;/span&gt; &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id_seq&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;note&lt;/span&gt; &lt;span class="nf"&gt;nvarchar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  SQL Server IDENTITY vs SEQUENCE: what is the difference?
&lt;/h2&gt;

&lt;p&gt;An &lt;code&gt;IDENTITY&lt;/code&gt; is a property of one column that numbers that table's rows, while a sequence is a separate schema object that hands out numbers to whoever asks, usually through a column default.&lt;/p&gt;

&lt;p&gt;That one difference produces every row of this table:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;IDENTITY&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Sequence default&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Belongs to&lt;/td&gt;
&lt;td&gt;One column of one table&lt;/td&gt;
&lt;td&gt;Its own object; any table or query can call it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;An insert that supplies its own id&lt;/td&gt;
&lt;td&gt;Refused (Msg 544) unless &lt;code&gt;SET IDENTITY_INSERT ON&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Accepted, then collides with the sequence later&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Per table&lt;/td&gt;
&lt;td&gt;One (Msg 2744 on a second)&lt;/td&gt;
&lt;td&gt;As many defaults as you like&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Added to an existing column&lt;/td&gt;
&lt;td&gt;No: &lt;code&gt;ALTER COLUMN ... IDENTITY&lt;/code&gt; is a syntax error&lt;/td&gt;
&lt;td&gt;Yes: &lt;code&gt;ADD CONSTRAINT ... DEFAULT ... FOR&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Step changed later&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ALTER SEQUENCE ... INCREMENT BY&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;TRUNCATE TABLE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Restarts at the seed&lt;/td&gt;
&lt;td&gt;Carries on&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Number before the insert&lt;/td&gt;
&lt;td&gt;No; read it back with &lt;code&gt;SCOPE_IDENTITY()&lt;/code&gt; or &lt;code&gt;OUTPUT&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;NEXT VALUE FOR&lt;/code&gt; in any query&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Jump after a crash (default settings)&lt;/td&gt;
&lt;td&gt;+1,000 for &lt;code&gt;int&lt;/code&gt;, +10,000 for &lt;code&gt;bigint&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Up to the cache; +49 on 2022 CU26&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  What happens when an import writes its own ids?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;IDENTITY&lt;/code&gt; refuses the row unless you switch on &lt;code&gt;IDENTITY_INSERT&lt;/code&gt;, while a sequence default accepts it and then hands out the same number later.&lt;/p&gt;

&lt;p&gt;On the identity table, &lt;code&gt;INSERT INTO orders_i (id, note) VALUES (10, 'x')&lt;/code&gt; fails with &lt;code&gt;Msg 544: Cannot insert explicit value for identity column in table 'orders_i' when IDENTITY_INSERT is set to OFF&lt;/code&gt;. The way through is &lt;code&gt;SET IDENTITY_INSERT orders_i ON&lt;/code&gt;, and it is limited to one table per session: switching it on for a second table in the same session returned &lt;code&gt;Msg 8107: IDENTITY_INSERT is already ON for table ... Cannot perform SET operation for table ...&lt;/code&gt;. A data load across many tables has to switch it off and on table by table.&lt;/p&gt;

&lt;p&gt;The sequence default looks friendlier and is the riskier of the two. An import wrote &lt;code&gt;id = 2&lt;/code&gt; with no complaint. The next insert that used the default got 1, and the one after that drew 2 from the sequence and failed: &lt;code&gt;Msg 2627: Violation of PRIMARY KEY constraint ... The duplicate key value is (2)&lt;/code&gt;. Nothing ties the sequence to the column, so after any load that brings its own ids, restart the sequence above the highest one with &lt;code&gt;ALTER SEQUENCE dbo.order_id_seq RESTART WITH &amp;lt;max + 1&amp;gt;&lt;/code&gt;. It is the same trap as PostgreSQL's &lt;code&gt;serial&lt;/code&gt;, described in &lt;a href="https://schemity.com/blog/postgres-serial-vs-identity/" rel="noopener noreferrer"&gt;serial vs identity&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Can you change an IDENTITY column later?
&lt;/h2&gt;

&lt;p&gt;No: SQL Server cannot add &lt;code&gt;IDENTITY&lt;/code&gt; to an existing column, remove it, or change its step, so any of those means a new table, usually with a copy of the rows.&lt;/p&gt;

&lt;p&gt;Three attempts, three refusals. &lt;code&gt;ALTER TABLE legacy ALTER COLUMN id int IDENTITY(1,1) NOT NULL&lt;/code&gt; is not even parsed (&lt;code&gt;Msg 156: Incorrect syntax near the keyword 'IDENTITY'&lt;/code&gt;). Putting a sequence default on the identity column instead fails with &lt;code&gt;Msg 1754: Defaults cannot be created on columns with an IDENTITY attribute&lt;/code&gt;. And Microsoft's &lt;a href="https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql-identity-property" rel="noopener noreferrer"&gt;IDENTITY property page&lt;/a&gt; states it plainly: "Once the identity property is set on a column, it can't be removed."&lt;/p&gt;

&lt;p&gt;The sequence side of the same changes is ordinary DDL. A legacy table with ids 1 to 3 and no numbering took a default on the existing key with &lt;code&gt;CREATE SEQUENCE dbo.legacy_seq AS int START WITH 4&lt;/code&gt; and &lt;code&gt;ALTER TABLE legacy ADD CONSTRAINT df_legacy_id DEFAULT (NEXT VALUE FOR dbo.legacy_seq) FOR id&lt;/code&gt;, and the next insert got 4. Changing the step is &lt;code&gt;ALTER SEQUENCE dbo.order_id_seq INCREMENT BY 10&lt;/code&gt;, effective on the next value. If a table's numbering is likely to change, that flexibility is the reason to pick the sequence on day one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Do IDENTITY and SEQUENCE both leave gaps?
&lt;/h2&gt;

&lt;p&gt;Yes, both do: neither gives back a number from a rolled-back insert, and at default settings both lose their cached numbers when the server stops unexpectedly.&lt;/p&gt;

&lt;p&gt;A rolled-back insert into the identity table left ids 1 and 3, and Microsoft's sequence docs say numbers are "consumed whether the transaction using the sequence number is committed or rolled back". The crash gap is the one that surprises people. After two inserts into each table, &lt;code&gt;SHUTDOWN WITH NOWAIT&lt;/code&gt; and a restart, the next insert got:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Key&lt;/th&gt;
&lt;th&gt;Before the restart&lt;/th&gt;
&lt;th&gt;After&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;int IDENTITY&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;1, 2&lt;/td&gt;
&lt;td&gt;1002&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;bigint IDENTITY&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;1, 2&lt;/td&gt;
&lt;td&gt;10002&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;int&lt;/code&gt; sequence, default cache&lt;/td&gt;
&lt;td&gt;1, 2&lt;/td&gt;
&lt;td&gt;51&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;int&lt;/code&gt; sequence, &lt;code&gt;NO CACHE&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;1, 2&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Microsoft's documented fix for the identity jump is &lt;code&gt;ALTER DATABASE SCOPED CONFIGURATION SET IDENTITY_CACHE = OFF&lt;/code&gt;, available since SQL Server 2017, at some insert cost; &lt;code&gt;NO CACHE&lt;/code&gt; does the same for a sequence, as the last row shows. If a number must have no gaps, neither object promises it; that needs a counter row updated in the same transaction as the insert.&lt;/p&gt;

&lt;p&gt;One more difference: &lt;code&gt;TRUNCATE TABLE orders_i&lt;/code&gt; started the identity again at 1, while after &lt;code&gt;TRUNCATE TABLE orders_s&lt;/code&gt; the sequence carried on at 3. Tests that truncate between runs get repeatable ids from &lt;code&gt;IDENTITY&lt;/code&gt; for free; a sequence also needs an &lt;code&gt;ALTER SEQUENCE ... RESTART&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does a bare CREATE SEQUENCE start at?
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;CREATE SEQUENCE&lt;/code&gt; with no options is a &lt;code&gt;bigint&lt;/code&gt; that starts at -9,223,372,036,854,775,808, so a sequence written without &lt;code&gt;AS&lt;/code&gt; and &lt;code&gt;START WITH&lt;/code&gt; hands out negative ids.&lt;/p&gt;

&lt;p&gt;Microsoft's &lt;a href="https://learn.microsoft.com/en-us/sql/t-sql/statements/create-sequence-transact-sql" rel="noopener noreferrer"&gt;CREATE SEQUENCE page&lt;/a&gt; says the default type is &lt;code&gt;bigint&lt;/code&gt; and the default start is "the minimum value for an ascending sequence object". Measured: &lt;code&gt;CREATE SEQUENCE dbo.bare_seq&lt;/code&gt; as a &lt;code&gt;bigint&lt;/code&gt; key's default gave the first row &lt;code&gt;id = -9223372036854775808&lt;/code&gt;. The same sequence as an &lt;code&gt;int&lt;/code&gt; key's default refused the first insert with &lt;code&gt;Msg 8115: Arithmetic overflow error converting expression to data type int&lt;/code&gt;. Write both clauses every time: &lt;code&gt;CREATE SEQUENCE dbo.order_id_seq AS int START WITH 1&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  When should a primary key use a sequence instead of IDENTITY?
&lt;/h2&gt;

&lt;p&gt;Use a sequence when the number is shared across tables, needed before the row exists, or likely to change; for every other key, &lt;code&gt;IDENTITY&lt;/code&gt; is the simpler choice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One number space across tables.&lt;/strong&gt; Invoices and credit notes that must never share a document number can both default to one sequence. With &lt;code&gt;CREATE SEQUENCE dbo.doc_no AS bigint START WITH 1000&lt;/code&gt;, alternate inserts gave invoice 1000, credit note 1001 and invoice 1002. While a default uses it, the sequence cannot be dropped by accident: &lt;code&gt;DROP SEQUENCE dbo.doc_no&lt;/code&gt; failed with &lt;code&gt;Msg 3729: Cannot DROP SEQUENCE 'dbo.doc_no' because it is being referenced by object 'DF__invoices__id__...'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A number before the insert.&lt;/strong&gt; &lt;code&gt;DECLARE @id int = NEXT VALUE FOR dbo.order_id_seq&lt;/code&gt; reserves an id the application can put on child rows, a file name or a message before the parent row is written, and &lt;code&gt;sp_sequence_get_range&lt;/code&gt; reserves a block of them for a batch. With &lt;code&gt;IDENTITY&lt;/code&gt; the id exists only after the insert.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Numbering you may change.&lt;/strong&gt; A new step or a move to a shared number space is one &lt;code&gt;ALTER&lt;/code&gt; on a sequence and a table rebuild on an identity. A new starting point is cheap on both: &lt;code&gt;ALTER SEQUENCE ... RESTART WITH&lt;/code&gt; or &lt;code&gt;DBCC CHECKIDENT ... RESEED&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Against that, &lt;code&gt;IDENTITY&lt;/code&gt; keeps the key and its numbering in one place, and refuses stray ids by default. For a table whose ids only ever come from the database, that is the better trade. The choice is the same one as &lt;a href="https://schemity.com/blog/postgres-uuid-vs-bigint-primary-key/" rel="noopener noreferrer"&gt;UUID vs bigint&lt;/a&gt; in one respect: it is cheap on day one and expensive once the table holds data.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Schemity handle IDENTITY and sequence keys on SQL Server?
&lt;/h2&gt;

&lt;p&gt;Schemity draws an &lt;code&gt;IDENTITY&lt;/code&gt; as an Auto increment tick with its start and step, keeps a sequence default as the column's default, and locks an identity change SQL Server cannot make in place.&lt;/p&gt;

&lt;p&gt;Schemity is database design software that reads your live database, shows the impact of every schema change before it runs, and keeps the diagram as a file in Git. To design the next one, the field editor's &lt;a href="https://schemity.com/doc/tables-and-fields/#how-do-i-make-the-database-number-a-key" rel="noopener noreferrer"&gt;Auto increment tick&lt;/a&gt; is written as &lt;code&gt;IDENTITY(start, step)&lt;/code&gt; on SQL Server, and a numbered column takes no default, which matches Msg 1754 above. On a column that already exists in the database, the tick, Start at and Step are locked, and the tick's tooltip says why: SQL Server fixes a column's auto-increment when the column is created, and changing it means rebuilding the table and copying its rows. A change made any other way, such as an edited diagram file, is refused when the migration is planned, for the same reason.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ft3irt64se85v5ye618nh.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ft3irt64se85v5ye618nh.webp" alt="Schemity's field editor for orders.id open beside the canvas, where orders.id is an INT marked auto and invoices.id and credit_notes.id default to NEXT VALUE FOR [dbo].[doc_no]: PK is ticked, the Auto increment tick is ticked but greyed out, Start at 1 and Step 1 are greyed out too, and the tooltip over the tick reads SQL Server fixes a column's auto-increment when the column is created. Changing it means rebuilding the table and copying its rows, which Schemity does not do." width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A key that defaults to a sequence is read from the live database with its &lt;code&gt;NEXT VALUE FOR&lt;/code&gt; default and written back as it is. Import SQL and the free &lt;a href="https://schemity.com/doc/migration-check-ci/" rel="noopener noreferrer"&gt;Migration Check&lt;/a&gt; read the same default in a script, along with SQL Server's own &lt;code&gt;ALTER TABLE ... ADD DEFAULT ... FOR&lt;/code&gt; form. Schemity does not create the sequence on SQL Server: the &lt;code&gt;CREATE SEQUENCE&lt;/code&gt;, with its &lt;code&gt;AS&lt;/code&gt; and &lt;code&gt;START WITH&lt;/code&gt;, stays in your migration. &lt;a href="https://schemity.com/doc/impact-analysis/" rel="noopener noreferrer"&gt;Impact analysis&lt;/a&gt;, a Schemity Pro feature, shows the rest of a key change, such as widening an identity from &lt;code&gt;int&lt;/code&gt; to &lt;code&gt;bigint&lt;/code&gt;, before it is applied.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fv3r3hommlqhiqr7qveae.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fv3r3hommlqhiqr7qveae.webp" alt="Schemity's canvas reading three tables from SQL Server 16.0.4265.3: orders.id is an INT primary key marked auto for its IDENTITY, with note NVARCHAR(50) defaulting to an empty string; invoices.id and credit_notes.id are BIGINT primary keys whose default reads NEXT VALUE FOR [dbo].[doc_no], each beside total DECIMAL(12,2) defaulting to 0" width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the next decision on the same key, read &lt;a href="https://schemity.com/blog/sqlserver-change-int-to-bigint-primary-key/" rel="noopener noreferrer"&gt;changing an INT primary key to BIGINT on SQL Server&lt;/a&gt;, and for what a foreign key should point at, &lt;a href="https://schemity.com/blog/natural-key-vs-surrogate-key-foreign-key/" rel="noopener noreferrer"&gt;natural key vs surrogate key&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>sqlserver</category>
      <category>database</category>
      <category>sql</category>
      <category>tsql</category>
    </item>
    <item>
      <title>Renaming a Column: What Breaks on PostgreSQL, MySQL, SQL Server and SQLite</title>
      <dc:creator>Son Tran</dc:creator>
      <pubDate>Sat, 28 Nov 2026 07:10:36 +0000</pubDate>
      <link>https://dev.to/tbson87/renaming-a-column-what-breaks-on-postgresql-mysql-sql-server-and-sqlite-1bee</link>
      <guid>https://dev.to/tbson87/renaming-a-column-what-breaks-on-postgresql-mysql-sql-server-and-sqlite-1bee</guid>
      <description>&lt;p&gt;&lt;em&gt;Disclosure: I build &lt;a href="https://schemity.com" rel="noopener noreferrer"&gt;Schemity&lt;/a&gt;, a desktop ERD tool - this post is from our blog and uses it for the examples.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; A column rename is a quick metadata change on every database, but what it does to the objects that read the column differs. PostgreSQL and SQLite rewrite views, checks, generated columns and indexes to the new name; MySQL and SQL Server refuse the rename while a &lt;code&gt;CHECK&lt;/code&gt; or generated column reads the column, and let it through while leaving views broken. On every engine but SQLite, a trigger or routine body can fail on its next run, so list what reads the column before you rename it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Renaming a column is safe for the data and unsafe for everything that reads the column by name, and each database draws that line in a different place. PostgreSQL and SQLite rewrite views, &lt;code&gt;CHECK&lt;/code&gt; constraints, generated columns and indexes to the new name. MySQL and SQL Server refuse the rename while a &lt;code&gt;CHECK&lt;/code&gt; or a generated column reads the column, and accept it while leaving views, triggers and routines pointing at a name that no longer exists.&lt;/p&gt;

&lt;p&gt;We ran the same rename on PostgreSQL 18.3 (and 16.8, with the same results), MySQL 8.4.11 (and 9.7.4, with the same results), SQL Server 2022 CU26 and SQLite 3.54.0, each in a throwaway container. The table is &lt;code&gt;orders&lt;/code&gt;, and the column being renamed is &lt;code&gt;total&lt;/code&gt;, which has collected the usual readers. This is the PostgreSQL version; the other three use the same objects in their own syntax, with the &lt;code&gt;CHECK&lt;/code&gt; named &lt;code&gt;orders_total_check&lt;/code&gt; as PostgreSQL names it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="no"&gt;IDENTITY&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="no"&gt;IDENTITY&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="kt"&gt;numeric&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;total_with_tax&lt;/span&gt; &lt;span class="kt"&gt;numeric&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;1.10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="no"&gt;STORED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;orders_total_idx&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;orders_big_idx&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;order_audit&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;old_total&lt;/span&gt; &lt;span class="kt"&gt;numeric&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;changed_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;order_totals&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;FUNCTION&lt;/span&gt; &lt;span class="nf"&gt;log_total_change&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;RETURNS&lt;/span&gt; &lt;span class="k"&gt;trigger&lt;/span&gt; &lt;span class="no"&gt;LANGUAGE&lt;/span&gt; &lt;span class="n"&gt;plpgsql&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; $$
&lt;span class="k"&gt;BEGIN&lt;/span&gt;
  &lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;order_audit&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;old_total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;OLD&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;OLD&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;RETURN&lt;/span&gt; &lt;span class="no"&gt;NEW&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;END&lt;/span&gt; $$&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TRIGGER&lt;/span&gt; &lt;span class="n"&gt;orders_total_audit&lt;/span&gt; &lt;span class="no"&gt;AFTER&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="no"&gt;OF&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
  &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="no"&gt;EACH&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt; &lt;span class="no"&gt;EXECUTE&lt;/span&gt; &lt;span class="k"&gt;FUNCTION&lt;/span&gt; &lt;span class="nf"&gt;log_total_change&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;FUNCTION&lt;/span&gt; &lt;span class="nf"&gt;customer_spend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;RETURNS&lt;/span&gt; &lt;span class="kt"&gt;numeric&lt;/span&gt; &lt;span class="no"&gt;LANGUAGE&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
  $$ &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;coalesce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; $$&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'a@example.com'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9lpdiw56srk3ct8jwnim.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9lpdiw56srk3ct8jwnim.webp" alt="The four objects read from PostgreSQL 18.3 on a Schemity canvas: customers, orders with total, the generated column total_with_tax shown as total * 1.10, one check and two indexes, order_audit with old_total, and the view order_totals with its id, customer_id and total columns" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The statement under test is &lt;code&gt;ALTER TABLE orders RENAME COLUMN total TO total_amount;&lt;/code&gt;, or &lt;code&gt;EXEC sp_rename 'dbo.orders.total', 'total_amount', 'COLUMN';&lt;/code&gt; on SQL Server.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens to each object when a column is renamed?
&lt;/h2&gt;

&lt;p&gt;Each object that reads the column does one of three things: it follows the rename, it blocks the rename, or it lets the rename through and fails later.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Object that reads the column&lt;/th&gt;
&lt;th&gt;PostgreSQL 18.3&lt;/th&gt;
&lt;th&gt;MySQL 8.4&lt;/th&gt;
&lt;th&gt;SQL Server 2022&lt;/th&gt;
&lt;th&gt;SQLite 3.54&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Plain index&lt;/td&gt;
&lt;td&gt;Follows&lt;/td&gt;
&lt;td&gt;Follows&lt;/td&gt;
&lt;td&gt;Follows&lt;/td&gt;
&lt;td&gt;Follows&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Foreign key, either side&lt;/td&gt;
&lt;td&gt;Follows&lt;/td&gt;
&lt;td&gt;Follows&lt;/td&gt;
&lt;td&gt;Follows&lt;/td&gt;
&lt;td&gt;Follows&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;CHECK&lt;/code&gt; constraint&lt;/td&gt;
&lt;td&gt;Follows&lt;/td&gt;
&lt;td&gt;Refuses, error 3959&lt;/td&gt;
&lt;td&gt;Refuses, Msg 15336&lt;/td&gt;
&lt;td&gt;Follows&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Generated or computed column&lt;/td&gt;
&lt;td&gt;Follows&lt;/td&gt;
&lt;td&gt;Refuses, error 3108&lt;/td&gt;
&lt;td&gt;Refuses, Msg 15336&lt;/td&gt;
&lt;td&gt;Follows&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Index on an expression or with a &lt;code&gt;WHERE&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Follows&lt;/td&gt;
&lt;td&gt;Refuses, error 3837&lt;/td&gt;
&lt;td&gt;Refuses, Msg 5074&lt;/td&gt;
&lt;td&gt;Follows&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;View&lt;/td&gt;
&lt;td&gt;Follows, keeps its own column name&lt;/td&gt;
&lt;td&gt;Breaks, error 1356&lt;/td&gt;
&lt;td&gt;Breaks, Msg 207&lt;/td&gt;
&lt;td&gt;Follows, and renames its own column&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Trigger body&lt;/td&gt;
&lt;td&gt;Breaks on the next &lt;code&gt;UPDATE&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Breaks on the next &lt;code&gt;UPDATE&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Breaks on the next &lt;code&gt;UPDATE&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Follows&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Function or procedure body&lt;/td&gt;
&lt;td&gt;Breaks on the next call&lt;/td&gt;
&lt;td&gt;Breaks on the next call&lt;/td&gt;
&lt;td&gt;Breaks on the next call&lt;/td&gt;
&lt;td&gt;Not applicable&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;"Follows" means the database changed the object for you: after the rename, PostgreSQL's catalog showed &lt;code&gt;CHECK ((total_amount &amp;gt;= (0)::numeric))&lt;/code&gt;, a generated expression of &lt;code&gt;(total_amount * 1.10)&lt;/code&gt; and a partial index &lt;code&gt;WHERE (total_amount &amp;gt; (1000)::numeric)&lt;/code&gt;. The names do not follow. The constraint is still called &lt;code&gt;orders_total_check&lt;/code&gt; and the index &lt;code&gt;orders_total_idx&lt;/code&gt;, and on PostgreSQL 18, which stores each &lt;code&gt;NOT NULL&lt;/code&gt; as a named constraint, that one is still &lt;code&gt;orders_total_not_null&lt;/code&gt;, so the catalog now describes a column that the names no longer mention.&lt;/p&gt;

&lt;p&gt;The lock is the small part. PostgreSQL took an &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; lock, which the &lt;a href="https://www.postgresql.org/docs/current/sql-altertable.html" rel="noopener noreferrer"&gt;&lt;code&gt;ALTER TABLE&lt;/code&gt; documentation&lt;/a&gt; says is acquired "unless explicitly noted", SQL Server took a schema modification lock (&lt;code&gt;Sch-M&lt;/code&gt;), and MySQL accepted &lt;code&gt;ALGORITHM=INSTANT&lt;/code&gt;. None of them reads a row, but each waits behind any transaction still using the table and holds up every query that arrives after it, so set a short &lt;code&gt;lock_timeout&lt;/code&gt; on PostgreSQL and retry rather than let it queue. MySQL makes one exception: renaming a column that another table's foreign key points at is refused as &lt;code&gt;INSTANT&lt;/code&gt; with error 1846 and runs as &lt;code&gt;INPLACE&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does renaming a column break views?
&lt;/h2&gt;

&lt;p&gt;Not on PostgreSQL or SQLite, which rewrite the view, but on MySQL, MariaDB and SQL Server the rename succeeds and the view fails the next time something reads it.&lt;/p&gt;

&lt;p&gt;PostgreSQL stores a view's columns by position, so after the rename &lt;code&gt;pg_get_viewdef&lt;/code&gt; returned &lt;code&gt;total_amount AS total&lt;/code&gt;: the view reads the new column and still calls its own column &lt;code&gt;total&lt;/code&gt;, and nothing that queries the view notices. SQLite takes the other route and edits the view's text. Its &lt;a href="https://www.sqlite.org/lang_altertable.html" rel="noopener noreferrer"&gt;&lt;code&gt;ALTER TABLE&lt;/code&gt; page&lt;/a&gt; says the name is changed "within all indexes, triggers, and views that reference the column", and the stored view became &lt;code&gt;SELECT id, customer_id, total_amount FROM orders&lt;/code&gt;. That renames the view's own column as well, so &lt;code&gt;SELECT total FROM order_totals&lt;/code&gt; now fails with &lt;code&gt;no such column: total&lt;/code&gt;. A SQLite rename moves the breakage one layer out, to whatever reads the view.&lt;/p&gt;

&lt;p&gt;MySQL keeps a view as text and does not edit it. The rename returned no warning, and the next &lt;code&gt;SELECT * FROM order_totals&lt;/code&gt; failed with &lt;code&gt;ERROR 1356 (HY000): View 'shop.order_totals' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them&lt;/code&gt;. MariaDB 11.4.13 did the same. SQL Server printed its one standing caution, &lt;code&gt;Changing any part of an object name could break scripts and stored procedures&lt;/code&gt;, and the view then failed with &lt;code&gt;Msg 207: Invalid column name 'total'&lt;/code&gt; and &lt;code&gt;Msg 4413: Could not use view or function 'order_totals' because of binding errors&lt;/code&gt;. Running &lt;code&gt;sp_refreshview&lt;/code&gt; on it failed with the same Msg 207. Microsoft's &lt;a href="https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-rename-transact-sql" rel="noopener noreferrer"&gt;&lt;code&gt;sp_rename&lt;/code&gt; page&lt;/a&gt; states the rule: "Renaming an object such as a table or column won't automatically rename references to that object."&lt;/p&gt;

&lt;p&gt;Trigger and routine bodies are the part no engine but SQLite follows. On PostgreSQL the trigger's column list was rewritten to &lt;code&gt;AFTER UPDATE OF total_amount&lt;/code&gt;, so it still fired, and its PL/pgSQL body then failed with &lt;code&gt;record "old" has no field "total"&lt;/code&gt;. On MySQL the same trigger failed with &lt;code&gt;Unknown column 'total' in 'OLD'&lt;/code&gt;, and on SQL Server with Msg 207. In all three cases the error is raised by an ordinary &lt;code&gt;UPDATE&lt;/code&gt; of an order, not by anything that mentions the trigger, so one rename stops the application's writes. PostgreSQL has one body that does follow. A SQL function written with &lt;code&gt;BEGIN ATOMIC&lt;/code&gt; is stored parsed, and a second copy of &lt;code&gt;customer_spend&lt;/code&gt; that we created that way kept working and showed &lt;code&gt;sum(orders.total_amount)&lt;/code&gt; afterwards.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which renames does the database refuse outright?
&lt;/h2&gt;

&lt;p&gt;MySQL and SQL Server refuse a rename while an object they would have to rewrite reads the column, and each object type has its own error.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Reader&lt;/th&gt;
&lt;th&gt;MySQL 8.4.11&lt;/th&gt;
&lt;th&gt;SQL Server 2022&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;CHECK&lt;/code&gt; constraint&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ERROR 3959: Check constraint 'orders_total_check' uses column 'total', hence column cannot be dropped or renamed.&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Msg 15336: Object 'dbo.orders.total' cannot be renamed because the object participates in enforced dependencies.&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Generated or computed column&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ERROR 3108: Column 'total' has a generated column dependency.&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Msg 15336&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Functional or filtered index&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ERROR 3837: Column 'total' has a functional index dependency and cannot be dropped or renamed.&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Msg 5074: The index 'orders_big_idx' is dependent on column 'total'.&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Schema-bound view&lt;/td&gt;
&lt;td&gt;Not applicable&lt;/td&gt;
&lt;td&gt;Msg 15336&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A refusal is the safe outcome, because nothing has changed yet. The fix is to drop the reader, rename the column, and create the reader again with the new name. MySQL takes all of that in one &lt;code&gt;ALTER TABLE&lt;/code&gt;, which either happens completely or not at all:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
  &lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="n"&gt;orders_total_check&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="no"&gt;RENAME&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;total_amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="n"&gt;orders_total_check&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total_amount&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same statement accepted a &lt;code&gt;DROP COLUMN&lt;/code&gt; and an &lt;code&gt;ADD COLUMN&lt;/code&gt; for the generated column beside the rename. Written as separate statements instead, each one commits on its own, so a migration that fails at the rename has already dropped the check; &lt;a href="https://schemity.com/blog/transactional-ddl-postgres-mysql-cockroachdb/" rel="noopener noreferrer"&gt;which databases roll back a failed migration&lt;/a&gt; covers that half. SQL Server needs separate statements, since the rename is a procedure call, but runs them inside one transaction.&lt;/p&gt;

&lt;p&gt;Either way the rename is no longer a catalog change. MySQL refused &lt;code&gt;ADD CONSTRAINT ... CHECK&lt;/code&gt; with &lt;code&gt;ALGORITHM=INPLACE&lt;/code&gt; (error 1845, &lt;code&gt;Try ALGORITHM=COPY&lt;/code&gt;), so adding the check back copies the whole table, and a stored generated column added back is computed for every row. MariaDB is not MySQL here: 11.4.13 accepted the plain rename and rewrote both the &lt;code&gt;CHECK&lt;/code&gt; and the generated column itself.&lt;/p&gt;

&lt;p&gt;SQLite refuses for a different reason. If any view in the database is already broken, even one that never touches &lt;code&gt;orders&lt;/code&gt;, the rename fails with &lt;code&gt;error in view broken: no such column: nope&lt;/code&gt;, because SQLite re-reads the whole schema to rewrite it.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you find what reads a column before renaming it?
&lt;/h2&gt;

&lt;p&gt;Ask the catalog for the objects it tracks, then search the bodies it does not track for the column name.&lt;/p&gt;

&lt;p&gt;On PostgreSQL, &lt;code&gt;pg_depend&lt;/code&gt; knows every object that will follow the rename, and a search of &lt;code&gt;pg_proc&lt;/code&gt; finds the bodies that will not:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="nf"&gt;pg_describe_object&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;classid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objsubid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;dependent&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_depend&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;pg_attribute&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;attrelid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;refobjid&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;attnum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;refobjsubid&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;refobjid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'orders'&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;attname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'total'&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;proname&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_proc&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;pg_namespace&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pronamespace&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nspname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'public'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prosqlbody&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prosrc&lt;/span&gt; &lt;span class="o"&gt;~*&lt;/span&gt; &lt;span class="s1"&gt;'\mtotal\M'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On 18.3 the first query returned seven rows: the &lt;code&gt;CHECK&lt;/code&gt; and the &lt;code&gt;NOT NULL&lt;/code&gt; constraint, the generated column, both indexes, the view and the trigger. On 16.8 it returned six, with no &lt;code&gt;NOT NULL&lt;/code&gt; row. The second returned &lt;code&gt;log_total_change&lt;/code&gt; and &lt;code&gt;customer_spend&lt;/code&gt;, the two bodies that broke.&lt;/p&gt;

&lt;p&gt;MySQL tracks none of this by column, so the search is by text across &lt;code&gt;information_schema&lt;/code&gt;: &lt;code&gt;VIEWS.VIEW_DEFINITION&lt;/code&gt;, &lt;code&gt;TRIGGERS.ACTION_STATEMENT&lt;/code&gt;, &lt;code&gt;ROUTINES.ROUTINE_DEFINITION&lt;/code&gt;, &lt;code&gt;CHECK_CONSTRAINTS.CHECK_CLAUSE&lt;/code&gt; and &lt;code&gt;COLUMNS.GENERATION_EXPRESSION&lt;/code&gt;, each matched with &lt;code&gt;REGEXP '\\btotal\\b'&lt;/code&gt;. On our table that found all five readers. Run it as a user who can read routine bodies: for anyone but the routine's definer, &lt;code&gt;ROUTINE_DEFINITION&lt;/code&gt; is &lt;code&gt;NULL&lt;/code&gt; until the user is granted &lt;code&gt;SHOW_ROUTINE&lt;/code&gt;, so the search returns nothing for that function and reports no error (measured on 8.4.11 and 9.7.4). On SQL Server, the &lt;code&gt;sp_rename&lt;/code&gt; page itself says to "use &lt;code&gt;sys.sql_expression_dependencies&lt;/code&gt; to list dependencies on the object before renaming it":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type_desc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;OBJECT_NAME&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;referencing_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_schema_bound_reference&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;bound&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sql_expression_dependencies&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;object_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;referencing_id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;referenced_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;OBJECT_ID&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'dbo.orders'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;referenced_minor_id&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;COLUMNPROPERTY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;OBJECT_ID&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'dbo.orders'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'total'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'ColumnId'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;bound&lt;/code&gt; column sorts the result into the two outcomes. The rows with 1 are tied to this column and refuse the rename: the &lt;code&gt;CHECK&lt;/code&gt;, and the computed column and the filtered index, which both appear under the table's own name. The rows with 0 (the view, the function and the procedure) are recorded against the table as a whole, so they are every object that reads &lt;code&gt;orders&lt;/code&gt;, and the ones that use &lt;code&gt;total&lt;/code&gt; break after the rename. It did not list our trigger, which reads the &lt;code&gt;deleted&lt;/code&gt; pseudo-table rather than &lt;code&gt;orders&lt;/code&gt; by name, so check a table's triggers by hand. None of these queries sees application code, an ORM model or a report that names the column.&lt;/p&gt;

&lt;h2&gt;
  
  
  Should you rename in place or add a new column?
&lt;/h2&gt;

&lt;p&gt;Rename in place when you can change every reader in the same deploy, and add a new column beside the old one when you cannot.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Rename in place&lt;/th&gt;
&lt;th&gt;New column, then switch&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Statements&lt;/td&gt;
&lt;td&gt;One, plus a drop and re-add for each reader the engine refuses under&lt;/td&gt;
&lt;td&gt;Add, backfill, switch readers, drop&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Readers of the old name&lt;/td&gt;
&lt;td&gt;All fail at the same moment&lt;/td&gt;
&lt;td&gt;Keep working until you move them&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Views, triggers, routines&lt;/td&gt;
&lt;td&gt;Fixed in the same migration, or broken&lt;/td&gt;
&lt;td&gt;Moved one at a time&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Old and new application versions running together&lt;/td&gt;
&lt;td&gt;One of them is wrong&lt;/td&gt;
&lt;td&gt;Both work&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cost on a large table&lt;/td&gt;
&lt;td&gt;None, or a table copy on MySQL when a &lt;code&gt;CHECK&lt;/code&gt; is added back&lt;/td&gt;
&lt;td&gt;A backfill and a second copy of the data for a while&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The in-place rename is right more often than its reputation suggests: a table one service owns, a maintenance window, or a database where the list from the previous section is short. The second route earns its extra steps when several application instances deploy gradually, since old code queries the old name the moment it is gone. Either way the dependent objects go in the same migration as the rename, in an order the database accepts.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Schemity shows what a column rename reaches
&lt;/h2&gt;

&lt;p&gt;Schemity lists the objects that read a renamed column, says which ones the database updates and which ones fail later, and writes the schema migration in the order that database accepts.&lt;/p&gt;

&lt;p&gt;Schemity is database design software that reads your live database, shows the impact of every schema change before it runs, and keeps the diagram as a file in Git. Rename &lt;code&gt;total&lt;/code&gt; on a diagram connected to the PostgreSQL database above and &lt;a href="https://schemity.com/doc/impact-analysis/" rel="noopener noreferrer"&gt;impact analysis&lt;/a&gt;, part of Schemity Pro, reports "4 objects depend on orders.total": the view, the generated column, the trigger and &lt;code&gt;customer_spend&lt;/code&gt;, leaving out the indexes and constraints that simply follow. It says of the view and the generated column that "the database updates it to match", and of the trigger and of &lt;code&gt;customer_spend&lt;/code&gt; that it "fails the next time it runs if it uses what changed". The planned migration is the single &lt;code&gt;RENAME COLUMN&lt;/code&gt;. On MySQL and SQL Server (a Schemity Pro connection) the same finding lists the view, the trigger and the routines as objects that may fail, though there it names every one that reads the table, because those databases do not record which column a view or routine uses. A MySQL routine whose body the connecting user cannot read is listed as one that may use the table, not left out.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4exfwrocx73vfzhzptfo.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4exfwrocx73vfzhzptfo.webp" alt="Schemity's Findings panel on a PostgreSQL 18.3 diagram after renaming orders.total to total_amount: the planned migration is one ALTER TABLE orders RENAME COLUMN statement, and the impact finding reads 4 objects depend on orders.total, with the view order_totals and the generated column total_with_tax updated by the database, and the trigger orders_total_audit and the function customer_spend failing the next time they run" width="799" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The migration is where the engines' refusals are handled. For the same rename on MySQL, Schemity plans five statements: &lt;code&gt;DROP CHECK&lt;/code&gt;, &lt;code&gt;DROP COLUMN&lt;/code&gt; for the generated column, &lt;code&gt;CHANGE COLUMN&lt;/code&gt; for the rename, then the generated column and the &lt;code&gt;CHECK&lt;/code&gt; added back reading &lt;code&gt;total_amount&lt;/code&gt;. It writes them as separate statements rather than the single &lt;code&gt;ALTER TABLE&lt;/code&gt; above, so on MySQL a failure partway leaves the earlier ones applied. On SQL Server, which needs Schemity Pro, it plans the same order around &lt;code&gt;sp_rename&lt;/code&gt;, and a filtered index that reads the column is dropped before the rename and created after it. On the databases Schemity Free connects to, the migration dialog shows that SQL before applying it, and renaming a foreign key column carries the new name into the key on every database.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr305ke5ly9gly56bjjjs.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr305ke5ly9gly56bjjjs.webp" alt="Schemity's Findings panel on a MySQL 9.7.4 diagram after renaming orders.total to total_amount: the planned migration is five ALTER TABLE statements, DROP CHECK orders_total_check, DROP COLUMN total_with_tax, CHANGE COLUMN total total_amount, ADD COLUMN total_with_tax reading total_amount and ADD CONSTRAINT orders_total_check, and the impact finding lists the view order_totals, the trigger orders_total_audit and the function customer_spend as failing the next time they run if they use what changed" width="800" height="707"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Running this post's table through the app is how the &lt;code&gt;CHECK&lt;/code&gt; and the filtered index got into that plan. Until then the app rebuilt the generated column but left the &lt;code&gt;CHECK&lt;/code&gt; in place, so MySQL stopped the migration at error 3959 with the generated column already dropped, and SQL Server stopped it at Msg 15336. Both are fixed for the next release, and we applied the new plan to both databases to confirm it. One case is still yours to handle: Schemity does not read MySQL functional indexes into the diagram, so a column one of them uses needs that index dropped and recreated by hand. That is what it takes to see the impact of every change: the same one-line rename is a different migration on each database.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to check before renaming a column
&lt;/h2&gt;

&lt;p&gt;Check the readers the database will not rewrite for you, which is a different list on each engine.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On PostgreSQL the rename will succeed; search trigger and function bodies, which it does not rewrite.&lt;/li&gt;
&lt;li&gt;On MySQL and SQL Server, drop each &lt;code&gt;CHECK&lt;/code&gt;, generated column and expression index that reads the column, rename, and add them back, in one &lt;code&gt;ALTER TABLE&lt;/code&gt; on MySQL, counting the table copy that costs.&lt;/li&gt;
&lt;li&gt;On MySQL, MariaDB and SQL Server, recreate every view that reads the column in the same migration.&lt;/li&gt;
&lt;li&gt;On SQLite, fix any view that is already broken first, then look for readers of the views, whose own columns are renamed too.&lt;/li&gt;
&lt;li&gt;Rename the constraints and indexes whose names still carry the old column name, or accept that they no longer match.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Dropping a column instead reaches a different set of objects, measured the same way in &lt;a href="https://schemity.com/blog/postgres-drop-column-what-it-drops/" rel="noopener noreferrer"&gt;what PostgreSQL &lt;code&gt;DROP COLUMN&lt;/code&gt; refuses and what it drops&lt;/a&gt;. When the rename arrives inside a generated migration, often as a drop and an add that loses the data, &lt;a href="https://schemity.com/blog/should-ai-agents-write-database-migrations/" rel="noopener noreferrer"&gt;reviewing what an AI agent's migration does&lt;/a&gt; covers how to catch it, and changing the column's type rather than its name is in &lt;a href="https://schemity.com/blog/postgres-alter-column-type-used-by-view/" rel="noopener noreferrer"&gt;altering a column a view depends on&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>mysql</category>
      <category>sqlserver</category>
      <category>database</category>
    </item>
    <item>
      <title>Natural Key vs Surrogate Key: Which One Should a Foreign Key Reference?</title>
      <dc:creator>Son Tran</dc:creator>
      <pubDate>Fri, 27 Nov 2026 01:12:24 +0000</pubDate>
      <link>https://dev.to/tbson87/natural-key-vs-surrogate-key-which-one-should-a-foreign-key-reference-2l0i</link>
      <guid>https://dev.to/tbson87/natural-key-vs-surrogate-key-which-one-should-a-foreign-key-reference-2l0i</guid>
      <description>&lt;p&gt;&lt;em&gt;Disclosure: I build &lt;a href="https://schemity.com" rel="noopener noreferrer"&gt;Schemity&lt;/a&gt;, a desktop ERD tool - this post is from our blog and uses it for the examples.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; Point foreign keys at a surrogate &lt;code&gt;id&lt;/code&gt; whenever the natural value can change, which covers SKUs, emails, usernames and product codes, and keep the natural value &lt;code&gt;UNIQUE&lt;/code&gt; on its own table. A natural-key foreign key turns every rename into a rewrite of the child rows, including history that should have kept the old value. A natural key is a reasonable target only for a short, stable standard code, such as an ISO country or currency code. On MySQL a string foreign key must also share the referenced column's collation exactly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A foreign key should reference a surrogate key, a generated &lt;code&gt;id&lt;/code&gt;, unless the natural value is a short code that never changes, because a foreign key copies the referenced value into every child row. Reference &lt;code&gt;products.sku&lt;/code&gt; and each order line holds the SKU; rename the SKU and every order line has to change with it, or the database refuses the rename. Reference &lt;code&gt;products.id&lt;/code&gt; and the SKU is free to change, since only the &lt;code&gt;products&lt;/code&gt; row holds it.&lt;/p&gt;

&lt;p&gt;The question is usually asked as natural key vs surrogate key, but the decision that costs something is the one about foreign keys. Everything below ran on PostgreSQL 18.3 and MySQL 8.4.11 in throwaway containers, with 10,000 products and 1,000,000 order lines in each child table. PostgreSQL does not index a foreign key column by itself, so the indexes are created here; MySQL creates them on its own.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="no"&gt;IDENTITY&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;sku&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;order_items_by_sku&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="no"&gt;IDENTITY&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;sku&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sku&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="k"&gt;CASCADE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;quantity&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;order_items_by_id&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="no"&gt;IDENTITY&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;product_id&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;quantity&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;order_items_by_sku&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sku&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;order_items_by_id&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What is the difference between a natural key and a surrogate key?
&lt;/h2&gt;

&lt;p&gt;A natural key is a value the business already uses to identify a row, such as a SKU, an email or an ISO country code; a surrogate key is a value the database makes up for the purpose, such as an identity or a UUID.&lt;/p&gt;

&lt;p&gt;Both identify the row, and both can be enforced unique. The difference is who controls the value. Nobody outside the database cares what &lt;code&gt;products.id&lt;/code&gt; is, so it never needs to change. The SKU belongs to merchandising, the email belongs to the user, and the product code belongs to a supplier. Any of them can decide it is wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does a natural-key foreign key cost when the value changes?
&lt;/h2&gt;

&lt;p&gt;Every child row that holds the old value has to be rewritten, history included, and without &lt;code&gt;ON UPDATE CASCADE&lt;/code&gt; the change is refused.&lt;/p&gt;

&lt;p&gt;With a plain foreign key, renaming a SKU that one row uses fails:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;lines_plain&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sku&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sku&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;lines_plain&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'ACME-MUG-000002'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;sku&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'ACME-CUP-000002'&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;sku&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'ACME-MUG-000002'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ERROR:  update or delete on table "products" violates foreign key constraint "lines_plain_sku_fkey" on table "lines_plain"
DETAIL:  Key (sku)=(ACME-MUG-000002) is still referenced from table "lines_plain".
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Updating the child first fails the same way, the other way round. The ways around it are inserting the product again under its new SKU, repointing the children and deleting the old row, or declaring the key &lt;code&gt;DEFERRABLE&lt;/code&gt; so both updates can land in one transaction.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;ON UPDATE CASCADE&lt;/code&gt;, PostgreSQL rewrites the children for you. Renaming one SKU with 100 order lines took 3 ms. Then the company rebranded: &lt;code&gt;UPDATE products SET sku = replace(sku, 'ACME-', 'NOVA-')&lt;/code&gt; renamed all 10,000 SKUs, and the cascade rewrote all 1,000,000 order lines. It took 22 seconds and doubled &lt;code&gt;order_items_by_sku&lt;/code&gt; from 57 MB to 115 MB until a vacuum, because PostgreSQL writes a new version of every row it updates. On &lt;code&gt;order_items_by_id&lt;/code&gt;, the same rebrand touches no order line at all.&lt;/p&gt;

&lt;p&gt;The cost that matters more is what the cascade did to the history. An order line from last year now says &lt;code&gt;NOVA-MUG-000001&lt;/code&gt;, a SKU that did not exist when the customer bought the mug, and every invoice or export rebuilt from it says the same. Schemas that need the SKU as sold keep a copy of it on the order line, with no foreign key, next to a &lt;code&gt;product_id&lt;/code&gt; that points at the product as it is now.&lt;/p&gt;

&lt;h2&gt;
  
  
  Are text foreign keys bigger or slower than integer ones?
&lt;/h2&gt;

&lt;p&gt;Less than people expect: on PostgreSQL 18.3 the index on 1,000,000 text foreign keys was 7.3 MB against 6.7 MB for &lt;code&gt;bigint&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A btree index stores each repeated value once (deduplication, since PostgreSQL 13), so 1,000,000 order lines over 10,000 SKUs cost little more than the same lines over 10,000 ids. The tables differed by 7 MB, 57 MB against 50 MB, for SKUs like &lt;code&gt;ACME-MUG-000001&lt;/code&gt;. Longer values, such as email addresses or composite natural keys, widen that gap. But storage is rarely the reason to choose. The reason is what happens when the value changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does MySQL add to a string foreign key?
&lt;/h2&gt;

&lt;p&gt;MySQL compares a string foreign key under its collation, so the two columns must share one exactly, and a case-insensitive collation lets values match that your code thinks are different.&lt;/p&gt;

&lt;p&gt;Each row below is a child table with &lt;code&gt;sku&lt;/code&gt; referencing &lt;code&gt;products.sku VARCHAR(40)&lt;/code&gt;, created under MySQL 8.4's default collation, &lt;code&gt;utf8mb4_0900_ai_ci&lt;/code&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Child column, or the row inserted&lt;/th&gt;
&lt;th&gt;MySQL 8.4.11&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;VARCHAR(40) COLLATE utf8mb4_bin&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Refused, error 3780, &lt;code&gt;... are incompatible&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;VARCHAR(40) CHARACTER SET latin1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Refused, error 3780&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;VARCHAR(20)&lt;/code&gt;, same collation&lt;/td&gt;
&lt;td&gt;Accepted, but a rename to a SKU over 20 characters is refused, error 1451&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A row holding &lt;code&gt;acme-mug-01&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Accepted: it references &lt;code&gt;ACME-MUG-01&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A row holding &lt;code&gt;ACME-MUG-01&lt;/code&gt; with a trailing space&lt;/td&gt;
&lt;td&gt;Refused, error 1452&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The fourth row is the one that bites. MySQL's default collation ignores case, so the foreign key is satisfied by &lt;code&gt;acme-mug-01&lt;/code&gt;. A comparison that respects case, such as &lt;code&gt;li.sku COLLATE utf8mb4_bin = p.sku&lt;/code&gt; or the same check in application code, then finds no product for that order line. The mismatch lasts until the next cascade: renaming the parent rewrote the child to the parent's exact value. A surrogate key has no collation, so none of this can happen to &lt;code&gt;product_id&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Natural key vs surrogate key: which should a foreign key reference?
&lt;/h2&gt;

&lt;p&gt;Reference the surrogate key, and keep the natural value unique on its own table, unless the natural value is a short standard code that does not change.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Natural value&lt;/th&gt;
&lt;th&gt;Changes?&lt;/th&gt;
&lt;th&gt;Foreign key should reference&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;SKU, product code&lt;/td&gt;
&lt;td&gt;Rebrands, supplier changes, typos&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;products.id&lt;/code&gt;, with the SKU as sold copied onto the order line&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Email, username&lt;/td&gt;
&lt;td&gt;Whenever the user wants&lt;/td&gt;
&lt;td&gt;&lt;code&gt;users.id&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Order number shown to customers&lt;/td&gt;
&lt;td&gt;Rarely, once issued&lt;/td&gt;
&lt;td&gt;Either; &lt;code&gt;orders.id&lt;/code&gt; keeps joins uniform&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ISO 3166 country code, ISO 4217 currency&lt;/td&gt;
&lt;td&gt;Rarely, by an outside standard&lt;/td&gt;
&lt;td&gt;The code itself, &lt;code&gt;CHAR(2)&lt;/code&gt; or &lt;code&gt;CHAR(3)&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A composite natural key, such as &lt;code&gt;(company_id, invoice_number)&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;When numbering rules change&lt;/td&gt;
&lt;td&gt;A surrogate &lt;code&gt;id&lt;/code&gt;, with the pair &lt;code&gt;UNIQUE&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The country code row is the honest exception. &lt;code&gt;addresses.country_code = 'VN'&lt;/code&gt; is readable without a join, the value is short, and when the standard does change, it withdraws a code or splits one country's code into several, which is a planned data migration that no cascade could express anyway. The same reasoning fails for a SKU, because a SKU changes on a business decision, not a standards body's.&lt;/p&gt;

&lt;p&gt;Choosing the surrogate does not mean dropping the natural key. &lt;code&gt;sku text NOT NULL UNIQUE&lt;/code&gt; still stops two products from sharing a SKU; it is just not what other tables point at.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you draw a natural-key foreign key without the MySQL mismatch?
&lt;/h2&gt;

&lt;p&gt;Draw the relation instead of typing the column, so the new column copies the key's type, character set and collation.&lt;/p&gt;

&lt;p&gt;Schemity is database design software that reads your live database, shows the impact of every schema change before it runs, and keeps the diagram as a file in Git. When you design the next one, a &lt;a href="https://schemity.com/doc/relationships/" rel="noopener noreferrer"&gt;relationship&lt;/a&gt; drawn from a parent creates the child's foreign key column from the parent's primary key. On a MySQL diagram where &lt;code&gt;countries&lt;/code&gt; is keyed by &lt;code&gt;code CHAR(2) CHARACTER SET ascii COLLATE ascii_bin&lt;/code&gt;, the relation to an empty &lt;code&gt;warehouses&lt;/code&gt; table planned:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="sb"&gt;`warehouses`&lt;/span&gt; &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="sb"&gt;`country_code`&lt;/span&gt; &lt;span class="kt"&gt;CHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;CHARACTER&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;ascii&lt;/span&gt; &lt;span class="k"&gt;COLLATE&lt;/span&gt; &lt;span class="n"&gt;ascii_bin&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="sb"&gt;`warehouses`&lt;/span&gt; &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="sb"&gt;`warehouses_country_code_fkey`&lt;/span&gt; &lt;span class="k"&gt;FOREIGN&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`country_code`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="sb"&gt;`countries`&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`code`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;DELETE&lt;/span&gt; &lt;span class="no"&gt;NO&lt;/span&gt; &lt;span class="no"&gt;ACTION&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="no"&gt;NO&lt;/span&gt; &lt;span class="no"&gt;ACTION&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjjajxrprsk9vk0tn5mv9.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjjajxrprsk9vk0tn5mv9.webp" alt="Schemity's change preview on a MySQL diagram after drawing a relation from countries to warehouses: the planned migration adds warehouses.country_code CHAR(2) CHARACTER SET ascii COLLATE ascii_bin NOT NULL and the foreign key warehouses_country_code_fkey to countries (code), with two planned changes, no lint findings and the impact line Rewrites warehouses, 16 KB on disk" width="800" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The column is named after the key it holds, &lt;code&gt;country_code&lt;/code&gt;, not &lt;code&gt;country_id&lt;/code&gt;, and it carries the key's character set and collation, the pair error 3780 checks. The same holds for a &lt;code&gt;products&lt;/code&gt; table keyed by &lt;code&gt;sku&lt;/code&gt;, which gives &lt;code&gt;order_items.product_sku&lt;/code&gt;. On a table that already has rows, add the column as nullable, fill it, then set &lt;code&gt;NOT NULL&lt;/code&gt; and add the key: MySQL fills a new &lt;code&gt;NOT NULL&lt;/code&gt; &lt;code&gt;CHAR&lt;/code&gt; column with &lt;code&gt;''&lt;/code&gt;, which no country has, so the foreign key would be refused with error 1452. Before you apply it, &lt;a href="https://schemity.com/doc/impact-analysis/" rel="noopener noreferrer"&gt;impact analysis&lt;/a&gt; (a Schemity Pro feature) reports that adding the foreign key rewrites &lt;code&gt;warehouses&lt;/code&gt;, which MySQL does while foreign key checks are on.&lt;/p&gt;

&lt;p&gt;Writing this post found two gaps, fixed for the next release. When an AI agent added the same relation over MCP, Schemity named the column &lt;code&gt;country_id&lt;/code&gt; whatever the parent's key was called, and ignored the diagram's naming convention; it now names it exactly as a drawn relation does. And the finding for a rewrite of a whole table, such as this one, read as an unfinished sentence.&lt;/p&gt;

&lt;h2&gt;
  
  
  Related reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://schemity.com/blog/soft-delete-vs-archive-table/" rel="noopener noreferrer"&gt;Soft delete vs archive table&lt;/a&gt; shows a natural-key foreign key turning a soft-delete migration into a dropped constraint.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://schemity.com/blog/postgres-uuid-vs-bigint-primary-key/" rel="noopener noreferrer"&gt;UUID or bigint primary key in PostgreSQL&lt;/a&gt; is the next decision once you have chosen a surrogate.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://schemity.com/blog/mysql-convert-utf8mb3-to-utf8mb4/" rel="noopener noreferrer"&gt;Converting MySQL tables from utf8mb3 to utf8mb4&lt;/a&gt; hits the same error 3780 from the other direction, when a conversion would leave the two columns of a key apart.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>database</category>
      <category>postgres</category>
      <category>mysql</category>
      <category>sql</category>
    </item>
    <item>
      <title>PostgreSQL Schema on CockroachDB: What It Refuses, What It Changes, and How to Check Yours</title>
      <dc:creator>Son Tran</dc:creator>
      <pubDate>Fri, 27 Nov 2026 01:11:34 +0000</pubDate>
      <link>https://dev.to/tbson87/postgresql-schema-on-cockroachdb-what-it-refuses-what-it-changes-and-how-to-check-yours-33p0</link>
      <guid>https://dev.to/tbson87/postgresql-schema-on-cockroachdb-what-it-refuses-what-it-changes-and-how-to-check-yours-33p0</guid>
      <description>&lt;p&gt;&lt;em&gt;Disclosure: I build &lt;a href="https://schemity.com" rel="noopener noreferrer"&gt;Schemity&lt;/a&gt;, a desktop ERD tool - this post is from our blog and uses it for the examples.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; A PostgreSQL schema rarely runs on CockroachDB unchanged. CockroachDB v26.3 has no range types, &lt;code&gt;money&lt;/code&gt;, &lt;code&gt;xml&lt;/code&gt; or &lt;code&gt;point&lt;/code&gt;, no C collation, no &lt;code&gt;DEFERRABLE&lt;/code&gt; foreign keys, and no BRIN, SP-GiST or &lt;code&gt;EXCLUDE&lt;/code&gt;. It also changes what it accepts: &lt;code&gt;integer&lt;/code&gt; becomes 64-bit, and a table created with &lt;code&gt;serial&lt;/code&gt; gets 19-digit ids, past what JavaScript can hold exactly. Read the schema for these before moving any data.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;CockroachDB accepts most of a PostgreSQL schema, refuses a specific list of types, indexes and constraints outright, and quietly changes two things it accepts: the size of &lt;code&gt;integer&lt;/code&gt; and what &lt;code&gt;serial&lt;/code&gt; means. The refusals are the easy part, because the statement fails and tells you. The quiet changes are the ones that reach production, such as an order id that a JavaScript client rounds to a different number.&lt;/p&gt;

&lt;p&gt;Everything below ran in throwaway containers. The schema was created on PostgreSQL 18.3, dumped with &lt;code&gt;pg_dump --schema-only&lt;/code&gt;, and each statement of the dump was run on CockroachDB v26.3.4. It is an ordinary schema: users with a &lt;code&gt;serial&lt;/code&gt; key and a &lt;code&gt;COLLATE "C"&lt;/code&gt; name, orders with an identity key and a deferred foreign key, an events table with &lt;code&gt;jsonb&lt;/code&gt;, &lt;code&gt;tsvector&lt;/code&gt;, a &lt;code&gt;tstzrange&lt;/code&gt; and a &lt;code&gt;point&lt;/code&gt;, and a BRIN index.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which PostgreSQL features does CockroachDB refuse?
&lt;/h2&gt;

&lt;p&gt;CockroachDB v26.3 refuses range types, a handful of PostgreSQL-only types, &lt;code&gt;COLLATE "C"&lt;/code&gt;, deferrable foreign keys, and every index method except btree, GIN, its vector indexes and GiST on a spatial column.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;In the PostgreSQL schema&lt;/th&gt;
&lt;th&gt;CockroachDB v26.3.4 says&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;tstzrange&lt;/code&gt;, &lt;code&gt;daterange&lt;/code&gt;, &lt;code&gt;int4range&lt;/code&gt;, &lt;code&gt;numrange&lt;/code&gt;, multiranges&lt;/td&gt;
&lt;td&gt;&lt;code&gt;type "tstzrange" does not exist&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;money&lt;/code&gt;, &lt;code&gt;xml&lt;/code&gt;, &lt;code&gt;point&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;unimplemented: this syntax&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;name text COLLATE "C"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;invalid locale C: language: tag is not well-formed&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;ADD CONSTRAINT ... DEFERRABLE INITIALLY DEFERRED&lt;/code&gt;, as &lt;code&gt;pg_dump&lt;/code&gt; writes it&lt;/td&gt;
&lt;td&gt;&lt;code&gt;at or near "deferred": syntax error: unimplemented&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;CREATE INDEX ... USING brin&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;unimplemented: this syntax&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;USING hash (col)&lt;/code&gt;, &lt;code&gt;USING spgist&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;unimplemented: this syntax&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;USING gist&lt;/code&gt; on a text column&lt;/td&gt;
&lt;td&gt;&lt;code&gt;no default operator class for access method "gin"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;EXCLUDE USING gist (room WITH =, during WITH &amp;amp;&amp;amp;)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;unimplemented: this syntax&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;CREATE INDEX ... NULLS NOT DISTINCT&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;syntax error&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;One failed &lt;code&gt;CREATE TABLE&lt;/code&gt; takes its whole table with it, so every index and foreign key on it fails next with &lt;code&gt;relation "events" does not exist&lt;/code&gt;. Read the first error per table, not the last.&lt;/p&gt;

&lt;p&gt;Some of these have a direct replacement. For &lt;code&gt;COLLATE "C"&lt;/code&gt;, drop the collation: CockroachDB's plain &lt;code&gt;STRING&lt;/code&gt; already sorts by bytes, &lt;code&gt;A&lt;/code&gt;, &lt;code&gt;B&lt;/code&gt;, &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt;, as C does, while a locale such as &lt;code&gt;en-US&lt;/code&gt; sorts &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;A&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt;, &lt;code&gt;B&lt;/code&gt; and should be chosen only for that order. A deferred foreign key becomes a plain one, which means each statement must insert the parent row before its children. Others take a design change. A &lt;code&gt;tstzrange&lt;/code&gt; column and the &lt;code&gt;EXCLUDE&lt;/code&gt; constraint that stopped bookings from overlapping become two timestamp columns and a check your application runs. Overlapping periods are then no longer something the database prevents.&lt;/p&gt;

&lt;p&gt;Once &lt;code&gt;events&lt;/code&gt; lost its &lt;code&gt;tstzrange&lt;/code&gt; and &lt;code&gt;point&lt;/code&gt;, CockroachDB took the rest of it: &lt;code&gt;jsonb&lt;/code&gt; with a GIN index and &lt;code&gt;tsvector&lt;/code&gt; with a GIN index, along with &lt;code&gt;timestamptz&lt;/code&gt; defaults, identity columns and &lt;code&gt;UNIQUE&lt;/code&gt; constraints elsewhere.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does CockroachDB change without an error?
&lt;/h2&gt;

&lt;p&gt;Two things: &lt;code&gt;integer&lt;/code&gt; columns become 64-bit, and &lt;code&gt;serial&lt;/code&gt; stops being a sequence.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SHOW default_int_size&lt;/code&gt; on a fresh CockroachDB cluster returns &lt;code&gt;8&lt;/code&gt;, so the dump's &lt;code&gt;id integer&lt;/code&gt; and &lt;code&gt;user_id integer&lt;/code&gt; are both created as &lt;code&gt;INT8&lt;/code&gt;. Nothing fails, and the two still match each other, but they no longer match the &lt;code&gt;int4&lt;/code&gt; your ORM, your other services and your PostgreSQL replica expect. &lt;code&gt;SET default_int_size = 4;&lt;/code&gt; keeps &lt;code&gt;integer&lt;/code&gt; as &lt;code&gt;INT4&lt;/code&gt;, with one catch measured here: sent on its own before the &lt;code&gt;CREATE TABLE&lt;/code&gt;, it worked; sent in the same batch as the &lt;code&gt;CREATE TABLE&lt;/code&gt;, the column still came out &lt;code&gt;bigint&lt;/code&gt;. A migration tool that sends a whole file as one query gets 64-bit integers anyway.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;serial&lt;/code&gt; changes only where the DDL says &lt;code&gt;serial&lt;/code&gt;. A &lt;code&gt;pg_dump&lt;/code&gt; never does: it writes &lt;code&gt;id integer&lt;/code&gt;, a &lt;code&gt;CREATE SEQUENCE&lt;/code&gt; and a &lt;code&gt;nextval&lt;/code&gt; default, and on CockroachDB those numbered the first rows 1 and 2. Hand-written DDL and migrations that say &lt;code&gt;serial&lt;/code&gt; or &lt;code&gt;bigserial&lt;/code&gt; are different. &lt;code&gt;SHOW serial_normalization&lt;/code&gt; returns &lt;code&gt;rowid&lt;/code&gt;, and the &lt;a href="https://docs.cockroachlabs.com/docs/v26.3/serial" rel="noopener noreferrer"&gt;CockroachDB docs&lt;/a&gt; state that in that mode "&lt;code&gt;SERIAL&lt;/code&gt; implies &lt;code&gt;DEFAULT unique_rowid()&lt;/code&gt;", always as &lt;code&gt;INT8&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;serial&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;--  1230620201065447425&lt;/span&gt;
&lt;span class="c1"&gt;--  1230620201065545729&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Those ids are larger than 9,007,199,254,740,991, the largest integer a JavaScript number holds exactly, so a browser or Node client that parses them as numbers rounds them. An API that returns &lt;code&gt;id: 1230620201065447425&lt;/code&gt; as JSON hands the frontend a different id than the one in the database. With &lt;code&gt;SET serial_normalization = 'sql_sequence'&lt;/code&gt; first, a &lt;code&gt;serial&lt;/code&gt; table numbered its rows 1 and 2 from a real sequence.&lt;/p&gt;

&lt;h2&gt;
  
  
  Serial, identity or UUID: which primary key should you use on CockroachDB?
&lt;/h2&gt;

&lt;p&gt;Use a &lt;code&gt;UUID&lt;/code&gt; key with &lt;code&gt;gen_random_uuid()&lt;/code&gt; for new tables, and keep a sequence only where existing ids must survive the move.&lt;/p&gt;

&lt;p&gt;CockroachDB printed the same &lt;code&gt;NOTICE&lt;/code&gt; for the &lt;code&gt;serial&lt;/code&gt; table, the identity table and the sequence-backed one: "using sequential values in a primary key does not perform as well as using random UUIDs." Its docs give the reason: a &lt;code&gt;UUID&lt;/code&gt; key "more uniformly scatters" new rows "across all of a table's underlying key-value ranges", while sequential keys send every insert to the same range.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Key&lt;/th&gt;
&lt;th&gt;On CockroachDB&lt;/th&gt;
&lt;th&gt;Use it when&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;serial&lt;/code&gt;, default settings&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;INT8 DEFAULT unique_rowid()&lt;/code&gt;, 19-digit ids&lt;/td&gt;
&lt;td&gt;Never by accident; only if every client reads ids as strings&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A sequence: the dump's &lt;code&gt;nextval&lt;/code&gt;, or &lt;code&gt;serial&lt;/code&gt; with &lt;code&gt;sql_sequence&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Ids from 1, with the &lt;code&gt;NOTICE&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;You copy existing rows and their ids must not change&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GENERATED ALWAYS AS IDENTITY&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Sequential, with the &lt;code&gt;NOTICE&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;The same as a sequence, written the standard way&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;UUID DEFAULT gen_random_uuid()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Random, spread over ranges&lt;/td&gt;
&lt;td&gt;New tables, and tables nothing outside refers to by number&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The middle rows are the migration case. Order 10482 that customers see in emails has to stay 10482, so those tables keep a sequence, and the hotspot it causes is a cost you accept for compatibility. PostgreSQL itself has no ranges to spread writes over, so the same question there is decided on other grounds.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you find these in your own schema before migrating?
&lt;/h2&gt;

&lt;p&gt;Dump the schema and read it against CockroachDB's rules, which takes one command and no CockroachDB cluster.&lt;/p&gt;

&lt;p&gt;Schemity is database design software that reads your live database, shows the impact of every schema change before it runs, and keeps the diagram as a file in Git. To understand your schema before it moves, its &lt;a href="https://schemity.com/doc/schema-lint/" rel="noopener noreferrer"&gt;schema lint&lt;/a&gt; has a set of CockroachDB rules, and the free &lt;a href="https://schemity.com/doc/migration-check-ci/" rel="noopener noreferrer"&gt;Migration Check&lt;/a&gt; command line tool runs them on a dump with no database at all:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pg_dump --schema-only mydb &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; schema.sql
schemity lint --dialect cockroachdb --schema schema.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the dump of the schema above it listed the refusals with the column or index each one is on, for example &lt;code&gt;events.at CockroachDB has no TSTZRANGE type, so creating this column fails.&lt;/code&gt;, &lt;code&gt;users.name CockroachDB refuses collation "C"&lt;/code&gt; and &lt;code&gt;orders_user_id_fkey is DEFERRABLE INITIALLY DEFERRED&lt;/code&gt;. It also reported the sequential primary keys on &lt;code&gt;users&lt;/code&gt;, &lt;code&gt;orders&lt;/code&gt; and &lt;code&gt;events&lt;/code&gt; with CockroachDB's own recommendation, and &lt;code&gt;orders_placed_at_idx&lt;/code&gt;, a btree index that leads with a &lt;code&gt;DEFAULT now()&lt;/code&gt; timestamp. That index sends every new order to the same range, and CockroachDB's answer to it is a hash-sharded index, &lt;code&gt;CREATE INDEX ON orders (placed_at) USING HASH&lt;/code&gt;, with &lt;code&gt;USING HASH&lt;/code&gt; after the column list.&lt;/p&gt;

&lt;p&gt;In the desktop app, CockroachDB is its own database type (a Schemity Pro feature). The type and index lists offer only what CockroachDB takes, the same lint runs in the lint drawer, and a migration it applies sends &lt;code&gt;SET default_int_size = 4&lt;/code&gt; as its own statement first, so an &lt;code&gt;integer&lt;/code&gt; you designed stays 32-bit.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqfwld0tekbp3zk5ejwyy.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqfwld0tekbp3zk5ejwyy.webp" alt="Schemity's lint drawer on a CockroachDB diagram after importing the PostgreSQL schema: 14 findings, 8 that fail at runtime, including no TSTZRANGE type for events.at, no POINT for events.location, no MONEY and XML on money_t, and the GiST and BRIN indexes CockroachDB cannot create" width="800" height="831"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Writing this post found gaps in those rules, fixed for the next release: the range types were not on the refused list, a GiST index on a non-spatial column or an SP-GiST index was not reported, and the collation and hash-sharding advice now reads as above. One refusal is still not linted: an &lt;code&gt;EXCLUDE&lt;/code&gt; constraint, which the diagram shows but the CockroachDB rules do not read yet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Related reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://schemity.com/blog/transactional-ddl-postgres-mysql-cockroachdb/" rel="noopener noreferrer"&gt;Transactional DDL in PostgreSQL, MySQL and CockroachDB&lt;/a&gt; covers the other CockroachDB surprise: each schema change in a migration commits on its own.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://schemity.com/blog/postgres-prevent-overlapping-date-ranges-exclude-vs-without-overlaps/" rel="noopener noreferrer"&gt;Preventing overlapping date ranges with EXCLUDE or WITHOUT OVERLAPS&lt;/a&gt; shows what a range column and its &lt;code&gt;EXCLUDE&lt;/code&gt; constraint do on PostgreSQL, which is what a CockroachDB move gives up.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://schemity.com/blog/postgres-uuid-vs-bigint-primary-key/" rel="noopener noreferrer"&gt;UUID or bigint primary key in PostgreSQL&lt;/a&gt; weighs the same key choice on a database with no ranges to spread writes over.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>cockroachdb</category>
      <category>postgres</category>
      <category>database</category>
      <category>sql</category>
    </item>
  </channel>
</rss>
