diff options
author | Peter Eisentraut <peter@eisentraut.org> | 2025-02-07 09:09:34 +0100 |
---|---|---|
committer | Peter Eisentraut <peter@eisentraut.org> | 2025-02-07 09:46:59 +0100 |
commit | 83ea6c54025bea67bcd4949a6d58d3fc11c3e21b (patch) | |
tree | 5a5d13a9f27cd08958d821656086dd1c054516f5 /src/backend/executor/nodeModifyTable.c | |
parent | cbc127917e04a978a788b8bc9d35a70244396d5b (diff) | |
download | postgresql-83ea6c54025bea67bcd4949a6d58d3fc11c3e21b.tar.gz postgresql-83ea6c54025bea67bcd4949a6d58d3fc11c3e21b.zip |
Virtual generated columns
This adds a new variant of generated columns that are computed on read
(like a view, unlike the existing stored generated columns, which are
computed on write, like a materialized view).
The syntax for the column definition is
... GENERATED ALWAYS AS (...) VIRTUAL
and VIRTUAL is also optional. VIRTUAL is the default rather than
STORED to match various other SQL products. (The SQL standard makes
no specification about this, but it also doesn't know about VIRTUAL or
STORED.) (Also, virtual views are the default, rather than
materialized views.)
Virtual generated columns are stored in tuples as null values. (A
very early version of this patch had the ambition to not store them at
all. But so much stuff breaks or gets confused if you have tuples
where a column in the middle is completely missing. This is a
compromise, and it still saves space over being forced to use stored
generated columns. If we ever find a way to improve this, a bit of
pg_upgrade cleverness could allow for upgrades to a newer scheme.)
The capabilities and restrictions of virtual generated columns are
mostly the same as for stored generated columns. In some cases, this
patch keeps virtual generated columns more restricted than they might
technically need to be, to keep the two kinds consistent. Some of
that could maybe be relaxed later after separate careful
considerations.
Some functionality that is currently not supported, but could possibly
be added as incremental features, some easier than others:
- index on or using a virtual column
- hence also no unique constraints on virtual columns
- extended statistics on virtual columns
- foreign-key constraints on virtual columns
- not-null constraints on virtual columns (check constraints are supported)
- ALTER TABLE / DROP EXPRESSION
- virtual column cannot have domain type
- virtual columns are not supported in logical replication
The tests in generated_virtual.sql have been copied over from
generated_stored.sql with the keyword replaced. This way we can make
sure the behavior is mostly aligned, and the differences can be
visible. Some tests for currently not supported features are
currently commented out.
Reviewed-by: Jian He <jian.universality@gmail.com>
Reviewed-by: Dean Rasheed <dean.a.rasheed@gmail.com>
Tested-by: Shlok Kyal <shlok.kyal.oss@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/a368248e-69e4-40be-9c07-6c3b5880b0a6@eisentraut.org
Diffstat (limited to 'src/backend/executor/nodeModifyTable.c')
-rw-r--r-- | src/backend/executor/nodeModifyTable.c | 41 |
1 files changed, 29 insertions, 12 deletions
diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index 349ed2d6d2c..a15e7863b0d 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -391,11 +391,14 @@ ExecCheckTIDVisible(EState *estate, } /* - * Initialize to compute stored generated columns for a tuple + * Initialize generated columns handling for a tuple + * + * This fills the resultRelInfo's ri_GeneratedExprsI/ri_NumGeneratedNeededI or + * ri_GeneratedExprsU/ri_NumGeneratedNeededU fields, depending on cmdtype. + * This is used only for stored generated columns. * - * This fills the resultRelInfo's ri_GeneratedExprsI/ri_NumGeneratedNeededI - * or ri_GeneratedExprsU/ri_NumGeneratedNeededU fields, depending on cmdtype. * If cmdType == CMD_UPDATE, the ri_extraUpdatedCols field is filled too. + * This is used by both stored and virtual generated columns. * * Note: usually, a given query would need only one of ri_GeneratedExprsI and * ri_GeneratedExprsU per result rel; but MERGE can need both, and so can @@ -403,9 +406,9 @@ ExecCheckTIDVisible(EState *estate, * UPDATE and INSERT actions. */ void -ExecInitStoredGenerated(ResultRelInfo *resultRelInfo, - EState *estate, - CmdType cmdtype) +ExecInitGenerated(ResultRelInfo *resultRelInfo, + EState *estate, + CmdType cmdtype) { Relation rel = resultRelInfo->ri_RelationDesc; TupleDesc tupdesc = RelationGetDescr(rel); @@ -416,7 +419,7 @@ ExecInitStoredGenerated(ResultRelInfo *resultRelInfo, MemoryContext oldContext; /* Nothing to do if no generated columns */ - if (!(tupdesc->constr && tupdesc->constr->has_generated_stored)) + if (!(tupdesc->constr && (tupdesc->constr->has_generated_stored || tupdesc->constr->has_generated_virtual))) return; /* @@ -442,7 +445,9 @@ ExecInitStoredGenerated(ResultRelInfo *resultRelInfo, for (int i = 0; i < natts; i++) { - if (TupleDescAttr(tupdesc, i)->attgenerated == ATTRIBUTE_GENERATED_STORED) + char attgenerated = TupleDescAttr(tupdesc, i)->attgenerated; + + if (attgenerated) { Expr *expr; @@ -467,8 +472,11 @@ ExecInitStoredGenerated(ResultRelInfo *resultRelInfo, } /* No luck, so prepare the expression for execution */ - ri_GeneratedExprs[i] = ExecPrepareExpr(expr, estate); - ri_NumGeneratedNeeded++; + if (attgenerated == ATTRIBUTE_GENERATED_STORED) + { + ri_GeneratedExprs[i] = ExecPrepareExpr(expr, estate); + ri_NumGeneratedNeeded++; + } /* If UPDATE, mark column in resultRelInfo->ri_extraUpdatedCols */ if (cmdtype == CMD_UPDATE) @@ -478,6 +486,13 @@ ExecInitStoredGenerated(ResultRelInfo *resultRelInfo, } } + if (ri_NumGeneratedNeeded == 0) + { + /* didn't need it after all */ + pfree(ri_GeneratedExprs); + ri_GeneratedExprs = NULL; + } + /* Save in appropriate set of fields */ if (cmdtype == CMD_UPDATE) { @@ -486,6 +501,8 @@ ExecInitStoredGenerated(ResultRelInfo *resultRelInfo, resultRelInfo->ri_GeneratedExprsU = ri_GeneratedExprs; resultRelInfo->ri_NumGeneratedNeededU = ri_NumGeneratedNeeded; + + resultRelInfo->ri_extraUpdatedCols_valid = true; } else { @@ -526,7 +543,7 @@ ExecComputeStoredGenerated(ResultRelInfo *resultRelInfo, if (cmdtype == CMD_UPDATE) { if (resultRelInfo->ri_GeneratedExprsU == NULL) - ExecInitStoredGenerated(resultRelInfo, estate, cmdtype); + ExecInitGenerated(resultRelInfo, estate, cmdtype); if (resultRelInfo->ri_NumGeneratedNeededU == 0) return; ri_GeneratedExprs = resultRelInfo->ri_GeneratedExprsU; @@ -534,7 +551,7 @@ ExecComputeStoredGenerated(ResultRelInfo *resultRelInfo, else { if (resultRelInfo->ri_GeneratedExprsI == NULL) - ExecInitStoredGenerated(resultRelInfo, estate, cmdtype); + ExecInitGenerated(resultRelInfo, estate, cmdtype); /* Early exit is impossible given the prior Assert */ Assert(resultRelInfo->ri_NumGeneratedNeededI > 0); ri_GeneratedExprs = resultRelInfo->ri_GeneratedExprsI; |