Skip to content

Commit 0f8cfaf

Browse files
committed
Retain relkind too in RTE_SUBQUERY entries for views.
47bb9db modified the ApplyRetrieveRule()'s conversion of a view's original RTE_RELATION entry into an RTE_SUBQUERY one to retain relid, rellockmode, and perminfoindex so that the executor can lock the view and check its permissions. It seems better to also retain relkind for cross-checking that the exception of an RTE_SUBQUERY entry being allowed to carry relation details only applies to views, so do so. Bump catversion because this changes the output format of RTE_SUBQUERY RTEs. Suggested-by: David Steele <david@pgmasters.net> Reviewed-by: David Steele <david@pgmasters.net> Reviewed-by: Álvaro Herrera <alvherre@alvh.no-ip.org> Discussion: https://postgr.es/m/3953179e-9540-e5d1-a743-4bef368785b0%40pgmasters.net
1 parent ae66716 commit 0f8cfaf

File tree

6 files changed

+22
-12
lines changed

6 files changed

+22
-12
lines changed

src/backend/executor/execMain.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,15 @@ ExecCheckPermissions(List *rangeTable, List *rteperminfos,
595595
if (rte->perminfoindex != 0)
596596
{
597597
/* Sanity checks */
598+
599+
/*
600+
* Only relation RTEs and subquery RTEs that were once relation
601+
* RTEs (views) have their perminfoindex set.
602+
*/
603+
Assert(rte->rtekind == RTE_RELATION ||
604+
(rte->rtekind == RTE_SUBQUERY &&
605+
rte->relkind == RELKIND_VIEW));
606+
598607
(void) getRTEPermissionInfo(rteperminfos, rte);
599608
/* Many-to-one mapping not allowed */
600609
Assert(!bms_is_member(rte->perminfoindex, indexset));

src/backend/nodes/outfuncs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ _outRangeTblEntry(StringInfo str, const RangeTblEntry *node)
513513
WRITE_BOOL_FIELD(security_barrier);
514514
/* we re-use these RELATION fields, too: */
515515
WRITE_OID_FIELD(relid);
516+
WRITE_CHAR_FIELD(relkind);
516517
WRITE_INT_FIELD(rellockmode);
517518
WRITE_UINT_FIELD(perminfoindex);
518519
break;

src/backend/nodes/readfuncs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ _readRangeTblEntry(void)
503503
READ_BOOL_FIELD(security_barrier);
504504
/* we re-use these RELATION fields, too: */
505505
READ_OID_FIELD(relid);
506+
READ_CHAR_FIELD(relkind);
506507
READ_INT_FIELD(rellockmode);
507508
READ_UINT_FIELD(perminfoindex);
508509
break;

src/backend/rewrite/rewriteHandler.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,11 +1851,10 @@ ApplyRetrieveRule(Query *parsetree,
18511851

18521852
/*
18531853
* Clear fields that should not be set in a subquery RTE. Note that we
1854-
* leave the relid, rellockmode, and perminfoindex fields set, so that the
1855-
* view relation can be appropriately locked before execution and its
1856-
* permissions checked.
1854+
* leave the relid, relkind, rellockmode, and perminfoindex fields set, so
1855+
* that the view relation can be appropriately locked before execution and
1856+
* its permissions checked.
18571857
*/
1858-
rte->relkind = 0;
18591858
rte->tablesample = NULL;
18601859
rte->inh = false; /* must not be set for a subquery */
18611860

src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
*/
5858

5959
/* yyyymmddN */
60-
#define CATALOG_VERSION_NO 202305211
60+
#define CATALOG_VERSION_NO 202306141
6161

6262
#endif

src/include/nodes/parsenodes.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,13 +1056,13 @@ typedef struct RangeTblEntry
10561056
* this RTE in the containing struct's list of same; 0 if permissions need
10571057
* not be checked for this RTE.
10581058
*
1059-
* As a special case, relid, rellockmode, and perminfoindex can also be
1060-
* set (nonzero) in an RTE_SUBQUERY RTE. This occurs when we convert an
1061-
* RTE_RELATION RTE naming a view into an RTE_SUBQUERY containing the
1062-
* view's query. We still need to perform run-time locking and permission
1063-
* checks on the view, even though it's not directly used in the query
1064-
* anymore, and the most expedient way to do that is to retain these
1065-
* fields from the old state of the RTE.
1059+
* As a special case, relid, relkind, rellockmode, and perminfoindex can
1060+
* also be set (nonzero) in an RTE_SUBQUERY RTE. This occurs when we
1061+
* convert an RTE_RELATION RTE naming a view into an RTE_SUBQUERY
1062+
* containing the view's query. We still need to perform run-time locking
1063+
* and permission checks on the view, even though it's not directly used
1064+
* in the query anymore, and the most expedient way to do that is to
1065+
* retain these fields from the old state of the RTE.
10661066
*
10671067
* As a special case, RTE_NAMEDTUPLESTORE can also set relid to indicate
10681068
* that the tuple format of the tuplestore is the same as the referenced

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy