[COMMITTERS] pgsql: Change unknown-type literals to type text in SELECT andRETURNIN - Mailing list pgsql-committers

From Tom Lane
Subject [COMMITTERS] pgsql: Change unknown-type literals to type text in SELECT andRETURNIN
Date
Msg-id E1cWOOJ-0000wy-8d@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Change unknown-type literals to type text in SELECT and RETURNING lists.

Previously, we left such literals alone if the query or subquery had
no properties forcing a type decision to be made (such as an ORDER BY or
DISTINCT clause using that output column).  This meant that "unknown" could
be an exposed output column type, which has never been a great idea because
it could result in strange failures later on.  For example, an outer query
that tried to do any operations on an unknown-type subquery output would
generally fail with some weird error like "failed to find conversion
function from unknown to text" or "could not determine which collation to
use for string comparison".  Also, if the case occurred in a CREATE VIEW's
query then the view would have an unknown-type column, causing similar
failures in queries trying to use the view.

To fix, at the tail end of parse analysis of a query, forcibly convert any
remaining "unknown" literals in its SELECT or RETURNING list to type text.
However, provide a switch to suppress that, and use it in the cases of
SELECT inside a set operation or INSERT command.  In those cases we already
had type resolution rules that make use of context information from outside
the subquery proper, and we don't want to change that behavior.

Also, change creation of an unknown-type column in a relation from a
warning to a hard error.  The error should be unreachable now in CREATE
VIEW or CREATE MATVIEW, but it's still possible to explicitly say "unknown"
in CREATE TABLE or CREATE (composite) TYPE.  We want to forbid that because
it's nothing but a foot-gun.

This change creates a pg_upgrade failure case: a matview that contains an
unknown-type column can't be pg_upgraded, because reparsing the matview's
defining query will now decide that the column is of type text, which
doesn't match the cstring-like storage that the old materialized column
would actually have.  Add a checking pass to detect that.  While at it,
we can detect tables or composite types that would fail, essentially
for free.  Those would fail safely anyway later on, but we might as
well fail earlier.

This patch is by me, but it owes something to previous investigations
by Rahila Syed.  Also thanks to Ashutosh Bapat and Michael Paquier for
review.

Discussion: https://postgr.es/m/CAH2L28uwwbL9HUM-WR=hromW1Cvamkn7O-g8fPY2m=_7muJ0oA@mail.gmail.com

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/1e7c4bb0049732ece651d993d03bb6772e5d281a

Modified Files
--------------
doc/src/sgml/ref/create_view.sgml                |  7 +-
doc/src/sgml/typeconv.sgml                       | 50 +++++++++++-
src/backend/catalog/heap.c                       | 14 +---
src/backend/parser/analyze.c                     | 29 ++++++-
src/backend/parser/parse_clause.c                |  3 +-
src/backend/parser/parse_cte.c                   | 11 ++-
src/backend/parser/parse_expr.c                  |  2 +-
src/backend/parser/parse_node.c                  |  1 +
src/backend/parser/parse_target.c                | 33 +++++++-
src/bin/pg_upgrade/check.c                       |  4 +
src/bin/pg_upgrade/pg_upgrade.h                  |  1 +
src/bin/pg_upgrade/version.c                     | 97 ++++++++++++++++++++++++
src/include/parser/analyze.h                     |  3 +-
src/include/parser/parse_node.h                  |  5 ++
src/include/parser/parse_target.h                |  1 +
src/test/regress/expected/create_table.out       |  8 ++
src/test/regress/expected/create_view.out        | 26 +++++++
src/test/regress/expected/matview.out            | 27 +++++++
src/test/regress/expected/subselect.out          | 31 ++++++++
src/test/regress/expected/with.out               | 20 ++++-
src/test/regress/output/create_function_1.source |  2 +-
src/test/regress/sql/create_table.sql            |  8 ++
src/test/regress/sql/create_view.sql             |  8 ++
src/test/regress/sql/matview.sql                 |  8 ++
src/test/regress/sql/subselect.sql               | 10 +++
src/test/regress/sql/with.sql                    | 13 +++-
26 files changed, 386 insertions(+), 36 deletions(-)


pgsql-committers by date:

Previous
From: Peter Eisentraut
Date:
Subject: [COMMITTERS] pgsql: doc: Update ALTER SEQUENCE documentation to match
Next
From: Tom Lane
Date:
Subject: [COMMITTERS] pgsql: Make UNKNOWN into an actual pseudo-type.