mirror of
https://github.com/postgres/postgres.git
synced 2025-06-16 06:01:02 +03:00
WITH CHECK OPTION support for auto-updatable VIEWs
For simple views which are automatically updatable, this patch allows the user to specify what level of checking should be done on records being inserted or updated. For 'LOCAL CHECK', new tuples are validated against the conditionals of the view they are being inserted into, while for 'CASCADED CHECK' the new tuples are validated against the conditionals for all views involved (from the top down). This option is part of the SQL specification. Dean Rasheed, reviewed by Pavel Stehule
This commit is contained in:
@ -8774,6 +8774,42 @@ ATExecSetRelOptions(Relation rel, List *defList, AlterTableType operation,
|
||||
break;
|
||||
}
|
||||
|
||||
/* Special-case validation of view options */
|
||||
if (rel->rd_rel->relkind == RELKIND_VIEW)
|
||||
{
|
||||
Query *view_query = get_view_query(rel);
|
||||
List *view_options = untransformRelOptions(newOptions);
|
||||
ListCell *cell;
|
||||
bool check_option = false;
|
||||
bool security_barrier = false;
|
||||
|
||||
foreach(cell, view_options)
|
||||
{
|
||||
DefElem *defel = (DefElem *) lfirst(cell);
|
||||
|
||||
if (pg_strcasecmp(defel->defname, "check_option") == 0)
|
||||
check_option = true;
|
||||
if (pg_strcasecmp(defel->defname, "security_barrier") == 0)
|
||||
security_barrier = defGetBoolean(defel);
|
||||
}
|
||||
|
||||
/*
|
||||
* If the check option is specified, look to see if the view is
|
||||
* actually auto-updatable or not.
|
||||
*/
|
||||
if (check_option)
|
||||
{
|
||||
const char *view_updatable_error =
|
||||
view_query_is_auto_updatable(view_query, security_barrier);
|
||||
|
||||
if (view_updatable_error)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
||||
errmsg("WITH CHECK OPTION is supported only on auto-updatable views"),
|
||||
errhint("%s", view_updatable_error)));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* All we need do here is update the pg_class row; the new options will be
|
||||
* propagated into relcaches during post-commit cache inval.
|
||||
|
Reference in New Issue
Block a user