2ndQuadrant is now part of EDB

Bringing together some of the world's top PostgreSQL experts.

2ndQuadrant | PostgreSQL
Mission Critical Databases
  • Contact us
  • EN
    • FR
    • IT
    • ES
    • DE
    • PT
  • Support & Services
  • Products
  • Downloads
    • Installers
      • Postgres Installer
      • 2UDA – Unified Data Analytics
    • Whitepapers
      • Business Case for PostgreSQL Support
      • Security Best Practices for PostgreSQL
    • Case Studies
      • Performance Tuning
        • BenchPrep
        • tastyworks
      • Distributed Clusters
        • ClickUp
        • European Space Agency (ESA)
        • Telefónica del Sur
        • Animal Logic
      • Database Administration
        • Agilis Systems
      • Professional Training
        • Met Office
        • London & Partners
      • Database Upgrades
        • Alfred Wegener Institute (AWI)
      • Database Migration
        • International Game Technology (IGT)
        • Healthcare Software Solutions (HSS)
        • Navionics
  • Postgres Learning Center
    • Webinars
      • Upcoming Webinars
      • Webinar Library
    • Whitepapers
      • Business Case for PostgreSQL Support
      • Security Best Practices for PostgreSQL
    • Blog
    • Training
      • Course Catalogue
    • Case Studies
      • Performance Tuning
        • BenchPrep
        • tastyworks
      • Distributed Clusters
        • ClickUp
        • European Space Agency (ESA)
        • Telefónica del Sur
        • Animal Logic
      • Database Administration
        • Agilis Systems
      • Professional Training
        • Met Office
        • London & Partners
      • Database Upgrades
        • Alfred Wegener Institute (AWI)
      • Database Migration
        • International Game Technology (IGT)
        • Healthcare Software Solutions (HSS)
        • Navionics
    • Books
      • PostgreSQL 11 Administration Cookbook
      • PostgreSQL 10 Administration Cookbook
      • PostgreSQL High Availability Cookbook – 2nd Edition
      • PostgreSQL 9 Administration Cookbook – 3rd Edition
      • PostgreSQL Server Programming Cookbook – 2nd Edition
      • PostgreSQL 9 Cookbook – Chinese Edition
    • Videos
    • Events
    • PostgreSQL
      • PostgreSQL – History
      • Who uses PostgreSQL?
      • PostgreSQL FAQ
      • PostgreSQL vs MySQL
      • The Business Case for PostgreSQL
      • Security Information
      • Documentation
  • About Us
    • About 2ndQuadrant
    • 2ndQuadrant’s Passion for PostgreSQL
    • News
    • Careers
    • Team Profile
  • Blog
  • Menu Menu
You are here: Home1 / Blog2 / Giuseppe's PlanetPostgreSQL3 / The CHECK clause for updatable views
2ndQuadrant Press

The CHECK clause for updatable views

January 8, 2015/0 Comments/in Giuseppe's PlanetPostgreSQL, PostgreSQL /by 2ndQuadrant Press

Written by Giuseppe Broccolo 

group of elephants

 

Since PostgreSQL 9.3, it is possible to update and insert into views directly, so long as the view refers to only one underlying table.

PostgreSQL 9.4 allows us to use the CHECK clause for INSERTs into updatable views. For example, consider a table composed of just one integer column; and consider two views, one on numbers divisible by 2 and one on numbers divisible by 3. If we try to insert the number 123 into the first view:

—-

$ CREATE TABLE some_data(id int4 PRIMARY KEY);

CREATE TABLE

$ CREATE VIEW first AS SELECT * FROM some_data WHERE 0 = id%2;

CREATE VIEW

$ CREATE VIEW second AS SELECT * FROM some_data WHERE 0 = id%3;

CREATE VIEW

$ INSERT INTO first(id) VALUES (123);

—-

It will be inserted into the underlying table, even though the view is only for numbers divisible by 2 (so the new value will not be visible in the view). In PostgreSQL 9.4 the CHECK clause has been introduced to properly manage INSERTs into views by checking in advance that values are compatible with the definition of the view.

There are two possible options:

* CASCADED CHECK – this is the default option, where checks cascade to other views defined on the same underlying table

* LOCAL CHECK – only the view that is the target of an INSERT is checked

Here it is shown how to use the CHECK clause in the above example:

—-

$ DROP VIEW first;

DROP VIEW

$ DROP VIEW second;

DROP VIEW

$ CREATE VIEW first AS SELECT * FROM some_data WHERE 0 = id % 2 WITH CHECK OPTION;

CREATE VIEW

$ CREATE VIEW second AS SELECT * FROM some_data WHERE 0 = id % 3 WITH CHECK OPTION;

CREATE VIEW

$ CREATE VIEW third AS SELECT * FROM first WHERE 0 = id % 3 WITH CHECK OPTION;

CREATE VIEW

$ INSERT INTO first(id) VALUES (14);

