Re: Constraints of view attributes

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: 51183341(at)gmx(dot)at
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Constraints of view attributes
Date: 2017-05-11 20:07:48
Message-ID: 27377.1494533268@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

<51183341(at)gmx(dot)at> writes:
> create table t1 (f1 text not null);
> create table t2 (f2 text);
> create view v as (select t1.f1, t2.f2 from t1, t2);

> Given v, How can a find out the t1.f1 has a not null constraint.

I assume what you actually mean is you want to know whether the
view v will always produce a non-null in that column. That's not
that easy. It is true in the above example, but consider for instance

create view v as select t1.f1, t2.f2 from t2 left join t1 on ...

With the outer join, that view column could produce nulls despite
the NOT NULL constraint on t1. There are other constructs such
as GROUPING SETS that break the equivalence, too. So you'd need a
fairly careful inspection of the view parsetree not only to find
which table is referenced, but whether there's something in the
view that defeats the deduction you'd like to make. There isn't
anything in Postgres right now that makes that type of inference,
let alone a way to export it to userland.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Neil Anderson 2017-05-11 20:26:53 Re:
Previous Message Adrian Klaver 2017-05-11 19:55:31 Re: Constraints of view attributes