Prevent CREATE TABLE LIKE/INHERITS from (mis) copying whole-row Vars.
If a CHECK constraint or index definition contained a whole-row Var (that
is, "table.*"), an attempt to copy that definition via CREATE TABLE LIKE or
table inheritance produced incorrect results: the copied Var still claimed
to have the rowtype of the source table, rather than the created table.
For the LIKE case, it seems reasonable to just throw error for this
situation, since the point of LIKE is that the new table is not permanently
coupled to the old, so there's no reason to assume its rowtype will stay
compatible. In the inheritance case, we should ideally allow such
constraints, but doing so will require nontrivial refactoring of CREATE
TABLE processing (because we'd need to know the OID of the new table's
rowtype before we adjust inherited CHECK constraints). In view of the lack
of previous complaints, that doesn't seem worth the risk in a back-patched
bug fix, so just make it throw error for the inheritance case as well.
Along the way, replace change_varattnos_of_a_node() with a more robust
function map_variable_attnos(), which is capable of being extended to
handle insertion of ConvertRowtypeExpr whenever we get around to fixing
the inheritance case nicely, and in the meantime it returns a failure
indication to the caller so that a helpful message with some context can be
thrown. Also, this code will do the right thing with subselects (if we
ever allow them in CHECK or indexes), and it range-checks varattnos before
using them to index into the map array.
Per report from Sergey Konoplev. Back-patch to all supported branches.
src/backend/commands/tablecmds.c | 131 ++++++------------------------------
src/backend/parser/parse_utilcmd.c | 84 +++++++++++++++++++----
src/backend/rewrite/rewriteManip.c | 113 +++++++++++++++++++++++++++++++
src/include/commands/tablecmds.h | 4 -
src/include/rewrite/rewriteManip.h | 5 ++
5 files changed, 210 insertions(+), 127 deletions(-)
pgsql-committers by date
|Next:||From: Tom Lane||Date: 2012-06-30 21:19:59|
|Subject: pgsql: Declare AnonymousShmem pointer as "void *".|
|Previous:||From: Peter Eisentraut||Date: 2012-06-30 20:43:49|
|Subject: pgsql: initdb: Update check_need_password for new options|