Skip site navigation (1) Skip section navigation (2)

pgsql: Prevent CREATE TABLE LIKE/INHERITS from (mis) copyingwhole-row

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Prevent CREATE TABLE LIKE/INHERITS from (mis) copyingwhole-row
Date: 2012-06-30 20:45:46
Message-ID: E1Sl4Y6-0003IS-M6@gemulon.postgresql.org (view raw or flat)
Thread:
Lists: pgsql-committers
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.

Branch
------
REL8_4_STABLE

Details
-------
http://git.postgresql.org/pg/commitdiff/5a547ae3cefa9a8d70dea8207d786b2b5bc9a2d2

Modified Files
--------------
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 LaneDate: 2012-06-30 21:19:59
Subject: pgsql: Declare AnonymousShmem pointer as "void *".
Previous:From: Peter EisentrautDate: 2012-06-30 20:43:49
Subject: pgsql: initdb: Update check_need_password for new options

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group