Commit 0d90dc16 authored by Robert Haas's avatar Robert Haas

Avoid a few more SET DATA TYPE table rewrites.

When the new type is an unconstrained domain over the old type, we don't
need to rewrite the table.

Noah Misch and Robert Haas
parent 8e1124ee
...@@ -766,13 +766,14 @@ ALTER TABLE <replaceable class="PARAMETER">name</replaceable> ...@@ -766,13 +766,14 @@ ALTER TABLE <replaceable class="PARAMETER">name</replaceable>
<para> <para>
Adding a column with a non-null default or changing the type of an Adding a column with a non-null default or changing the type of an
existing column will require the entire table and indexes to be rewritten. existing column will require the entire table and indexes to be rewritten.
As an exception, if the old type type is binary coercible to the new As an exception, if the <literal>USING</> clause does not change the column
type and the <literal>USING</> clause does not change the column contents, contents and the old type is either binary coercible to the new type or
a table rewrite is not needed, but any indexes on the affected columns an unconstrained domain over the new type, a table rewrite is not needed,
must still be rebuilt. Adding or removing a system <literal>oid</> column but any indexes on the affected columns must still be rebuilt. Adding or
also requires rewriting the entire table. Table and/or index rebuilds may removing a system <literal>oid</> column also requires rewriting the entire
take a significant amount of time for a large table; and will temporarily table. Table and/or index rebuilds may take a significant amount of time
require as much as double the disk space. for a large table; and will temporarily require as much as double the disk
space.
</para> </para>
<para> <para>
......
...@@ -6632,10 +6632,14 @@ ATPrepAlterColumnType(List **wqueue, ...@@ -6632,10 +6632,14 @@ ATPrepAlterColumnType(List **wqueue,
} }
/* /*
* When the data type of a column is changed, a rewrite might not be require * When the data type of a column is changed, a rewrite might not be required
* if the data type is being changed to its current type, or more interestingly * if the new type is sufficiently identical to the old one, and the USING
* to a type to which it is binary coercible. But we must check carefully that * clause isn't trying to insert some other value. It's safe to skip the
* the USING clause isn't trying to insert some other value. * rewrite if the old type is binary coercible to the new type, or if the
* new type is an unconstrained domain over the old type. In the case of a
* constrained domain, we could get by with scanning the table and checking
* the constraint rather than actually rewriting it, but we don't currently
* try to do that.
*/ */
static bool static bool
ATColumnChangeRequiresRewrite(Node *expr, AttrNumber varattno) ATColumnChangeRequiresRewrite(Node *expr, AttrNumber varattno)
...@@ -6649,6 +6653,14 @@ ATColumnChangeRequiresRewrite(Node *expr, AttrNumber varattno) ...@@ -6649,6 +6653,14 @@ ATColumnChangeRequiresRewrite(Node *expr, AttrNumber varattno)
return false; return false;
else if (IsA(expr, RelabelType)) else if (IsA(expr, RelabelType))
expr = (Node *) ((RelabelType *) expr)->arg; expr = (Node *) ((RelabelType *) expr)->arg;
else if (IsA(expr, CoerceToDomain))
{
CoerceToDomain *d = (CoerceToDomain *) expr;
if (GetDomainConstraints(d->resulttype) != NIL)
return true;
expr = (Node *) d->arg;
}
else else
return true; return true;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment