diff options
author | Robert Haas <rhaas@postgresql.org> | 2012-02-07 12:08:26 -0500 |
---|---|---|
committer | Robert Haas <rhaas@postgresql.org> | 2012-02-07 12:08:26 -0500 |
commit | 3cc0800829a6dda5347497337b0cf43848da4acf (patch) | |
tree | a489c43476d04b73fe90bac20c274ccc8987bb2f /src/backend/utils/adt/numeric.c | |
parent | af7914c6627bcf0b0ca614e9ce95d3f8056602bf (diff) | |
download | postgresql-3cc0800829a6dda5347497337b0cf43848da4acf.tar.gz postgresql-3cc0800829a6dda5347497337b0cf43848da4acf.zip |
Add a transform function for numeric typmod coercisions.
This enables ALTER TABLE to skip table and index rebuilds when a column
is changed to an unconstrained numeric, or when the scale is unchanged
and the precision does not decrease.
Noah Misch, with a few stylistic changes and a fix for an OID
collision by me.
Diffstat (limited to 'src/backend/utils/adt/numeric.c')
-rw-r--r-- | src/backend/utils/adt/numeric.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index 9121418190f..6885349de58 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -30,6 +30,8 @@ #include "catalog/pg_type.h" #include "libpq/pqformat.h" #include "miscadmin.h" +#include "nodes/nodeFuncs.h" +#include "parser/parse_clause.h" #include "utils/array.h" #include "utils/builtins.h" #include "utils/int8.h" @@ -713,6 +715,52 @@ numeric_send(PG_FUNCTION_ARGS) /* + * numeric_transform() - + * + * Flatten calls to our length coercion function that solely represent + * increases in allowable precision. Scale changes mutate every datum, so + * they are unoptimizable. Some values, e.g. 1E-1001, can only fit into an + * unconstrained numeric, so a change from an unconstrained numeric to any + * constrained numeric is also unoptimizable. + */ +Datum +numeric_transform(PG_FUNCTION_ARGS) +{ + FuncExpr *expr = (FuncExpr *) PG_GETARG_POINTER(0); + Node *typmod; + Node *ret = NULL; + + if (!IsA(expr, FuncExpr)) + PG_RETURN_POINTER(ret); + + Assert(list_length(expr->args) == 2); + typmod = lsecond(expr->args); + + if (IsA(typmod, Const)) + { + Node *source = linitial(expr->args); + int32 old_typmod = exprTypmod(source); + int32 new_typmod = DatumGetInt32(((Const *) typmod)->constvalue); + int32 old_scale = (old_typmod - VARHDRSZ) & 0xffff; + int32 new_scale = (new_typmod - VARHDRSZ) & 0xffff; + int32 old_precision = (old_typmod - VARHDRSZ) >> 16 & 0xffff; + int32 new_precision = (new_typmod - VARHDRSZ) >> 16 & 0xffff; + + /* + * If new_typmod < VARHDRSZ, the destination is unconstrained; that's + * always OK. If old_typmod >= VARHDRSZ, the source is constained. + * and we're OK if the scale is unchanged and the precison is not + * decreasing. See further nodes in function header comment. + */ + if (new_typmod < VARHDRSZ || (old_typmod >= VARHDRSZ && + new_scale == old_scale && new_precision >= old_precision)) + ret = relabel_to_typmod(source, new_typmod); + } + + PG_RETURN_POINTER(ret); +} + +/* * numeric() - * * This is a special function called by the Postgres database system |