Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * inherit.c
4 : : * Routines to process child relations in inheritance trees
5 : : *
6 : : * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/optimizer/util/inherit.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : : #include "postgres.h"
16 : :
17 : : #include "access/sysattr.h"
18 : : #include "access/table.h"
19 : : #include "catalog/partition.h"
20 : : #include "catalog/pg_inherits.h"
21 : : #include "catalog/pg_type.h"
22 : : #include "miscadmin.h"
23 : : #include "nodes/makefuncs.h"
24 : : #include "optimizer/appendinfo.h"
25 : : #include "optimizer/inherit.h"
26 : : #include "optimizer/optimizer.h"
27 : : #include "optimizer/pathnode.h"
28 : : #include "optimizer/plancat.h"
29 : : #include "optimizer/planmain.h"
30 : : #include "optimizer/planner.h"
31 : : #include "optimizer/prep.h"
32 : : #include "optimizer/restrictinfo.h"
33 : : #include "parser/parsetree.h"
34 : : #include "parser/parse_relation.h"
35 : : #include "partitioning/partdesc.h"
36 : : #include "partitioning/partprune.h"
37 : : #include "utils/rel.h"
38 : :
39 : :
40 : : static void expand_partitioned_rtentry(PlannerInfo *root, RelOptInfo *relinfo,
41 : : RangeTblEntry *parentrte,
42 : : Index parentRTindex, Relation parentrel,
43 : : Bitmapset *parent_updatedCols,
44 : : PlanRowMark *top_parentrc, LOCKMODE lockmode);
45 : : static void expand_single_inheritance_child(PlannerInfo *root,
46 : : RangeTblEntry *parentrte,
47 : : Index parentRTindex, Relation parentrel,
48 : : PlanRowMark *top_parentrc, Relation childrel,
49 : : RangeTblEntry **childrte_p,
50 : : Index *childRTindex_p);
51 : : static Bitmapset *translate_col_privs(const Bitmapset *parent_privs,
52 : : List *translated_vars);
53 : : static Bitmapset *translate_col_privs_multilevel(PlannerInfo *root,
54 : : RelOptInfo *rel,
55 : : RelOptInfo *parent_rel,
56 : : Bitmapset *parent_cols);
57 : : static void expand_appendrel_subquery(PlannerInfo *root, RelOptInfo *rel,
58 : : RangeTblEntry *rte, Index rti);
59 : :
60 : :
61 : : /*
62 : : * expand_inherited_rtentry
63 : : * Expand a rangetable entry that has the "inh" bit set.
64 : : *
65 : : * "inh" is only allowed in two cases: RELATION and SUBQUERY RTEs.
66 : : *
67 : : * "inh" on a plain RELATION RTE means that it is a partitioned table or the
68 : : * parent of a traditional-inheritance set. In this case we must add entries
69 : : * for all the interesting child tables to the query's rangetable, and build
70 : : * additional planner data structures for them, including RelOptInfos,
71 : : * AppendRelInfos, and possibly PlanRowMarks.
72 : : *
73 : : * Note that the original RTE is considered to represent the whole inheritance
74 : : * set. In the case of traditional inheritance, the first of the generated
75 : : * RTEs is an RTE for the same table, but with inh = false, to represent the
76 : : * parent table in its role as a simple member of the inheritance set. For
77 : : * partitioning, we don't need a second RTE because the partitioned table
78 : : * itself has no data and need not be scanned.
79 : : *
80 : : * "inh" on a SUBQUERY RTE means that it's the parent of a UNION ALL group,
81 : : * which is treated as an appendrel similarly to inheritance cases; however,
82 : : * we already made RTEs and AppendRelInfos for the subqueries. We only need
83 : : * to build RelOptInfos for them, which is done by expand_appendrel_subquery.
84 : : */
85 : : void
1842 tgl@sss.pgh.pa.us 86 :CBC 8626 : expand_inherited_rtentry(PlannerInfo *root, RelOptInfo *rel,
87 : : RangeTblEntry *rte, Index rti)
88 : : {
89 : : Oid parentOID;
90 : : Relation oldrelation;
91 : : LOCKMODE lockmode;
92 : : PlanRowMark *oldrc;
93 : 8626 : bool old_isParent = false;
94 : 8626 : int old_allMarkTypes = 0;
95 : :
96 [ - + ]: 8626 : Assert(rte->inh); /* else caller error */
97 : :
98 [ + + ]: 8626 : if (rte->rtekind == RTE_SUBQUERY)
99 : : {
100 : 883 : expand_appendrel_subquery(root, rel, rte, rti);
1921 alvherre@alvh.no-ip. 101 : 883 : return;
102 : : }
103 : :
1842 tgl@sss.pgh.pa.us 104 [ - + ]: 7743 : Assert(rte->rtekind == RTE_RELATION);
105 : :
1921 alvherre@alvh.no-ip. 106 : 7743 : parentOID = rte->relid;
107 : :
108 : : /*
109 : : * We used to check has_subclass() here, but there's no longer any need
110 : : * to, because subquery_planner already did.
111 : : */
112 : :
113 : : /*
114 : : * The rewriter should already have obtained an appropriate lock on each
115 : : * relation named in the query, so we can open the parent relation without
116 : : * locking it. However, for each child relation we add to the query, we
117 : : * must obtain an appropriate lock, because this will be the first use of
118 : : * those relations in the parse/rewrite/plan pipeline. Child rels should
119 : : * use the same lockmode as their parent.
120 : : */
1874 rhaas@postgresql.org 121 : 7743 : oldrelation = table_open(parentOID, NoLock);
1921 alvherre@alvh.no-ip. 122 : 7743 : lockmode = rte->rellockmode;
123 : :
124 : : /*
125 : : * If parent relation is selected FOR UPDATE/SHARE, we need to mark its
126 : : * PlanRowMark as isParent = true, and generate a new PlanRowMark for each
127 : : * child.
128 : : */
129 : 7743 : oldrc = get_plan_rowmark(root->rowMarks, rti);
130 [ + + ]: 7743 : if (oldrc)
131 : : {
1842 tgl@sss.pgh.pa.us 132 : 754 : old_isParent = oldrc->isParent;
1921 alvherre@alvh.no-ip. 133 : 754 : oldrc->isParent = true;
134 : : /* Save initial value of allMarkTypes before children add to it */
1842 tgl@sss.pgh.pa.us 135 : 754 : old_allMarkTypes = oldrc->allMarkTypes;
136 : : }
137 : :
138 : : /* Scan the inheritance set and expand it */
1874 rhaas@postgresql.org 139 [ + + ]: 7743 : if (oldrelation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
140 : : {
141 : : RTEPermissionInfo *perminfo;
142 : :
495 alvherre@alvh.no-ip. 143 : 6522 : perminfo = getRTEPermissionInfo(root->parse->rteperminfos, rte);
144 : :
145 : : /*
146 : : * Partitioned table, so set up for partitioning.
147 : : */
1921 148 [ - + ]: 6522 : Assert(rte->relkind == RELKIND_PARTITIONED_TABLE);
149 : :
150 : : /*
151 : : * Recursively expand and lock the partitions. While at it, also
152 : : * extract the partition key columns of all the partitioned tables.
153 : : */
1842 tgl@sss.pgh.pa.us 154 : 6522 : expand_partitioned_rtentry(root, rel, rte, rti,
155 : : oldrelation,
156 : : perminfo->updatedCols,
157 : : oldrc, lockmode);
158 : : }
159 : : else
160 : : {
161 : : /*
162 : : * Ordinary table, so process traditional-inheritance children. (Note
163 : : * that partitioned tables are not allowed to have inheritance
164 : : * children, so it's not possible for both cases to apply.)
165 : : */
166 : : List *inhOIDs;
167 : : ListCell *l;
168 : :
169 : : /* Scan for all members of inheritance set, acquire needed locks */
1874 rhaas@postgresql.org 170 : 1221 : inhOIDs = find_all_inheritors(parentOID, lockmode, NULL);
171 : :
172 : : /*
173 : : * We used to special-case the situation where the table no longer has
174 : : * any children, by clearing rte->inh and exiting. That no longer
175 : : * works, because this function doesn't get run until after decisions
176 : : * have been made that depend on rte->inh. We have to treat such
177 : : * situations as normal inheritance. The table itself should always
178 : : * have been found, though.
179 : : */
1842 tgl@sss.pgh.pa.us 180 [ - + ]: 1221 : Assert(inhOIDs != NIL);
181 [ - + ]: 1221 : Assert(linitial_oid(inhOIDs) == parentOID);
182 : :
183 : : /* Expand simple_rel_array and friends to hold child objects. */
184 : 1221 : expand_planner_arrays(root, list_length(inhOIDs));
185 : :
186 : : /*
187 : : * Expand inheritance children in the order the OIDs were returned by
188 : : * find_all_inheritors.
189 : : */
1921 alvherre@alvh.no-ip. 190 [ + - + + : 4560 : foreach(l, inhOIDs)
+ + ]
191 : : {
192 : 3340 : Oid childOID = lfirst_oid(l);
193 : : Relation newrelation;
194 : : RangeTblEntry *childrte;
195 : : Index childRTindex;
196 : :
197 : : /* Open rel if needed; we already have required locks */
198 [ + + ]: 3340 : if (childOID != parentOID)
1910 andres@anarazel.de 199 : 2119 : newrelation = table_open(childOID, NoLock);
200 : : else
1921 alvherre@alvh.no-ip. 201 : 1221 : newrelation = oldrelation;
202 : :
203 : : /*
204 : : * It is possible that the parent table has children that are temp
205 : : * tables of other backends. We cannot safely access such tables
206 : : * (because of buffering issues), and the best thing to do seems
207 : : * to be to silently ignore them.
208 : : */
209 [ + + + + : 3340 : if (childOID != parentOID && RELATION_IS_OTHER_TEMP(newrelation))
+ + ]
210 : : {
1910 andres@anarazel.de 211 : 21 : table_close(newrelation, lockmode);
1921 alvherre@alvh.no-ip. 212 : 21 : continue;
213 : : }
214 : :
215 : : /* Create RTE and AppendRelInfo, plus PlanRowMark if needed. */
1842 tgl@sss.pgh.pa.us 216 : 3319 : expand_single_inheritance_child(root, rte, rti, oldrelation,
217 : : oldrc, newrelation,
218 : : &childrte, &childRTindex);
219 : :
220 : : /* Create the otherrel RelOptInfo too. */
221 : 3318 : (void) build_simple_rel(root, childRTindex, rel);
222 : :
223 : : /* Close child relations, but keep locks */
1921 alvherre@alvh.no-ip. 224 [ + + ]: 3318 : if (childOID != parentOID)
1910 andres@anarazel.de 225 : 2097 : table_close(newrelation, NoLock);
226 : : }
227 : : }
228 : :
229 : : /*
230 : : * Some children might require different mark types, which would've been
231 : : * reported into oldrc. If so, add relevant entries to the top-level
232 : : * targetlist and update parent rel's reltarget. This should match what
233 : : * preprocess_targetlist() would have added if the mark types had been
234 : : * requested originally.
235 : : *
236 : : * (Someday it might be useful to fold these resjunk columns into the
237 : : * row-identity-column management used for UPDATE/DELETE. Today is not
238 : : * that day, however.)
239 : : */
1842 tgl@sss.pgh.pa.us 240 [ + + ]: 7742 : if (oldrc)
241 : : {
242 : 754 : int new_allMarkTypes = oldrc->allMarkTypes;
243 : : Var *var;
244 : : TargetEntry *tle;
245 : : char resname[32];
246 : 754 : List *newvars = NIL;
247 : :
248 : : /* Add TID junk Var if needed, unless we had it already */
1047 249 [ + + ]: 754 : if (new_allMarkTypes & ~(1 << ROW_MARK_COPY) &&
250 [ + + ]: 752 : !(old_allMarkTypes & ~(1 << ROW_MARK_COPY)))
251 : : {
252 : : /* Need to fetch TID */
253 : 2 : var = makeVar(oldrc->rti,
254 : : SelfItemPointerAttributeNumber,
255 : : TIDOID,
256 : : -1,
257 : : InvalidOid,
258 : : 0);
259 : 2 : snprintf(resname, sizeof(resname), "ctid%u", oldrc->rowmarkId);
260 : 2 : tle = makeTargetEntry((Expr *) var,
261 : 2 : list_length(root->processed_tlist) + 1,
262 : : pstrdup(resname),
263 : : true);
264 : 2 : root->processed_tlist = lappend(root->processed_tlist, tle);
265 : 2 : newvars = lappend(newvars, var);
266 : : }
267 : :
268 : : /* Add whole-row junk Var if needed, unless we had it already */
1842 269 [ + + ]: 754 : if ((new_allMarkTypes & (1 << ROW_MARK_COPY)) &&
270 [ + + ]: 23 : !(old_allMarkTypes & (1 << ROW_MARK_COPY)))
271 : : {
1842 tgl@sss.pgh.pa.us 272 :UBC 0 : var = makeWholeRowVar(planner_rt_fetch(oldrc->rti, root),
1842 tgl@sss.pgh.pa.us 273 [ + - ]:CBC 19 : oldrc->rti,
274 : : 0,
275 : : false);
276 : 19 : snprintf(resname, sizeof(resname), "wholerow%u", oldrc->rowmarkId);
277 : 19 : tle = makeTargetEntry((Expr *) var,
278 : 19 : list_length(root->processed_tlist) + 1,
279 : : pstrdup(resname),
280 : : true);
281 : 19 : root->processed_tlist = lappend(root->processed_tlist, tle);
282 : 19 : newvars = lappend(newvars, var);
283 : : }
284 : :
285 : : /* Add tableoid junk Var, unless we had it already */
286 [ + - ]: 754 : if (!old_isParent)
287 : : {
288 : 754 : var = makeVar(oldrc->rti,
289 : : TableOidAttributeNumber,
290 : : OIDOID,
291 : : -1,
292 : : InvalidOid,
293 : : 0);
294 : 754 : snprintf(resname, sizeof(resname), "tableoid%u", oldrc->rowmarkId);
295 : 754 : tle = makeTargetEntry((Expr *) var,
296 : 754 : list_length(root->processed_tlist) + 1,
297 : : pstrdup(resname),
298 : : true);
299 : 754 : root->processed_tlist = lappend(root->processed_tlist, tle);
300 : 754 : newvars = lappend(newvars, var);
301 : : }
302 : :
303 : : /*
304 : : * Add the newly added Vars to parent's reltarget. We needn't worry
305 : : * about the children's reltargets, they'll be made later.
306 : : */
606 307 : 754 : add_vars_to_targetlist(root, newvars, bms_make_singleton(0));
308 : : }
309 : :
1910 andres@anarazel.de 310 : 7742 : table_close(oldrelation, NoLock);
311 : : }
312 : :
313 : : /*
314 : : * expand_partitioned_rtentry
315 : : * Recursively expand an RTE for a partitioned table.
316 : : */
317 : : static void
1842 tgl@sss.pgh.pa.us 318 : 8082 : expand_partitioned_rtentry(PlannerInfo *root, RelOptInfo *relinfo,
319 : : RangeTblEntry *parentrte,
320 : : Index parentRTindex, Relation parentrel,
321 : : Bitmapset *parent_updatedCols,
322 : : PlanRowMark *top_parentrc, LOCKMODE lockmode)
323 : : {
324 : : PartitionDesc partdesc;
325 : : Bitmapset *live_parts;
326 : : int num_live_parts;
327 : : int i;
328 : :
329 : 8082 : check_stack_depth();
330 : :
331 [ - + ]: 8082 : Assert(parentrte->inh);
332 : :
1865 rhaas@postgresql.org 333 : 8082 : partdesc = PartitionDirectoryLookup(root->glob->partition_directory,
334 : : parentrel);
335 : :
336 : : /* A partitioned table should always have a partition descriptor. */
1921 alvherre@alvh.no-ip. 337 [ - + ]: 8082 : Assert(partdesc);
338 : :
339 : : /*
340 : : * Note down whether any partition key cols are being updated. Though it's
341 : : * the root partitioned table's updatedCols we are interested in,
342 : : * parent_updatedCols provided by the caller contains the root partrel's
343 : : * updatedCols translated to match the attribute ordering of parentrel.
344 : : */
345 [ + + ]: 8082 : if (!root->partColsUpdated)
346 : 7929 : root->partColsUpdated =
495 347 : 7929 : has_partition_attrs(parentrel, parent_updatedCols, NULL);
348 : :
349 : : /* Nothing further to do here if there are no partitions. */
1921 350 [ + + ]: 8082 : if (partdesc->nparts == 0)
351 : 18 : return;
352 : :
353 : : /*
354 : : * Perform partition pruning using restriction clauses assigned to parent
355 : : * relation. live_parts will contain PartitionDesc indexes of partitions
356 : : * that survive pruning. Below, we will initialize child objects for the
357 : : * surviving partitions.
358 : : */
985 drowley@postgresql.o 359 : 8064 : relinfo->live_parts = live_parts = prune_append_rel_partitions(relinfo);
360 : :
361 : : /* Expand simple_rel_array and friends to hold child objects. */
1842 tgl@sss.pgh.pa.us 362 : 8064 : num_live_parts = bms_num_members(live_parts);
363 [ + + ]: 8064 : if (num_live_parts > 0)
364 : 7917 : expand_planner_arrays(root, num_live_parts);
365 : :
366 : : /*
367 : : * We also store partition RelOptInfo pointers in the parent relation.
368 : : * Since we're palloc0'ing, slots corresponding to pruned partitions will
369 : : * contain NULL.
370 : : */
371 [ - + ]: 8064 : Assert(relinfo->part_rels == NULL);
372 : 8064 : relinfo->part_rels = (RelOptInfo **)
373 : 8064 : palloc0(relinfo->nparts * sizeof(RelOptInfo *));
374 : :
375 : : /*
376 : : * Create a child RTE for each live partition. Note that unlike
377 : : * traditional inheritance, we don't need a child RTE for the partitioned
378 : : * table itself, because it's not going to be scanned.
379 : : */
380 : 8064 : i = -1;
381 [ + + ]: 24044 : while ((i = bms_next_member(live_parts, i)) >= 0)
382 : : {
1921 alvherre@alvh.no-ip. 383 : 15980 : Oid childOID = partdesc->oids[i];
384 : : Relation childrel;
385 : : RangeTblEntry *childrte;
386 : : Index childRTindex;
387 : : RelOptInfo *childrelinfo;
388 : :
389 : : /* Open rel, acquiring required locks */
1874 rhaas@postgresql.org 390 : 15980 : childrel = table_open(childOID, lockmode);
391 : :
392 : : /*
393 : : * Temporary partitions belonging to other sessions should have been
394 : : * disallowed at definition, but for paranoia's sake, let's double
395 : : * check.
396 : : */
1921 alvherre@alvh.no-ip. 397 [ + + - + ]: 15980 : if (RELATION_IS_OTHER_TEMP(childrel))
1921 alvherre@alvh.no-ip. 398 [ # # ]:UBC 0 : elog(ERROR, "temporary relation from another session found as partition");
399 : :
400 : : /* Create RTE and AppendRelInfo, plus PlanRowMark if needed. */
1921 alvherre@alvh.no-ip. 401 :CBC 15980 : expand_single_inheritance_child(root, parentrte, parentRTindex,
402 : : parentrel, top_parentrc, childrel,
403 : : &childrte, &childRTindex);
404 : :
405 : : /* Create the otherrel RelOptInfo too. */
1842 tgl@sss.pgh.pa.us 406 : 15980 : childrelinfo = build_simple_rel(root, childRTindex, relinfo);
407 : 15980 : relinfo->part_rels[i] = childrelinfo;
1467 efujita@postgresql.o 408 : 31960 : relinfo->all_partrels = bms_add_members(relinfo->all_partrels,
409 : 15980 : childrelinfo->relids);
410 : :
411 : : /* If this child is itself partitioned, recurse */
1921 alvherre@alvh.no-ip. 412 [ + + ]: 15980 : if (childrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
413 : : {
495 414 : 1560 : AppendRelInfo *appinfo = root->append_rel_array[childRTindex];
415 : : Bitmapset *child_updatedCols;
416 : :
417 : 1560 : child_updatedCols = translate_col_privs(parent_updatedCols,
418 : : appinfo->translated_vars);
419 : :
1842 tgl@sss.pgh.pa.us 420 : 1560 : expand_partitioned_rtentry(root, childrelinfo,
421 : : childrte, childRTindex,
422 : : childrel,
423 : : child_updatedCols,
424 : : top_parentrc, lockmode);
425 : : }
426 : :
427 : : /* Close child relation, but keep locks */
1910 andres@anarazel.de 428 : 15980 : table_close(childrel, NoLock);
429 : : }
430 : : }
431 : :
432 : : /*
433 : : * expand_single_inheritance_child
434 : : * Build a RangeTblEntry and an AppendRelInfo, plus maybe a PlanRowMark.
435 : : *
436 : : * We now expand the partition hierarchy level by level, creating a
437 : : * corresponding hierarchy of AppendRelInfos and RelOptInfos, where each
438 : : * partitioned descendant acts as a parent of its immediate partitions.
439 : : * (This is a difference from what older versions of PostgreSQL did and what
440 : : * is still done in the case of table inheritance for unpartitioned tables,
441 : : * where the hierarchy is flattened during RTE expansion.)
442 : : *
443 : : * PlanRowMarks still carry the top-parent's RTI, and the top-parent's
444 : : * allMarkTypes field still accumulates values from all descendents.
445 : : *
446 : : * "parentrte" and "parentRTindex" are immediate parent's RTE and
447 : : * RTI. "top_parentrc" is top parent's PlanRowMark.
448 : : *
449 : : * The child RangeTblEntry and its RTI are returned in "childrte_p" and
450 : : * "childRTindex_p" resp.
451 : : */
452 : : static void
1921 alvherre@alvh.no-ip. 453 : 19299 : expand_single_inheritance_child(PlannerInfo *root, RangeTblEntry *parentrte,
454 : : Index parentRTindex, Relation parentrel,
455 : : PlanRowMark *top_parentrc, Relation childrel,
456 : : RangeTblEntry **childrte_p,
457 : : Index *childRTindex_p)
458 : : {
459 : 19299 : Query *parse = root->parse;
171 amitlan@postgresql.o 460 : 19299 : Oid parentOID PG_USED_FOR_ASSERTS_ONLY =
461 : : RelationGetRelid(parentrel);
1921 alvherre@alvh.no-ip. 462 : 19299 : Oid childOID = RelationGetRelid(childrel);
463 : : RangeTblEntry *childrte;
464 : : Index childRTindex;
465 : : AppendRelInfo *appinfo;
466 : : TupleDesc child_tupdesc;
467 : : List *parent_colnames;
468 : : List *child_colnames;
469 : :
470 : : /*
471 : : * Build an RTE for the child, and attach to query's rangetable list. We
472 : : * copy most scalar fields of the parent's RTE, but replace relation OID,
473 : : * relkind, and inh for the child. Set the child's securityQuals to
474 : : * empty, because we only want to apply the parent's RLS conditions
475 : : * regardless of what RLS properties individual children may have. (This
476 : : * is an intentional choice to make inherited RLS work like regular
477 : : * permissions checks.) The parent securityQuals will be propagated to
478 : : * children along with other base restriction clauses, so we don't need to
479 : : * do it here. Other infrastructure of the parent RTE has to be
480 : : * translated to match the child table's column ordering, which we do
481 : : * below, so a "flat" copy is sufficient to start with.
482 : : */
1595 tgl@sss.pgh.pa.us 483 : 19299 : childrte = makeNode(RangeTblEntry);
484 : 19299 : memcpy(childrte, parentrte, sizeof(RangeTblEntry));
485 [ - + ]: 19299 : Assert(parentrte->rtekind == RTE_RELATION); /* else this is dubious */
1921 alvherre@alvh.no-ip. 486 : 19299 : childrte->relid = childOID;
487 : 19299 : childrte->relkind = childrel->rd_rel->relkind;
488 : : /* A partitioned child will need to be expanded further. */
1846 tgl@sss.pgh.pa.us 489 [ + + ]: 19299 : if (childrte->relkind == RELKIND_PARTITIONED_TABLE)
490 : : {
491 [ - + ]: 1560 : Assert(childOID != parentOID);
1921 alvherre@alvh.no-ip. 492 : 1560 : childrte->inh = true;
493 : : }
494 : : else
495 : 17739 : childrte->inh = false;
496 : 19299 : childrte->securityQuals = NIL;
497 : :
498 : : /* No permission checking for child RTEs. */
171 amitlan@postgresql.o 499 : 19299 : childrte->perminfoindex = 0;
500 : :
501 : : /* Link not-yet-fully-filled child RTE into data structures */
1921 alvherre@alvh.no-ip. 502 : 19299 : parse->rtable = lappend(parse->rtable, childrte);
503 : 19299 : childRTindex = list_length(parse->rtable);
1595 tgl@sss.pgh.pa.us 504 : 19299 : *childrte_p = childrte;
1921 alvherre@alvh.no-ip. 505 : 19299 : *childRTindex_p = childRTindex;
506 : :
507 : : /*
508 : : * Build an AppendRelInfo struct for each parent/child pair.
509 : : */
1846 tgl@sss.pgh.pa.us 510 : 19299 : appinfo = make_append_rel_info(parentrel, childrel,
511 : : parentRTindex, childRTindex);
1842 512 : 19298 : root->append_rel_list = lappend(root->append_rel_list, appinfo);
513 : :
514 : : /* tablesample is probably null, but copy it */
1595 515 : 19298 : childrte->tablesample = copyObject(parentrte->tablesample);
516 : :
517 : : /*
518 : : * Construct an alias clause for the child, which we can also use as eref.
519 : : * This is important so that EXPLAIN will print the right column aliases
520 : : * for child-table columns. (Since ruleutils.c doesn't have any easy way
521 : : * to reassociate parent and child columns, we must get the child column
522 : : * aliases right to start with. Note that setting childrte->alias forces
523 : : * ruleutils.c to use these column names, which it otherwise would not.)
524 : : */
525 : 19298 : child_tupdesc = RelationGetDescr(childrel);
526 : 19298 : parent_colnames = parentrte->eref->colnames;
527 : 19298 : child_colnames = NIL;
528 [ + + ]: 73910 : for (int cattno = 0; cattno < child_tupdesc->natts; cattno++)
529 : : {
530 : 54612 : Form_pg_attribute att = TupleDescAttr(child_tupdesc, cattno);
531 : : const char *attname;
532 : :
533 [ + + ]: 54612 : if (att->attisdropped)
534 : : {
535 : : /* Always insert an empty string for a dropped column */
536 : 1117 : attname = "";
537 : : }
538 [ + + + - ]: 105055 : else if (appinfo->parent_colnos[cattno] > 0 &&
539 : 51560 : appinfo->parent_colnos[cattno] <= list_length(parent_colnames))
540 : : {
541 : : /* Duplicate the query-assigned name for the parent column */
542 : 51560 : attname = strVal(list_nth(parent_colnames,
543 : : appinfo->parent_colnos[cattno] - 1));
544 : : }
545 : : else
546 : : {
547 : : /* New column, just use its real name */
548 : 1935 : attname = NameStr(att->attname);
549 : : }
550 : 54612 : child_colnames = lappend(child_colnames, makeString(pstrdup(attname)));
551 : : }
552 : :
553 : : /*
554 : : * We just duplicate the parent's table alias name for each child. If the
555 : : * plan gets printed, ruleutils.c has to sort out unique table aliases to
556 : : * use, which it can handle.
557 : : */
558 : 19298 : childrte->alias = childrte->eref = makeAlias(parentrte->eref->aliasname,
559 : : child_colnames);
560 : :
561 : : /*
562 : : * Store the RTE and appinfo in the respective PlannerInfo arrays, which
563 : : * the caller must already have allocated space for.
564 : : */
1842 565 [ - + ]: 19298 : Assert(childRTindex < root->simple_rel_array_size);
566 [ - + ]: 19298 : Assert(root->simple_rte_array[childRTindex] == NULL);
567 : 19298 : root->simple_rte_array[childRTindex] = childrte;
568 [ - + ]: 19298 : Assert(root->append_rel_array[childRTindex] == NULL);
569 : 19298 : root->append_rel_array[childRTindex] = appinfo;
570 : :
571 : : /*
572 : : * Build a PlanRowMark if parent is marked FOR UPDATE/SHARE.
573 : : */
1921 alvherre@alvh.no-ip. 574 [ + + ]: 19298 : if (top_parentrc)
575 : : {
576 : 1064 : PlanRowMark *childrc = makeNode(PlanRowMark);
577 : :
578 : 1064 : childrc->rti = childRTindex;
579 : 1064 : childrc->prti = top_parentrc->rti;
580 : 1064 : childrc->rowmarkId = top_parentrc->rowmarkId;
581 : : /* Reselect rowmark type, because relkind might not match parent */
582 : 1064 : childrc->markType = select_rowmark_type(childrte,
583 : : top_parentrc->strength);
584 : 1064 : childrc->allMarkTypes = (1 << childrc->markType);
585 : 1064 : childrc->strength = top_parentrc->strength;
586 : 1064 : childrc->waitPolicy = top_parentrc->waitPolicy;
587 : :
588 : : /*
589 : : * We mark RowMarks for partitioned child tables as parent RowMarks so
590 : : * that the executor ignores them (except their existence means that
591 : : * the child tables will be locked using the appropriate mode).
592 : : */
593 : 1064 : childrc->isParent = (childrte->relkind == RELKIND_PARTITIONED_TABLE);
594 : :
595 : : /* Include child's rowmark type in top parent's allMarkTypes */
596 : 1064 : top_parentrc->allMarkTypes |= childrc->allMarkTypes;
597 : :
598 : 1064 : root->rowMarks = lappend(root->rowMarks, childrc);
599 : : }
600 : :
601 : : /*
602 : : * If we are creating a child of the query target relation (only possible
603 : : * in UPDATE/DELETE/MERGE), add it to all_result_relids, as well as
604 : : * leaf_result_relids if appropriate, and make sure that we generate
605 : : * required row-identity data.
606 : : */
1110 tgl@sss.pgh.pa.us 607 [ + + ]: 19298 : if (bms_is_member(parentRTindex, root->all_result_relids))
608 : : {
609 : : /* OK, record the child as a result rel too. */
610 : 2712 : root->all_result_relids = bms_add_member(root->all_result_relids,
611 : : childRTindex);
612 : :
613 : : /* Non-leaf partitions don't need any row identity info. */
614 [ + + ]: 2712 : if (childrte->relkind != RELKIND_PARTITIONED_TABLE)
615 : : {
616 : : Var *rrvar;
617 : :
618 : 2439 : root->leaf_result_relids = bms_add_member(root->leaf_result_relids,
619 : : childRTindex);
620 : :
621 : : /*
622 : : * If we have any child target relations, assume they all need to
623 : : * generate a junk "tableoid" column. (If only one child survives
624 : : * pruning, we wouldn't really need this, but it's not worth
625 : : * thrashing about to avoid it.)
626 : : */
627 : 2439 : rrvar = makeVar(childRTindex,
628 : : TableOidAttributeNumber,
629 : : OIDOID,
630 : : -1,
631 : : InvalidOid,
632 : : 0);
633 : 2439 : add_row_identity_var(root, rrvar, childRTindex, "tableoid");
634 : :
635 : : /* Register any row-identity columns needed by this child. */
636 : 2439 : add_row_identity_columns(root, childRTindex,
637 : : childrte, childrel);
638 : : }
639 : : }
1921 alvherre@alvh.no-ip. 640 : 19298 : }
641 : :
642 : : /*
643 : : * get_rel_all_updated_cols
644 : : * Returns the set of columns of a given "simple" relation that are
645 : : * updated by this query.
646 : : */
647 : : Bitmapset *
495 648 : 35 : get_rel_all_updated_cols(PlannerInfo *root, RelOptInfo *rel)
649 : : {
650 : : Index relid;
651 : : RangeTblEntry *rte;
652 : : RTEPermissionInfo *perminfo;
653 : : Bitmapset *updatedCols,
654 : : *extraUpdatedCols;
655 : :
656 [ - + ]: 35 : Assert(root->parse->commandType == CMD_UPDATE);
657 [ + + - + ]: 35 : Assert(IS_SIMPLE_REL(rel));
658 : :
659 : : /*
660 : : * We obtain updatedCols for the query's result relation. Then, if
661 : : * necessary, we map it to the column numbers of the relation for which
662 : : * they were requested.
663 : : */
664 : 35 : relid = root->parse->resultRelation;
665 [ + - ]: 35 : rte = planner_rt_fetch(relid, root);
666 : 35 : perminfo = getRTEPermissionInfo(root->parse->rteperminfos, rte);
667 : :
668 : 35 : updatedCols = perminfo->updatedCols;
669 : :
670 [ + + ]: 35 : if (rel->relid != relid)
671 : : {
672 : 17 : RelOptInfo *top_parent_rel = find_base_rel(root, relid);
673 : :
674 [ - + - - : 17 : Assert(IS_OTHER_REL(rel));
- - ]
675 : :
676 : 17 : updatedCols = translate_col_privs_multilevel(root, rel, top_parent_rel,
677 : : updatedCols);
678 : : }
679 : :
680 : : /*
681 : : * Now we must check to see if there are any generated columns that depend
682 : : * on the updatedCols, and add them to the result.
683 : : */
465 tgl@sss.pgh.pa.us 684 : 35 : extraUpdatedCols = get_dependent_generated_columns(root, rel->relid,
685 : : updatedCols);
686 : :
495 alvherre@alvh.no-ip. 687 : 35 : return bms_union(updatedCols, extraUpdatedCols);
688 : : }
689 : :
690 : : /*
691 : : * translate_col_privs
692 : : * Translate a bitmapset representing per-column privileges from the
693 : : * parent rel's attribute numbering to the child's.
694 : : *
695 : : * The only surprise here is that we don't translate a parent whole-row
696 : : * reference into a child whole-row reference. That would mean requiring
697 : : * permissions on all child columns, which is overly strict, since the
698 : : * query is really only going to reference the inherited columns. Instead
699 : : * we set the per-column bits for all inherited columns.
700 : : */
701 : : static Bitmapset *
1915 702 : 1579 : translate_col_privs(const Bitmapset *parent_privs,
703 : : List *translated_vars)
704 : : {
705 : 1579 : Bitmapset *child_privs = NULL;
706 : : bool whole_row;
707 : : int attno;
708 : : ListCell *lc;
709 : :
710 : : /* System attributes have the same numbers in all tables */
711 [ + + ]: 11053 : for (attno = FirstLowInvalidHeapAttributeNumber + 1; attno < 0; attno++)
712 : : {
713 [ - + ]: 9474 : if (bms_is_member(attno - FirstLowInvalidHeapAttributeNumber,
714 : : parent_privs))
1915 alvherre@alvh.no-ip. 715 :UBC 0 : child_privs = bms_add_member(child_privs,
716 : : attno - FirstLowInvalidHeapAttributeNumber);
717 : : }
718 : :
719 : : /* Check if parent has whole-row reference */
1915 alvherre@alvh.no-ip. 720 :CBC 1579 : whole_row = bms_is_member(InvalidAttrNumber - FirstLowInvalidHeapAttributeNumber,
721 : : parent_privs);
722 : :
723 : : /* And now translate the regular user attributes, using the vars list */
724 : 1579 : attno = InvalidAttrNumber;
725 [ + - + + : 5733 : foreach(lc, translated_vars)
+ + ]
726 : : {
727 : 4154 : Var *var = lfirst_node(Var, lc);
728 : :
729 : 4154 : attno++;
730 [ + + ]: 4154 : if (var == NULL) /* ignore dropped columns */
731 : 63 : continue;
732 [ + - + + ]: 8182 : if (whole_row ||
733 : 4091 : bms_is_member(attno - FirstLowInvalidHeapAttributeNumber,
734 : : parent_privs))
735 : 270 : child_privs = bms_add_member(child_privs,
736 : 270 : var->varattno - FirstLowInvalidHeapAttributeNumber);
737 : : }
738 : :
739 : 1579 : return child_privs;
740 : : }
741 : :
742 : : /*
743 : : * translate_col_privs_multilevel
744 : : * Recursively translates the column numbers contained in 'parent_cols'
745 : : * to the column numbers of a descendant relation given by 'rel'
746 : : *
747 : : * Note that because this is based on translate_col_privs, it will expand
748 : : * a whole-row reference into all inherited columns. This is not an issue
749 : : * for current usages, but beware.
750 : : */
751 : : static Bitmapset *
465 tgl@sss.pgh.pa.us 752 : 19 : translate_col_privs_multilevel(PlannerInfo *root, RelOptInfo *rel,
753 : : RelOptInfo *parent_rel,
754 : : Bitmapset *parent_cols)
755 : : {
756 : : AppendRelInfo *appinfo;
757 : :
758 : : /* Fast path for easy case. */
759 [ - + ]: 19 : if (parent_cols == NULL)
465 tgl@sss.pgh.pa.us 760 :UBC 0 : return NULL;
761 : :
762 : : /* Recurse if immediate parent is not the top parent. */
465 tgl@sss.pgh.pa.us 763 [ + + ]:CBC 19 : if (rel->parent != parent_rel)
764 : : {
765 [ + - ]: 2 : if (rel->parent)
766 : 2 : parent_cols = translate_col_privs_multilevel(root, rel->parent,
767 : : parent_rel,
768 : : parent_cols);
769 : : else
465 tgl@sss.pgh.pa.us 770 [ # # ]:UBC 0 : elog(ERROR, "rel with relid %u is not a child rel", rel->relid);
771 : : }
772 : :
773 : : /* Now translate for this child. */
465 tgl@sss.pgh.pa.us 774 [ - + ]:CBC 19 : Assert(root->append_rel_array != NULL);
775 : 19 : appinfo = root->append_rel_array[rel->relid];
776 [ - + ]: 19 : Assert(appinfo != NULL);
777 : :
778 : 19 : return translate_col_privs(parent_cols, appinfo->translated_vars);
779 : : }
780 : :
781 : : /*
782 : : * expand_appendrel_subquery
783 : : * Add "other rel" RelOptInfos for the children of an appendrel baserel
784 : : *
785 : : * "rel" is a subquery relation that has the rte->inh flag set, meaning it
786 : : * is a UNION ALL subquery that's been flattened into an appendrel, with
787 : : * child subqueries listed in root->append_rel_list. We need to build
788 : : * a RelOptInfo for each child relation so that we can plan scans on them.
789 : : */
790 : : static void
1842 791 : 883 : expand_appendrel_subquery(PlannerInfo *root, RelOptInfo *rel,
792 : : RangeTblEntry *rte, Index rti)
793 : : {
794 : : ListCell *l;
795 : :
796 [ + - + + : 3530 : foreach(l, root->append_rel_list)
+ + ]
797 : : {
798 : 2647 : AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
799 : 2647 : Index childRTindex = appinfo->child_relid;
800 : : RangeTblEntry *childrte;
801 : : RelOptInfo *childrel;
802 : :
803 : : /* append_rel_list contains all append rels; ignore others */
804 [ + + ]: 2647 : if (appinfo->parent_relid != rti)
805 : 573 : continue;
806 : :
807 : : /* find the child RTE, which should already exist */
808 [ - + ]: 2074 : Assert(childRTindex < root->simple_rel_array_size);
809 : 2074 : childrte = root->simple_rte_array[childRTindex];
810 [ - + ]: 2074 : Assert(childrte != NULL);
811 : :
812 : : /* Build the child RelOptInfo. */
813 : 2074 : childrel = build_simple_rel(root, childRTindex, rel);
814 : :
815 : : /* Child may itself be an inherited rel, either table or subquery. */
816 [ + + ]: 2074 : if (childrte->inh)
817 : 106 : expand_inherited_rtentry(root, childrel, childrte, childRTindex);
818 : : }
819 : 883 : }
820 : :
821 : :
822 : : /*
823 : : * apply_child_basequals
824 : : * Populate childrel's base restriction quals from parent rel's quals,
825 : : * translating Vars using appinfo and re-checking for quals which are
826 : : * constant-TRUE or constant-FALSE when applied to this child relation.
827 : : *
828 : : * If any of the resulting clauses evaluate to constant false or NULL, we
829 : : * return false and don't apply any quals. Caller should mark the relation as
830 : : * a dummy rel in this case, since it doesn't need to be scanned. Constant
831 : : * true quals are ignored.
832 : : */
833 : : bool
834 : 21372 : apply_child_basequals(PlannerInfo *root, RelOptInfo *parentrel,
835 : : RelOptInfo *childrel, RangeTblEntry *childRTE,
836 : : AppendRelInfo *appinfo)
837 : : {
838 : : List *childquals;
839 : : Index cq_min_security;
840 : : ListCell *lc;
841 : :
842 : : /*
843 : : * The child rel's targetlist might contain non-Var expressions, which
844 : : * means that substitution into the quals could produce opportunities for
845 : : * const-simplification, and perhaps even pseudoconstant quals. Therefore,
846 : : * transform each RestrictInfo separately to see if it reduces to a
847 : : * constant or pseudoconstant. (We must process them separately to keep
848 : : * track of the security level of each qual.)
849 : : */
850 : 21372 : childquals = NIL;
851 : 21372 : cq_min_security = UINT_MAX;
852 [ + + + + : 35025 : foreach(lc, parentrel->baserestrictinfo)
+ + ]
853 : : {
854 : 13698 : RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
855 : : Node *childqual;
856 : : ListCell *lc2;
857 : :
858 [ - + ]: 13698 : Assert(IsA(rinfo, RestrictInfo));
859 : 13698 : childqual = adjust_appendrel_attrs(root,
860 : 13698 : (Node *) rinfo->clause,
861 : : 1, &appinfo);
862 : 13698 : childqual = eval_const_expressions(root, childqual);
863 : : /* check for flat-out constant */
864 [ + - + + ]: 13698 : if (childqual && IsA(childqual, Const))
865 : : {
866 [ + - ]: 78 : if (((Const *) childqual)->constisnull ||
867 [ + + ]: 78 : !DatumGetBool(((Const *) childqual)->constvalue))
868 : : {
869 : : /* Restriction reduces to constant FALSE or NULL */
870 : 45 : return false;
871 : : }
872 : : /* Restriction reduces to constant TRUE, so drop it */
873 : 39 : continue;
874 : : }
875 : : /* might have gotten an AND clause, if so flatten it */
876 [ + - + + : 27240 : foreach(lc2, make_ands_implicit((Expr *) childqual))
+ + ]
877 : : {
878 : 13626 : Node *onecq = (Node *) lfirst(lc2);
879 : : bool pseudoconstant;
880 : : RestrictInfo *childrinfo;
881 : :
882 : : /* check for pseudoconstant (no Vars or volatile functions) */
883 : 13626 : pseudoconstant =
884 [ + + ]: 13649 : !contain_vars_of_level(onecq, 0) &&
885 [ + - ]: 23 : !contain_volatile_functions(onecq);
886 [ + + ]: 13626 : if (pseudoconstant)
887 : : {
888 : : /* tell createplan.c to check for gating quals */
889 : 23 : root->hasPseudoConstantQuals = true;
890 : : }
891 : : /* reconstitute RestrictInfo with appropriate properties */
2 drowley@postgresql.o 892 :GNC 13626 : childrinfo = make_restrictinfo(root,
893 : : (Expr *) onecq,
894 : 13626 : rinfo->is_pushed_down,
895 : 13626 : rinfo->has_clone,
896 : 13626 : rinfo->is_clone,
897 : : pseudoconstant,
898 : : rinfo->security_level,
899 : : NULL, NULL, NULL);
900 : :
901 : : /* Restriction is proven always false */
902 [ + + ]: 13626 : if (restriction_is_always_false(root, childrinfo))
903 : 6 : return false;
904 : : /* Restriction is proven always true, so drop it */
905 [ + + ]: 13620 : if (restriction_is_always_true(root, childrinfo))
906 : 6 : continue;
907 : :
908 : 13614 : childquals = lappend(childquals, childrinfo);
909 : : /* track minimum security level among child quals */
1842 tgl@sss.pgh.pa.us 910 :CBC 13614 : cq_min_security = Min(cq_min_security, rinfo->security_level);
911 : : }
912 : : }
913 : :
914 : : /*
915 : : * In addition to the quals inherited from the parent, we might have
916 : : * securityQuals associated with this particular child node. (Currently
917 : : * this can only happen in appendrels originating from UNION ALL;
918 : : * inheritance child tables don't have their own securityQuals, see
919 : : * expand_single_inheritance_child().) Pull any such securityQuals up
920 : : * into the baserestrictinfo for the child. This is similar to
921 : : * process_security_barrier_quals() for the parent rel, except that we
922 : : * can't make any general deductions from such quals, since they don't
923 : : * hold for the whole appendrel.
924 : : */
925 [ + + ]: 21327 : if (childRTE->securityQuals)
926 : : {
927 : 6 : Index security_level = 0;
928 : :
929 [ + - + + : 12 : foreach(lc, childRTE->securityQuals)
+ + ]
930 : : {
931 : 6 : List *qualset = (List *) lfirst(lc);
932 : : ListCell *lc2;
933 : :
934 [ + - + + : 12 : foreach(lc2, qualset)
+ + ]
935 : : {
936 : 6 : Expr *qual = (Expr *) lfirst(lc2);
937 : :
938 : : /* not likely that we'd see constants here, so no check */
939 : 6 : childquals = lappend(childquals,
1179 940 : 6 : make_restrictinfo(root, qual,
941 : : true,
942 : : false, false,
943 : : false,
944 : : security_level,
945 : : NULL, NULL, NULL));
1842 946 : 6 : cq_min_security = Min(cq_min_security, security_level);
947 : : }
948 : 6 : security_level++;
949 : : }
950 [ - + ]: 6 : Assert(security_level <= root->qual_security_level);
951 : : }
952 : :
953 : : /*
954 : : * OK, we've got all the baserestrictinfo quals for this child.
955 : : */
956 : 21327 : childrel->baserestrictinfo = childquals;
957 : 21327 : childrel->baserestrict_min_security = cq_min_security;
958 : :
959 : 21327 : return true;
960 : : }
|