Skip to content

Commit 7736ab0

Browse files
committed
Disallow converting an inheritance child table to a view.
Generally, members of inheritance trees must be plain tables (or, in more recent versions, foreign tables). ALTER TABLE INHERIT rejects creating an inheritance relationship that has a view at either end. When DefineQueryRewrite attempts to convert a relation to a view, it already had checks prohibiting doing so for partitioning parents or children as well as traditional-inheritance parents ... but it neglected to check that a traditional-inheritance child wasn't being converted. Since the planner assumes that any inheritance child is a table, this led to making plans that tried to do a physical scan on a view, causing failures (or even crashes, in recent versions). One could imagine trying to support such a case by expanding the view normally, but since the rewriter runs before the planner does inheritance expansion, it would take some very fundamental refactoring to make that possible. There are probably a lot of other parts of the system that don't cope well with such a situation, too. For now, just forbid it. Per bug #16856 from Yang Lin. Back-patch to all supported branches. (In versions before v10, this includes back-patching the portion of commit 501ed02 that added has_superclass(). Perhaps the lack of that infrastructure partially explains the missing check.) Discussion: https://postgr.es/m/16856-0363e05c6e1612fd@postgresql.org
1 parent e152ccc commit 7736ab0

File tree

5 files changed

+62
-6
lines changed

5 files changed

+62
-6
lines changed

src/backend/catalog/pg_inherits.c

+26
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,32 @@ has_subclass(Oid relationId)
254254
return result;
255255
}
256256

257+
/*
258+
* has_superclass - does this relation inherit from another?
259+
*
260+
* Unlike has_subclass, this can be relied on to give an accurate answer.
261+
* However, the caller must hold a lock on the given relation so that it
262+
* can't be concurrently added to or removed from an inheritance hierarchy.
263+
*/
264+
bool
265+
has_superclass(Oid relationId)
266+
{
267+
Relation catalog;
268+
SysScanDesc scan;
269+
ScanKeyData skey;
270+
bool result;
271+
272+
catalog = heap_open(InheritsRelationId, AccessShareLock);
273+
ScanKeyInit(&skey, Anum_pg_inherits_inhrelid, BTEqualStrategyNumber,
274+
F_OIDEQ, ObjectIdGetDatum(relationId));
275+
scan = systable_beginscan(catalog, InheritsRelidSeqnoIndexId, true,
276+
NULL, 1, &skey);
277+
result = HeapTupleIsValid(systable_getnext(scan));
278+
systable_endscan(scan);
279+
heap_close(catalog, AccessShareLock);
280+
281+
return result;
282+
}
257283

258284
/*
259285
* Given two type OIDs, determine whether the first is a complex type

src/backend/rewrite/rewriteDefine.c

+13-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "catalog/indexing.h"
2626
#include "catalog/namespace.h"
2727
#include "catalog/objectaccess.h"
28+
#include "catalog/pg_inherits_fn.h"
2829
#include "catalog/pg_rewrite.h"
2930
#include "catalog/storage.h"
3031
#include "commands/policy.h"
@@ -411,12 +412,12 @@ DefineQueryRewrite(char *rulename,
411412
*
412413
* If so, check that the relation is empty because the storage for the
413414
* relation is going to be deleted. Also insist that the rel not have
414-
* any triggers, indexes, child tables, policies, or RLS enabled.
415-
* (Note: these tests are too strict, because they will reject
416-
* relations that once had such but don't anymore. But we don't
417-
* really care, because this whole business of converting relations to
418-
* views is just a kluge to allow dump/reload of views that
419-
* participate in circular dependencies.)
415+
* any triggers, indexes, child or parent tables, RLS policies, or RLS
416+
* enabled. (Note: some of these tests are too strict, because they
417+
* will reject relations that once had such but don't anymore. But we
418+
* don't really care, because this whole business of converting
419+
* relations to views is just a kluge to allow dump/reload of views
420+
* that participate in circular dependencies.)
420421
*/
421422
if (event_relation->rd_rel->relkind != RELKIND_VIEW &&
422423
event_relation->rd_rel->relkind != RELKIND_MATVIEW)
@@ -453,6 +454,12 @@ DefineQueryRewrite(char *rulename,
453454
errmsg("could not convert table \"%s\" to a view because it has child tables",
454455
RelationGetRelationName(event_relation))));
455456

457+
if (has_superclass(RelationGetRelid(event_relation)))
458+
ereport(ERROR,
459+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
460+
errmsg("could not convert table \"%s\" to a view because it has parent tables",
461+
RelationGetRelationName(event_relation))));
462+
456463
if (event_relation->rd_rel->relrowsecurity)
457464
ereport(ERROR,
458465
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),

src/include/catalog/pg_inherits_fn.h

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern List *find_inheritance_children(Oid parentrelId, LOCKMODE lockmode);
2121
extern List *find_all_inheritors(Oid parentrelId, LOCKMODE lockmode,
2222
List **parents);
2323
extern bool has_subclass(Oid relationId);
24+
extern bool has_superclass(Oid relationId);
2425
extern bool typeInheritsFrom(Oid subclassTypeId, Oid superclassTypeId);
2526

2627
#endif /* PG_INHERITS_FN_H */

src/test/regress/expected/rules.out

+11
Original file line numberDiff line numberDiff line change
@@ -2498,6 +2498,17 @@ select reltoastrelid, relkind, relfrozenxid
24982498
(1 row)
24992499

25002500
drop view fooview;
2501+
-- cannot convert an inheritance parent or child to a view, though
2502+
create table fooview (x int, y text);
2503+
create table fooview_child () inherits (fooview);
2504+
create rule "_RETURN" as on select to fooview do instead
2505+
select 1 as x, 'aaa'::text as y;
2506+
ERROR: could not convert table "fooview" to a view because it has child tables
2507+
create rule "_RETURN" as on select to fooview_child do instead
2508+
select 1 as x, 'aaa'::text as y;
2509+
ERROR: could not convert table "fooview_child" to a view because it has parent tables
2510+
drop table fooview cascade;
2511+
NOTICE: drop cascades to table fooview_child
25012512
--
25022513
-- check for planner problems with complex inherited UPDATES
25032514
--

src/test/regress/sql/rules.sql

+11
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,17 @@ select reltoastrelid, relkind, relfrozenxid
898898

899899
drop view fooview;
900900

901+
-- cannot convert an inheritance parent or child to a view, though
902+
create table fooview (x int, y text);
903+
create table fooview_child () inherits (fooview);
904+
905+
create rule "_RETURN" as on select to fooview do instead
906+
select 1 as x, 'aaa'::text as y;
907+
create rule "_RETURN" as on select to fooview_child do instead
908+
select 1 as x, 'aaa'::text as y;
909+
910+
drop table fooview cascade;
911+
901912
--
902913
-- check for planner problems with complex inherited UPDATES
903914
--

0 commit comments

Comments
 (0)