Hi Zsolt,

thank you so much for testing this!

On Wed Nov 18, 2026 at 11:40 PM CET, Zsolt Parragi wrote:
>
> 1. The generation expression can change when it is reparsed based on
> session GUCs
>
> CREATE TABLE g (a float8, b float8);
> INSERT INTO g VALUES (1, 1 + '3.141592653589793'::float8);
> ALTER TABLE g ADD CONSTRAINT c CHECK (pg_datum_image_equal(b, a +
> '3.141592653589793'::float8));
> SET extra_float_digits = 0;
> ALTER TABLE g ALTER b ADD GENERATED USING CONSTRAINT c STORED;
> RESET extra_float_digits;
> \d g
>  b | double precision | generated always as (a +
> '3.14159265358979'::double precision) stored
> UPDATE g SET a = a;
> -- ERROR:  new row for relation "g" violates check constraint "c"
> -- DETAIL:  Failing row contains (1, 4.14159265358979).
>
> float4/timestamptz is also affected.

Nice catch, I fixed this by temporarily setting extra_float_digits=3 and
datestyle=ISO when reparsing the expression. This seems to be a common
workaround, used in a few other places too so I think it shouldn't be
problematic here. (Ideally we could instead tell the deparse
infrastructure that we need output which is round-trip safe, and not
merely meant for display... but the final output is anyway produced by
the type's output function...)

In a sibling thread about logical replication I saw that they are
discussing the same problem, and also need to pin
intervalstyle=postgres. Pg_dump too explicitly says "avoid using
sql_standard intervalstyle".

For the purpose of this command though I couldn't come up with an
interval value that actually is problematic when deparsed/reparsed with
the same setting like we need to do, so I left that out. Happy to add a
test if you can see something I didn't :)

> 2. BEFORE triggers with WHEN on NEW.b survive the conversion
>
> CREATE TABLE t (a int, b int); INSERT INTO t VALUES (1, 2);
> ALTER TABLE t ADD CONSTRAINT c CHECK (pg_datum_image_equal(b, a*2));
> CREATE TRIGGER w_null BEFORE INSERT OR UPDATE ON t FOR EACH ROW WHEN
> (NEW.b IS NULL) EXECUTE FUNCTION trg();
> CREATE TRIGGER w_100  BEFORE INSERT OR UPDATE ON t FOR EACH ROW WHEN
> (NEW.b = 100) EXECUTE FUNCTION trg();
> ALTER TABLE t ALTER b ADD GENERATED USING CONSTRAINT c STORED;   -- succeeds
> INSERT INTO t (a) VALUES (50);      -- fires w_null, not w_100, stored b = 100
> -- pg_dump | psql:
> -- ERROR:  BEFORE trigger's WHEN condition cannot reference NEW
> generated columns
>
> CREATE TRIGGER refuses this combination (trigger.c, "BEFORE trigger's
> WHEN condition cannot reference NEW generated columns"), because the
> generated value isn't computed yet when WHEN runs.
> Note that this is partially preexisting, the whole-row variant (WHEN
> (NEW IS DISTINCT FROM OLD)) is already reachable on master with ADD
> COLUMN ... GENERATED, but the NEW.b one is new.

I could detect this for both a direct reference to the column and a
whole-row reference. The command now bails with (respectively):

  ERROR:  cannot convert column "b" to generated
  DETAIL:  Column NEW."b" is referenced by the WHEN clause of trigger 
"t_trigger".

and

  ERROR:  cannot convert column "b" to generated
  DETAIL:  The WHEN clause of trigger "t_trigger" references a row which 
encompasses NEW."b".

I added tests for both.

> 3. Multiple inheritance
>
> CREATE TABLE p1 (a int, b int); CREATE TABLE p2 (a int, b int);
> CREATE TABLE c () INHERITS (p1, p2);
> ALTER TABLE p1 ADD CONSTRAINT k CHECK (pg_datum_image_equal(b, a*2));
> INSERT INTO c VALUES (1, 2);
> ALTER TABLE p1 ALTER b ADD GENERATED USING CONSTRAINT k STORED;   -- succeeds
> ALTER TABLE p1 DROP CONSTRAINT k;
> UPDATE p2 SET b = 999;
> SELECT a, b FROM c;   -- 1 | 999   (expression gives 2)
> -- pg_dump | psql:
> -- ERROR:  inherited column "b" has a generation conflict
> -- ERROR:  relation "public.c" does not exist
>
> c.b becomes generated while p2.b stays a regular column. This seems to
> come from DROP EXPRESSION: it has the same problem in the other
> direction on master (drop the expression on one of two generated
> parents), which probably deserves a separate fix.

This was pretty interesting to look into. I don't see a way for the user
to transform the whole hierarchy when multiple inheritance adds parent
nodes which aren't reachable by the root node, but I can detect that
case and bail. So this shape is allowed when running the command on "A":

  A
 / \
B   C
 \ /
  D

while this is rejected:

  A   A'
 / \ /
B   C
 \ /
  D

The check is for now inlined in the ATPrep*() function for this command,
but ideally it would be done in such a way that it can be reused by DROP
EXPRESSION. I'd keep this separated from this patch though and start a
new thread. At a first glance, I'd like if this was checked (optionally
of course) by ATSimpleRecursion(), because that already calls
find_all_inheritors() (the function that can count the n. of parent
nodes we've visited for each node, allowing us to detect extra parents).
Doing it there would save us an additional call to find_all_inheritors()
which I guess can be quite a lot of work when called on big inheritance
hierarchies.

Anyway what I've done seems good enough for this patch, we can have a
look at the above separately. I've added tests for both shapes mentioned
above.

> 4. Typed tables are converted although CREATE TABLE OF refuses generated 
> columns
>
> CREATE TYPE tt AS (a int, b int);
> CREATE TABLE s6 OF tt;
> INSERT INTO s6 VALUES (1, 2);
> ALTER TABLE s6 ADD CONSTRAINT c CHECK (pg_datum_image_equal(b, a + 1));
> ALTER TABLE s6 ALTER b ADD GENERATED USING CONSTRAINT c STORED;   -- succeeds
> -- pg_dump | psql:
> -- ERROR:  generated columns are not supported on typed tables
> -- ERROR:  relation "public.s6" does not exist

I did not think of this at all. Thanks! It's now detected and rejected,
with a regression test.

> 5. FK check also rejects the referenced side
>
> CREATE TABLE pk (a int, b int UNIQUE);
> CREATE TABLE fk (x int REFERENCES pk(b) ON UPDATE CASCADE);
> ALTER TABLE pk ADD CONSTRAINT k CHECK (pg_datum_image_equal(b, a*2));
> ALTER TABLE pk ALTER b ADD GENERATED USING CONSTRAINT k STORED;
> -- ERROR:  cannot convert column "b" to generated
> -- DETAIL:  Column "b" is part of a foreign key with an ON DELETE / ON
> UPDATE action.
>
> Master only restricts the actions when the referencing columns are
> generated, a generated UNIQUE column can be referenced with ON UPDATE
> CASCADE.
> The detail is also a bit misleading, ON DELETE CASCADE is accepted.

I forgot to check that we're on the referencing side (conrelid). Fixed,
and added a test. I also changed the error message, hopefully it's
clearer now:

   ERROR:  cannot convert column "b" to generated
  -DETAIL:  Column "b" is part of a foreign key with an ON DELETE / ON UPDATE 
action.
  +DETAIL:  Column "b" is part of a foreign key with a restricted ON DELETE / 
ON UPDATE action.

What do you think?


Thanks again for your testing effort,


Alberto


-- 
Alberto Piai
Sensational AG
Zürich, Switzerland

>From e3666d230cb47f21b7feea9087fcd1cffcf35203 Mon Sep 17 00:00:00 2001
From: Matthias van de Meent <[email protected]>
Date: Wed, 18 Nov 2026 17:10:37 +0100
Subject: [PATCH v14 1/2] Add SQL-level datum equality tests

This enables improved performance for users that need to test for
exact bytewise differences in SQL; e.g. to see if an UPDATE is
really necessary with a set of externally provided values.

A workaround around this limitation was often possible, but no
generic method was available that was this performant, nor as
accessible to all types.
---
 doc/src/sgml/func/func-comparison.sgml       | 25 ++++++
 src/backend/utils/adt/pseudotypes.c          | 51 ++++++++++++
 src/include/catalog/pg_proc.dat              |  7 ++
 src/test/regress/expected/misc_functions.out | 81 ++++++++++++++++++++
 src/test/regress/expected/opr_sanity.out     |  1 +
 src/test/regress/sql/misc_functions.sql      | 25 ++++++
 6 files changed, 190 insertions(+)

diff --git a/doc/src/sgml/func/func-comparison.sgml 
b/doc/src/sgml/func/func-comparison.sgml
index ecb1d89463a..df806d992c3 100644
--- a/doc/src/sgml/func/func-comparison.sgml
+++ b/doc/src/sgml/func/func-comparison.sgml
@@ -653,6 +653,31 @@ SELECT NOT(ROW(table.*) IS NOT NULL) FROM TABLE; -- detect 
at least one null in
         <returnvalue>1</returnvalue>
        </para></entry>
       </row>
+
+      <row>
+       <entry role="func_table_entry"><para role="func_signature">
+        <indexterm>
+         <primary>pg_datum_image_equal</primary>
+        </indexterm>
+        <function>pg_datum_image_equal</function> ( <type>anyelement</type>, 
<type>anyelement</type> )
+        <returnvalue>boolean</returnvalue>
+       </para>
+       <para>
+        Returns whether the values have the same exact binary representation.
+       </para>
+       <para>
+        <literal>pg_datum_image_equal('1.0'::numeric, '1.0'::numeric)</literal>
+        <returnvalue>true</returnvalue>
+       </para>
+       <para>
+        <literal>pg_datum_image_equal('1.0'::numeric, 
'1.00'::numeric)</literal>
+        <returnvalue>false</returnvalue>
+       </para>
+       <para>
+        <literal>pg_datum_image_equal(NULL::numeric, NULL::numeric)</literal>
+        <returnvalue>true</returnvalue>
+       </para></entry>
+      </row>
      </tbody>
     </tgroup>
    </table>
diff --git a/src/backend/utils/adt/pseudotypes.c 
b/src/backend/utils/adt/pseudotypes.c
index 4581c4b1697..e867cf329a3 100644
--- a/src/backend/utils/adt/pseudotypes.c
+++ b/src/backend/utils/adt/pseudotypes.c
@@ -23,7 +23,9 @@
 #include "postgres.h"
 
 #include "libpq/pqformat.h"
+#include "utils/datum.h"
 #include "utils/fmgrprotos.h"
+#include "utils/lsyscache.h"
 
 
 /*
@@ -375,3 +377,52 @@ PSEUDOTYPE_DUMMY_IO_FUNCS(anyelement);
 PSEUDOTYPE_DUMMY_IO_FUNCS(anynonarray);
 PSEUDOTYPE_DUMMY_IO_FUNCS(anycompatible);
 PSEUDOTYPE_DUMMY_IO_FUNCS(anycompatiblenonarray);
+
+/*
+ * Compares two datums of the same (any) type, and returns whether they have
+ * the same binary representation.
+ */
+Datum
+pg_datum_image_equal(PG_FUNCTION_ARGS)
+{
+       bool            eq;
+
+       if (PG_ARGISNULL(0) != PG_ARGISNULL(1))
+       {
+               eq = false;
+       }
+       else if (PG_ARGISNULL(0))
+       {
+               /* both NULL */
+               eq = true;
+       }
+       else
+       {
+               Oid                     typ;
+               Datum           arg0;
+               Datum           arg1;
+               bool            typbyval;
+               char            typalign;
+               int16           typlen;
+
+               typ = get_fn_expr_argtype(fcinfo->flinfo, 0);
+
+               if (!OidIsValid(typ))
+               {
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                                        errmsg("could not determine type")));
+               }
+
+               Assert(typ == get_fn_expr_argtype(fcinfo->flinfo, 1));
+
+               arg0 = PG_GETARG_DATUM(0);
+               arg1 = PG_GETARG_DATUM(1);
+
+               get_typlenbyvalalign(typ, &typlen, &typbyval, &typalign);
+
+               eq = datum_image_eq(arg0, arg1, typbyval, typlen);
+       }
+
+       PG_RETURN_BOOL(eq);
+}
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 50fb182cb79..4e716d223a6 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -12788,4 +12788,11 @@
   proname => 'hashoid8extended', prorettype => 'int8',
   proargtypes => 'oid8 int8', prosrc => 'hashoid8extended' },
 
+{ oid => '9200',
+  descr => 'test if two values have the same binary representation',
+  proname => 'pg_datum_image_equal', proisstrict => 'f',
+  proleakproof => 't', prorettype => 'bool',
+  proargtypes => 'anyelement anyelement',
+  prosrc => 'pg_datum_image_equal' },
+
 ]
diff --git a/src/test/regress/expected/misc_functions.out 
b/src/test/regress/expected/misc_functions.out
index 35d6ee2ce9e..7976ad8e925 100644
--- a/src/test/regress/expected/misc_functions.out
+++ b/src/test/regress/expected/misc_functions.out
@@ -901,3 +901,84 @@ SELECT test_pg_locale_apis(to_regcollation('en-x-icu'));
  
 (1 row)
 
