# PostgreSQL Rewrites Your CHECK Constraints: Which Schema Differences Are Real Drift

> PostgreSQL keeps a parsed `CHECK`, not your text, so `status IN ('pending', 'paid')` comes back as `= ANY (ARRAY[...])` and `BETWEEN` as two comparisons. Those are the same rule. A `DEFAULT 'now'` frozen to the moment the table was created, or a value whose case changed, is not. Compare database to database, never your migration's text to a dump.

Source: https://schemity.com/blog/postgres-check-constraint-rewritten-schema-drift/

A schema diff that compares your migration's text with what the database prints will report `CHECK` constraints and defaults as changed even when nothing changed, because PostgreSQL and MySQL store a parsed rule and print it back in their own words. Most of those differences are spelling. A few are real, and they are the ones a text diff makes easiest to miss.

Here is an ordinary table, written the way a migration writes it:

```sql
CREATE TABLE orders (
  id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  status varchar(20) NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'paid', 'shipped')),
  quantity int NOT NULL CHECK (quantity BETWEEN 1 AND 100),
  channel text NOT NULL DEFAULT 'web' CHECK (channel NOT IN ('test', 'internal')),
  is_gift bool NOT NULL DEFAULT FALSE,
  created_at timestamptz NOT NULL DEFAULT now(),
  imported_at timestamptz NOT NULL DEFAULT 'now'
);
```

Everything below was run on PostgreSQL 18.3 and MySQL 8.4.11 in throwaway containers. Ask PostgreSQL for this table back, with `\d orders`, `pg_dump --schema-only` or `pg_get_constraintdef`, and not one of the three `CHECK` constraints reads the way it was written.

## Why does postgres change my check constraint?

Because PostgreSQL keeps the parsed expression, not your text, and rebuilds the text every time you ask for it.

A `CHECK` is stored as an expression tree in `pg_constraint`. When `\d`, `pg_dump` or an ORM's introspection asks for it, `pg_get_constraintdef` turns the tree back into SQL, and the [PostgreSQL manual](https://www.postgresql.org/docs/current/functions-info.html) says what that text is: "a decompiled reconstruction, not the original text of the command." The reconstruction expands shorthand, adds the casts the parser inserted, and brackets every sub-expression:

| Written in the migration | Printed by `pg_get_constraintdef`, inside `CHECK (...)` |
|---|---|
| `status IN ('pending', 'paid', 'shipped')` on `varchar(20)` | `((status)::text = ANY ((ARRAY['pending'::character varying, 'paid'::character varying, 'shipped'::character varying])::text[]))` |
| `quantity BETWEEN 1 AND 100` | `((quantity >= 1) AND (quantity <= 100))` |
| `channel NOT IN ('test', 'internal')` | `(channel <> ALL (ARRAY['test'::text, 'internal'::text]))` |

