]> begriffs open source - ai-pg/blob - full-docs/html/ddl-generated-columns.html
Include latest toc output
[ai-pg] / full-docs / html / ddl-generated-columns.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>5.4. Generated Columns</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="ddl-identity-columns.html" title="5.3. Identity Columns" /><link rel="next" href="ddl-constraints.html" title="5.5. Constraints" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">5.4. Generated Columns</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="ddl-identity-columns.html" title="5.3. Identity Columns">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="ddl.html" title="Chapter 5. Data Definition">Up</a></td><th width="60%" align="center">Chapter 5. Data Definition</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="ddl-constraints.html" title="5.5. Constraints">Next</a></td></tr></table><hr /></div><div class="sect1" id="DDL-GENERATED-COLUMNS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">5.4. Generated Columns <a href="#DDL-GENERATED-COLUMNS" class="id_link">#</a></h2></div></div></div><a id="id-1.5.4.6.2" class="indexterm"></a><p>
3    A generated column is a special column that is always computed from other
4    columns.  Thus, it is for columns what a view is for tables.  There are two
5    kinds of generated columns: stored and virtual.  A stored generated column
6    is computed when it is written (inserted or updated) and occupies storage
7    as if it were a normal column.  A virtual generated column occupies no
8    storage and is computed when it is read.  Thus, a virtual generated column
9    is similar to a view and a stored generated column is similar to a
10    materialized view (except that it is always updated automatically).
11   </p><p>
12    To create a generated column, use the <code class="literal">GENERATED ALWAYS
13    AS</code> clause in <code class="command">CREATE TABLE</code>, for example:
14 </p><pre class="programlisting">
15 CREATE TABLE people (
16     ...,
17     height_cm numeric,
18     height_in numeric <span class="emphasis"><strong>GENERATED ALWAYS AS (height_cm / 2.54)</strong></span>
19 );
20 </pre><p>
21    A generated column is by default of the virtual kind.  Use the keywords
22    <code class="literal">VIRTUAL</code> or <code class="literal">STORED</code> to make the choice
23    explicit.  See <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a> for more details.
24   </p><p>
25    A generated column cannot be written to directly.  In
26    <code class="command">INSERT</code> or <code class="command">UPDATE</code> commands, a value
27    cannot be specified for a generated column, but the keyword
28    <code class="literal">DEFAULT</code> may be specified.
29   </p><p>
30    Consider the differences between a column with a default and a generated
31    column.  The column default is evaluated once when the row is first
32    inserted if no other value was provided; a generated column is updated
33    whenever the row changes and cannot be overridden.  A column default may
34    not refer to other columns of the table; a generation expression would
35    normally do so.  A column default can use volatile functions, for example
36    <code class="literal">random()</code> or functions referring to the current time;
37    this is not allowed for generated columns.
38   </p><p>
39    Several restrictions apply to the definition of generated columns and
40    tables involving generated columns:
41
42    </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
43       The generation expression can only use immutable functions and cannot
44       use subqueries or reference anything other than the current row in any
45       way.
46      </p></li><li class="listitem"><p>
47       A generation expression cannot reference another generated column.
48      </p></li><li class="listitem"><p>
49       A generation expression cannot reference a system column, except
50       <code class="varname">tableoid</code>.
51      </p></li><li class="listitem"><p>
52       A virtual generated column cannot have a user-defined type, and the
53       generation expression of a virtual generated column must not reference
54       user-defined functions or types, that is, it can only use built-in
55       functions or types.  This applies also indirectly, such as for functions
56       or types that underlie operators or casts.  (This restriction does not
57       exist for stored generated columns.)
58      </p></li><li class="listitem"><p>
59       A generated column cannot have a column default or an identity definition.
60      </p></li><li class="listitem"><p>
61       A generated column cannot be part of a partition key.
62      </p></li><li class="listitem"><p>
63       Foreign tables can have generated columns.  See <a class="xref" href="sql-createforeigntable.html" title="CREATE FOREIGN TABLE"><span class="refentrytitle">CREATE FOREIGN TABLE</span></a> for details.
64      </p></li><li class="listitem"><p>For inheritance and partitioning:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
65         If a parent column is a generated column, its child column must also
66         be a generated column of the same kind (stored or virtual); however,
67         the child column can have a different generation expression.
68        </p><p>
69         For stored generated columns, the generation expression that is
70         actually applied during insert or update of a row is the one
71         associated with the table that the row is physically in.  (This is
72         unlike the behavior for column defaults: for those, the default value
73         associated with the table named in the query applies.)  For virtual
74         generated columns, the generation expression of the table named in the
75         query applies when a table is read.
76        </p></li><li class="listitem"><p>
77         If a parent column is not a generated column, its child column must
78         not be generated either.
79        </p></li><li class="listitem"><p>
80         For inherited tables, if you write a child column definition without
81         any <code class="literal">GENERATED</code> clause in <code class="command">CREATE TABLE
82         ... INHERITS</code>, then its <code class="literal">GENERATED</code> clause
83         will automatically be copied from the parent.  <code class="command">ALTER TABLE
84         ... INHERIT</code> will insist that parent and child columns
85         already match as to generation status, but it will not require their
86         generation expressions to match.
87        </p></li><li class="listitem"><p>
88         Similarly for partitioned tables, if you write a child column
89         definition without any <code class="literal">GENERATED</code> clause
90         in <code class="command">CREATE TABLE ... PARTITION OF</code>, then
91         its <code class="literal">GENERATED</code> clause will automatically be copied
92         from the parent.  <code class="command">ALTER TABLE ... ATTACH PARTITION</code>
93         will insist that parent and child columns already match as to
94         generation status, but it will not require their generation
95         expressions to match.
96        </p></li><li class="listitem"><p>
97         In case of multiple inheritance, if one parent column is a generated
98         column, then all parent columns must be generated columns.  If they
99         do not all have the same generation expression, then the desired
100         expression for the child must be specified explicitly.
101        </p></li></ul></div></li></ul></div><p>
102   </p><p>
103    Additional considerations apply to the use of generated columns.
104    </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
105       Generated columns maintain access privileges separately from their
106       underlying base columns.  So, it is possible to arrange it so that a
107       particular role can read from a generated column but not from the
108       underlying base columns.
109      </p><p>
110       For virtual generated columns, this is only fully secure if the
111       generation expression uses only leakproof functions (see <a class="xref" href="sql-createfunction.html" title="CREATE FUNCTION"><span class="refentrytitle">CREATE FUNCTION</span></a>), but this is not enforced by the system.
112      </p></li><li class="listitem"><p>
113       Privileges of functions used in generation expressions are checked when
114       the expression is actually executed, on write or read respectively, as
115       if the generation expression had been called directly from the query
116       using the generated column.  The user of a generated column must have
117       permissions to call all functions used by the generation expression.
118       Functions in the generation expression are executed with the privileges
119       of the user executing the query or the function owner, depending on
120       whether the functions are defined as <code class="literal">SECURITY INVOKER</code>
121       or <code class="literal">SECURITY DEFINER</code>.
122       
123      </p></li><li class="listitem"><p>
124       Generated columns are, conceptually, updated after
125       <code class="literal">BEFORE</code> triggers have run.  Therefore, changes made to
126       base columns in a <code class="literal">BEFORE</code> trigger will be reflected in
127       generated columns.  But conversely, it is not allowed to access
128       generated columns in <code class="literal">BEFORE</code> triggers.
129      </p></li><li class="listitem"><p>
130       Generated columns are allowed to be replicated during logical replication
131       according to the <code class="command">CREATE PUBLICATION</code> parameter
132       <a class="link" href="sql-createpublication.html#SQL-CREATEPUBLICATION-PARAMS-WITH-PUBLISH-GENERATED-COLUMNS">
133       <code class="literal">publish_generated_columns</code></a> or by including them
134       in the column list of the <code class="command">CREATE PUBLICATION</code> command.
135       This is currently only supported for stored generated columns.
136       See <a class="xref" href="logical-replication-gencols.html" title="29.6. Generated Column Replication">Section 29.6</a> for details.
137      </p></li></ul></div><p>
138   </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ddl-identity-columns.html" title="5.3. Identity Columns">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ddl.html" title="Chapter 5. Data Definition">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ddl-constraints.html" title="5.5. Constraints">Next</a></td></tr><tr><td width="40%" align="left" valign="top">5.3. Identity Columns </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 5.5. Constraints</td></tr></table></div></body></html>