+-- test pg_datum_image_equal(..., ...)
+WITH values AS (
+    SELECT val::numeric FROM (
+        VALUES ('1.0'),
+               ('1.00'),
+               ('2.0'),
+               ('2.1'),
+               (NULL)
+    ) AS v(val)
+)
+SELECT a.val a, b.val b, a.val = b.val eq, pg_datum_image_equal(a.val, b.val) 
pdie
+FROM values a CROSS JOIN values b;
+  a   |  b   | eq | pdie 
+------+------+----+------
+  1.0 |  1.0 | t  | t
+  1.0 | 1.00 | t  | f
+  1.0 |  2.0 | f  | f
+  1.0 |  2.1 | f  | f
+  1.0 |      |    | f
+ 1.00 |  1.0 | t  | f
+ 1.00 | 1.00 | t  | t
+ 1.00 |  2.0 | f  | f
+ 1.00 |  2.1 | f  | f
+ 1.00 |      |    | f
+  2.0 |  1.0 | f  | f
+  2.0 | 1.00 | f  | f
+  2.0 |  2.0 | t  | t
+  2.0 |  2.1 | f  | f
+  2.0 |      |    | f
+  2.1 |  1.0 | f  | f
+  2.1 | 1.00 | f  | f
+  2.1 |  2.0 | f  | f
+  2.1 |  2.1 | t  | t
+  2.1 |      |    | f
+      |  1.0 |    | f
+      | 1.00 |    | f
+      |  2.0 |    | f
+      |  2.1 |    | f
+      |      |    | t
+(25 rows)
+
+WITH values AS (
+    SELECT val::jsonb FROM (
+        VALUES ('{"key": 1.0}'),
+               ('{"key": 1.00}'),
+               ('{"key": 2.0}'),
+               ('{"key": null}'),
+               (NULL)
+    ) AS v(val)
+)
+SELECT a.val a, b.val b, a.val = b.val eq, pg_datum_image_equal(a.val, b.val) 
pdie
+FROM values a CROSS JOIN values b;
+       a       |       b       | eq | pdie 
+---------------+---------------+----+------
+ {"key": 1.0}  | {"key": 1.0}  | t  | t
+ {"key": 1.0}  | {"key": 1.00} | t  | f
+ {"key": 1.0}  | {"key": 2.0}  | f  | f
+ {"key": 1.0}  | {"key": null} | f  | f
+ {"key": 1.0}  |               |    | f
+ {"key": 1.00} | {"key": 1.0}  | t  | f
+ {"key": 1.00} | {"key": 1.00} | t  | t
+ {"key": 1.00} | {"key": 2.0}  | f  | f
+ {"key": 1.00} | {"key": null} | f  | f
+ {"key": 1.00} |               |    | f
+ {"key": 2.0}  | {"key": 1.0}  | f  | f
+ {"key": 2.0}  | {"key": 1.00} | f  | f
+ {"key": 2.0}  | {"key": 2.0}  | t  | t
+ {"key": 2.0}  | {"key": null} | f  | f
+ {"key": 2.0}  |               |    | f
+ {"key": null} | {"key": 1.0}  | f  | f
+ {"key": null} | {"key": 1.00} | f  | f
+ {"key": null} | {"key": 2.0}  | f  | f
+ {"key": null} | {"key": null} | t  | t
+ {"key": null} |               |    | f
+               | {"key": 1.0}  |    | f
+               | {"key": 1.00} |    | f
+               | {"key": 2.0}  |    | f
+               | {"key": null} |    | f
+               |               |    | t
+(25 rows)
+
diff --git a/src/test/regress/expected/opr_sanity.out 
b/src/test/regress/expected/opr_sanity.out
index 907d63daa3f..281c46ff6ec 100644
--- a/src/test/regress/expected/opr_sanity.out
+++ b/src/test/regress/expected/opr_sanity.out
@@ -892,6 +892,7 @@ tid_block(tid)
 tid_offset(tid)
 uuid_larger(uuid,uuid)
 uuid_smaller(uuid,uuid)
+pg_datum_image_equal(anyelement,anyelement)
 -- Check that functions without argument are not marked as leakproof.
 SELECT p1.oid::regprocedure
 FROM pg_proc p1 JOIN pg_namespace pn
diff --git a/src/test/regress/sql/misc_functions.sql 
b/src/test/regress/sql/misc_functions.sql
index 94b91964c04..e509b05c658 100644
--- a/src/test/regress/sql/misc_functions.sql
+++ b/src/test/regress/sql/misc_functions.sql
@@ -381,3 +381,28 @@ SELECT test_pg_locale_apis(to_regcollation('pg_c_utf8'));
 
 -- en-x-icu is present when ICU collations were imported at initdb.
 SELECT test_pg_locale_apis(to_regcollation('en-x-icu'));
+
+-- test pg_datum_image_equal(..., ...)
+WITH values AS (
+    SELECT val::numeric FROM (
+        VALUES ('1.0'),
+               ('1.00'),
+               ('2.0'),
+               ('2.1'),
+               (NULL)
+    ) AS v(val)
+)
+SELECT a.val a, b.val b, a.val = b.val eq, pg_datum_image_equal(a.val, b.val) 
pdie
+FROM values a CROSS JOIN values b;
+
+WITH values AS (
+    SELECT val::jsonb FROM (
+        VALUES ('{"key": 1.0}'),
+               ('{"key": 1.00}'),
+               ('{"key": 2.0}'),
+               ('{"key": null}'),
+               (NULL)
+    ) AS v(val)
+)
+SELECT a.val a, b.val b, a.val = b.val eq, pg_datum_image_equal(a.val, b.val) 
pdie
+FROM values a CROSS JOIN values b;

base-commit: 4483697be0e0d22b9b4fd847e8bf2d998c56a741
-- 
2.54.0

>From 0c6e4e9c18f8d327eeed57b63430fca8460e0be4 Mon Sep 17 00:00:00 2001
From: Alberto Piai <[email protected]>
Date: Wed, 25 Nov 2026 18:51:01 +0100
Subject: [PATCH v14 2/2] Support changing a column into a stored generated
 column

This adds an ALTER TABLE subcommand which turns a regular column into a
stored generated column:

  ... ALTER col ADD GENERATED USING CONSTRAINT constr_name STORED

The main purpose of this command is to make it possible to add a stored
generated column without rewriting the table under an AccessExclusive
lock.

Before running this command, the table should have been prepared by
adding a regular column, backfilling it with data matching the intended
generation expression, and adding the constr_name constraint to prove
that the data does satisfy said generation expression.

The constraint must have a specific structure to be usable for this
operation:

  CHECK (pg_datum_image_equal(column_name, expr))

The column will then be changed into a stored generated column, with the
"expr" from the constraint as its generator expression. The operation
will be performed without rewriting the table, and without any
verification scan.

The syntax is chosen for its similarity to:

  ... ALTER COLUMN ... ADD GENERATED ... AS IDENTITY

with the difference that:

- since we are not talking about IDENTITY columns, ALWAYS is implicit
  and does not need to be explicitly supplied

- STORED must always be supplied, to make it clear that we are talking
  about stored and not virtual generated columns (VIRTUAL is the default
  when omitted in other commands, so we mandate STORED here to be
  consistent)

The use of the pg_datum_image_equal() function in the constraint is
necessary to ensure that the data in the column is exactly what would be
produced by the generator expression: if we used logical = instead, it
would be possible to store data inconsistent with the generator
expression by using values which are equal according to =, but don't
have the same image (for example numeric 1.0 and 1.00).

This new operation fits together with SET EXPRESSION and DROP
EXPRESSION: the latter works in the opposite direction, turning a
generated column into a regular column.
---
 doc/src/sgml/ref/alter_table.sgml             |  41 +
 src/backend/commands/tablecmds.c              | 741 +++++++++++++++++-
 src/backend/parser/gram.y                     |  18 +
 src/bin/psql/t/010_tab_completion.pl          |  22 +-
 src/bin/psql/tab-complete.in.c                |  43 +-
 src/include/nodes/parsenodes.h                |   1 +
 src/test/modules/injection_points/Makefile    |   2 +-
 .../injection_points/expected/alter_table.out |  36 +
 src/test/modules/injection_points/meson.build |   1 +
 .../injection_points/sql/alter_table.sql      |  24 +
 .../test_ddl_deparse/test_ddl_deparse.c       |   3 +
 src/test/regress/expected/alter_table.out     | 584 ++++++++++++++
 src/test/regress/sql/alter_table.sql          | 396 ++++++++++
 src/tools/pgindent/typedefs.list              |   1 +
 14 files changed, 1906 insertions(+), 7 deletions(-)
 create mode 100644 src/test/modules/injection_points/expected/alter_table.out
 create mode 100644 src/test/modules/injection_points/sql/alter_table.sql

diff --git a/doc/src/sgml/ref/alter_table.sgml 
b/doc/src/sgml/ref/alter_table.sgml
index 0f9d698d170..090a485e979 100644
--- a/doc/src/sgml/ref/alter_table.sgml
+++ b/doc/src/sgml/ref/alter_table.sgml
@@ -46,6 +46,7 @@ ALTER TABLE [ IF EXISTS ] <replaceable 
class="parameter">name</replaceable>
     ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> 
SET DEFAULT <replaceable class="parameter">expression</replaceable>
     ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> 
DROP DEFAULT
     ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> 
{ SET | DROP } NOT NULL
+    ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> 
ADD GENERATED USING CONSTRAINT <replaceable 
class="parameter">constraint_name</replaceable> STORED
     ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> 
SET EXPRESSION AS ( <replaceable class="parameter">expression</replaceable> )
     ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> 
DROP EXPRESSION [ IF EXISTS ]
     ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> 
ADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( 
<replaceable>sequence_options</replaceable> ) ]
@@ -266,6 +267,46 @@ WITH ( MODULUS <replaceable 
class="parameter">numeric_literal</replaceable>, REM
     </listitem>
    </varlistentry>
 
+   <varlistentry 
id="sql-altertable-desc-add-generated-always-stored-using-constraint">
+    <term><literal>ADD GENERATED USING CONSTRAINT</literal></term>
+    <listitem>
+     <para>
+      This form changes a regular column into a stored generated column, using
+      the expression from the given <literal>CHECK</literal> constraint as
+      generation expression. The operation will be performed without rewriting
+      the table, which avoids holding an <literal>ACCESS EXCLUSIVE</literal>
+      lock for a possibly long time.
+     </para>
+
+     <para>
+      Before using this command, the table will usually have been prepared by
+      adding a regular column, backfilling it with values matching the intended
+      generation expression and adding a constraint to ensure that the
+      generation expression is satisfied. Note that the constraint can also be
+      added without holding an <literal>ACCESS EXCLUSIVE</literal> lock while
+      the table is scanned, using <literal>NOT VALID</literal> and
+      <literal>VALIDATE CONSTRAINT</literal>.
+     </para>
+
+     <para>
+      The constraint must be of the form <literal>CHECK ( pg_datum_image_equal(
+      <replaceable class="parameter">column_name</replaceable>,
+      <replaceable class="parameter">expr</replaceable>) )</literal>.
+     </para>
+
+     <para>
+      After this command has completed, <literal>column_name</literal> will be 
a
+      stored generated column with <literal>expr</literal> as its generation
+      expression.
+     </para>
+
+     <para>
+      Note: the column <replaceable class="parameter">column_name</replaceable>
+      must be defined without a <literal>DEFAULT</literal> value.
+     </para>
+    </listitem>
+   </varlistentry>
+
    <varlistentry id="sql-altertable-desc-set-expression">
     <term><literal>SET EXPRESSION AS</literal></term>
     <listitem>
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index e67c9c3768b..d5e36a36757 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -102,6 +102,7 @@
 #include "utils/acl.h"
 #include "utils/builtins.h"
 #include "utils/fmgroids.h"
+#include "utils/injection_point.h"
 #include "utils/inval.h"
 #include "utils/lsyscache.h"
 #include "utils/memutils.h"
@@ -330,6 +331,17 @@ struct DropRelationCallbackState
        char            actual_relpersistence;
 };
 
+/*
+ * Used by findUsableConstraintForAddGenStored to give a hint about why no
+ * constraint matched.
+ */
+typedef enum AddGenConstrError
+{
+       ADD_GEN_CONSTR_NOT_FOUND = 0,
+       ADD_GEN_CONSTR_NOT_VALID,
+       ADD_GEN_CONSTR_SHAPE_MISMATCH,
+} AddGenConstrError;
+
 /* Alter table target-type flags for ATSimplePermissions */
 #define                ATT_TABLE                               0x0001
 #define                ATT_VIEW                                0x0002
@@ -770,7 +782,22 @@ static List *GetParentedForeignKeyRefs(Relation partition);
 static void ATDetachCheckNoForeignKeyRefs(Relation partition);
 static char GetAttributeCompression(Oid atttypid, const char *compression);
 static char GetAttributeStorage(Oid atttypid, const char *storagemode);
-
+static void ATPrepAddGenStored(Relation rel, AlterTableCmd *cmd, bool recurse, 
bool recursing);
+static void checkDependenciesForAddGenStored(Relation rel,
+                                                                               
         AttrNumber attnum,
+                                                                               
         const char *colName);
+static char *checkTriggerQualForAddGenStored(Oid trigger_oid,
+                                                                               
         int varno, AttrNumber attnum);
+static Node *findUsableConstraintForAddGenStored(Relation rel,
+                                                                               
                 AttrNumber attnum,
+                                                                               
                 const char *conname,
+                                                                               
                 AddGenConstrError *reason);
+static Node *matchDatumImageEqualOnVar(Node *expr, AttrNumber attnum);
+static Node *reconstructRawExpr(Relation rel, Node *cookedExpr);
+static ObjectAddress ATExecAddGeneratedStored(AlteredTableInfo *tab,
+                                                                               
          Relation rel,
+                                                                               
          const char *colName,
+                                                                               
          Constraint *def);
 
 /* ----------------------------------------------------------------
  *             DefineRelation
@@ -4784,6 +4811,7 @@ AlterTableGetLockLevel(List *cmds)
                        case AT_AddIdentity:
                        case AT_DropIdentity:
                        case AT_SetIdentity:
+                       case AT_AddGeneratedStored:
                        case AT_SetExpression:
                        case AT_DropExpression:
                        case AT_SetCompression:
@@ -5103,6 +5131,14 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd 
*cmd,
                        ATSimpleRecursion(wqueue, rel, cmd, recurse, lockmode, 
context);
                        pass = AT_PASS_SET_EXPRESSION;
                        break;
+               case AT_AddGeneratedStored: /* ALTER COLUMN ADD GENERATED USING
+                                                                        * 
CONSTRAINT */
+                       ATSimplePermissions(cmd->subtype, rel,
+                                                               ATT_TABLE | 
ATT_PARTITIONED_TABLE);
+                       ATSimpleRecursion(wqueue, rel, cmd, recurse, lockmode, 
context);
+                       ATPrepAddGenStored(rel, cmd, recurse, recursing);
+                       pass = AT_PASS_ADD_OTHERCONSTR;
+                       break;
                case AT_DropExpression: /* ALTER COLUMN DROP EXPRESSION */
                        ATSimplePermissions(cmd->subtype, rel,
                                                                ATT_TABLE | 
ATT_PARTITIONED_TABLE | ATT_FOREIGN_TABLE);
@@ -5503,6 +5539,12 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab,
                case AT_SetExpression:
                        address = ATExecSetExpression(tab, rel, cmd->name, 
cmd->def, lockmode);
                        break;
+               case AT_AddGeneratedStored:
+                       Assert(IsA(cmd->def, Constraint));
+                       address = ATExecAddGeneratedStored(tab, rel,
+                                                                               
           cmd->name,
+                                                                               
           (Constraint *) cmd->def);
+                       break;
                case AT_DropExpression:
                        address = ATExecDropExpression(rel, cmd->name, 
cmd->missing_ok, lockmode);
                        break;
@@ -6371,13 +6413,19 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
                }
 
                if (newrel)
+               {
                        ereport(DEBUG1,
                                        (errmsg_internal("rewriting table 
\"%s\"",
                                                                         
RelationGetRelationName(oldrel))));
+                       INJECTION_POINT("alter-table-phase-3-rewrite", NULL);
+               }
                else