The manual defines each rewrite as equivalent. `IN` is "a shorthand notation for" `expression = value1 OR expression = value2 OR ...`, `NOT IN` is the same with `<>` and `AND` ([row and array comparisons](https://www.postgresql.org/docs/current/functions-comparisons.html)), and `a BETWEEN x AND y` "is equivalent to `a >= x AND a <= y`" ([comparison predicates](https://www.postgresql.org/docs/current/functions-comparison.html)). Columns and defaults get the same treatment: `int` prints as `integer`, `bool` as `boolean`, `timestamptz` as `timestamp with time zone`, and `DEFAULT 'pending'` as `'pending'::character varying`.

So the first question about any reported difference is not "what changed" but "did the rule change, or only its spelling".

## Which schema differences are only spelling?

Every rewrite in the table above is spelling: the database accepts and rejects exactly the same rows as the rule you wrote.

MySQL rewrites less, but it rewrites. Here is a MySQL version of the table, written with `int(11)` and `boolean` the way older tutorials and generators still write them, from a `utf8mb4` connection:

```sql
CREATE TABLE orders (
  id bigint AUTO_INCREMENT PRIMARY KEY,
  status varchar(20) NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'paid', 'shipped')),
  quantity int(11) NOT NULL CHECK (quantity BETWEEN 1 AND 100),
  is_gift boolean NOT NULL DEFAULT FALSE,
  created_at timestamp NOT NULL DEFAULT now()
);
```

MySQL 8.4.11 prints it back with `SHOW CREATE TABLE` like this:

| Written | MySQL 8.4 prints |
|---|---|
| `quantity int(11) NOT NULL` | `` `quantity` int NOT NULL `` |
| `is_gift boolean NOT NULL DEFAULT FALSE` | `` `is_gift` tinyint(1) NOT NULL DEFAULT '0' `` |
| `created_at timestamp NOT NULL DEFAULT now()` | `` `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP `` |
| `CHECK (status IN ('pending', 'paid', 'shipped'))` | ``CONSTRAINT `orders_chk_1` CHECK ((`status` in (_utf8mb4'pending',_utf8mb4'paid',_utf8mb4'shipped')))`` |

The [MySQL manual](https://dev.mysql.com/doc/refman/8.4/en/numeric-type-syntax.html) says the integer display width "is deprecated", which is why `int(11)` comes back as `int`, and that `BOOL` and `BOOLEAN` "are synonyms for `TINYINT(1)`". The unnamed `CHECK` gets a generated name, `orders_chk_1`, from the table name, `_chk_` and a number.

The `_utf8mb4` in front of each value is a [character set introducer](https://dev.mysql.com/doc/refman/8.4/en/charset-introducer.html), and MySQL records the one each literal had when the constraint was created. A literal written without one takes the connection's character set. We ran the identical `CREATE TABLE` twice on one MySQL 8.4.11 server, once after `SET NAMES utf8mb4` and once after `SET NAMES latin1`, and `information_schema.CHECK_CONSTRAINTS` stored two different texts for one rule:

```sql
-- created from a utf8mb4 client
(`status` in (_utf8mb4'pending',_utf8mb4'paid'))
-- created from a latin1 client
(`status` in (_latin1'pending',_latin1'paid'))
```

The stock `mysql` client in the official Docker image connected as `latin1`. If production was migrated from one client and staging from another, every `CHECK` with a string in it differs between them. For plain ASCII values none of those differences is a change to the rule; a non-ASCII value is another matter. We created `CHECK (v IN ('café'))` once from a `latin1` connection and once from a `utf8mb4` one, sending the same bytes. Inserting `'café'` over a `utf8mb4` connection worked on the second table and failed the `CHECK` on the first: same migration, a different rule. (MySQL also stored that one-value `IN` as `=`, one more respelling.)

## Which differences are real schema drift?

A difference is real when the database would accept or reject a different row, or fill in a different value, and two of them look like spelling at first glance.

**A default that was frozen at creation time.** The table above says `DEFAULT 'now'` on `imported_at` and `DEFAULT now()` on `created_at`. They look like two spellings of one default. They are not:

```sql
SELECT column_name, column_default
FROM information_schema.columns
WHERE table_name = 'orders' AND column_name IN ('created_at', 'imported_at');
```

```
 column_name |                      column_default
-------------+----------------------------------------------------------
 created_at  | now()
 imported_at | '2026-11-25 02:47:42.91941+00'::timestamp with time zone
```

`'now'` is a string the timestamp type converts "to a specific time value as soon as they are read", in the words of the [PostgreSQL date/time documentation](https://www.postgresql.org/docs/current/datatype-datetime.html), so the `CREATE TABLE` stored the moment it ran. Every row inserted afterwards gets that same timestamp: a row we inserted later got `02:47:43.16312` in `created_at` and the table's creation time, `02:47:42.91941`, in `imported_at`. In a dump this is easy to read past as "PostgreSQL printing my default its own way", which is exactly the habit the spelling rewrites teach.

**A value whose case changed.** `CHECK (status IN ('Paid', 'Shipped'))` and `CHECK (status IN ('paid', 'shipped'))` differ only in case, and on PostgreSQL they are different rules: PostgreSQL 18.3 refused `'paid'` under the first, and so did MySQL 8.4.11 on a `utf8mb4_bin` column. Under MySQL's default `utf8mb4_0900_ai_ci` collation both accept the same rows, so the only sign that someone changed the constraint is its text, and a comparison that ignores case will never show it. Text inside quotes is data. A comparison that folds case to ignore `IN` versus `in` has to stop folding at the first quote.

**MySQL's boolean is a small integer.** Not drift, but a gap the `tinyint(1)` in the dump is telling you about: `INSERT INTO orders (status, quantity, is_gift) VALUES ('paid', 1, 7)` succeeded on MySQL 8.4.11 and stored 7. If the column must hold only 0 and 1, add `CHECK (is_gift IN (0, 1))`.

| Difference you see | Same rule? | What to do |
|---|---|---|
| `IN` vs `= ANY (ARRAY[...])`, `NOT IN` vs `<> ALL`, `BETWEEN` vs `>= AND <=` | Yes | Ignore |
| Added casts, brackets, `int` vs `integer`, `int(11)` vs `int` | Yes | Ignore |
| `_latin1'x'` vs `_utf8mb4'x'` in a MySQL `CHECK` | Yes, for ASCII values | Pin the client character set in migrations |
| `DEFAULT 'now'` vs `DEFAULT now()` | No | Replace the frozen default |
| `'Paid'` vs `'paid'` inside a `CHECK` or default | No, except under a case-insensitive collation | Decide which one the application writes |

## How do you compare a migration to the live schema without false drift?

Apply the migrations to a scratch database and compare the two databases' own output, so both sides go through the same reconstruction.

Comparing a schema migration file, a `schema.sql` or an ORM model with a dump of production compares your spelling with the database's, and every one of the first two rows of the table above shows up as a change. Comparing two databases does not: run the migrations into an empty throwaway database of the same version, then print the rules from both with the same query and diff the outputs.

```sql
SELECT conrelid::regclass::text AS table_name, conname, pg_get_constraintdef(oid) AS definition
FROM pg_constraint
WHERE contype = 'c' AND connamespace = 'public'::regnamespace
ORDER BY 1, 2;
```

Both sides now print `= ANY (ARRAY[...])`, so what remains in the diff is a real difference: a constraint one side lacks, a value list that differs, a default frozen on one side. The same works for defaults with `SELECT table_name, column_name, column_default FROM information_schema.columns WHERE table_schema = 'public' ORDER BY 1, 2;`, and for whole tables with `pg_dump --schema-only --restrict-key=schema` from both databases (without a fixed key, every dump since the August 2025 minor releases carries a random one, and two dumps of one schema differ). On MySQL, add `SET NAMES utf8mb4;` to the top of every migration so each environment stores the same introducers, or strip `_charset` prefixes from both sides before comparing.

Two limits. The scratch database must run the same major version, because the reconstruction can change between releases. And this compares structure only: it says the two databases hold the same rules, not that the rows in production satisfy them, which matters for a constraint added `NOT VALID`.

## How does Schemity read a CHECK constraint PostgreSQL has rewritten?

Schemity reads the rule, not the spelling: a value-list `CHECK` comes back as its column and its values, and a saved, refreshed or imported diagram compares `CHECK` constraints by what they enforce and defaults by value.

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 it reads what the database stores and shows it as a rule you can check at a glance. On the `orders` table above, connected live to PostgreSQL 18.3, the `status` check reads as the column `status` with the values `pending`, `paid` and `shipped`, and lint treats the column as restricted ([lint](https://schemity.com/doc/schema-lint/) is a Schemity Pro feature). The Field details panel puts the two readings side by side: the three values as the rule, and under them `orders_status_check` as PostgreSQL stores it, `((status) = ANY ((ARRAY['pending', 'paid', 'shipped'])))`. Types arrive in plain SQL: `quantity int` reads as `INTEGER` and `is_gift bool` as `BOOLEAN`.

`imported_at`, on the other hand, shows its real default, the moment that copy of the table was created: `2026-11-25 03:29:12.285669+00`. Schemity does not tidy a frozen default back into something that looks like `now()`, because that is the one rewrite that changed what the column does.

![Schemity connected to a PostgreSQL 18.3 orders table: on the canvas, imported_at shows the frozen default 2026-11-25 03:29:12.285669+00 while created_at shows NOW(), status is underlined as a checked column with default pending, and the footer counts 3 check constraints. The Field details panel for orders.status lists the CHECK constraint values pending, paid and shipped, and below them orders_status_check as PostgreSQL stores it: ((status) = ANY ((ARRAY['pending', 'paid', 'shipped'])))](https://schemity.com/images/blog/postgres-check-constraint-rewritten-schema-drift/canvas-orders-field-details.webp)

When the diagram and the database spell one rule two ways, Schemity compares them by meaning. That matters in three places: [Refresh](https://schemity.com/doc/resync-database/) after the database changed under unsaved edits, Import SQL of a migration script over a diagram read from the live database, and the [migration SQL](https://schemity.com/doc/migration-sql-diff/) a save would run. We tested the third directly while writing this post. On a diagram of the table above, plus a `refunds` table created by hand in `psql`, we restated the database's rules the way a person types them, `channel NOT IN ('test', 'internal')` on `orders` and `amount BETWEEN 1 AND 1000` on `refunds`, and Schemity planned no statements. Changing one value to `'Test'` planned `ALTER TABLE "orders" DROP CONSTRAINT "orders_channel_check";` and the matching `ADD CONSTRAINT`, as it should. The Findings drawer, a Schemity Pro feature, shows that one change with its impact; Schemity Free shows the same migration SQL before it is applied.

![Schemity's Findings drawer after the orders_channel_check rule was edited to channel NOT IN ('Test', 'internal') on a PostgreSQL 18.3 diagram: the planned migration is two statements, ALTER TABLE "orders" DROP CONSTRAINT "orders_channel_check" and ALTER TABLE "orders" ADD CONSTRAINT "orders_channel_check" CHECK ((channel NOT IN ('Test', 'internal'))); Planned changes lists one, Check constraint on orders (channel) changed; Lint shows no findings; Impact lists Adds a CHECK on orders, fails if an existing row breaks it (not counted)](https://schemity.com/images/blog/postgres-check-constraint-rewritten-schema-drift/findings-check-case-change.webp)

Running this scenario also found two cases Schemity got wrong, both fixed in the next release. A drafted `BETWEEN`, `IN` or `NOT IN` `CHECK` that a framework's migration then created counted as changed against the database's `>= AND <=`, `= ANY` or `<> ALL` form, so the next save would have dropped and re-added a constraint nobody changed. And on PostgreSQL, MySQL and Snowflake a `CHECK` value edited only in case, `'Paid'` to `'paid'`, planned no migration at all, because the comparison folded case inside quoted values too. Both now follow the drift table above: spelling is ignored, quoted values are kept exactly.

## Related reading

If the differences you are chasing are between environments rather than between a migration and a database, [comparing staging and production database schemas](https://schemity.com/blog/comparing-staging-and-production-database-schemas/) covers that comparison, and [tracking schema changes made outside migrations](https://schemity.com/blog/postgres-track-schema-changes/) covers how they get there. For choosing the rule in the first place, see [enum vs `CHECK` constraint vs lookup table](https://schemity.com/blog/postgres-enum-vs-check-constraint-vs-lookup-table/). The [PostgreSQL round trip on Mastodon and Saleor](https://schemity.com/blog/postgres-round-trip-mastodon-saleor/) measures how much of a real schema survives being read and written back.

## Frequently asked questions

### Why does PostgreSQL change my `CHECK` constraint?

PostgreSQL stores a `CHECK` as a parsed expression tree and prints it back with `pg_get_constraintdef`, which the manual calls a decompiled reconstruction, not the original text. On PostgreSQL 18.3, `status IN ('pending', 'paid')` on a `varchar` column came back as `(status)::text = ANY ((ARRAY['pending'::character varying, 'paid'::character varying])::text[])`. The rule is the same; only the spelling changed.

### Is `= ANY (ARRAY[...])` the same as `IN` in a PostgreSQL `CHECK` constraint?

Yes, for a list of values. PostgreSQL turns `x IN ('a', 'b')` into `x = ANY (ARRAY['a', 'b'])` when it stores the constraint, `x NOT IN (...)` into `x <> ALL (ARRAY[...])`, and `x BETWEEN 1 AND 100` into `x >= 1 AND x <= 100`. A schema diff that compares text will report each of these as a change although no row is checked differently.

### Why does MySQL put `_utf8mb4` or `_latin1` in my `CHECK` constraint?

MySQL records the character set introducer of every string literal in the stored expression, and a literal written without one takes the connection's character set. On MySQL 8.4.11 the same `CREATE TABLE` stored `_utf8mb4'paid'` from a `utf8mb4` client and `_latin1'paid'` from a `latin1` client, so two environments built from one migration print different `CHECK` text. Run `SET NAMES utf8mb4` at the top of the migration so every environment stores the same spelling.