INSERT 0 1

$ INSERT INTO first(id) VALUES (15);

ERROR:  new row violates WITH CHECK OPTION for view “first”

$ INSERT INTO second(id) VALUES (15);

INSERT 0 1

$ INSERT INTO third(id) VALUES (6);

INSERT 0 1

$ INSERT INTO third(id) VALUES (15);

ERROR:  new row violates WITH CHECK OPTION for view “first”

Note that the view “third” is defined on the view “first”.

The value ’14’ is correctly inserted in the first view, while the value ’15’ can be inserted only into second, not first – as expected. We can insert ‘6’ into the third view because it is divisible by both 3 and 2. The error about inserting ’15’ into the third view even though it is divisible by 3 is because it violates the divisible-by-2 CHECK clause on the parent view, first.  In this case, it is not sufficient to use a LOCAL CHECK clause in both views to work around the problem:

—-

$ DROP VIEW first;

DROP VIEW

$ DROP VIEW third;

DROP VIEW

$ CREATE VIEW first AS SELECT * FROM some_data WHERE 0 = id % 2 WITH LOCAL CHECK OPTION;

CREATE VIEW

$ CREATE VIEW third AS SELECT * FROM first WHERE 0 = id % 3 WITH LOCAL CHECK OPTION;

CREATE VIEW

$ INSERT INTO third(id) VALUES (15);

ERROR:  new row violates WITH CHECK OPTION for view “first”

—-

The working example is shown here:

—-

$ DROP VIEW first;

DROP VIEW

$ DROP VIEW third;

DROP VIEW

$ CREATE VIEW first AS SELECT * FROM some_data WHERE 0 = id % 2;

CREATE VIEW

$ CREATE VIEW third AS SELECT * FROM first WHERE 0 = id % 3 WITH LOCAL CHECK OPTION;

CREATE VIEW

$ INSERT INTO third(id) VALUES (15);

INSERT 0 1

—-

Conclusions

This new check mechanism can be applied directly on updatable views during the INSERT phase. It reinforces more and more the role of the database in maintaining data integrity.

Tags: CHECK, CHECK clause, devops, postgres 9.4, PostgreSQL 9.4, updatable views
Share this entry
  • Share on Facebook
  • Share on Twitter
  • Share on WhatsApp
  • Share on LinkedIn
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

Get in touch with us!

Recent Posts

  • Random Data December 3, 2020
  • Webinar: COMMIT Without Fear – The Beauty of CAMO [Follow Up] November 13, 2020
  • Full-text search since PostgreSQL 8.3 November 5, 2020
  • Random numbers November 3, 2020
  • Webinar: Best Practices for Bulk Data Loading in PostgreSQL [Follow Up] November 2, 2020

Featured External Blogs

Tomas Vondra's Blog

Our Bloggers

  • Simon Riggs
  • Alvaro Herrera
  • Andrew Dunstan
  • Craig Ringer
  • Francesco Canovai
  • Gabriele Bartolini
  • Giulio Calacoci
  • Ian Barwick
  • Marco Nenciarini
  • Mark Wong
  • Pavan Deolasee
  • Petr Jelinek
  • Shaun Thomas
  • Tomas Vondra
  • Umair Shahid

PostgreSQL Cloud

2QLovesPG 2UDA 9.6 backup Barman BDR Business Continuity community conference database DBA development devops disaster recovery greenplum Hot Standby JSON JSONB logical replication monitoring OmniDB open source Orange performance PG12 pgbarman pglogical PG Phriday postgres Postgres-BDR postgres-xl PostgreSQL PostgreSQL 9.6 PostgreSQL10 PostgreSQL11 PostgreSQL 11 PostgreSQL 11 New Features postgresql repmgr Recovery replication security sql wal webinar webinars

Support & Services

24/7 Production Support

Developer Support

Remote DBA for PostgreSQL

PostgreSQL Database Monitoring

PostgreSQL Health Check

PostgreSQL Performance Tuning

Database Security Audit

Upgrade PostgreSQL

PostgreSQL Migration Assessment

Migrate from Oracle to PostgreSQL

Products

HA Postgres Clusters

Postgres-BDR®

2ndQPostgres

pglogical

repmgr

Barman

Postgres Cloud Manager

SQL Firewall

Postgres-XL

OmniDB

Postgres Installer

2UDA

Postgres Learning Center

Introducing Postgres

Blog

Webinars

Books

Videos

Training

Case Studies

Events

About Us

About 2ndQuadrant

What does 2ndQuadrant Mean?

News

Careers 

Team Profile

© 2ndQuadrant Ltd. All rights reserved. | Privacy Policy
  • Twitter
  • LinkedIn
  • Facebook
  • Youtube
  • Mail
Japan PostgreSQL Conference 2014 How monitoring of WAL archiving improves with PostgreSQL 9.4 and pg_stat_ar...
Scroll to top
×