pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Temporarily disable not null constraints
4+ messages / 4 participants
[nested] [flat]

* Temporarily disable not null constraints
@ 2020-12-03 19:58  Nagaraj Raj <nagaraj.sf@yahoo.com>
  0 siblings, 3 replies; 4+ messages in thread

From: Nagaraj Raj @ 2020-12-03 19:58 UTC (permalink / raw)
  To: Pgsql Performance <pgsql-performance@lists.postgresql.org>

Hi,
Can we disable not null constraints temporarily in the session-based transaction, like we disable FK constraints? 
SETsession_replication_role = ‘replica’; alter table table_name disable trigger user;”

above two options are working for unique constraints violation exception. 
Thanks,Rj

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

* Re: Temporarily disable not null constraints
@ 2020-12-03 20:09  Michael Lewis <mlewis@entrata.com>
  parent: Nagaraj Raj <nagaraj.sf@yahoo.com>
  2 siblings, 0 replies; 4+ messages in thread

From: Michael Lewis @ 2020-12-03 20:09 UTC (permalink / raw)
  To: Nagaraj Raj <nagaraj.sf@yahoo.com>; +Cc: Pgsql Performance <pgsql-performance@lists.postgresql.org>

On Thu, Dec 3, 2020 at 1:00 PM Nagaraj Raj <nagaraj.sf@yahoo.com> wrote:

> Hi,
>
> Can we disable not null constraints temporarily in the session-based
> transaction, like we disable FK constraints?
>
> SET session_replication_role = ‘replica’;
> alter table table_name disable trigger user;”
>
> above two options are working for unique constraints violation exception.
>
> Thanks,
> Rj
>


You can alter the column and remove the not null constraint, do your work,
and then add it back, but it will have to verify all rows have that column
set, that is, you can't leave some of them null.

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

* Re: Temporarily disable not null constraints
@ 2020-12-03 20:13  Milos Babic <milos.babic@gmail.com>
  parent: Nagaraj Raj <nagaraj.sf@yahoo.com>
  2 siblings, 0 replies; 4+ messages in thread

From: Milos Babic @ 2020-12-03 20:13 UTC (permalink / raw)
  To: Nagaraj Raj <nagaraj.sf@yahoo.com>; +Cc: Pgsql Performance <pgsql-performance@lists.postgresql.org>

generally, you shouldn't be disabling your constraints, especially if you
are having multiple parallel processes accessing your db.
instead, you should create them DEFERRABLE and have them checked at the end
of your transaction.

regarding your question about NOT NULL: it is not possible to have it
deferred (please check this page:
https://www.postgresql.org/docs/13/sql-set-constraints.html)
you may alter your column, remove it, and then get it back, but still all
rows will have to be checked, which I doubt you would like to see on a
large table.

regards, milos



On Thu, Dec 3, 2020 at 9:00 PM Nagaraj Raj <nagaraj.sf@yahoo.com> wrote:

> Hi,
>
> Can we disable not null constraints temporarily in the session-based
> transaction, like we disable FK constraints?
>
> SET session_replication_role = ‘replica’;
> alter table table_name disable trigger user;”
>
> above two options are working for unique constraints violation exception.
>
> Thanks,
> Rj
>

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

* Re: Temporarily disable not null constraints
@ 2020-12-03 22:34  Justin Pryzby <pryzby@telsasoft.com>
  parent: Nagaraj Raj <nagaraj.sf@yahoo.com>
  2 siblings, 0 replies; 4+ messages in thread

From: Justin Pryzby @ 2020-12-03 22:34 UTC (permalink / raw)
  To: Nagaraj Raj <nagaraj.sf@yahoo.com>; +Cc: pgsql-performance@lists.postgresql.org

On Thu, Dec 03, 2020 at 07:58:15PM +0000, Nagaraj Raj wrote:
> Can we disable not null constraints temporarily in the session-based transaction, like we disable FK constraints? 

If you're trying to temporarily violate the not-null constraint..
I don't know if it's a good idea..

..but maybe this feature in v12 helps you:

https://www.postgresql.org/docs/12/sql-altertable.html
| Ordinarily this is checked during the ALTER TABLE by scanning the entire table; however, if a valid CHECK constraint is found which proves no NULL can exist, then the table scan is skipped.

When you're done violating constraints, you can
ALTER .. ADD CONSTRAINT .. CHECK (.. IS NOT NULL) NOT VALID, and then
ALTER .. VALIDATE CONSTRAINT, and then ALTER column SET NOT NULL.

-- 
Justin





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


end of thread, other threads:[~2020-12-03 22:34 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-12-03 19:58 Temporarily disable not null constraints Nagaraj Raj <nagaraj.sf@yahoo.com>
2020-12-03 20:09 ` Michael Lewis <mlewis@entrata.com>
2020-12-03 20:13 ` Milos Babic <milos.babic@gmail.com>
2020-12-03 22:34 ` Justin Pryzby <pryzby@telsasoft.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