pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
set not null column using existing constraint
5+ messages / 4 participants
[nested] [flat]

* set not null column using existing constraint
@ 2018-01-10 12:09 Marco Piovan <mpiovan@workwave.com>
  2018-01-10 15:51 ` Re: set not null column using existing constraint Keith <keith@keithf4.com>
  2018-01-10 16:00 ` Re: set not null column using existing constraint Rui DeSousa <rui.desousa@icloud.com>
  0 siblings, 2 replies; 5+ messages in thread

From: Marco Piovan @ 2018-01-10 12:09 UTC (permalink / raw)
  To: pgsql-admin

Hello,
I want to create a primary key using  an existing unique index.
I have already created the index concurrently and I have already validated
a not null constraint on the same column.

Looking at the postgres documentation (
https://www.postgresql.org/docs/9.6/static/sql-altertable.html):

"If PRIMARY KEY is specified, and the index's columns are not already
marked NOT NULL, then this command will attempt to do ALTER COLUMN SET NOT
NULL against each such column. That requires a full table scan to verify
the column(s) contain no nulls. In all other cases, this is a fast
operation."

Is it possible to specify the already validated not null constraint and
avoid another scan of the table (with lock)?

Best regards

-- 
Marco Piovan

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: set not null column using existing constraint
  2018-01-10 12:09 set not null column using existing constraint Marco Piovan <mpiovan@workwave.com>
@ 2018-01-10 15:51 ` Keith <keith@keithf4.com>
  1 sibling, 0 replies; 5+ messages in thread

From: Keith @ 2018-01-10 15:51 UTC (permalink / raw)
  To: Marco Piovan <mpiovan@workwave.com>; +Cc: pgsql-admin

On Wed, Jan 10, 2018 at 7:09 AM, Marco Piovan <mpiovan@workwave.com> wrote:

> Hello,
> I want to create a primary key using  an existing unique index.
> I have already created the index concurrently and I have already validated
> a not null constraint on the same column.
>
> Looking at the postgres documentation (https://www.postgresql.org/
> docs/9.6/static/sql-altertable.html):
>
> "If PRIMARY KEY is specified, and the index's columns are not already
> marked NOT NULL, then this command will attempt to do ALTER COLUMN SET
> NOT NULL against each such column. That requires a full table scan to
> verify the column(s) contain no nulls. In all other cases, this is a fast
> operation."
>
> Is it possible to specify the already validated not null constraint and
> avoid another scan of the table (with lock)?
>
> Best regards
>
> --
> Marco Piovan
>

As the docs say, If the NOT NULL constraint exists on the given column and
is valid, it will not do the full table scan. Just use the USING clause to
the command to set the primary key giving it the unique index you already
made.

ALTER TABLE ... ADD PRIMARY KEY USING unique_index;

No need to specify the constraint.

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: set not null column using existing constraint
  2018-01-10 12:09 set not null column using existing constraint Marco Piovan <mpiovan@workwave.com>
@ 2018-01-10 16:00 ` Rui DeSousa <rui.desousa@icloud.com>
  2018-01-10 16:02   ` Re: set not null column using existing constraint Marco Piovan <mpiovan@workwave.com>
  1 sibling, 1 reply; 5+ messages in thread

From: Rui DeSousa @ 2018-01-10 16:00 UTC (permalink / raw)
  To: Marco Piovan <mpiovan@workwave.com>; +Cc: pgsql-admin

Marco,

Since the column has already been altered to be not null then attaching the primary key will not need to rescan the table to mark the column as not null again — just use the “using index" clause.  

i.e.

alter table rui.member add primary key using index member_pkey;


> On Jan 10, 2018, at 7:09 AM, Marco Piovan <mpiovan@workwave.com> wrote:
> 
> Hello,
> I want to create a primary key using  an existing unique index. 
> I have already created the index concurrently and I have already validated a not null constraint on the same column.
> 
> Looking at the postgres documentation (https://www.postgresql.org/docs/9.6/static/sql-altertable.html <https://www.postgresql.org/docs/9.6/static/sql-altertable.html;):
> 
> "If PRIMARY KEY is specified, and the index's columns are not already marked NOT NULL, then this command will attempt to do ALTER COLUMN SET NOT NULL against each such column. That requires a full table scan to verify the column(s) contain no nulls. In all other cases, this is a fast operation."
> 
> Is it possible to specify the already validated not null constraint and avoid another scan of the table (with lock)?
> 
> Best regards
> 
> -- 
> Marco Piovan

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: set not null column using existing constraint
  2018-01-10 12:09 set not null column using existing constraint Marco Piovan <mpiovan@workwave.com>
  2018-01-10 16:00 ` Re: set not null column using existing constraint Rui DeSousa <rui.desousa@icloud.com>
@ 2018-01-10 16:02   ` Marco Piovan <mpiovan@workwave.com>
  2018-01-10 16:13     ` Re: set not null column using existing constraint David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Marco Piovan @ 2018-01-10 16:02 UTC (permalink / raw)
  To: Rui DeSousa <rui.desousa@icloud.com>; +Cc: pgsql-admin

The column is not marked as NOT NULL but a valid constraint  "CHECK (column
IS NOT NULL)" exists.

