Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5c45ff4

Browse files
tglsfdcpull[bot]
authored andcommitted
Fix ruleutils issues with dropped cols in functions-returning-composite.
Due to lack of concern for the case in the dependency code, it's possible to drop a column of a composite type even though stored queries have references to the dropped column via functions-in-FROM that return the composite type. There are "soft" references, namely FROM-clause aliases for such columns, and "hard" references, that is actual Vars referring to them. The right fix for hard references is to add dependencies preventing the drop; something we've known for many years and not done (and this commit still doesn't address it). A "soft" reference shouldn't prevent a drop though. We've been around on this before (cf. 9b35ddc, 2c4debb), but nobody had noticed that the current behavior can result in dump/reload failures, because ruleutils.c can print more column aliases than the underlying composite type now has. So we need to rejigger the column-alias-handling code to treat such columns as dropped and not print aliases for them. Rather than writing new code for this, I used expandRTE() which already knows how to figure out which function result columns are dropped. I'd initially thought maybe we could use expandRTE() in all cases, but that fails for EXPLAIN's purposes, because the planner strips a lot of RTE infrastructure that expandRTE() needs. So this patch just uses it for unplanned function RTEs and otherwise does things the old way. If there is a hard reference (Var), then removing the column alias causes us to fail to print the Var, since there's no longer a name to print. Failing seems less desirable than printing a made-up name, so I made it print "?dropped?column?" instead. Per report from Timo Stolz. Back-patch to all supported branches. Discussion: https://postgr.es/m/5c91267e-3b6d-5795-189c-d15a55d61dbb@nullachtvierzehn.de
1 parent 8868b02 commit 5c45ff4

File tree

4 files changed

+68
-22
lines changed

4 files changed

+68
-22
lines changed

src/backend/parser/parse_relation.c

+3
Original file line numberDiff line numberDiff line change
@@ -3198,6 +3198,9 @@ expandNSItemAttrs(ParseState *pstate, ParseNamespaceItem *nsitem,
31983198
*
31993199
* "*" is returned if the given attnum is InvalidAttrNumber --- this case
32003200
* occurs when a Var represents a whole tuple of a relation.
3201+
*
3202+
* It is caller's responsibility to not call this on a dropped attribute.
3203+
* (You will get some answer for such cases, but it might not be sensible.)
32013204
*/
32023205
char *
32033206
get_rte_attribute_name(RangeTblEntry *rte, AttrNumber attnum)

src/backend/utils/adt/ruleutils.c

+44-12
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "parser/parse_func.h"
5454
#include "parser/parse_node.h"
5555
#include "parser/parse_oper.h"
56+
#include "parser/parse_relation.h"
5657
#include "parser/parser.h"
5758
#include "parser/parsetree.h"
5859
#include "rewrite/rewriteHandler.h"
@@ -4323,9 +4324,9 @@ set_relation_column_names(deparse_namespace *dpns, RangeTblEntry *rte,
43234324
int j;
43244325

43254326
/*
4326-
* Extract the RTE's "real" column names. This is comparable to
4327-
* get_rte_attribute_name, except that it's important to disregard dropped
4328-
* columns. We put NULL into the array for a dropped column.
4327+
* Construct an array of the current "real" column names of the RTE.
4328+
* real_colnames[] will be indexed by physical column number, with NULL
4329+
* entries for dropped columns.
43294330
*/
43304331
if (rte->rtekind == RTE_RELATION)
43314332
{
@@ -4352,19 +4353,43 @@ set_relation_column_names(deparse_namespace *dpns, RangeTblEntry *rte,
43524353
}
43534354
else
43544355
{
4355-
/* Otherwise use the column names from eref */
4356+
/* Otherwise get the column names from eref or expandRTE() */
4357+
List *colnames;
43564358
ListCell *lc;
43574359

4358-
ncolumns = list_length(rte->eref->colnames);
4360+
/*
4361+
* Functions returning composites have the annoying property that some
4362+
* of the composite type's columns might have been dropped since the
4363+
* query was parsed. If possible, use expandRTE() to handle that
4364+
* case, since it has the tedious logic needed to find out about
4365+
* dropped columns. However, if we're explaining a plan, then we
4366+
* don't have rte->functions because the planner thinks that won't be
4367+
* needed later, and that breaks expandRTE(). So in that case we have
4368+
* to rely on rte->eref, which may lead us to report a dropped
4369+
* column's old name; that seems close enough for EXPLAIN's purposes.
4370+
*
4371+
* For non-RELATION, non-FUNCTION RTEs, we can just look at rte->eref,
4372+
* which should be sufficiently up-to-date: no other RTE types can
4373+
* have columns get dropped from under them after parsing.
4374+
*/
4375+
if (rte->rtekind == RTE_FUNCTION && rte->functions != NIL)
4376+
{
4377+
/* Since we're not creating Vars, rtindex etc. don't matter */
4378+
expandRTE(rte, 1, 0, -1, true /* include dropped */ ,
4379+
&colnames, NULL);
4380+
}
4381+
else
4382+
colnames = rte->eref->colnames;
4383+
4384+
ncolumns = list_length(colnames);
43594385
real_colnames = (char **) palloc(ncolumns * sizeof(char *));
43604386

43614387
i = 0;
4362-
foreach(lc, rte->eref->colnames)
4388+
foreach(lc, colnames)
43634389
{
43644390
/*
4365-
* If the column name shown in eref is an empty string, then it's
4366-
* a column that was dropped at the time of parsing the query, so
4367-
* treat it as dropped.
4391+
* If the column name we find here is an empty string, then it's a
4392+
* dropped column, so change to NULL.
43684393
*/
43694394
char *cname = strVal(lfirst(lc));
43704395

@@ -7301,9 +7326,16 @@ get_variable(Var *var, int levelsup, bool istoplevel, deparse_context *context)
73017326
elog(ERROR, "invalid attnum %d for relation \"%s\"",
73027327
attnum, rte->eref->aliasname);
73037328
attname = colinfo->colnames[attnum - 1];
7304-
if (attname == NULL) /* dropped column? */
7305-
elog(ERROR, "invalid attnum %d for relation \"%s\"",
7306-
attnum, rte->eref->aliasname);
7329+
7330+
/*
7331+
* If we find a Var referencing a dropped column, it seems better to
7332+
* print something (anything) than to fail. In general this should
7333+
* not happen, but there are specific cases involving functions
7334+
* returning named composite types where we don't sufficiently enforce
7335+
* that you can't drop a column that's referenced in some view.
7336+
*/
7337+
if (attname == NULL)
7338+
attname = "?dropped?column?";
73077339
}
73087340
else
73097341
{

src/test/regress/expected/create_view.out

+17-8
Original file line numberDiff line numberDiff line change
@@ -1600,17 +1600,26 @@ select * from tt14v;
16001600
begin;
16011601
-- this perhaps should be rejected, but it isn't:
16021602
alter table tt14t drop column f3;
1603-
-- f3 is still in the view ...
1603+
-- column f3 is still in the view, sort of ...
16041604
select pg_get_viewdef('tt14v', true);
1605-
pg_get_viewdef
1606-
--------------------------------
1607-
SELECT t.f1, +
1608-
t.f3, +
1609-
t.f4 +
1610-
FROM tt14f() t(f1, f3, f4);
1605+
pg_get_viewdef
1606+
---------------------------------
1607+
SELECT t.f1, +
1608+
t."?dropped?column?" AS f3,+
1609+
t.f4 +
1610+
FROM tt14f() t(f1, f4);
16111611
(1 row)
16121612

1613-
-- but will fail at execution
1613+
-- ... and you can even EXPLAIN it ...
1614+
explain (verbose, costs off) select * from tt14v;
1615+
QUERY PLAN
1616+
----------------------------------------
1617+
Function Scan on testviewschm2.tt14f t
1618+
Output: t.f1, t.f3, t.f4
1619+
Function Call: tt14f()
1620+
(3 rows)
1621+
1622+
-- but it will fail at execution
16141623
select f1, f4 from tt14v;
16151624
f1 | f4
16161625
-----+----

src/test/regress/sql/create_view.sql

+4-2
Original file line numberDiff line numberDiff line change
@@ -580,9 +580,11 @@ begin;
580580
-- this perhaps should be rejected, but it isn't:
581581
alter table tt14t drop column f3;
582582

583-
-- f3 is still in the view ...
583+
-- column f3 is still in the view, sort of ...
584584
select pg_get_viewdef('tt14v', true);
585-
-- but will fail at execution
585+
-- ... and you can even EXPLAIN it ...
586+
explain (verbose, costs off) select * from tt14v;
587+
-- but it will fail at execution
586588
select f1, f4 from tt14v;
587589
select * from tt14v;
588590

0 commit comments

Comments
 (0)