+               {
                        ereport(DEBUG1,
                                        (errmsg_internal("verifying table 
\"%s\"",
                                                                         
RelationGetRelationName(oldrel))));
+                       INJECTION_POINT("alter-table-phase-3-verify", NULL);
+               }
 
                if (newrel)
                {
@@ -6697,6 +6745,8 @@ alter_table_type_to_string(AlterTableType cmdtype)
                        return "ALTER COLUMN ... SET NOT NULL";
                case AT_SetExpression:
                        return "ALTER COLUMN ... SET EXPRESSION";
+               case AT_AddGeneratedStored:
+                       return "ALTER COLUMN ... ADD GENERATED STORED";
                case AT_DropExpression:
                        return "ALTER COLUMN ... DROP EXPRESSION";
                case AT_SetStatistics:
@@ -9409,6 +9459,695 @@ ATExecSetExpression(AlteredTableInfo *tab, Relation 
rel, const char *colName,
        return address;
 }
 
+/*
+ * Preparation phase for
+ *
+ * ALTER COLUMN col ADD GENERATED USING CONSTRAINT name STORED
+ *
+ * In an inheritance hierarchy, it is only valid to alter the type of the
+ * whole hierarchy at once.
+ */
+static void
+ATPrepAddGenStored(Relation rel, AlterTableCmd *cmd, bool recurse, bool 
recursing)
+{
+       /*
+        * This routine is called on the top table directly with 
recursing=false,
+        * and on all children tables via ATSimpleRecursion with recursing=true.
+        *
+        * At the top level, forbid ONLY (i.e. recurse=false) if there are child
+        * tables. We only check this at the top level, otherwise we would 
prevent
+        * this operation from being applied to hierarchies with depth > 2.
+        *
+        * The caller already holds an AccessExclusiveLock on the rel, so taking
+        * locks in find_inheritance_children is not necessary.
+        */
+       if (!recursing && !recurse &&
+               find_inheritance_children(RelationGetRelid(rel), NoLock))
+               ereport(ERROR,
+                               errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                               errmsg("cannot convert column \"%s\" to 
generated", cmd->name),
+                               errdetail("Converting only part of a 
partitioning or inheritance hierarchy is not supported."),
+                               errhint("Use this command on the root table 
without specifying ONLY."));
+
+       /*
+        * Don't allow this operation to be applied to inherited columns 
directly.
+        */
+       if (!recursing)
+       {
+               HeapTuple       tuple;
+               Form_pg_attribute attTup;
+
+               tuple = SearchSysCacheCopyAttName(RelationGetRelid(rel), 
cmd->name);
+               if (!HeapTupleIsValid(tuple))
+                       ereport(ERROR,
+                                       errcode(ERRCODE_UNDEFINED_COLUMN),
+                                       errmsg("column \"%s\" of relation 
\"%s\" does not exist",
+                                                  cmd->name, 
RelationGetRelationName(rel)));
+
+               attTup = (Form_pg_attribute) GETSTRUCT(tuple);
+
+               if (attTup->attinhcount > 0)
+                       ereport(ERROR,
+                                       errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                                       errmsg("cannot convert column \"%s\" to 
generated", cmd->name),
+                                       errdetail("Converting only part of a 
partitioning or inheritance hierarchy is not supported."),
+                                       errhint("Use this command on the root 
table without specifying ONLY."));
+       }
+
+       /*
+        * Prevent this command from being applied to an inheritance hierarchy
+        * where any of the children has a parent *outside* of the hierarchy 
we're
+        * trying to transform. Only check this at the top level.
+        */
+       if (!recursing && recurse)
+       {
+               List       *child_oids;
+               List       *child_nparents = NULL;
+               ListCell   *l_oid,
+                                  *l_nparents;
+
+               child_oids = find_all_inheritors(RelationGetRelid(rel), NoLock,
+                                                                               
 &child_nparents);
+
+               forboth(l_oid, child_oids, l_nparents, child_nparents)
+               {
+                       HeapTuple       tuple;
+                       Form_pg_attribute attTup;
+                       Oid                     childrelid = lfirst_oid(l_oid);
+                       int                     nparents = 
lfirst_int(l_nparents);
+
+                       if (childrelid == RelationGetRelid(rel))
+                               continue;
+
+                       tuple = SearchSysCacheAttName(childrelid, cmd->name);
+                       if (!HeapTupleIsValid(tuple))
+                               ereport(ERROR,
+                                               
errcode(ERRCODE_UNDEFINED_COLUMN),
+                                               errmsg("column \"%s\" of 
relation \"%s\" does not exist",
+                                                          cmd->name, 
RelationGetRelationName(rel)));
+
+                       attTup = (Form_pg_attribute) GETSTRUCT(tuple);
+
+                       if (attTup->attinhcount > nparents)
+                               ereport(ERROR,
+                                               
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                                               errmsg("cannot convert column 
\"%s\" to generated", cmd->name),
+                                               errdetail("Converting only part 
of a multiple-inheritance hierarchy is not supported."),
+                                               errhint("Run this command on a 
root node which covers the whole hierarchy, if there is one."));
+
+                       ReleaseSysCache(tuple);
+               }
+       }
+}
+
+/*
+ * Detect dependencies which should stop us from turning a regular column
+ * into a stored generated column.
+ */
+static void
+checkDependenciesForAddGenStored(Relation rel,
+                                                                AttrNumber 
attnum,
+                                                                const char 
*colName)
+{
+       Relation        pg_depend;
+       ScanKeyData key[3];
+       SysScanDesc scan;
+       HeapTuple       depTup;
+
+       pg_depend = table_open(DependRelationId, AccessShareLock);
+
+       ScanKeyInit(&key[0],
+                               Anum_pg_depend_refclassid,
+                               BTEqualStrategyNumber, F_OIDEQ,
+                               ObjectIdGetDatum(RelationRelationId));
+       ScanKeyInit(&key[1],
+                               Anum_pg_depend_refobjid,
+                               BTEqualStrategyNumber, F_OIDEQ,
+                               ObjectIdGetDatum(RelationGetRelid(rel)));
+       ScanKeyInit(&key[2],
+                               Anum_pg_depend_refobjsubid,
+                               BTEqualStrategyNumber, F_INT4EQ,
+                               Int32GetDatum(attnum));
+
+       scan = systable_beginscan(pg_depend, DependReferenceIndexId, true,
+                                                         NULL, 3, key);
+
+       while (HeapTupleIsValid(depTup = systable_getnext(scan)))
+       {
+               Form_pg_depend dep = GETSTRUCT(depTup);
+               ObjectAddress foundObject;
+
+               foundObject.classId = dep->classid;
+               foundObject.objectId = dep->objid;
+               foundObject.objectSubId = dep->objsubid;
+
+               switch (foundObject.classId)
+               {
+                       case RelationRelationId:
+                               {
+                                       char            relKind = 
get_rel_relkind(foundObject.objectId);
+
+                                       /*
+                                        * While it is possible to alter any 
sequence to be owned
+                                        * by an arbitrary column, the most 
likely legitimate use
+                                        * is for a serial column. Let's assume 
this is the case
+                                        * for the sake of a more helpful error 
message.
+                                        */
+                                       if (relKind == RELKIND_SEQUENCE)
+                                               ereport(ERROR,
+                                                               
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                                                                errmsg("cannot 
convert column \"%s\" to generated", colName),
+                                                                
errdetail("Column \"%s\" of relation \"%s\" is a serial column (depends on 
\"%s\").",
+                                                                               
   colName, RelationGetRelationName(rel),
+                                                                               
   getObjectDescription(&foundObject, false))));
+                                       break;
+                               }
+                       case AttrDefaultRelationId:
+                               {
+                                       ObjectAddress col = 
GetAttrDefaultColumnAddress(foundObject.objectId);
+
+                                       if (col.objectId == 
RelationGetRelid(rel) &&
+                                               col.objectSubId == attnum)
+                                       {
+                                               /*
+                                                * Ignore the column's own 
default expression. We
+                                                * handle sequences above, and 
for a column which is
+                                                * already a generated column 
we should never get
+                                                * here.
+                                                */
+                                       }
+                                       else
+                                       {
+                                               ereport(ERROR,
+                                                               
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                                                                errmsg("cannot 
convert column \"%s\" to generated", colName),
+                                                                
errdetail("Column \"%s\" is referenced by generated column \"%s\".",
+                                                                               
   colName,
+                                                                               
   get_attname(col.objectId, col.objectSubId, false))));
+                                       }
+                                       break;
+                               }
+                       case ConstraintRelationId:
+                               {
+                                       HeapTuple       conTup;
+                                       Form_pg_constraint con;
+
+                                       conTup = SearchSysCache1(CONSTROID, 
ObjectIdGetDatum(foundObject.objectId));
+                                       if (!HeapTupleIsValid(conTup))
+                                               elog(ERROR, "cache lookup 
failed for constraint %u", foundObject.objectId);
+                                       con = (Form_pg_constraint) 
GETSTRUCT(conTup);
+
+                                       if (con->conrelid == 
RelationGetRelid(rel) &&
+                                               con->contype == 
CONSTRAINT_FOREIGN &&
+                                               (con->confupdtype == 
FKCONSTR_ACTION_SETNULL ||
+                                                con->confupdtype == 
FKCONSTR_ACTION_SETDEFAULT ||
+                                                con->confupdtype == 
FKCONSTR_ACTION_CASCADE ||
+                                                con->confdeltype == 
FKCONSTR_ACTION_SETNULL ||
+                                                con->confdeltype == 
FKCONSTR_ACTION_SETDEFAULT))
+                                               ereport(ERROR,
+                                                               
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                                                               errmsg("cannot 
convert column \"%s\" to generated", colName),
+                                                               
errdetail("Column \"%s\" is part of a foreign key with a restricted ON DELETE / 
ON UPDATE action.",
+                                                                               
  colName));
+
+                                       ReleaseSysCache(conTup);
+                                       break;
+                               }
+                       case TriggerRelationId:
+                               {
+                                       char       *invalidTrg;
+
+                                       invalidTrg = 
checkTriggerQualForAddGenStored(foundObject.objectId,
+                                                                               
                                                 PRS2_NEW_VARNO,
+                                                                               
                                                 attnum);
+
+                                       if (invalidTrg != NULL)
+                                               ereport(ERROR,
+                                                               
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                                                               errmsg("cannot 
convert column \"%s\" to generated", colName),
+                                                               
errdetail("Column NEW.\"%s\" is referenced by the WHEN clause of trigger 
\"%s\".",
+                                                                               
  colName, invalidTrg));
+
+                                       break;
+                               }
+                       default:
+                               /* Other dependencies aren't a problem. */
+                               break;
+               }
+       }
+
+       systable_endscan(scan);
+
+       /*
+        * Now scan again for dependencies on the whole row.
+        */
+       ScanKeyInit(&key[2],
+                               Anum_pg_depend_refobjsubid,
+                               BTEqualStrategyNumber, F_INT4EQ,
+                               Int32GetDatum(InvalidAttrNumber));
+
+       scan = systable_beginscan(pg_depend, DependReferenceIndexId, true,
+                                                         NULL, 3, key);
+
+       while (HeapTupleIsValid(depTup = systable_getnext(scan)))
+       {
+               Form_pg_depend dep = GETSTRUCT(depTup);
+               ObjectAddress foundObject;
+
+               foundObject.classId = dep->classid;
+               foundObject.objectId = dep->objid;
+               foundObject.objectSubId = dep->objsubid;
+
+               switch (foundObject.classId)
+               {
+                       case TriggerRelationId:
+                               {
+                                       char       *invalidTrg;
+
+                                       invalidTrg = 
checkTriggerQualForAddGenStored(foundObject.objectId,
+                                                                               
                                                 PRS2_NEW_VARNO,
+                                                                               
                                                 InvalidAttrNumber);
+
+                                       if (invalidTrg != NULL)
+                                               ereport(ERROR,
+                                                               
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                                                               errmsg("cannot 
convert column \"%s\" to generated", colName),
+                                                               errdetail("The 
WHEN clause of trigger \"%s\" references a row which encompasses NEW.\"%s\".",
+                                                                               
  invalidTrg, colName));
+
+                                       break;
+                               }
+                       default:
+                               /* Other dependencies aren't a problem. */
+                               break;
+               }
+       }
+
+       systable_endscan(scan);
+       table_close(pg_depend, AccessShareLock);
+}
+
+/*
+ * An update/insert before trigger's qual cannot reference a stored generated
+ * column. If the trigger is update/insert and a reference to the given
+ * varno/varattno is found in its qual expression, this returns the name of the
+ * trigger (so the caller can use it in the error message), NULL otherwise.
+ */
+static char *
+checkTriggerQualForAddGenStored(Oid trigger_oid, int varno, AttrNumber attnum)
+{
+       Relation        pg_trigger;
+       HeapTuple       tup;
+       Form_pg_trigger trg;
+       char       *result = NULL;
+
+       pg_trigger = table_open(TriggerRelationId, AccessShareLock);
+
+       tup = get_catalog_object_by_oid(pg_trigger,
+                                                                       
Anum_pg_trigger_oid, trigger_oid);
+       if (tup == NULL)
+               ereport(ERROR,
+                               errmsg_internal("could not find trigger %u", 
trigger_oid));
+
+       trg = (Form_pg_trigger) GETSTRUCT(tup);
+
+       if (TRIGGER_FOR_BEFORE(trg->tgtype) &&
+               (TRIGGER_FOR_UPDATE(trg->tgtype) ||
+                TRIGGER_FOR_INSERT(trg->tgtype)))
+       {
+               Datum           tgqual;
+               bool            tgqual_is_null;
+
+               tgqual = heap_getattr(tup, Anum_pg_trigger_tgqual,
+                                                         
RelationGetDescr(pg_trigger),
+                                                         &tgqual_is_null);
+
+               if (!tgqual_is_null)
+               {
+                       char       *expr_str;
+                       Node       *expr;
+                       Bitmapset  *refs = NULL;
+
+                       expr_str = TextDatumGetCString(tgqual);
+                       expr = stringToNode(expr_str);
+
+                       pull_varattnos(expr, varno, &refs);
+
+                       if (bms_is_member(attnum - 
FirstLowInvalidHeapAttributeNumber, refs))
+                       {
+                               result = pstrdup(NameStr(trg->tgname));
+                       }
+               }
+       }
+
+       heap_freetuple(tup);
+       table_close(pg_trigger, AccessShareLock);
+
+       return result;
+}
+
+/*
+ * Subroutine for ATExecAddGeneratedStored, used to determine whether the given
+ * constraint proves that the values are equal to some expression.
+ *
+ * Given a rel, a column and a constraint name, we look up a valid CHECK
+ * constraint on the rel, with the given name, with a specific shape:
+ *
+ *   CHECK ( pg_datum_image_equal(column, expr) )
+ *
+ * If a valid constraint is found, this returns the expr node, otherwise
+ * it returns null and sets an error code in *reason, allowing the caller to
+ * provide an appropriate error message.
+ */
+static Node *
+findUsableConstraintForAddGenStored(Relation rel, AttrNumber attnum,
+                                                                       const 
char *conname,
+                                                                       
AddGenConstrError *reason)
+{
+       Relation        pg_constraint;
+       HeapTuple       conTup;
+       SysScanDesc scan;
+       ScanKeyData key[3];
+       Node       *foundExpr;
+
+       pg_constraint = table_open(ConstraintRelationId, AccessShareLock);
+       ScanKeyInit(&key[0],
+                               Anum_pg_constraint_conrelid,
+                               BTEqualStrategyNumber, F_OIDEQ,
+                               ObjectIdGetDatum(rel->rd_id));
+       ScanKeyInit(&key[1],
+                               Anum_pg_constraint_contypid,
+                               BTEqualStrategyNumber, F_OIDEQ,
+                               ObjectIdGetDatum(InvalidOid));
+       ScanKeyInit(&key[2],
+                               Anum_pg_constraint_conname,
+                               BTEqualStrategyNumber, F_NAMEEQ,
+                               CStringGetDatum(conname));
+       scan = systable_beginscan(pg_constraint, 
ConstraintRelidTypidNameIndexId,
+                                                         true, NULL, 3, key);
+
+       foundExpr = NULL;
+       *reason = ADD_GEN_CONSTR_NOT_FOUND;
+
+       if (HeapTupleIsValid(conTup = systable_getnext(scan)))
+       {
+               Form_pg_constraint con = GETSTRUCT(conTup);
+
+               if (con->contype == CONSTRAINT_CHECK)
+               {
+                       /* !conenforced implies !convalidated, but let's be 
explicit */
+                       if (!con->convalidated || !con->conenforced)
+                       {
+                               *reason = ADD_GEN_CONSTR_NOT_VALID;
+                       }
+                       else
+                       {
+                               Datum           val;
+                               char       *conbin;
+                               Node       *conexpr;
+
+                               val = SysCacheGetAttrNotNull(CONSTROID, conTup,
+                                                                               
         Anum_pg_constraint_conbin);
+                               conbin = TextDatumGetCString(val);
+                               conexpr = stringToNode(conbin);
+
+                               foundExpr = matchDatumImageEqualOnVar(conexpr, 
attnum);
+                               if (foundExpr == NULL)
+                                       *reason = ADD_GEN_CONSTR_SHAPE_MISMATCH;
+                       }
+               }
+       }
+
+       systable_endscan(scan);
+       table_close(pg_constraint, AccessShareLock);
+
+       return foundExpr;
+}
+
+/*
+ * Given an expression tree and an attnum, this tries to match
+ *
+ *   pg_datum_image_equal(arg1, arg2)
+ *
+ * where either arg1 or arg2 is a Var referencing the given attno, returning
+ * the other argument.
+ */
+static Node *
+matchDatumImageEqualOnVar(Node *expr, AttrNumber attnum)
+{
+       if (IsA(expr, FuncExpr))
+       {
+               FuncExpr   *fexpr = (FuncExpr *) expr;
+
+               if (fexpr->funcid == F_PG_DATUM_IMAGE_EQUAL &&
+                       list_length(fexpr->args) == 2)
+               {
+                       Node       *left = linitial(fexpr->args),
+                                          *right = lsecond(fexpr->args);
+
+                       /* Support both orders of the operands */
+                       if (IsA(left, Var) && ((Var *) left)->varattno == 
attnum)
+                       {
+                               Assert(((Var *) left)->vartype == 
exprType(right));
+                               return right;
+                       }
+                       if (IsA(right, Var) && ((Var *) right)->varattno == 
attnum)
+                       {
+                               Assert(((Var *) right)->vartype == 
exprType(left));
+                               return left;
+                       }
+               }
+       }
+
+       return NULL;
+}
+
+/*
+ * Reconstruct a raw expression from a given cooked expression by deparsing it
+ * and running it through raw_parser().
+ */
+static Node *
+reconstructRawExpr(Relation rel, Node *cookedExpr)
+{
+       char       *deparsedExpr;
+       List       *ctx,
+                          *parseResult;
+       int                     nestLevel;
+
+       /*
+        * Some GUCs affect how values are represented in the deparsed tree. 
Make
+        * sure they're set such that we don't lose information in the 
round-trip
+        * through deparse_expression / raw_parser.
+        */
+       nestLevel = NewGUCNestLevel();
+
+       (void) set_config_option("datestyle", "ISO",
+                                                        PGC_USERSET, 
PGC_S_SESSION,
+                                                        GUC_ACTION_SAVE, true, 
0, false);
+       (void) set_config_option("extra_float_digits", "3",
+                                                        PGC_USERSET, 
PGC_S_SESSION,
+                                                        GUC_ACTION_SAVE, true, 
0, false);
+
+       ctx = deparse_context_for(RelationGetRelationName(rel),
+                                                         
RelationGetRelid(rel));
+
+       deparsedExpr = deparse_expression(cookedExpr, ctx, false, false);
+
+       parseResult = raw_parser(deparsedExpr, RAW_PARSE_PLPGSQL_EXPR);
+       if (list_length(parseResult) != 1)
+               ereport(ERROR,
+                               (errcode(ERRCODE_INTERNAL_ERROR),
+                                errmsg_internal("cannot re-parse constraint 
expr into a raw expression")));
+
+       if (IsA(linitial(parseResult), RawStmt))
+       {
+               RawStmt    *stmt = linitial(parseResult);
+
+               if (IsA(stmt->stmt, SelectStmt))
+               {
+                       SelectStmt *select = (SelectStmt *) stmt->stmt;
+
+                       if (list_length(select->targetList) == 1 &&
+                               IsA(linitial(select->targetList), ResTarget))
+                       {
+                               ResTarget  *resTarget = 
linitial(select->targetList);
+
+                               AtEOXact_GUC(true, nestLevel);
+                               return resTarget->val;
+                       }
+               }
+       }
+
+       ereport(ERROR,
+                       errcode(ERRCODE_INTERNAL_ERROR),
+                       errmsg_internal("re-parsed expr does not match the 
expected structure"));
+}
+
+/*
+ * ALTER COLUMN col ADD GENERATED USING CONSTRAINT name STORED
+ *
+ * Change a regular column into a stored generated column without a table
+ * rewrite, using the expression contained in the given constraint.
+ *
+ * The constraint must be a CHECK constraint proving that the expression is
+ * already satisfied by all the values in the column (see
+ * findUsableConstraintForAddGenStored).
+ */
+static ObjectAddress
+ATExecAddGeneratedStored(AlteredTableInfo *tab,
+                                                Relation rel,
+                                                const char *colName,
+                                                Constraint *def)
+{
+       Form_pg_class relTup;
+       HeapTuple       tuple;
+       Form_pg_attribute attTup;
+       AttrNumber      attnum;
+       Bitmapset  *colRefs;
+       bool            is_expr;
+       ObjectAddress address;
+       Relation        pg_attribute;
+       Node       *foundConstraintExpr;
+       AddGenConstrError reason;
+       Node       *newRawDefExpr;
+       RawColumnDefault *rawDefault;
+       List       *cookedResult;
+
+       Assert(def->raw_expr == NULL);
+       Assert(def->cooked_expr == NULL);
+       Assert(def->conname != NULL);
+       Assert(def->generated_when == ATTRIBUTE_IDENTITY_ALWAYS);
+       Assert(def->generated_kind == ATTRIBUTE_GENERATED_STORED);
+
+       relTup = RelationGetForm(rel);
+       if (relTup->reloftype != InvalidOid)
+               ereport(ERROR,
+                               errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                               errmsg("generated columns are not supported on 
typed tables"));
+
+       tuple = SearchSysCacheCopyAttName(RelationGetRelid(rel), colName);
+       if (!HeapTupleIsValid(tuple))
+               ereport(ERROR,
+                               (errcode(ERRCODE_UNDEFINED_COLUMN),
+                                errmsg("column \"%s\" of relation \"%s\" does 
not exist",
+                                               colName, 
RelationGetRelationName(rel))));
+
+       attTup = (Form_pg_attribute) GETSTRUCT(tuple);
+
+       attnum = attTup->attnum;
+       if (attnum <= 0)
+               ereport(ERROR,
+                               (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                                errmsg("cannot alter system column \"%s\"",
+                                               colName)));
+
+       if (attTup->attidentity)
+               ereport(ERROR,
+                               
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                                errmsg("cannot convert column \"%s\" to 
generated", colName),
+                                errdetail("Column \"%s\" of relation \"%s\" is 
an identity column.",
+                                                  colName, 
RelationGetRelationName(rel))));
+
+       if (attTup->attgenerated)
+               ereport(ERROR,
+                               
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                                errmsg("cannot convert column \"%s\" to 
generated", colName),
+                                errdetail("Column \"%s\" of relation \"%s\" is 
already a generated column.",
+                                                  colName, 
RelationGetRelationName(rel))));
+
+       if (attTup->atthasmissing)
+               ereport(ERROR,
+                               
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                                errmsg("cannot convert column \"%s\" to 
generated", colName),
+                                errdetail("Cannot ensure that column \"%s\" of 
relation \"%s\" has been completely backfilled.",
+                                                  colName, 
RelationGetRelationName(rel)),
+                                errhint("Drop the column and create it again 
without a DEFAULT value.")));
+
+       /*
+        * This column might be referenced directly in a partition key, or 
through
+        * a whole-row expression.
+        */
+       colRefs = bms_make_singleton(attnum - 
FirstLowInvalidHeapAttributeNumber);
+       colRefs = bms_add_member(colRefs, 0 - 
FirstLowInvalidHeapAttributeNumber);
+       if (has_partition_attrs(rel, colRefs, &is_expr))
+               ereport(ERROR,
+                               
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                                errmsg("cannot convert column \"%s\" to 
generated", colName),
+                                errdetail("Column \"%s\" is referenced in the 
partition key of relation \"%s\".",
+                                                  colName, 
RelationGetRelationName(rel))));
+
+       checkDependenciesForAddGenStored(rel, attnum, colName);
+
+       /*
+        * Now, try to find the constraint by name, and see if it has the
+        * necessary structure to prove that the values are consistent.
+        */
+       foundConstraintExpr = findUsableConstraintForAddGenStored(rel, attnum,
+                                                                               
                                          def->conname, &reason);
+       if (foundConstraintExpr == NULL)
+       {
+               if (reason == ADD_GEN_CONSTR_NOT_VALID)
+                       ereport(ERROR,
+                                       
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                                       errmsg("cannot convert column \"%s\" to 
generated", colName),
+                                       errdetail("The constraint \"%s\" is not 
valid.", def->conname));
+               if (reason == ADD_GEN_CONSTR_SHAPE_MISMATCH)
+                       ereport(ERROR,
+                                       
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                                       errmsg("cannot convert column \"%s\" to 
generated", colName),
+                                       errdetail("The constraint \"%s\" is not 
of the form CHECK ( pg_datum_image_equal(\"%s\", expr) ).",
+                                                         def->conname, 
colName));
+               ereport(ERROR,
+                               
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                               errmsg("cannot convert column \"%s\" to 
generated", colName),
+                               errdetail("Could not find CHECK constraint 
\"%s\".", def->conname));
+       }
+
+       /* Mark as generated stored in pg_attribute */
+       pg_attribute = table_open(AttributeRelationId, RowExclusiveLock);
+       attTup->attgenerated = ATTRIBUTE_GENERATED_STORED;
+       CatalogTupleUpdate(pg_attribute, &tuple->t_self, tuple);
+       table_close(pg_attribute, RowExclusiveLock);
+
+       heap_freetuple(tuple);
+
+       /* Make above changes visible */
+       CommandCounterIncrement();
+
+       /* Recover a raw parse tree for the expression found in the constraint 
*/
+       newRawDefExpr = reconstructRawExpr(rel, foundConstraintExpr);
+
+       /*
+        * Remove previous default value, if any, and store the new generator
+        * expression.
+        */
+       RemoveAttrDefault(RelationGetRelid(rel), attnum, DROP_RESTRICT,
+                                         false, false);
+
+       rawDefault = palloc0_object(RawColumnDefault);
+       rawDefault->attnum = attnum;
+       rawDefault->raw_default = newRawDefExpr;
+       rawDefault->generated = ATTRIBUTE_GENERATED_STORED;
+
+       cookedResult = AddRelationNewConstraints(rel, list_make1(rawDefault), 
NIL,
+                                                                               
         false /* allow_merge */ ,
+                                                                               
         true /* is_local */ ,
+                                                                               
         false /* is_internal */ ,
+                                                                               
         NULL /* queryString */ );
+
+       if (list_length(cookedResult) != 1)
+               ereport(ERROR,
+                               (errcode(ERRCODE_INTERNAL_ERROR),
+                                errmsg_internal("cannot store constraint as 
default value")));
+
+       InvokeObjectPostAlterHook(RelationRelationId,
+                                                         
RelationGetRelid(rel), attnum);
+
+       ObjectAddressSubSet(address, RelationRelationId,
+                                               RelationGetRelid(rel), attnum);
+       return address;
+}
+
 /*
  * ALTER TABLE ALTER COLUMN DROP EXPRESSION
  */
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index 0563453fe24..55a8807c2de 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -2642,6 +2642,24 @@ alter_table_cmd:
                                        n->name = $3;
                                        n->def = (Node *) c;
 
+                                       $$ = (Node *) n;
+                               }
+                       /* ALTER TABLE <name> ALTER [COLUMN] <colname> ADD 
GENERATED USING CONSTRAINT constraint_name STORED */
+                       | ALTER opt_column ColId ADD_P GENERATED USING 
CONSTRAINT name STORED
+                               {
+                                       AlterTableCmd *n = 
makeNode(AlterTableCmd);
+                                       Constraint *c = makeNode(Constraint);
+
+                                       c->conname = $8;
+                                       c->contype = CONSTR_GENERATED;
+                                       c->generated_when = 
ATTRIBUTE_IDENTITY_ALWAYS;
+                                       c->generated_kind = 
ATTRIBUTE_GENERATED_STORED;
+                                       c->location = @8;
+
+                                       n->subtype = AT_AddGeneratedStored;
+                                       n->name = $3;
+                                       n->def = (Node *) c;
+
                                        $$ = (Node *) n;
                                }
                        /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET 
<sequence options>/RESET */
diff --git a/src/bin/psql/t/010_tab_completion.pl 
b/src/bin/psql/t/010_tab_completion.pl
index 1d2e5f5b92a..32871f14aad 100644
--- a/src/bin/psql/t/010_tab_completion.pl
+++ b/src/bin/psql/t/010_tab_completion.pl
@@ -44,7 +44,10 @@ $node->safe_psql('postgres',
          . "CREATE TABLE mytab246 (f1 int, f2 text);\n"
          . "CREATE TABLE \"mixedName\" (f1 int, f2 text);\n"
          . "CREATE TYPE enum1 AS ENUM ('foo', 'bar', 'baz', 'BLACK');\n"
-         . "CREATE PUBLICATION some_publication;\n");
+         . "CREATE PUBLICATION some_publication;\n"
+         . "CREATE TABLE gencol_test (a int primary key, b int);\n"
+         . "ALTER TABLE gencol_test ADD CONSTRAINT check_gen CHECK 
(pg_datum_image_equal(b, a + 1));\n"
+);
 
 # In a VPATH build, we'll be started in the source directory, but we want
 # to run in the build directory so that we can use relative paths to
@@ -422,6 +425,23 @@ check_completion(
 
 clear_line();
 
+check_completion("ALTER TABLE gencol_test ALTER COLUMN b A\t", qr/ADD /,
+       "complete ALTER COLUMN <col> ADD");
+
+check_completion("G\t", qr/GENERATED /,
+       "complete ALTER COLUMN <col> ADD GENERATED");
+
+check_completion("U\t", qr/USING CONSTRAINT /,
+       "complete ALTER COLUMN <col> ADD GENERATED USING CONSTRAINT");
+
+check_completion("\t", qr/check_gen /,
+       "complete ALTER COLUMN <col> ADD GENERATED USING CONSTRAINT offers 
check constraint names");
+
+check_completion("S\t", qr/STORED /,
+       "complete ALTER COLUMN <col> ADD GENERATED USING CONSTRAINT constr_name 
STORED");
+
+clear_query();
+
 # send psql an explicit \q to shut it down, else pty won't close properly
 $h->quit or die "psql returned $?";
 
diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index 96de78cd087..00fc9d20950 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -489,6 +489,15 @@ static const SchemaQuery 
Query_for_constraint_of_table_not_validated = {
        .refnamespace = "c1.relnamespace",
 };
 
+static const SchemaQuery Query_for_check_constraint_of_table = {
+       .catname = "pg_catalog.pg_constraint con, pg_catalog.pg_class c1",
+       .selcondition = "con.conrelid=c1.oid and con.contype='c'",
+       .result = "con.conname",
+       .refname = "c1.relname",
+       .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
+       .refnamespace = "c1.relnamespace",
+};
+
 static const SchemaQuery Query_for_constraint_of_type = {
        .catname = "pg_catalog.pg_constraint con, pg_catalog.pg_type t",
        .selcondition = "con.contypid=t.oid",
@@ -2951,13 +2960,39 @@ match_previous_words(int pattern_id,
        /* ALTER TABLE ALTER [COLUMN] <foo> ADD GENERATED */
        else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", 
MatchAny, "ADD", "GENERATED") ||
                         Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, 
"ADD", "GENERATED"))
-               COMPLETE_WITH("ALWAYS", "BY DEFAULT");
-       /* ALTER TABLE ALTER [COLUMN] <foo> ADD GENERATED */
+               COMPLETE_WITH("ALWAYS", "BY DEFAULT", "USING CONSTRAINT");
+       /* ALTER TABLE ALTER [COLUMN] <foo> ADD GENERATED ALWAYS */
        else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", 
MatchAny, "ADD", "GENERATED", "ALWAYS") ||
-                        Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, 
"ADD", "GENERATED", "ALWAYS") ||
-                        Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", 
MatchAny, "ADD", "GENERATED", "BY", "DEFAULT") ||
+                        Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, 
"ADD", "GENERATED", "ALWAYS"))
+               COMPLETE_WITH("AS IDENTITY");
+       /* ALTER TABLE ALTER [COLUMN] <foo> ADD GENERATED BY DEFAULT */
+       else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", 
MatchAny, "ADD", "GENERATED", "BY", "DEFAULT") ||
                         Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, 
"ADD", "GENERATED", "BY", "DEFAULT"))
                COMPLETE_WITH("AS IDENTITY");
+       /* ALTER TABLE ALTER [COLUMN] <foo> ADD GENERATED USING CONSTRAINT */
+       else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", 
MatchAny, "ADD", "GENERATED", "USING", "CONSTRAINT"))
+       {
+               set_completion_reference(prev8_wd);
+               COMPLETE_WITH_SCHEMA_QUERY(Query_for_check_constraint_of_table);
+       }
+
+       /*
+        * ALTER TABLE ALTER [COLUMN] <foo> ADD GENERATED USING CONSTRAINT
+        */
+       else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "ADD", 
"GENERATED", "USING", "CONSTRAINT"))
+       {
+               set_completion_reference(prev7_wd);
+               COMPLETE_WITH_SCHEMA_QUERY(Query_for_check_constraint_of_table);
+       }
+
+       /*
+        * ALTER TABLE ALTER [COLUMN] <foo> ADD GENERATED USING CONSTRAINT
+        * constr_name
+        */
+       else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", 
MatchAny, "ADD", "GENERATED", "USING", "CONSTRAINT", MatchAny))
+               COMPLETE_WITH("STORED");
+       else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "ADD", 
"GENERATED", "USING", "CONSTRAINT", MatchAny))
+               COMPLETE_WITH("STORED");
        /* ALTER TABLE ALTER [COLUMN] <foo> SET */
        else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", 
MatchAny, "SET") ||
                         Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, 
"SET"))
diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h
index 5be1d4a944b..c40baa3cd4a 100644
--- a/src/include/nodes/parsenodes.h
+++ b/src/include/nodes/parsenodes.h
@@ -2467,6 +2467,7 @@ typedef enum AlterTableType
        AT_CookedColumnDefault,         /* add a pre-cooked column default */
        AT_DropNotNull,                         /* alter column drop not null */
        AT_SetNotNull,                          /* alter column set not null */
+       AT_AddGeneratedStored,          /* add generated using constraint */
        AT_SetExpression,                       /* alter column set expression 
*/
        AT_DropExpression,                      /* alter column drop expression 
*/
        AT_SetStatistics,                       /* alter column set statistics 
*/
diff --git a/src/test/modules/injection_points/Makefile 
b/src/test/modules/injection_points/Makefile
index a7a16d5016a..9317925c11a 100644
--- a/src/test/modules/injection_points/Makefile
+++ b/src/test/modules/injection_points/Makefile
@@ -11,7 +11,7 @@ PGFILEDESC = "injection_points - facility for injection 
points"
 
 EXTRA_INSTALL = contrib/test_decoding
 
-REGRESS = injection_points hashagg portal_bind reindex_conc vacuum
+REGRESS = injection_points hashagg portal_bind reindex_conc vacuum alter_table
 REGRESS_OPTS = --dlpath=$(top_builddir)/src/test/regress
 
 # wait_cleanup terminates another backend, whose FATAL message can be lost
diff --git a/src/test/modules/injection_points/expected/alter_table.out 
b/src/test/modules/injection_points/expected/alter_table.out
new file mode 100644
index 00000000000..5f53b2a29c3
--- /dev/null
+++ b/src/test/modules/injection_points/expected/alter_table.out
@@ -0,0 +1,36 @@
+-- Tests for ALTER TABLE
+CREATE EXTENSION injection_points;
+SELECT injection_points_set_local();
+ injection_points_set_local 
+----------------------------
+ 
+(1 row)
+
+SELECT injection_points_attach('alter-table-phase-3-rewrite', 'notice');
+ injection_points_attach 
+-------------------------
+ 
+(1 row)
+
+SELECT injection_points_attach('alter-table-phase-3-verify', 'notice');
+ injection_points_attach 
+-------------------------
+ 
+(1 row)
+
+CREATE SCHEMA testgen_inj;
+-- Check that the table isn't being rewritten nor scanned during phase 3,
+-- even if other objects depend on the column we are changing.
+CREATE TABLE testgen_inj.t1 (a INT, b INT NOT NULL);
+INSERT INTO testgen_inj.t1 (a, b) VALUES (1, 2);
+ALTER TABLE testgen_inj.t1 ADD CONSTRAINT c1 CHECK (b > 0);
+NOTICE:  notice triggered for injection point alter-table-phase-3-verify
+ALTER TABLE testgen_inj.t1 ADD CONSTRAINT c2 CHECK (pg_datum_image_equal(b, a 
* 2));
+NOTICE:  notice triggered for injection point alter-table-phase-3-verify
+CREATE INDEX ON testgen_inj.t1 (b);
+ALTER TABLE testgen_inj.t1 ALTER b
+    ADD GENERATED USING CONSTRAINT c2 STORED;
+-- we expect to *not* see a "alter-table-phase-3-*" notice here
+DROP TABLE testgen_inj.t1;
+DROP SCHEMA testgen_inj;
+DROP EXTENSION injection_points;
diff --git a/src/test/modules/injection_points/meson.build 
b/src/test/modules/injection_points/meson.build
index cf9cb219548..410eb0c461a 100644
--- a/src/test/modules/injection_points/meson.build
+++ b/src/test/modules/injection_points/meson.build
@@ -65,6 +65,7 @@ tests += {
       'portal_bind',
       'reindex_conc',
       'vacuum',
+      'alter_table',
     ],
     'regress_args': ['--dlpath', meson.project_build_root() / 
'src/test/regress'],
     # The injection points are cluster-wide, so disable installcheck
diff --git a/src/test/modules/injection_points/sql/alter_table.sql 
b/src/test/modules/injection_points/sql/alter_table.sql
new file mode 100644
index 00000000000..135204e1f7a
--- /dev/null
+++ b/src/test/modules/injection_points/sql/alter_table.sql
@@ -0,0 +1,24 @@
+-- Tests for ALTER TABLE
+CREATE EXTENSION injection_points;
+
+SELECT injection_points_set_local();
+
+SELECT injection_points_attach('alter-table-phase-3-rewrite', 'notice');
+SELECT injection_points_attach('alter-table-phase-3-verify', 'notice');
+
+CREATE SCHEMA testgen_inj;
+
+-- Check that the table isn't being rewritten nor scanned during phase 3,
+-- even if other objects depend on the column we are changing.
+CREATE TABLE testgen_inj.t1 (a INT, b INT NOT NULL);
+INSERT INTO testgen_inj.t1 (a, b) VALUES (1, 2);
+ALTER TABLE testgen_inj.t1 ADD CONSTRAINT c1 CHECK (b > 0);
+ALTER TABLE testgen_inj.t1 ADD CONSTRAINT c2 CHECK (pg_datum_image_equal(b, a 
* 2));
+CREATE INDEX ON testgen_inj.t1 (b);
+ALTER TABLE testgen_inj.t1 ALTER b
+    ADD GENERATED USING CONSTRAINT c2 STORED;
+-- we expect to *not* see a "alter-table-phase-3-*" notice here
+DROP TABLE testgen_inj.t1;
+DROP SCHEMA testgen_inj;
+
+DROP EXTENSION injection_points;
diff --git a/src/test/modules/test_ddl_deparse/test_ddl_deparse.c 
b/src/test/modules/test_ddl_deparse/test_ddl_deparse.c
index 105872a3517..47b8337307d 100644
--- a/src/test/modules/test_ddl_deparse/test_ddl_deparse.c
+++ b/src/test/modules/test_ddl_deparse/test_ddl_deparse.c
@@ -129,6 +129,9 @@ get_altertable_subcmdinfo(PG_FUNCTION_ARGS)
                        case AT_SetNotNull:
                                strtype = "SET NOT NULL";
                                break;
+                       case AT_AddGeneratedStored:
+                               strtype = "ADD GENERATED STORED";
+                               break;
                        case AT_SetExpression:
                                strtype = "SET EXPRESSION";
                                break;
diff --git a/src/test/regress/expected/alter_table.out 
b/src/test/regress/expected/alter_table.out
index f99b2b9e663..ef4d9b87861 100644
--- a/src/test/regress/expected/alter_table.out
+++ b/src/test/regress/expected/alter_table.out
@@ -5199,3 +5199,587 @@ drop publication pub1;
 drop schema alter1 cascade;
 drop schema alter2 cascade;
 NOTICE:  drop cascades to table alter2.t1
+-- Tests for ALTER COLUMN ... ADD GENERATED USING CONSTRAINT name STORED
+-- turning a regular column into a stored generated column without a rewrite
+create schema tgen;
+create table tgen.t1 (a int, b int);
+insert into tgen.t1 (a, b)
+  select x, x * 2 from generate_series(1, 5) x;
+begin;
+alter table tgen.t1 add constraint chk_gen check (pg_datum_image_equal(b, a * 
2));
+alter table tgen.t1 alter column b
+  add generated using constraint chk_gen stored;
+\d tgen.t1
+                               Table "tgen.t1"
+ Column |  Type   | Collation | Nullable |              Default               
+--------+---------+-----------+----------+------------------------------------
+ a      | integer |           |          | 
+ b      | integer |           |          | generated always as (a * 2) stored
+Check constraints:
+    "chk_gen" CHECK (pg_datum_image_equal(b, a * 2))
+
+insert into tgen.t1 (a, b) values (10, 20);
+ERROR:  cannot insert a non-DEFAULT value into column "b"
+DETAIL:  Column "b" is a generated column.
+rollback;
+-- test that we accept the constraint with the operands swapped, too
+begin;
+alter table tgen.t1 add constraint chk_gen check (pg_datum_image_equal(a * 2, 
b));
+alter table tgen.t1 alter column b
+    add generated using constraint chk_gen stored;
+\d tgen.t1
+                               Table "tgen.t1"
+ Column |  Type   | Collation | Nullable |              Default               
+--------+---------+-----------+----------+------------------------------------
+ a      | integer |           |          | 
+ b      | integer |           |          | generated always as (a * 2) stored
+Check constraints:
+    "chk_gen" CHECK (pg_datum_image_equal(a * 2, b))
+
+insert into tgen.t1 (a, b) values (10, 20);
+ERROR:  cannot insert a non-DEFAULT value into column "b"
+DETAIL:  Column "b" is a generated column.
+rollback;
+begin;
+update tgen.t1 set b = a;
+alter table tgen.t1 add constraint chk_gen check (pg_datum_image_equal(a, b));
+alter table tgen.t1 alter column b
+    add generated using constraint chk_gen stored;
+\d tgen.t1
+                             Table "tgen.t1"
+ Column |  Type   | Collation | Nullable |            Default             
+--------+---------+-----------+----------+--------------------------------
+ a      | integer |           |          | 
+ b      | integer |           |          | generated always as (a) stored
+Check constraints:
+    "chk_gen" CHECK (pg_datum_image_equal(a, b))
+
+insert into tgen.t1 (a, b) values (10, 10);
+ERROR:  cannot insert a non-DEFAULT value into column "b"
+DETAIL:  Column "b" is a generated column.
+rollback;
+-- check that neither the table nor indexes are rewritten
+begin;
+alter table tgen.t1
+    add constraint chk_gen check (pg_datum_image_equal(b, a * 2));
+create index t1_a_idx on tgen.t1 (a);
+select pg_relation_filenode('tgen.t1') as t1_filenode_before \gset
+select pg_relation_filenode('tgen.t1_a_idx') as t1_idx_filenode_before \gset
+alter table tgen.t1 alter column b
+  add generated using constraint chk_gen stored;
+select pg_relation_filenode('tgen.t1') as t1_filenode_after \gset
+select pg_relation_filenode('tgen.t1_a_idx') as t1_idx_filenode_after \gset
+select :t1_filenode_before = :t1_filenode_after as did_skip_table_rewrite,
+       :t1_idx_filenode_before = :t1_idx_filenode_after as 
did_skip_idx_rewrite;
+ did_skip_table_rewrite | did_skip_idx_rewrite 
+------------------------+----------------------
+ t                      | t
+(1 row)
+
+rollback;
+-- test the whole process for adding a stored generated column without
+-- long-lived exclusive locks. The t_migration database is intentionally
+-- left behind for pg_dump/pg_restore tests.
+create table tgen.t_migration (a int);
+select pg_relation_filenode('tgen.t_migration') as t_migration_filenode_before 
\gset
+insert into tgen.t_migration select x from generate_series(1, 5) x;
+-- test nulls, too
+insert into tgen.t_migration (a) values (null);
+alter table tgen.t_migration add column b int;
+-- take care of new and updated columns
+create function tgen.gen () returns trigger language plpgsql as $$
+begin
+  new.b = new.a * 2; return new;
+end
+$$;
+create trigger tgen_trig
+    before insert or update on tgen.t_migration
+    for each row execute function tgen.gen();
+-- add the constraint as not valid: enforced only for new and updated rows
+begin;
+alter table tgen.t_migration
+    add constraint chk_gen check (pg_datum_image_equal(b, a * 2)) not valid;
+select locktype, mode from pg_locks
+  where relation = 'tgen.t_migration'::regclass and granted;
+ locktype |        mode         
+----------+---------------------
+ relation | AccessExclusiveLock
+(1 row)
+
+commit;
+insert into tgen.t_migration (a) values (100), (200), (300);
+-- backfill existing rows at the appropriate pace
+update tgen.t_migration set b = a * 2 where b is null;
+-- validate: this scans the table, but without an exclusive lock
+begin;
+alter table tgen.t_migration validate constraint chk_gen;
+select locktype, mode from pg_locks
+  where relation = 'tgen.t_migration'::regclass and granted;
+ locktype |           mode           
+----------+--------------------------
+ relation | ShareUpdateExclusiveLock
+(1 row)
+
+commit;
+-- now the schema update, which doesn't need to rewrite the table thanks to
+-- the constraint
+begin;
+alter table tgen.t_migration alter column b
+    add generated using constraint chk_gen stored;
+select locktype, mode from pg_locks
+where relation = 'tgen.t_migration'::regclass and granted;
+ locktype |        mode         
+----------+---------------------
+ relation | AccessExclusiveLock
+(1 row)
+
+commit;
+select pg_relation_filenode('tgen.t_migration') as t_migration_filenode_after 
\gset
+select :t_migration_filenode_before = :t_migration_filenode_after as 
did_skip_rewrite;
+ did_skip_rewrite 
+------------------
+ t
+(1 row)
+
+select * from tgen.t_migration;
+  a  |  b  
+-----+-----
+ 100 | 200
+ 200 | 400
+ 300 | 600
+   1 |   2
+   2 |   4
+   3 |   6
+   4 |   8
+   5 |  10
+     |    
+(9 rows)
+
+-- verify that it's still possible to insert rows (the trigger is still
+-- installed at this point)
+insert into tgen.t_migration (a) values (400);
+insert into tgen.t_migration (a) values (500);
+\d tgen.t_migration
+                           Table "tgen.t_migration"
+ Column |  Type   | Collation | Nullable |              Default               
+--------+---------+-----------+----------+------------------------------------
+ a      | integer |           |          | 
+ b      | integer |           |          | generated always as (a * 2) stored
+Check constraints:
+    "chk_gen" CHECK (pg_datum_image_equal(b, a * 2))
+Triggers:
+    tgen_trig BEFORE INSERT OR UPDATE ON tgen.t_migration FOR EACH ROW EXECUTE 
FUNCTION tgen.gen()
+
+select * from tgen.t_migration order by a nulls first;
+  a  |  b   
+-----+------
+     |     
+   1 |    2
+   2 |    4
+   3 |    6
+   4 |    8
+   5 |   10
+ 100 |  200
+ 200 |  400
+ 300 |  600
+ 400 |  800
+ 500 | 1000
+(11 rows)
+
+-- test support for partitioned tables
+create table tgen.tpart (a int, b int) partition by hash (a);
+alter table tgen.tpart
+    add constraint chk_gen check (pg_datum_image_equal(b, a * 2));
+create table tgen.tpart_p1 partition of tgen.tpart
+    for values with (modulus 2, remainder 0);
+create table tgen.tpart_p2 partition of tgen.tpart
+    for values with (modulus 2, remainder 1);
+insert into tgen.tpart (a, b) select x, x * 2 from generate_series(1, 5) x;
+-- altering a single partition is not allowed
+alter table tgen.tpart_p1 alter column b
+    add generated using constraint chk_gen stored;
+ERROR:  cannot convert column "b" to generated
+DETAIL:  Converting only part of a partitioning or inheritance hierarchy is 
not supported.
+HINT:  Use this command on the root table without specifying ONLY.
+-- altering only the parent table is not allowed
+alter table only tgen.tpart alter column b
+    add generated using constraint chk_gen stored;
+ERROR:  cannot convert column "b" to generated
+DETAIL:  Converting only part of a partitioning or inheritance hierarchy is 
not supported.
+HINT:  Use this command on the root table without specifying ONLY.
+-- altering the parent table, recursing
+alter table tgen.tpart alter column b
+    add generated using constraint chk_gen stored;
+-- expected: all the partitions have been changed
+\d tgen.tpart
+                        Partitioned table "tgen.tpart"
+ Column |  Type   | Collation | Nullable |              Default               
+--------+---------+-----------+----------+------------------------------------
+ a      | integer |           |          | 
+ b      | integer |           |          | generated always as (a * 2) stored
+Partition key: HASH (a)
+Check constraints:
+    "chk_gen" CHECK (pg_datum_image_equal(b, a * 2))
+Number of partitions: 2 (Use \d+ to list them.)
+
+\d tgen.tpart_p1
+                            Table "tgen.tpart_p1"
+ Column |  Type   | Collation | Nullable |              Default               
+--------+---------+-----------+----------+------------------------------------
+ a      | integer |           |          | 
+ b      | integer |           |          | generated always as (a * 2) stored
+Partition of: tgen.tpart FOR VALUES WITH (modulus 2, remainder 0)
+Check constraints:
+    "chk_gen" CHECK (pg_datum_image_equal(b, a * 2))
+
+\d tgen.tpart_p2
+                            Table "tgen.tpart_p2"
+ Column |  Type   | Collation | Nullable |              Default               
+--------+---------+-----------+----------+------------------------------------
+ a      | integer |           |          | 
+ b      | integer |           |          | generated always as (a * 2) stored
+Partition of: tgen.tpart FOR VALUES WITH (modulus 2, remainder 1)
+Check constraints:
+    "chk_gen" CHECK (pg_datum_image_equal(b, a * 2))
+
+-- tpart intentionally left behind for pg_dump/pg_restore tests
+-- test support for inheritance and subpartitions
+create table tgen.root (a int, b int, c int,
+    constraint chk_gen check ( pg_datum_image_equal(c, a+b) ));
+create table tgen.intermediate () inherits (tgen.root);
+create table tgen.leaf () inherits (tgen.intermediate);
+-- it's only allowed to change the whole hierarchy at once...
+begin;
+alter table tgen.root alter column c
+    add generated using constraint chk_gen stored;
+rollback;
+-- ... hence all these should result in an error
+alter table only tgen.root alter column c
+    add generated using constraint chk_gen stored;
+ERROR:  cannot convert column "c" to generated
+DETAIL:  Converting only part of a partitioning or inheritance hierarchy is 
not supported.
+HINT:  Use this command on the root table without specifying ONLY.
+alter table tgen.intermediate alter column c
+    add generated using constraint chk_gen stored;
+ERROR:  cannot convert column "c" to generated
+DETAIL:  Converting only part of a partitioning or inheritance hierarchy is 
not supported.
+HINT:  Use this command on the root table without specifying ONLY.
+alter table only tgen.intermediate alter column c
+    add generated using constraint chk_gen stored;
+ERROR:  cannot convert column "c" to generated
+DETAIL:  Converting only part of a partitioning or inheritance hierarchy is 
not supported.
+HINT:  Use this command on the root table without specifying ONLY.
+alter table tgen.leaf alter column c
+    add generated using constraint chk_gen stored;
+ERROR:  cannot convert column "c" to generated
+DETAIL:  Converting only part of a partitioning or inheritance hierarchy is 
not supported.
+HINT:  Use this command on the root table without specifying ONLY.
+alter table only tgen.leaf alter column c
+    add generated using constraint chk_gen stored;
+ERROR:  cannot convert column "c" to generated
+DETAIL:  Converting only part of a partitioning or inheritance hierarchy is 
not supported.
+HINT:  Use this command on the root table without specifying ONLY.
+drop table tgen.root cascade;
+NOTICE:  drop cascades to 2 other objects
+DETAIL:  drop cascades to table tgen.intermediate
+drop cascades to table tgen.leaf
+-- test multiple inheritance:
+-- we can't transform a whole hierarchy if some nodes in the graphs isn't an
+-- inheritor of the root node we're looking at
+create table tgen.root1 (a int);
+create table tgen.root2 (a int);
+create table tgen.intermediate1 () inherits (tgen.root1, tgen.root2);
+NOTICE:  merging multiple inherited definitions of column "a"
+create table tgen.intermediate2 () inherits (tgen.root1);
+create table tgen.leaf1 () inherits (tgen.intermediate1, tgen.intermediate2);
+NOTICE:  merging multiple inherited definitions of column "a"
+alter table tgen.root1 add constraint chk_gen check (pg_datum_image_equal(a, 
1));
+NOTICE:  merging constraint "chk_gen" with inherited definition
+alter table tgen.root1 alter a add generated using constraint chk_gen stored;
+ERROR:  cannot convert column "a" to generated
+DETAIL:  Converting only part of a multiple-inheritance hierarchy is not 
supported.
+HINT:  Run this command on a root node which covers the whole hierarchy, if 
there is one.
+drop table tgen.root1 cascade;
+NOTICE:  drop cascades to 3 other objects
+DETAIL:  drop cascades to table tgen.intermediate1
+drop cascades to table tgen.intermediate2
+drop cascades to table tgen.leaf1
+drop table tgen.root2;
+-- supported if we can transform the whole hierarchy starting at the root node
+create table tgen.root1 (a int);
+create table tgen.intermediate1 () inherits (tgen.root1);
+create table tgen.intermediate2 () inherits (tgen.root1);
+create table tgen.leaf1 () inherits (tgen.intermediate1, tgen.intermediate2);
+NOTICE:  merging multiple inherited definitions of column "a"
+alter table tgen.root1 add constraint chk_gen check (pg_datum_image_equal(a, 
1));
+NOTICE:  merging constraint "chk_gen" with inherited definition
+alter table tgen.root1 alter a add generated using constraint chk_gen stored;
+drop table tgen.root1 cascade;
+NOTICE:  drop cascades to 3 other objects
+DETAIL:  drop cascades to table tgen.intermediate1
+drop cascades to table tgen.intermediate2
+drop cascades to table tgen.leaf1
+-- tests for invalid invocations
+-- invalid: only supports STORED. The following are syntax errors.
+alter table tgen.t1 alter column b add generated using constraint chk_gen;
+ERROR:  syntax error at or near ";"
+LINE 1: ...en.t1 alter column b add generated using constraint chk_gen;
+                                                                      ^
+alter table tgen.t1 alter column b add generated using constraint chk_gen 
virtual;
+ERROR:  syntax error at or near "virtual"
+LINE 1: ...ter column b add generated using constraint chk_gen virtual;
+                                                               ^
+-- invalid: b is already a generated column
+begin;
+alter table tgen.t1 add constraint chk_gen check (pg_datum_image_equal(b, a * 
2));
+alter table tgen.t1 alter b add generated using constraint chk_gen stored;
+alter table tgen.t1 alter b add generated using constraint chk_gen stored;
+ERROR:  cannot convert column "b" to generated
+DETAIL:  Column "b" of relation "t1" is already a generated column.
+rollback;
+-- invalid: b is already a generated column
+create table tgen.t2 (a int, b int not null generated always as (a * 2) 
stored);
+alter table tgen.t2 add constraint chk_gen check (pg_datum_image_equal(b, a * 
2));
+alter table tgen.t2 alter column b add generated using constraint chk_gen 
stored;
+ERROR:  cannot convert column "b" to generated
+DETAIL:  Column "b" of relation "t2" is already a generated column.
+drop table tgen.t2;
+-- invalid: b is an identity column
+create table tgen.t2 (a int, b int generated always as identity);
+alter table tgen.t2 add constraint chk_gen check (pg_datum_image_equal(b, 1));
+alter table tgen.t2 alter column b add generated using constraint chk_gen 
stored;
+ERROR:  cannot convert column "b" to generated
+DETAIL:  Column "b" of relation "t2" is an identity column.
+drop table tgen.t2;
+create table tgen.t2 (a int, b int generated by default as identity);
+alter table tgen.t2 add constraint chk_gen check (pg_datum_image_equal(b, 1));
+alter table tgen.t2 alter column b add generated using constraint chk_gen 
stored;
+ERROR:  cannot convert column "b" to generated
+DETAIL:  Column "b" of relation "t2" is an identity column.
+drop table tgen.t2;
+-- invalid: b is a serial column
+create table tgen.t2 (a bigint, b bigserial);
+alter table tgen.t2 add constraint chk_gen check (pg_datum_image_equal(b, 
1::bigint));
+alter table tgen.t2 alter column b add generated using constraint chk_gen 
stored;
+ERROR:  cannot convert column "b" to generated
+DETAIL:  Column "b" of relation "t2" is a serial column (depends on "sequence 
tgen.t2_b_seq").
+drop table tgen.t2;
+-- invalid: c is referenced by another generated column
+create table tgen.t2 (a int, b int generated always as (c + 1), c int);
+alter table tgen.t2 add constraint chk_gen check (pg_datum_image_equal(c, 1));
+alter table tgen.t2 alter column c add generated using constraint chk_gen 
stored;
+ERROR:  cannot convert column "c" to generated
+DETAIL:  Column "c" is referenced by generated column "b".
+drop table tgen.t2;
+-- invalid: c references another generated column
+create table tgen.t2 (a int, b int generated always as (a + 1), c int);
+alter table tgen.t2 add constraint chk_gen check (pg_datum_image_equal(c, b + 
1));
+alter table tgen.t2 alter column c add generated using constraint chk_gen 
stored;
+ERROR:  cannot use generated column "b" in column generation expression
+DETAIL:  A generated column cannot reference another generated column.
+drop table tgen.t2;
+-- invalid: b is referenced in a partition key
+create table tgen.t2 (a int, b int not null) partition by hash (b);
+alter table tgen.t2 add constraint chk_gen check (pg_datum_image_equal(b, a + 
1));
+alter table tgen.t2 alter b add generated using constraint chk_gen stored;
+ERROR:  cannot convert column "b" to generated
+DETAIL:  Column "b" is referenced in the partition key of relation "t2".
+drop table tgen.t2;
+-- test for a whole-row reference
+-- since it's not possible to reference schema.table in partition by range,
+-- temporarily hack the search_path
+-- the two tables are intentionally left behind for pg_dump/pg_restore tests
+show search_path \gset
+set search_path to tgen, public;
+create table t_wholerow_1 (a int, b int) partition by range ((t_wholerow_1));
+alter table tgen.t_wholerow_1 add constraint chk_gen check 
(pg_datum_image_equal(b, a + 1));
+alter table tgen.t_wholerow_1 alter b add generated using constraint chk_gen 
stored;
+ERROR:  cannot convert column "b" to generated
+DETAIL:  Column "b" is referenced in the partition key of relation 
"t_wholerow_1".
+create table t_wholerow_2 (a int, b int) partition by range ((t_wholerow_2 is 
null));
+alter table tgen.t_wholerow_2 add constraint chk_gen check 
(pg_datum_image_equal(b, a + 1));
+alter table tgen.t_wholerow_2 alter b add generated using constraint chk_gen 
stored;
+ERROR:  cannot convert column "b" to generated
+DETAIL:  Column "b" is referenced in the partition key of relation 
"t_wholerow_2".
+set search_path to :search_path;
+-- invalid: b is referenced in a triggers's WHEN (before, update or insert)
+begin;
+create function t_trigger_f() returns trigger as $$ begin return null; end $$ 
language plpgsql;
+create trigger t_trigger before insert or update on tgen.t1
+    for each row when (new.b is null) execute function t_trigger_f();
+alter table tgen.t1
+    add constraint chk_gen check (pg_datum_image_equal(b, a * 2));
+alter table tgen.t1 alter column b
+    add generated using constraint chk_gen stored;
+ERROR:  cannot convert column "b" to generated
+DETAIL:  Column NEW."b" is referenced by the WHEN clause of trigger 
"t_trigger".
+rollback;
+-- invalid: b is referenced in a triggers's WHEN through a whole-row var
+-- (before, update or insert)
+begin;
+create function t_trigger_f() returns trigger as $$ begin return null; end $$ 
language plpgsql;
+create trigger t_trigger before insert or update on tgen.t1
+    for each row when (new is null) execute function t_trigger_f();
+alter table tgen.t1
+    add constraint chk_gen check (pg_datum_image_equal(b, a * 2));
+alter table tgen.t1 alter column b
+    add generated using constraint chk_gen stored;
+ERROR:  cannot convert column "b" to generated
+DETAIL:  The WHEN clause of trigger "t_trigger" references a row which 
encompasses NEW."b".
+rollback;
+-- OK to have a reference in an after trigger
+begin;
+create function t_trigger_f() returns trigger as $$ begin return null; end $$ 
language plpgsql;
+create trigger t_trigger after insert or update on tgen.t1
+    for each row when (new.b is null) execute function t_trigger_f();
+alter table tgen.t1
+    add constraint chk_gen check (pg_datum_image_equal(b, a * 2));
+alter table tgen.t1 alter column b
+    add generated using constraint chk_gen stored;
+rollback;
+-- not allowed on a typed table
+create type tgen.ttype as (a int);
+create table tgen.ttab of tgen.ttype;
+alter table tgen.ttab add constraint chk_gen check (pg_datum_image_equal(a, 
1));
+alter table tgen.ttab alter a add generated using constraint chk_gen stored;
+ERROR:  generated columns are not supported on typed tables
+-- fails when the constraint does not exist
+alter table tgen.t1 alter column b
+    add generated using constraint chk_gen_does_not_exist stored;
+ERROR:  cannot convert column "b" to generated
+DETAIL:  Could not find CHECK constraint "chk_gen_does_not_exist".
+-- fails when the constraint is not valid
+begin;
+alter table tgen.t1
+    add constraint chk_gen check (pg_datum_image_equal(b, a * 2)) not valid;
+alter table tgen.t1 alter column b
+    add generated using constraint chk_gen stored;
+ERROR:  cannot convert column "b" to generated
+DETAIL:  The constraint "chk_gen" is not valid.
+rollback;
+-- fails when the constraint is not enforced
+begin;
+alter table tgen.t1
+    add constraint chk_gen check (pg_datum_image_equal(b, a * 2)) not enforced;
+alter table tgen.t1 alter column b
+    add generated using constraint chk_gen stored;
+ERROR:  cannot convert column "b" to generated
+DETAIL:  The constraint "chk_gen" is not valid.
+rollback;
+-- fails when the constraint exists but doesn't have the expected shape
+begin;
+alter table tgen.t1 add constraint chk_gen check (b = a * 2);
+alter table tgen.t1 alter column b add generated using constraint chk_gen 
stored;
+ERROR:  cannot convert column "b" to generated
+DETAIL:  The constraint "chk_gen" is not of the form CHECK ( 
pg_datum_image_equal("b", expr) ).
+rollback;
+begin;
+alter table tgen.t1 add constraint chk_gen check (b >= a * 2);
+alter table tgen.t1 alter column b add generated using constraint chk_gen 
stored;
+ERROR:  cannot convert column "b" to generated
+DETAIL:  The constraint "chk_gen" is not of the form CHECK ( 
pg_datum_image_equal("b", expr) ).
+rollback;
+create table tgen.t2 (a int, b int);
+-- invalid: expr must be immutable
+alter table tgen.t2 add constraint chk_gen check (pg_datum_image_equal(b, (a + 
random())::int));
+alter table tgen.t2 alter column b
+    add generated using constraint chk_gen stored;
+ERROR:  generation expression is not immutable
+drop table tgen.t2;
+drop table tgen.t1;
+-- invalid: the column is on the referencing side of a foreign key with
+-- on delete or on update actions
+create table tgen.tpkey (a int primary key);
+create table tgen.tfkey (a int primary key, b int references tgen.tpkey(a) on 
delete set null);
+alter table tgen.tfkey add constraint chk_gen check (pg_datum_image_equal(b, a 
+ 1));
+alter table tgen.tfkey alter column b add generated using constraint chk_gen 
stored;
+ERROR:  cannot convert column "b" to generated
+DETAIL:  Column "b" is part of a foreign key with a restricted ON DELETE / ON 
UPDATE action.
+drop table tgen.tfkey;
+create table tgen.tfkey (a int primary key, b int references tgen.tpkey(a) on 
update cascade);
+alter table tgen.tfkey add constraint chk_gen check (pg_datum_image_equal(b, a 
+ 1));
+alter table tgen.tfkey alter column b add generated using constraint chk_gen 
stored;
+ERROR:  cannot convert column "b" to generated
+DETAIL:  Column "b" is part of a foreign key with a restricted ON DELETE / ON 
UPDATE action.
+drop table tgen.tfkey;
+-- OK to have the stored generated column on the referenced side
+create table tgen.tfkey (a int primary key, b int references tgen.tpkey(a) on 
update cascade);
+alter table tgen.tpkey add constraint chk_gen check (pg_datum_image_equal(a, 
1));
+alter table tgen.tpkey alter a add generated using constraint chk_gen stored;
+drop table tgen.tfkey;
+drop table tgen.tpkey;
+-- some GUCs affect how values are represented at deparse time: ensure we can
+-- reconstruct the generator expression properly when they're set.
+select setting as prev_extra_float_digits from pg_settings where name = 
'extra_float_digits' \gset
+select setting as prev_datestyle from pg_settings where name = 'DateStyle' 
\gset
+set extra_float_digits = 1;
+set datestyle = ISO;
+create table tgen.t_roundtrip (a_float8 float8, a_ts timestamp);
+alter table tgen.t_roundtrip
+    add constraint c1 check (pg_datum_image_equal(a_float8, 
'3.141592653589793'::float8)),
+    add constraint c2 check (pg_datum_image_equal(a_ts, '2026-01-02 
14:59:03.141529'::timestamp));
+set extra_float_digits = 0; -- loses one digit
+set datestyle = German, MDY; -- output as DD.MM, but read back as MM.DD
+alter table tgen.t_roundtrip alter a_float8 add generated using constraint c1 
stored;
+alter table tgen.t_roundtrip alter a_ts add generated using constraint c2 
stored;
+-- visually verify that the generator expression is correct
+set extra_float_digits = 1;
+set datestyle = ISO;
+select pg_get_expr(adbin, 'tgen.t_roundtrip'::regclass) from pg_attrdef
+  where adrelid = 'tgen.t_roundtrip'::regclass;
+                        pg_get_expr                        
+-----------------------------------------------------------
+ '3.141592653589793'::double precision
+ '2026-01-02 14:59:03.141529'::timestamp without time zone
+(2 rows)
+
+-- if the conversion was wrong, we wouldn't be able to insert default values 
here
+insert into tgen.t_roundtrip (a_float8, a_ts) values
+  (default, default);
+set extra_float_digits = :prev_extra_float_digits;
+set datestyle = :prev_datestyle;
+drop table tgen.t_roundtrip;
+-- invalid: the column has atthasmissing
+-- intentionally left behind for pg_dump/pg_restore tests
+create table tgen.t_missingval (a int);
+insert into tgen.t_missingval select generate_series(1, 3);
+alter table tgen.t_missingval add column b int default 42;
+insert into tgen.t_missingval (a) values (4);
+alter table tgen.t_missingval add constraint chk_gen
+  check (pg_datum_image_equal(b, case when a < 5 then 42 else a * 2 end));
+insert into tgen.t_missingval (a, b) values (5, 10);
+alter table tgen.t_missingval alter b add generated using constraint chk_gen 
stored;
+ERROR:  cannot convert column "b" to generated
+DETAIL:  Cannot ensure that column "b" of relation "t_missingval" has been 
completely backfilled.
+HINT:  Drop the column and create it again without a DEFAULT value.
+-- intentionally left behind for pg_dump/pg_restore tests
+create table tgen.t_simple (a int, b int not null);
+insert into tgen.t_simple (a, b) values (1, 2), (2, 3);
+alter table tgen.t_simple add constraint chk_gen check 
(pg_datum_image_equal(b, a + 1));
+alter table tgen.t_simple alter b add generated using constraint chk_gen 
stored;
+\d tgen.t_simple
+                            Table "tgen.t_simple"
+ Column |  Type   | Collation | Nullable |              Default               
+--------+---------+-----------+----------+------------------------------------
+ a      | integer |           |          | 
+ b      | integer |           | not null | generated always as (a + 1) stored
+Check constraints:
+    "chk_gen" CHECK (pg_datum_image_equal(b, a + 1))
+
+-- intentionally left behind for pg_dump/pg_restore tests
+create table tgen.t_tsvect (a text, b text, c tsvector);
+insert into tgen.t_tsvect (a, b) values ('foo', 'bar'), ('bar', 'baz');
+update tgen.t_tsvect set c = to_tsvector('english', a || ' ' ||  b);
+alter table tgen.t_tsvect add constraint chk_gen
+    check ( pg_datum_image_equal(c, to_tsvector('english', a || ' ' ||  b)) );
+alter table tgen.t_tsvect alter c add generated using constraint chk_gen 
stored;
+\d tgen.t_tsvect
+                                                      Table "tgen.t_tsvect"
+ Column |   Type   | Collation | Nullable |                                    
    Default                                        
+--------+----------+-----------+----------+---------------------------------------------------------------------------------------
+ a      | text     |           |          | 
+ b      | text     |           |          | 
+ c      | tsvector |           |          | generated always as 
(to_tsvector('english'::regconfig, (a || ' '::text) || b)) stored
+Check constraints:
+    "chk_gen" CHECK (pg_datum_image_equal(c, to_tsvector('english'::regconfig, 
(a || ' '::text) || b)))
+
diff --git a/src/test/regress/sql/alter_table.sql 
b/src/test/regress/sql/alter_table.sql
index a6b4265b811..1dd22282e8d 100644
--- a/src/test/regress/sql/alter_table.sql
+++ b/src/test/regress/sql/alter_table.sql
@@ -3401,3 +3401,399 @@ alter table alter1.t1 set schema alter2;
 drop publication pub1;
 drop schema alter1 cascade;
 drop schema alter2 cascade;
+
+-- Tests for ALTER COLUMN ... ADD GENERATED USING CONSTRAINT name STORED
+-- turning a regular column into a stored generated column without a rewrite
+create schema tgen;
+
+create table tgen.t1 (a int, b int);
+insert into tgen.t1 (a, b)
+  select x, x * 2 from generate_series(1, 5) x;
+
+begin;
+alter table tgen.t1 add constraint chk_gen check (pg_datum_image_equal(b, a * 
2));
+alter table tgen.t1 alter column b
+  add generated using constraint chk_gen stored;
+\d tgen.t1
+insert into tgen.t1 (a, b) values (10, 20);
+rollback;
+
+-- test that we accept the constraint with the operands swapped, too
+begin;
+alter table tgen.t1 add constraint chk_gen check (pg_datum_image_equal(a * 2, 
b));
+alter table tgen.t1 alter column b
+    add generated using constraint chk_gen stored;
+\d tgen.t1
+insert into tgen.t1 (a, b) values (10, 20);
+rollback;
+begin;
+update tgen.t1 set b = a;
+alter table tgen.t1 add constraint chk_gen check (pg_datum_image_equal(a, b));
+alter table tgen.t1 alter column b
+    add generated using constraint chk_gen stored;
+\d tgen.t1
+insert into tgen.t1 (a, b) values (10, 10);
+rollback;
+
+-- check that neither the table nor indexes are rewritten
+begin;
+alter table tgen.t1
+    add constraint chk_gen check (pg_datum_image_equal(b, a * 2));
+create index t1_a_idx on tgen.t1 (a);
+select pg_relation_filenode('tgen.t1') as t1_filenode_before \gset
+select pg_relation_filenode('tgen.t1_a_idx') as t1_idx_filenode_before \gset
+alter table tgen.t1 alter column b
+  add generated using constraint chk_gen stored;
+select pg_relation_filenode('tgen.t1') as t1_filenode_after \gset
+select pg_relation_filenode('tgen.t1_a_idx') as t1_idx_filenode_after \gset
+select :t1_filenode_before = :t1_filenode_after as did_skip_table_rewrite,
+       :t1_idx_filenode_before = :t1_idx_filenode_after as 
did_skip_idx_rewrite;
+rollback;
+
+-- test the whole process for adding a stored generated column without
+-- long-lived exclusive locks. The t_migration database is intentionally
+-- left behind for pg_dump/pg_restore tests.
+create table tgen.t_migration (a int);
+select pg_relation_filenode('tgen.t_migration') as t_migration_filenode_before 
\gset
+insert into tgen.t_migration select x from generate_series(1, 5) x;
+-- test nulls, too
+insert into tgen.t_migration (a) values (null);
+alter table tgen.t_migration add column b int;
+-- take care of new and updated columns
+create function tgen.gen () returns trigger language plpgsql as $$
+begin
+  new.b = new.a * 2; return new;
+end
+$$;
+create trigger tgen_trig
+    before insert or update on tgen.t_migration
+    for each row execute function tgen.gen();
+-- add the constraint as not valid: enforced only for new and updated rows
+begin;
+alter table tgen.t_migration
+    add constraint chk_gen check (pg_datum_image_equal(b, a * 2)) not valid;
+select locktype, mode from pg_locks
+  where relation = 'tgen.t_migration'::regclass and granted;
+commit;
+insert into tgen.t_migration (a) values (100), (200), (300);
+-- backfill existing rows at the appropriate pace
+update tgen.t_migration set b = a * 2 where b is null;
+-- validate: this scans the table, but without an exclusive lock
+begin;
+alter table tgen.t_migration validate constraint chk_gen;
+select locktype, mode from pg_locks
+  where relation = 'tgen.t_migration'::regclass and granted;
+commit;
+-- now the schema update, which doesn't need to rewrite the table thanks to
+-- the constraint
+begin;
+alter table tgen.t_migration alter column b
+    add generated using constraint chk_gen stored;
+select locktype, mode from pg_locks
+where relation = 'tgen.t_migration'::regclass and granted;
+commit;
+select pg_relation_filenode('tgen.t_migration') as t_migration_filenode_after 
\gset
+select :t_migration_filenode_before = :t_migration_filenode_after as 
did_skip_rewrite;
+select * from tgen.t_migration;
+-- verify that it's still possible to insert rows (the trigger is still
+-- installed at this point)
+insert into tgen.t_migration (a) values (400);
+insert into tgen.t_migration (a) values (500);
+\d tgen.t_migration
+select * from tgen.t_migration order by a nulls first;
+
+-- test support for partitioned tables
+create table tgen.tpart (a int, b int) partition by hash (a);
+alter table tgen.tpart
+    add constraint chk_gen check (pg_datum_image_equal(b, a * 2));
+create table tgen.tpart_p1 partition of tgen.tpart
+    for values with (modulus 2, remainder 0);
+create table tgen.tpart_p2 partition of tgen.tpart
+    for values with (modulus 2, remainder 1);
+insert into tgen.tpart (a, b) select x, x * 2 from generate_series(1, 5) x;
+
+-- altering a single partition is not allowed
+alter table tgen.tpart_p1 alter column b
+    add generated using constraint chk_gen stored;
+
+-- altering only the parent table is not allowed
+alter table only tgen.tpart alter column b
+    add generated using constraint chk_gen stored;
+
+-- altering the parent table, recursing
+alter table tgen.tpart alter column b
+    add generated using constraint chk_gen stored;
+-- expected: all the partitions have been changed
+\d tgen.tpart
+\d tgen.tpart_p1
+\d tgen.tpart_p2
+
+-- tpart intentionally left behind for pg_dump/pg_restore tests
+
+-- test support for inheritance and subpartitions
+create table tgen.root (a int, b int, c int,
+    constraint chk_gen check ( pg_datum_image_equal(c, a+b) ));
+create table tgen.intermediate () inherits (tgen.root);
+create table tgen.leaf () inherits (tgen.intermediate);
+
+-- it's only allowed to change the whole hierarchy at once...
+begin;
+alter table tgen.root alter column c
+    add generated using constraint chk_gen stored;
+rollback;
+
+-- ... hence all these should result in an error
+alter table only tgen.root alter column c
+    add generated using constraint chk_gen stored;
+alter table tgen.intermediate alter column c
+    add generated using constraint chk_gen stored;
+alter table only tgen.intermediate alter column c
+    add generated using constraint chk_gen stored;
+alter table tgen.leaf alter column c
+    add generated using constraint chk_gen stored;
+alter table only tgen.leaf alter column c
+    add generated using constraint chk_gen stored;
+
+drop table tgen.root cascade;
+
+-- test multiple inheritance:
+-- we can't transform a whole hierarchy if some nodes in the graphs isn't an
+-- inheritor of the root node we're looking at
+create table tgen.root1 (a int);
+create table tgen.root2 (a int);
+create table tgen.intermediate1 () inherits (tgen.root1, tgen.root2);
+create table tgen.intermediate2 () inherits (tgen.root1);
+create table tgen.leaf1 () inherits (tgen.intermediate1, tgen.intermediate2);
+alter table tgen.root1 add constraint chk_gen check (pg_datum_image_equal(a, 
1));
+alter table tgen.root1 alter a add generated using constraint chk_gen stored;
+drop table tgen.root1 cascade;
+drop table tgen.root2;
+
+-- supported if we can transform the whole hierarchy starting at the root node
+create table tgen.root1 (a int);
+create table tgen.intermediate1 () inherits (tgen.root1);
+create table tgen.intermediate2 () inherits (tgen.root1);
+create table tgen.leaf1 () inherits (tgen.intermediate1, tgen.intermediate2);
+alter table tgen.root1 add constraint chk_gen check (pg_datum_image_equal(a, 
1));
+alter table tgen.root1 alter a add generated using constraint chk_gen stored;
+drop table tgen.root1 cascade;
+
+-- tests for invalid invocations
+-- invalid: only supports STORED. The following are syntax errors.
+alter table tgen.t1 alter column b add generated using constraint chk_gen;
+alter table tgen.t1 alter column b add generated using constraint chk_gen 
virtual;
+
+-- invalid: b is already a generated column
+begin;
+alter table tgen.t1 add constraint chk_gen check (pg_datum_image_equal(b, a * 
2));
+alter table tgen.t1 alter b add generated using constraint chk_gen stored;
+alter table tgen.t1 alter b add generated using constraint chk_gen stored;
+rollback;
+
+-- invalid: b is already a generated column
+create table tgen.t2 (a int, b int not null generated always as (a * 2) 
stored);
+alter table tgen.t2 add constraint chk_gen check (pg_datum_image_equal(b, a * 
2));
+alter table tgen.t2 alter column b add generated using constraint chk_gen 
stored;
+drop table tgen.t2;
+
+-- invalid: b is an identity column
+create table tgen.t2 (a int, b int generated always as identity);
+alter table tgen.t2 add constraint chk_gen check (pg_datum_image_equal(b, 1));
+alter table tgen.t2 alter column b add generated using constraint chk_gen 
stored;
+drop table tgen.t2;
+create table tgen.t2 (a int, b int generated by default as identity);
+alter table tgen.t2 add constraint chk_gen check (pg_datum_image_equal(b, 1));
+alter table tgen.t2 alter column b add generated using constraint chk_gen 
stored;
+drop table tgen.t2;
+
+-- invalid: b is a serial column
+create table tgen.t2 (a bigint, b bigserial);
+alter table tgen.t2 add constraint chk_gen check (pg_datum_image_equal(b, 
1::bigint));
+alter table tgen.t2 alter column b add generated using constraint chk_gen 
stored;
+drop table tgen.t2;
+
+-- invalid: c is referenced by another generated column
+create table tgen.t2 (a int, b int generated always as (c + 1), c int);
+alter table tgen.t2 add constraint chk_gen check (pg_datum_image_equal(c, 1));
+alter table tgen.t2 alter column c add generated using constraint chk_gen 
stored;
+drop table tgen.t2;
+
+-- invalid: c references another generated column
+create table tgen.t2 (a int, b int generated always as (a + 1), c int);
+alter table tgen.t2 add constraint chk_gen check (pg_datum_image_equal(c, b + 
1));
+alter table tgen.t2 alter column c add generated using constraint chk_gen 
stored;
+drop table tgen.t2;
+
+-- invalid: b is referenced in a partition key
+create table tgen.t2 (a int, b int not null) partition by hash (b);
+alter table tgen.t2 add constraint chk_gen check (pg_datum_image_equal(b, a + 
1));
+alter table tgen.t2 alter b add generated using constraint chk_gen stored;
+drop table tgen.t2;
+-- test for a whole-row reference
+-- since it's not possible to reference schema.table in partition by range,
+-- temporarily hack the search_path
+-- the two tables are intentionally left behind for pg_dump/pg_restore tests
+show search_path \gset
+set search_path to tgen, public;
+create table t_wholerow_1 (a int, b int) partition by range ((t_wholerow_1));
+alter table tgen.t_wholerow_1 add constraint chk_gen check 
(pg_datum_image_equal(b, a + 1));
+alter table tgen.t_wholerow_1 alter b add generated using constraint chk_gen 
stored;
+create table t_wholerow_2 (a int, b int) partition by range ((t_wholerow_2 is 
null));
+alter table tgen.t_wholerow_2 add constraint chk_gen check 
(pg_datum_image_equal(b, a + 1));
+alter table tgen.t_wholerow_2 alter b add generated using constraint chk_gen 
stored;
+set search_path to :search_path;
+
+-- invalid: b is referenced in a triggers's WHEN (before, update or insert)
+begin;
+create function t_trigger_f() returns trigger as $$ begin return null; end $$ 
language plpgsql;
+create trigger t_trigger before insert or update on tgen.t1
+    for each row when (new.b is null) execute function t_trigger_f();
+alter table tgen.t1
+    add constraint chk_gen check (pg_datum_image_equal(b, a * 2));
+alter table tgen.t1 alter column b
+    add generated using constraint chk_gen stored;
+rollback;
+-- invalid: b is referenced in a triggers's WHEN through a whole-row var
+-- (before, update or insert)
+begin;
+create function t_trigger_f() returns trigger as $$ begin return null; end $$ 
language plpgsql;
+create trigger t_trigger before insert or update on tgen.t1
+    for each row when (new is null) execute function t_trigger_f();
+alter table tgen.t1
+    add constraint chk_gen check (pg_datum_image_equal(b, a * 2));
+alter table tgen.t1 alter column b
+    add generated using constraint chk_gen stored;
+rollback;
+
+-- OK to have a reference in an after trigger
+begin;
+create function t_trigger_f() returns trigger as $$ begin return null; end $$ 
language plpgsql;
+create trigger t_trigger after insert or update on tgen.t1
+    for each row when (new.b is null) execute function t_trigger_f();
+alter table tgen.t1
+    add constraint chk_gen check (pg_datum_image_equal(b, a * 2));
+alter table tgen.t1 alter column b
+    add generated using constraint chk_gen stored;
+rollback;
+
+-- not allowed on a typed table
+create type tgen.ttype as (a int);
+create table tgen.ttab of tgen.ttype;
+alter table tgen.ttab add constraint chk_gen check (pg_datum_image_equal(a, 
1));
+alter table tgen.ttab alter a add generated using constraint chk_gen stored;
+
+-- fails when the constraint does not exist
+alter table tgen.t1 alter column b
+    add generated using constraint chk_gen_does_not_exist stored;
+
+-- fails when the constraint is not valid
+begin;
+alter table tgen.t1
+    add constraint chk_gen check (pg_datum_image_equal(b, a * 2)) not valid;
+alter table tgen.t1 alter column b
+    add generated using constraint chk_gen stored;
+rollback;
+
+-- fails when the constraint is not enforced
+begin;
+alter table tgen.t1
+    add constraint chk_gen check (pg_datum_image_equal(b, a * 2)) not enforced;
+alter table tgen.t1 alter column b
+    add generated using constraint chk_gen stored;
+rollback;
+
+-- fails when the constraint exists but doesn't have the expected shape
+begin;
+alter table tgen.t1 add constraint chk_gen check (b = a * 2);
+alter table tgen.t1 alter column b add generated using constraint chk_gen 
stored;
+rollback;
+begin;
+alter table tgen.t1 add constraint chk_gen check (b >= a * 2);
+alter table tgen.t1 alter column b add generated using constraint chk_gen 
stored;
+rollback;
+
+create table tgen.t2 (a int, b int);
+-- invalid: expr must be immutable
+alter table tgen.t2 add constraint chk_gen check (pg_datum_image_equal(b, (a + 
random())::int));
+alter table tgen.t2 alter column b
+    add generated using constraint chk_gen stored;
+drop table tgen.t2;
+
+drop table tgen.t1;
+
+-- invalid: the column is on the referencing side of a foreign key with
+-- on delete or on update actions
+create table tgen.tpkey (a int primary key);
+
+create table tgen.tfkey (a int primary key, b int references tgen.tpkey(a) on 
delete set null);
+alter table tgen.tfkey add constraint chk_gen check (pg_datum_image_equal(b, a 
+ 1));
+alter table tgen.tfkey alter column b add generated using constraint chk_gen 
stored;
+drop table tgen.tfkey;
+
+create table tgen.tfkey (a int primary key, b int references tgen.tpkey(a) on 
update cascade);
+alter table tgen.tfkey add constraint chk_gen check (pg_datum_image_equal(b, a 
+ 1));
+alter table tgen.tfkey alter column b add generated using constraint chk_gen 
stored;
+drop table tgen.tfkey;
+
+-- OK to have the stored generated column on the referenced side
+create table tgen.tfkey (a int primary key, b int references tgen.tpkey(a) on 
update cascade);
+alter table tgen.tpkey add constraint chk_gen check (pg_datum_image_equal(a, 
1));
+alter table tgen.tpkey alter a add generated using constraint chk_gen stored;
+drop table tgen.tfkey;
+
+drop table tgen.tpkey;
+
+-- some GUCs affect how values are represented at deparse time: ensure we can
+-- reconstruct the generator expression properly when they're set.
+select setting as prev_extra_float_digits from pg_settings where name = 
'extra_float_digits' \gset
+select setting as prev_datestyle from pg_settings where name = 'DateStyle' 
\gset
+set extra_float_digits = 1;
+set datestyle = ISO;
+create table tgen.t_roundtrip (a_float8 float8, a_ts timestamp);
+alter table tgen.t_roundtrip
+    add constraint c1 check (pg_datum_image_equal(a_float8, 
'3.141592653589793'::float8)),
+    add constraint c2 check (pg_datum_image_equal(a_ts, '2026-01-02 
14:59:03.141529'::timestamp));
+set extra_float_digits = 0; -- loses one digit
+set datestyle = German, MDY; -- output as DD.MM, but read back as MM.DD
+alter table tgen.t_roundtrip alter a_float8 add generated using constraint c1 
stored;
+alter table tgen.t_roundtrip alter a_ts add generated using constraint c2 
stored;
+
+-- visually verify that the generator expression is correct
+set extra_float_digits = 1;
+set datestyle = ISO;
+select pg_get_expr(adbin, 'tgen.t_roundtrip'::regclass) from pg_attrdef
+  where adrelid = 'tgen.t_roundtrip'::regclass;
+
+-- if the conversion was wrong, we wouldn't be able to insert default values 
here
+insert into tgen.t_roundtrip (a_float8, a_ts) values
+  (default, default);
+set extra_float_digits = :prev_extra_float_digits;
+set datestyle = :prev_datestyle;
+drop table tgen.t_roundtrip;
+
+-- invalid: the column has atthasmissing
+-- intentionally left behind for pg_dump/pg_restore tests
+create table tgen.t_missingval (a int);
+insert into tgen.t_missingval select generate_series(1, 3);
+alter table tgen.t_missingval add column b int default 42;
+insert into tgen.t_missingval (a) values (4);
+alter table tgen.t_missingval add constraint chk_gen
+  check (pg_datum_image_equal(b, case when a < 5 then 42 else a * 2 end));
+insert into tgen.t_missingval (a, b) values (5, 10);
+alter table tgen.t_missingval alter b add generated using constraint chk_gen 
stored;
+
+-- intentionally left behind for pg_dump/pg_restore tests
+create table tgen.t_simple (a int, b int not null);
+insert into tgen.t_simple (a, b) values (1, 2), (2, 3);
+alter table tgen.t_simple add constraint chk_gen check 
(pg_datum_image_equal(b, a + 1));
+alter table tgen.t_simple alter b add generated using constraint chk_gen 
stored;
+\d tgen.t_simple
+
+-- intentionally left behind for pg_dump/pg_restore tests
+create table tgen.t_tsvect (a text, b text, c tsvector);
+insert into tgen.t_tsvect (a, b) values ('foo', 'bar'), ('bar', 'baz');
+update tgen.t_tsvect set c = to_tsvector('english', a || ' ' ||  b);
+alter table tgen.t_tsvect add constraint chk_gen
+    check ( pg_datum_image_equal(c, to_tsvector('english', a || ' ' ||  b)) );
+alter table tgen.t_tsvect alter c add generated using constraint chk_gen 
stored;
+\d tgen.t_tsvect
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index f10eb469266..ba2400509c6 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -27,6 +27,7 @@ AcquireSampleRowsFunc
 ActionList
 ActiveSnapshotElt
 AddForeignUpdateTargets_function
+AddGenConstrError
 AddrInfo
 AffixNode
 AffixNodeData
-- 
2.54.0

Reply via email to