I haven't found a way to use that specific constraint and avoid the table
rescan.

Regards

On Wed, Jan 10, 2018 at 5:00 PM, Rui DeSousa <rui.desousa@icloud.com> wrote:

> Marco,
>
> Since the column has already been altered to be not null then attaching
> the primary key will not need to rescan the table to mark the column as not
> null again — just use the “using index" clause.
>
> i.e.
>
> alter table rui.member add primary key using index member_pkey;
>
>
> On Jan 10, 2018, at 7:09 AM, Marco Piovan <mpiovan@workwave.com> wrote:
>
> Hello,
> I want to create a primary key using  an existing unique index.
> I have already created the index concurrently and I have already validated
> a not null constraint on the same column.
>
> Looking at the postgres documentation (https://www.postgresql.org/
> docs/9.6/static/sql-altertable.html):
>
> "If PRIMARY KEY is specified, and the index's columns are not already
> marked NOT NULL, then this command will attempt to do ALTER COLUMN SET
> NOT NULL against each such column. That requires a full table scan to
> verify the column(s) contain no nulls. In all other cases, this is a fast
> operation."
>
> Is it possible to specify the already validated not null constraint and
> avoid another scan of the table (with lock)?
>
> Best regards
>
> --
> Marco Piovan
>
>
>


-- 
*Marco Piovan*

Senior System Engineer
mpiovan@workwave.com | workwave.com

Via Luigi Da Porto, 2/C - 37122 Verona (VR) - Italy

​
<https://www.workwave.com/;    <https://www.facebook.com/WorkWaveCo;
<https://www.linkedin.com/company/workwave;   <https://twitter.com/WorkWave;
   <https://plus.google.com/u/0/108362178327115857089/posts;

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: set not null column using existing constraint
  2018-01-10 12:09 set not null column using existing constraint Marco Piovan <mpiovan@workwave.com>
  2018-01-10 16:00 ` Re: set not null column using existing constraint Rui DeSousa <rui.desousa@icloud.com>
  2018-01-10 16:02   ` Re: set not null column using existing constraint Marco Piovan <mpiovan@workwave.com>
@ 2018-01-10 16:13     ` David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: David G. Johnston @ 2018-01-10 16:13 UTC (permalink / raw)
  To: Marco Piovan <mpiovan@workwave.com>; +Cc: Rui DeSousa <rui.desousa@icloud.com>; pgsql-admin

On Wednesday, January 10, 2018, Marco Piovan <mpiovan@workwave.com> wrote:

> The column is not marked as NOT NULL but a valid constraint  "CHECK
> (column IS NOT NULL)" exists.
>
> I haven't found a way to use that specific constraint and avoid the table
> rescan.
>

It probably doesn't exist then, which is not that surprising; no one
foresaw people using a check constraint for not null when an explicit
specifier exists.

David J.

^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2018-01-10 16:13 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-01-10 12:09 set not null column using existing constraint Marco Piovan <mpiovan@workwave.com>
2018-01-10 15:51 ` Keith <keith@keithf4.com>
2018-01-10 16:00 ` Rui DeSousa <rui.desousa@icloud.com>
2018-01-10 16:02   ` Marco Piovan <mpiovan@workwave.com>
2018-01-10 16:13     ` David G. Johnston <david.g.johnston@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox