74 bool *returning_flag);
85 const char *attrName);
96 int varno,
Query *parsetree,
bool *hasUpdate);
150 bool forUpdatePushedDown)
162 foreach(l, parsetree->
rtable)
188 else if (forUpdatePushedDown)
193 lockmode = rte->rellockmode;
196 lockmode = rte->rellockmode;
204 rte->relkind = rel->
rd_rel->relkind;
223 foreach(ll, rte->joinaliasvars)
226 Var *aliasvar = aliasitem;
238 if (aliasvar &&
IsA(aliasvar,
Var))
250 if (aliasvar->
varno != curinputvarno)
252 curinputvarno = aliasvar->
varno;
253 if (curinputvarno >= rt_index)
254 elog(
ERROR,
"unexpected varno %d in JOIN RTE %d",
255 curinputvarno, rt_index);
256 curinputrte =
rt_fetch(curinputvarno,
266 newaliasvars =
lappend(newaliasvars, aliasitem);
268 rte->joinaliasvars = newaliasvars;
279 (forUpdatePushedDown ||
301 if (parsetree->hasSubLinks)
356 bool *returning_flag)
362 Query **sub_action_ptr;
381 current_varno = rt_index;
411 foreach(lc, sub_action->
rtable)
449 List *perminfos_tail = sub_action->rteperminfos;
457 sub_action->rteperminfos =
copyObject(parsetree->rteperminfos);
459 rtable_tail, perminfos_tail);
466 if (parsetree->hasSubLinks && !sub_action->hasSubLinks)
468 foreach(lc, parsetree->
rtable)
475 sub_action->hasSubLinks =
479 sub_action->hasSubLinks =
483 sub_action->hasSubLinks =
487 sub_action->hasSubLinks =
494 sub_action->hasSubLinks |=
496 if (sub_action->hasSubLinks)
508 sub_action->hasRowSecurity |= parsetree->hasRowSecurity;
535 if (newjointree !=
NIL)
545 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
546 errmsg(
"conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
555 if (parsetree->hasSubLinks && !sub_action->hasSubLinks)
556 sub_action->hasSubLinks =
578 foreach(lc, parsetree->
cteList)
583 foreach(lc2, sub_action->
cteList)
589 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
590 errmsg(
"WITH query name \"%s\" appears in both a rule action and the query being rewritten",
602 sub_action->hasRecursive |= parsetree->hasRecursive;
603 sub_action->hasModifyingCTE |= parsetree->hasModifyingCTE;
617 if (sub_action->hasModifyingCTE && rule_action != sub_action)
619 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
620 errmsg(
"INSERT ... SELECT rule actions are not supported for queries having data-modifying statements in WITH")));
628 AddQual(sub_action, rule_qual);
643 sub_action = (
Query *)
649 sub_action->resultRelation,
656 *sub_action_ptr = sub_action;
658 rule_action = sub_action;
673 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
674 errmsg(
"cannot have RETURNING lists in multiple rules")));
675 *returning_flag =
true;
678 parsetree->resultRelation,
683 rule_action->resultRelation,
686 &rule_action->hasSubLinks);
689 rule_action->returningOldAlias = parsetree->returningOldAlias;
690 rule_action->returningNewAlias = parsetree->returningNewAlias;
696 if (parsetree->hasSubLinks && !rule_action->hasSubLinks)
697 rule_action->hasSubLinks =
720 foreach(l, newjointree)
780 int values_rte_index,
804 next_junk_attrno = numattrs + 1;
806 foreach(temp, targetList)
810 if (!old_tle->resjunk)
813 attrno = old_tle->
resno;
814 if (attrno < 1 || attrno > numattrs)
815 elog(
ERROR,
"bogus resno %d in targetlist", attrno);
819 if (att_tup->attisdropped)
823 new_tles[attrno - 1] =
825 new_tles[attrno - 1],
840 if (old_tle->
resno != next_junk_attrno)
843 old_tle->
resno = next_junk_attrno;
845 junk_tlist =
lappend(junk_tlist, old_tle);
850 for (attrno = 1; attrno <= numattrs; attrno++)
858 if (att_tup->attisdropped)
866 apply_default = ((new_tle == NULL && commandType ==
CMD_INSERT) ||
871 int values_attrno = 0;
874 if (values_rte && new_tle &&
IsA(new_tle->
expr,
Var))
878 if (var->
varno == values_rte_index)
887 if (att_tup->attidentity == ATTRIBUTE_IDENTITY_ALWAYS && !apply_default)
890 apply_default =
true;
899 if (values_attrno != 0)
901 if (default_only_cols == NULL)
905 apply_default =
true;
910 (
errcode(ERRCODE_GENERATED_ALWAYS),
911 errmsg(
"cannot insert a non-DEFAULT value into column \"%s\"",
913 errdetail(
"Column \"%s\" is an identity column defined as GENERATED ALWAYS.",
915 errhint(
"Use OVERRIDING SYSTEM VALUE to override.")));
924 if (att_tup->attidentity == ATTRIBUTE_IDENTITY_BY_DEFAULT &&
926 apply_default =
true;
933 if (att_tup->attgenerated && !apply_default)
939 if (values_attrno != 0)
941 if (default_only_cols == NULL)
945 apply_default =
true;
950 (
errcode(ERRCODE_GENERATED_ALWAYS),
951 errmsg(
"cannot insert a non-DEFAULT value into column \"%s\"",
953 errdetail(
"Column \"%s\" is a generated column.",
962 if (values_attrno != 0 && apply_default && unused_values_attrnos)
974 if (att_tup->attidentity == ATTRIBUTE_IDENTITY_ALWAYS &&
975 new_tle && !apply_default)
977 (
errcode(ERRCODE_GENERATED_ALWAYS),
978 errmsg(
"column \"%s\" can only be updated to DEFAULT",
980 errdetail(
"Column \"%s\" is an identity column defined as GENERATED ALWAYS.",
983 if (att_tup->attgenerated && new_tle && !apply_default)
985 (
errcode(ERRCODE_GENERATED_ALWAYS),
986 errmsg(
"column \"%s\" can only be updated to DEFAULT",
988 errdetail(
"Column \"%s\" is a generated column.",
992 if (att_tup->attgenerated)
1000 else if (apply_default)
1020 att_tup->attcollation,
1033 new_tlist =
lappend(new_tlist, new_tle);
1051 const char *attrName)
1062 if (prior_tle == NULL)
1102 prior_expr = (
Node *) prior_tle->
expr;
1117 if (src_input == NULL ||
1118 prior_input == NULL ||
1121 (
errcode(ERRCODE_SYNTAX_ERROR),
1122 errmsg(
"multiple assignments to same column \"%s\"",
1128 priorbottom = prior_input;
1133 if (newbottom == NULL)
1135 priorbottom = newbottom;
1137 if (!
equal(priorbottom, src_input))
1139 (
errcode(ERRCODE_SYNTAX_ERROR),
1140 errmsg(
"multiple assignments to same column \"%s\"",
1153 memcpy(fstore, prior_expr,
sizeof(
FieldStore));
1164 memcpy(fstore, src_expr,
sizeof(
FieldStore));
1165 fstore->
arg = (
Expr *) prior_expr;
1167 newexpr = (
Node *) fstore;
1175 newexpr = (
Node *) sbsref;
1189 newcoerce->
arg = (
Expr *) newexpr;
1190 newexpr = (
Node *) newcoerce;
1235 Oid atttype = att_tup->atttypid;
1236 int32 atttypmod = att_tup->atttypmod;
1240 if (att_tup->attidentity)
1245 nve->
typeId = att_tup->atttypid;
1247 return (
Node *) nve;
1253 if (att_tup->atthasdef)
1257 elog(
ERROR,
"default expression not found for attribute %d of relation \"%s\"",
1265 if (expr == NULL && !att_tup->attgenerated)
1288 (
errcode(ERRCODE_DATATYPE_MISMATCH),
1289 errmsg(
"column \"%s\" is of type %s"
1290 " but default expression is of type %s",
1294 errhint(
"You will need to rewrite or cast the expression.")));
1311 foreach(lc2, sublist)
1339 if (default_only_cols == NULL)
1343 foreach(lc2, sublist)
1356 foreach(lc2, sublist)
1374 return default_only_cols;
1422 bool isAutoUpdatableView;
1447 attrnos = (
int *)
palloc0(numattrs *
sizeof(
int));
1461 Assert(attrno >= 1 && attrno <= numattrs);
1462 attrnos[attrno - 1] = tle->
resno;
1472 isAutoUpdatableView =
false;
1473 if (target_relation->
rd_rel->relkind == RELKIND_VIEW &&
1483 parsetree->resultRelation, parsetree, &hasUpdate);
1491 rule_lock->
qual == NULL)
1504 isAutoUpdatableView =
true;
1519 foreach(lc2, sublist)
1522 int attrno = attrnos[
i++];
1546 elog(
ERROR,
"cannot set value in column %d to DEFAULT",
i);
1547 Assert(attrno > 0 && attrno <= target_relation->rd_att->natts);
1550 if (!att_tup->attisdropped)
1562 if (isAutoUpdatableView)
1565 newList =
lappend(newList, col);
1566 allReplaced =
false;
1572 att_tup->attcollation,
1576 newList =
lappend(newList, new_expr);
1579 newList =
lappend(newList, col);
1581 newValues =
lappend(newValues, newList);
1613 foreach(lc2, sublist)
1626 newList =
lappend(newList, col);
1628 newValues =
lappend(newValues, newList);
1650 if (rulelocks == NULL)
1655 if (parsetree->resultRelation != varno)
1661 for (
i = 0;
i < nlocks;
i++)
1692 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1693 errmsg(
"cannot execute MERGE on relation \"%s\"",
1695 errdetail(
"MERGE is not supported for relations with rules."));
1698 if (oneLock->
event == event)
1702 matching_locks =
lappend(matching_locks, oneLock);
1706 return matching_locks;
1726 elog(
ERROR,
"expected just one rule action");
1727 if (
rule->qual != NULL)
1728 elog(
ERROR,
"cannot handle qualified ON SELECT rule");
1734 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1735 errmsg(
"access to non-system view \"%s\" is restricted",
1738 if (rt_index == parsetree->resultRelation)
1784 parsetree->resultRelation, 0);
1803 elog(
ERROR,
"unrecognized commandType: %d",
1843 parsetree->hasRowSecurity |= rule_action->hasRowSecurity;
1872 while (
list_length(rte->eref->colnames) < numCols)
1874 rte->eref->colnames =
lappend(rte->eref->colnames,
1919 strength, waitPolicy,
true);
1939 elog(
ERROR,
"unrecognized node type: %d",
1996 int origResultRelation = parsetree->resultRelation;
2006 foreach(lc, parsetree->
cteList)
2010 if (cte->search_clause || cte->cycle_clause)
2048 parsetree->hasRowSecurity |= rte->
subquery->hasRowSecurity;
2069 if (rte->relkind == RELKIND_MATVIEW)
2090 if (rt_index != parsetree->resultRelation &&
2098 if (rt_index == parsetree->resultRelation &&
2099 rt_index != origResultRelation)
2115 for (
i = 0;
i <
rules->numLocks;
i++)
2133 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2134 errmsg(
"infinite recursion detected in rules for relation \"%s\"",
2157 foreach(lc, parsetree->
cteList)
2168 parsetree->hasRowSecurity |= ((
Query *) cte->
ctequery)->hasRowSecurity;
2175 if (parsetree->hasSubLinks)
2199 foreach(lc, parsetree->
rtable)
2203 List *securityQuals;
2204 List *withCheckOptions;
2205 bool hasRowSecurity;
2212 (rte->relkind != RELKIND_RELATION &&
2213 rte->relkind != RELKIND_PARTITIONED_TABLE))
2222 &securityQuals, &withCheckOptions,
2223 &hasRowSecurity, &hasSubLinks);
2225 if (securityQuals !=
NIL || withCheckOptions !=
NIL)
2238 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
2239 errmsg(
"infinite recursion detected in policy for relation \"%s\"",
2287 rte->securityQuals);
2289 parsetree->withCheckOptions =
list_concat(withCheckOptions,
2290 parsetree->withCheckOptions);
2298 parsetree->hasRowSecurity =
true;
2300 parsetree->hasSubLinks =
true;
2352 parsetree->resultRelation,
2357 &parsetree->hasSubLinks);
2399 bool *returning_flag,
2400 Query **qual_product)
2408 Node *event_qual = rule_lock->
qual;
2416 if (event_qual != NULL)
2421 *instead_flag =
true;
2443 if (*qual_product == NULL)
2461 event_qual, rt_index, event,
2464 rule_action->querySource = qsrc;
2465 rule_action->canSetTag =
false;
2467 results =
lappend(results, rule_action);
2499 elog(
ERROR,
"invalid _RETURN rule action specification");
2505 elog(
ERROR,
"failed to find _RETURN rule for view");
2545 switch (
action->commandType)
2569 elog(
ERROR,
"unrecognized CmdType: %d", (
int) event);
2602 return gettext_noop(
"Junk view columns are not updatable.");
2607 return gettext_noop(
"View columns that are not columns of their base relation are not updatable.");
2610 return gettext_noop(
"View columns that refer to system columns are not updatable.");
2613 return gettext_noop(
"View columns that return whole-row references are not updatable.");
2674 return gettext_noop(
"Views containing DISTINCT are not automatically updatable.");
2677 return gettext_noop(
"Views containing GROUP BY are not automatically updatable.");
2680 return gettext_noop(
"Views containing HAVING are not automatically updatable.");
2683 return gettext_noop(
"Views containing UNION, INTERSECT, or EXCEPT are not automatically updatable.");
2686 return gettext_noop(
"Views containing WITH are not automatically updatable.");
2689 return gettext_noop(
"Views containing LIMIT or OFFSET are not automatically updatable.");
2700 if (viewquery->hasAggs)
2701 return gettext_noop(
"Views that return aggregate functions are not automatically updatable.");
2703 if (viewquery->hasWindowFuncs)
2704 return gettext_noop(
"Views that return window functions are not automatically updatable.");
2706 if (viewquery->hasTargetSRFs)
2707 return gettext_noop(
"Views that return set-returning functions are not automatically updatable.");
2714 return gettext_noop(
"Views that do not select from a single table or view are not automatically updatable.");
2718 return gettext_noop(
"Views that do not select from a single table or view are not automatically updatable.");
2722 (base_rte->relkind != RELKIND_RELATION &&
2723 base_rte->relkind != RELKIND_FOREIGN_TABLE &&
2724 base_rte->relkind != RELKIND_VIEW &&
2725 base_rte->relkind != RELKIND_PARTITIONED_TABLE))
2726 return gettext_noop(
"Views that do not select from a single table or view are not automatically updatable.");
2729 return gettext_noop(
"Views containing TABLESAMPLE are not automatically updatable.");
2753 return gettext_noop(
"Views that have no updatable columns are not automatically updatable.");
2787 char **non_updatable_col)
2801 if (updatable_cols != NULL)
2802 *updatable_cols = NULL;
2803 if (non_updatable_col != NULL)
2804 *non_updatable_col = NULL;
2811 const char *col_update_detail;
2816 if (col_update_detail == NULL)
2819 if (updatable_cols != NULL)
2825 if (non_updatable_col != NULL)
2826 *non_updatable_col = tle->resname;
2827 return col_update_detail;
2868 List *outer_reloids,
2869 bool include_triggers,
2876#define ALL_EVENTS ((1 << CMD_INSERT) | (1 << CMD_UPDATE) | (1 << CMD_DELETE))
2900 if (rel->
rd_rel->relkind == RELKIND_RELATION ||
2901 rel->
rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
2909 if (rulelocks != NULL)
2931 if (include_triggers)
2954 if (rel->
rd_rel->relkind == RELKIND_FOREIGN_TABLE)
2976 if (rel->
rd_rel->relkind == RELKIND_VIEW)
2995 &updatable_cols, NULL);
2997 if (include_cols != NULL)
3015 if (base_rte->relkind != RELKIND_RELATION &&
3016 base_rte->relkind != RELKIND_PARTITIONED_TABLE)
3018 baseoid = base_rte->relid;
3029 events |= auto_events;
3070 foreach(lc, targetlist)
3091 if (tle != NULL && !tle->resjunk &&
IsA(tle->
expr,
Var))
3099 elog(
ERROR,
"attribute number %d not found in view targetlist",
3124 List *mergeActionList,
3133 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3134 errmsg(
"cannot insert into view \"%s\"",
3137 errhint(
"To enable inserting into the view, provide an INSTEAD OF INSERT trigger or an unconditional ON INSERT DO INSTEAD rule."));
3141 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3142 errmsg(
"cannot update view \"%s\"",
3145 errhint(
"To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule."));
3149 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3150 errmsg(
"cannot delete from view \"%s\"",
3153 errhint(
"To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule."));
3163 switch (
action->commandType)
3168 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3169 errmsg(
"cannot insert into view \"%s\"",
3172 errhint(
"To enable inserting into the view using MERGE, provide an INSTEAD OF INSERT trigger."));
3177 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3178 errmsg(
"cannot update view \"%s\"",
3181 errhint(
"To enable updating the view using MERGE, provide an INSTEAD OF UPDATE trigger."));
3186 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3187 errmsg(
"cannot delete from view \"%s\"",
3190 errhint(
"To enable deleting from the view using MERGE, provide an INSTEAD OF DELETE trigger."));
3201 elog(
ERROR,
"unrecognized CmdType: %d", (
int) command);
3220 bool insert_or_update;
3221 const char *auto_update_detail;
3232 List *view_targetlist;
3245 view_rte =
rt_fetch(parsetree->resultRelation, parsetree->
rtable);
3264 insert_or_update =
true;
3274 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3275 errmsg(
"access to non-system view \"%s\" is restricted",
3284 auto_update_detail =
3287 if (auto_update_detail)
3291 auto_update_detail);
3297 if (insert_or_update)
3300 char *non_updatable_col;
3353 &non_updatable_col);
3354 if (auto_update_detail)
3364 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3365 errmsg(
"cannot insert into column \"%s\" of view \"%s\"",
3372 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3373 errmsg(
"cannot update column \"%s\" of view \"%s\"",
3380 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3381 errmsg(
"cannot merge into column \"%s\" of view \"%s\"",
3408 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3409 errmsg(
"cannot merge into view \"%s\"",
3411 errdetail(
"MERGE is not supported for views with INSTEAD OF triggers for some actions but not all."),
3412 errhint(
"To enable merging into the view, either provide a full set of INSTEAD OF triggers or drop the existing INSTEAD OF triggers."));
3441 base_rte->relkind = base_rel->
rd_rel->relkind;
3449 if (viewquery->hasSubLinks)
3480 new_rte->
inh =
false;
3516 new_rte->perminfoindex = 0;
3564 new_rte->securityQuals = view_rte->securityQuals;
3565 view_rte->securityQuals =
NIL;
3571 parsetree = (
Query *)
3573 parsetree->resultRelation,
3589 parsetree->resultRelation,
3592 Assert(parsetree->resultRelation == new_rt_index);
3615 if (view_tle != NULL && !view_tle->resjunk &&
IsA(view_tle->
expr,
Var))
3618 elog(
ERROR,
"attribute number %d not found in view targetlist",
3635 if (view_tle != NULL && !view_tle->resjunk &&
IsA(view_tle->
expr,
Var))
3636 tle->resno = ((
Var *) view_tle->
expr)->varattno;
3638 elog(
ERROR,
"attribute number %d not found in view targetlist",
3652 Index old_exclRelIndex,
3672 if (view_tle != NULL && !view_tle->resjunk &&
IsA(view_tle->
expr,
Var))
3675 elog(
ERROR,
"attribute number %d not found in view targetlist",
3694 new_exclRte = new_exclNSItem->
p_rte;
3695 new_exclRte->relkind = RELKIND_COMPOSITE_TYPE;
3697 new_exclRte->perminfoindex = 0;
3721 new_exclRelIndex, 0);
3732 &parsetree->hasSubLinks);
3772 new_rte->securityQuals =
lcons(viewqual, new_rte->securityQuals);
3783 if (!parsetree->hasSubLinks)
3796 if (insert_or_update)
3809 if (parsetree->withCheckOptions !=
NIL)
3830 if (has_wco && (cascaded || viewquery->
jointree->
quals != NULL))
3841 parsetree->withCheckOptions =
lcons(wco,
3842 parsetree->withCheckOptions);
3855 if (!parsetree->hasSubLinks &&
3884 int num_ctes_processed)
3887 bool instead =
false;
3888 bool returning =
false;
3889 bool updatableview =
false;
3890 Query *qual_product = NULL;
3906 foreach(lc1, parsetree->
cteList)
3920 newstuff =
RewriteQuery(ctequery, rewrite_events, 0, 0);
3942 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3943 errmsg(
"DO INSTEAD NOTIFY rules are not supported for data-modifying statements in WITH")));
3946 Assert(!ctequery->canSetTag);
3950 else if (newstuff ==
NIL)
3953 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3954 errmsg(
"DO INSTEAD NOTHING rules are not supported for data-modifying statements in WITH")));
3961 foreach(lc2, newstuff)
3967 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3968 errmsg(
"conditional DO INSTEAD rules are not supported for data-modifying statements in WITH")));
3971 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3972 errmsg(
"DO ALSO rules are not supported for data-modifying statements in WITH")));
3976 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3977 errmsg(
"multi-statement DO INSTEAD rules are not supported for data-modifying statements in WITH")));
3992 int result_relation;
3996 int product_orig_rt_length;
3997 List *product_queries;
3998 bool hasUpdate =
false;
3999 int values_rte_index = 0;
4000 bool defaults_remaining =
false;
4002 result_relation = parsetree->resultRelation;
4003 Assert(result_relation != 0);
4039 if (values_rte != NULL)
4040 elog(
ERROR,
"more than one VALUES RTE found");
4043 values_rte_index = rtr->
rtindex;
4050 Bitmapset *unused_values_attrnos = NULL;
4055 parsetree->override,
4059 &unused_values_attrnos);
4063 unused_values_attrnos))
4064 defaults_remaining =
true;
4072 parsetree->override,
4083 parsetree->override,
4094 parsetree->override,
4109 switch (
action->commandType)
4139 elog(
ERROR,
"unrecognized commandType: %d", (
int) event);
4145 result_relation, parsetree, &hasUpdate);
4164 if (defaults_remaining && product_queries !=
NIL)
4177 foreach(n, product_queries)
4203 elog(
ERROR,
"failed to find VALUES RTE in product query");
4225 rt_entry_relation->
rd_rel->relkind == RELKIND_VIEW &&
4234 if (qual_product != NULL)
4238 gettext_noop(
"Views with conditional DO INSTEAD rules are not automatically updatable."));
4254 product_queries =
lcons(parsetree, product_queries);
4256 product_queries =
lappend(product_queries, parsetree);
4267 updatableview =
true;
4274 if (product_queries !=
NIL)
4279 foreach(n, rewrite_events)
4283 rev->
event == event)
4285 (
errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
4286 errmsg(
"infinite recursion detected in rules for relation \"%s\"",
4293 rewrite_events =
lappend(rewrite_events, rev);
4295 foreach(n, product_queries)
4312 product_orig_rt_length,
4313 num_ctes_processed);
4327 if ((instead || qual_product != NULL) &&
4335 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4336 errmsg(
"cannot perform INSERT RETURNING on relation \"%s\"",
4338 errhint(
"You need an unconditional ON INSERT DO INSTEAD rule with a RETURNING clause.")));
4342 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4343 errmsg(
"cannot perform UPDATE RETURNING on relation \"%s\"",
4345 errhint(
"You need an unconditional ON UPDATE DO INSTEAD rule with a RETURNING clause.")));
4349 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4350 errmsg(
"cannot perform DELETE RETURNING on relation \"%s\"",
4352 errhint(
"You need an unconditional ON DELETE DO INSTEAD rule with a RETURNING clause.")));
4355 elog(
ERROR,
"unrecognized commandType: %d",
4366 (product_queries !=
NIL || hasUpdate) &&
4369 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4370 errmsg(
"INSERT with ON CONFLICT clause cannot be used with table that has INSERT or UPDATE rules")));
4391 if (qual_product != NULL)
4392 rewritten =
lcons(qual_product, rewritten);
4394 rewritten =
lcons(parsetree, rewritten);
4398 if (qual_product != NULL)
4399 rewritten =
lappend(rewritten, qual_product);
4401 rewritten =
lappend(rewritten, parsetree);
4417 foreach(lc1, rewritten)
4426 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4427 errmsg(
"WITH cannot be used in a query that is rewritten by rules into multiple queries")));
4460 for (
int i = 0;
i < tupdesc->
natts;
i++)
4464 if (attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
4529 Assert(att_tup->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL);
4532 if (defexpr == NULL)
4533 elog(
ERROR,
"no generation expression found for column number %d of table \"%s\"",
4541 attcollid = att_tup->attcollation;
4550 defexpr = (
Node *) ce;
4569 int64 input_query_id = parsetree->queryId;
4574 bool foundOriginalQuery;
4581 Assert(parsetree->canSetTag);
4598 foreach(l, querylist)
4604 query->queryId = input_query_id;
4606 results =
lappend(results, query);
4626 foundOriginalQuery =
false;
4635 Assert(query->canSetTag);
4636 Assert(!foundOriginalQuery);
4637 foundOriginalQuery =
true;
4638#ifndef USE_ASSERT_CHECKING
4644 Assert(!query->canSetTag);
4648 lastInstead = query;
4652 if (!foundOriginalQuery && lastInstead != NULL)
4653 lastInstead->canSetTag =
true;
#define InvalidAttrNumber
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
int bms_next_member(const Bitmapset *a, int prevbit)
Bitmapset * bms_del_member(Bitmapset *a, int x)
bool bms_is_member(int x, const Bitmapset *a)
Bitmapset * bms_add_member(Bitmapset *a, int x)
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
int errdetail_internal(const char *fmt,...)
int errdetail(const char *fmt,...)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
bool equal(const void *a, const void *b)
int ExecCleanTargetListLength(List *targetlist)
FdwRoutine * GetFdwRoutineForRelation(Relation relation, bool makecopy)
Assert(PointerIsAligned(start, uint64))
if(TABLE==NULL||TABLE_index==NULL)
List * lappend(List *list, void *datum)
List * list_concat(List *list1, const List *list2)
List * list_concat_copy(const List *list1, const List *list2)
List * lappend_oid(List *list, Oid datum)
List * lcons(void *datum, List *list)
List * list_delete_last(List *list)
bool list_member_oid(const List *list, Oid datum)
Node * get_typdefault(Oid typid)
Alias * makeAlias(const char *aliasname, List *colnames)
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Var * makeWholeRowVar(RangeTblEntry *rte, int varno, Index varlevelsup, bool allowScalar)
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
TargetEntry * flatCopyTargetEntry(TargetEntry *src_tle)
char * pstrdup(const char *in)
void pfree(void *pointer)
void * palloc0(Size size)
Oid exprType(const Node *expr)
Oid exprCollation(const Node *expr)
Node * strip_implicit_coercions(Node *node)
#define query_tree_walker(q, w, c, f)
#define expression_tree_walker(n, w, c)
#define QTW_IGNORE_RC_SUBQUERIES
#define IsA(nodeptr, _type_)
#define castNode(_type_, nodeptr)
Node * coerce_null_to_domain(Oid typid, int32 typmod, Oid collation, int typlen, bool typbyval)
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
ParseState * make_parsestate(ParseState *parentParseState)
ParseNamespaceItem * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
RTEPermissionInfo * getRTEPermissionInfo(List *rteperminfos, RangeTblEntry *rte)
RowMarkClause * get_parse_rowmark(Query *qry, Index rtindex)
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
bool get_rte_attribute_is_dropped(RangeTblEntry *rte, AttrNumber attnum)
RTEPermissionInfo * addRTEPermissionInfo(List **rteperminfos, RangeTblEntry *rte)
#define ACL_SELECT_FOR_UPDATE
void applyLockingClause(Query *qry, Index rtindex, LockClauseStrength strength, LockWaitPolicy waitPolicy, bool pushedDown)
List * BuildOnConflictExcludedTargetlist(Relation targetrel, Index exclRelIndex)
#define rt_fetch(rangetable_index, rangetable)
FormData_pg_attribute * Form_pg_attribute
Oid getIdentitySequence(Relation rel, AttrNumber attnum, bool missing_ok)
#define lfirst_node(type, lc)
static int list_length(const List *l)
#define linitial_node(type, l)
#define foreach_current_index(var_or_cell)
#define foreach_delete_current(lst, var_or_cell)
#define foreach_node(type, var, lst)
int restrict_nonsystem_relation_kind
@ OVERRIDING_SYSTEM_VALUE
#define RelationGetRelid(relation)
#define RelationHasCheckOption(relation)
#define RelationHasSecurityInvoker(relation)
#define RelationGetDescr(relation)
#define RelationGetNumberOfAttributes(relation)
#define RelationGetRelationName(relation)
#define RelationHasCascadedCheckOption(relation)
#define RelationIsSecurityView(relation)
#define RULE_FIRES_ON_ORIGIN
#define RULE_FIRES_ON_REPLICA
static void markQueryForLocking(Query *qry, Node *jtnode, LockClauseStrength strength, LockWaitPolicy waitPolicy, bool pushedDown)
static Query * ApplyRetrieveRule(Query *parsetree, RewriteRule *rule, int rt_index, Relation relation, List *activeRIRs)
static Query * rewriteRuleAction(Query *parsetree, Query *rule_action, Node *rule_qual, int rt_index, CmdType event, bool *returning_flag)
static TargetEntry * process_matched_tle(TargetEntry *src_tle, TargetEntry *prior_tle, const char *attrName)
static List * RewriteQuery(Query *parsetree, List *rewrite_events, int orig_rt_length, int num_ctes_processed)
static bool fireRIRonSubLink(Node *node, fireRIRonSubLink_context *context)
static List * adjustJoinTreeList(Query *parsetree, bool removert, int rt_index)
void AcquireRewriteLocks(Query *parsetree, bool forExecute, bool forUpdatePushedDown)
bool view_has_instead_trigger(Relation view, CmdType event, List *mergeActionList)
static const char * view_cols_are_auto_updatable(Query *viewquery, Bitmapset *required_cols, Bitmapset **updatable_cols, char **non_updatable_col)
struct fireRIRonSubLink_context fireRIRonSubLink_context
static List * fireRules(Query *parsetree, int rt_index, CmdType event, List *locks, bool *instead_flag, bool *returning_flag, Query **qual_product)
static Query * CopyAndAddInvertedQual(Query *parsetree, Node *rule_qual, int rt_index, CmdType event)
int relation_is_updatable(Oid reloid, List *outer_reloids, bool include_triggers, Bitmapset *include_cols)
Query * get_view_query(Relation view)
static bool acquireLocksOnSubLinks(Node *node, acquireLocksOnSubLinks_context *context)
static bool rewriteValuesRTE(Query *parsetree, RangeTblEntry *rte, int rti, Relation target_relation, Bitmapset *unused_cols)
static Node * get_assignment_input(Node *node)
static Bitmapset * adjust_view_column_set(Bitmapset *cols, List *targetlist)
const char * view_query_is_auto_updatable(Query *viewquery, bool check_cols)
struct acquireLocksOnSubLinks_context acquireLocksOnSubLinks_context
static const char * view_col_is_auto_updatable(RangeTblRef *rtr, TargetEntry *tle)
static bool searchForDefault(RangeTblEntry *rte)
struct rewrite_event rewrite_event
static Query * fireRIRrules(Query *parsetree, List *activeRIRs)
static Query * rewriteTargetView(Query *parsetree, Relation view)
List * QueryRewrite(Query *parsetree)
static Node * expand_generated_columns_internal(Node *node, Relation rel, int rt_index, RangeTblEntry *rte, int result_relation)
static Bitmapset * findDefaultOnlyColumns(RangeTblEntry *rte)
static void rewriteValuesRTEToNulls(Query *parsetree, RangeTblEntry *rte)
Node * build_generation_expression(Relation rel, int attrno)
static List * matchLocks(CmdType event, Relation relation, int varno, Query *parsetree, bool *hasUpdate)
void error_view_not_updatable(Relation view, CmdType command, List *mergeActionList, const char *detail)
Node * build_column_default(Relation rel, int attrno)
static List * rewriteTargetListIU(List *targetList, CmdType commandType, OverridingKind override, Relation target_relation, RangeTblEntry *values_rte, int values_rte_index, Bitmapset **unused_values_attrnos)
Node * expand_generated_columns_in_expr(Node *node, Relation rel, int rt_index)
void ChangeVarNodes(Node *node, int rt_index, int new_index, int sublevels_up)
void OffsetVarNodes(Node *node, int offset, int sublevels_up)
bool checkExprHasSubLink(Node *node)
void CombineRangeTables(List **dst_rtable, List **dst_perminfos, List *src_rtable, List *src_perminfos)
void AddQual(Query *parsetree, Node *qual)
bool rangeTableEntry_used(Node *node, int rt_index, int sublevels_up)
Query * getInsertSelectQuery(Query *parsetree, Query ***subquery_ptr)
void AddInvertedQual(Query *parsetree, Node *qual)
Node * ReplaceVarsFromTargetList(Node *node, int target_varno, int sublevels_up, RangeTblEntry *target_rte, List *targetlist, int result_relation, ReplaceVarsNoMatchOption nomatch_option, int nomatch_varno, bool *outer_hasSubLinks)
@ REPLACEVARS_SUBSTITUTE_NULL
@ REPLACEVARS_CHANGE_VARNO
@ REPLACEVARS_REPORT_ERROR
CommonTableExpr * rewriteSearchAndCycle(CommonTableExpr *cte)
void get_row_security_policies(Query *root, RangeTblEntry *rte, int rt_index, List **securityQuals, List **withCheckOptions, bool *hasRowSecurity, bool *hasSubLinks)
void relation_close(Relation relation, LOCKMODE lockmode)
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
void check_stack_depth(void)
ExecForeignInsert_function ExecForeignInsert
ExecForeignUpdate_function ExecForeignUpdate
ExecForeignDelete_function ExecForeignDelete
IsForeignRelUpdatable_function IsForeignRelUpdatable
OnConflictExpr * onConflict
struct TableSampleClause * tablesample
LockClauseStrength strength
LockWaitPolicy waitPolicy
bool trig_update_instead_row
bool trig_delete_instead_row
bool trig_insert_instead_row
bool has_generated_virtual
#define FirstLowInvalidHeapAttributeNumber
void table_close(Relation relation, LOCKMODE lockmode)
Relation table_open(Oid relationId, LOCKMODE lockmode)
#define RESTRICT_RELKIND_VIEW
#define FirstNormalObjectId
int SessionReplicationRole
#define SESSION_REPLICATION_ROLE_REPLICA
Node * TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum)
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
String * makeString(char *str)
bool contain_vars_of_level(Node *node, int levelsup)
static struct rule * rules