pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix handling of CREATE TABLE LIKE with inheritance.
Date: Fri, 21 Aug 2020 19:01:16 +0000
Message-ID: <E1k9CHs-0003LN-LU@gemulon.postgresql.org> (raw)

Fix handling of CREATE TABLE LIKE with inheritance.

If a CREATE TABLE command uses both LIKE and traditional inheritance,
Vars in CHECK constraints and expression indexes that are absorbed
from a LIKE parent table tended to get mis-numbered, resulting in
wrong answers and/or bizarre error messages (though probably not any
actual crashes, thanks to validation occurring in the executor).

In v12 and up, the same could happen to Vars in GENERATED expressions,
even in cases with no LIKE clause but multiple traditional-inheritance
parents.

The cause of the problem for LIKE is that parse_utilcmd.c supposed
it could renumber such Vars correctly during transformCreateStmt(),
which it cannot since we have not yet accounted for columns added via
inheritance.  Fix that by postponing processing of LIKE INCLUDING
CONSTRAINTS, DEFAULTS, GENERATED, INDEXES till after we've performed
DefineRelation().

The error with GENERATED and multiple inheritance is a simple oversight
in MergeAttributes(); it knows it has to renumber Vars in inherited
CHECK constraints, but forgot to apply the same processing to inherited
GENERATED expressions (a/k/a defaults).

Per bug #16272 from Tom Gottfried.  The non-GENERATED variants of the
issue are ancient, presumably dating right back to the addition of
CREATE TABLE LIKE; hence back-patch to all supported branches.

Discussion: https://postgr.es/m/16272-6e32da020e9a9381@postgresql.org

Branch
------
REL9_6_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/37681d7e942e31256efdd9b562a750f1742ac111

Modified Files
--------------
src/backend/parser/parse_utilcmd.c              | 174 +++++++++++++++++-------
src/backend/tcop/utility.c                      |  36 ++++-
src/include/parser/parse_utilcmd.h              |   2 +
src/test/regress/expected/create_table_like.out |  38 +++++-
src/test/regress/sql/create_table_like.sql      |  18 ++-
5 files changed, 213 insertions(+), 55 deletions(-)



view thread (7+ messages)  latest in thread

Message-ID: <E1k9CHs-0003LN-LU@gemulon.postgresql.org>
Permalink:  ../E1k9CHs-0003LN-LU@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1k9CHs-0003LN-LU@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us
  Subject: Re: pgsql: Fix handling of CREATE TABLE LIKE with inheritance.
  In-Reply-To: <E1k9CHs-0003LN-LU@gemulon.postgresql.org>
  References: <E1k9CHs-0003LN-LU@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox