pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Bug with "CHECK" when part of ALTER TABLE ... ADD COLUMN IF NOT EXISTS ... CHECK ...
5+ messages / 3 participants
[nested] [flat]

* Bug with "CHECK" when part of ALTER TABLE ... ADD COLUMN IF NOT EXISTS ... CHECK ...
@ 2019-09-24 18:37 Alfred R. Fuller <alfred.fuller@gmail.com>
  2019-09-24 20:27 ` Re: Bug with "CHECK" when part of ALTER TABLE ... ADD COLUMN IF NOT EXISTS ... CHECK ... Fabrízio de Royes Mello <fabriziomello@gmail.com>
  2019-09-24 20:27 ` Re: Bug with "CHECK" when part of ALTER TABLE ... ADD COLUMN IF NOT EXISTS ... CHECK ... Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 5+ messages in thread

From: Alfred R. Fuller @ 2019-09-24 18:37 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org

Hi,

I ran into what seems to be a bug with this command.

Postgres version: PostgreSQL 11.5 (Debian 11.5-1.pgdg90+1) on
x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0
20170516, 64-bit

Expected behavior:
If the column exists nothing is altered.

Actual behavior:
The check is always added regardless if the column exists or not.

Reproduction steps:

CREATE TABLE "element_instances" (
  "instance_id" UUID NOT NULL DEFAULT uuid_generate_v4(),
  "generation" INTEGER NOT NULL DEFAULT 1,
  "element" CHARACTER VARYING(1024) NOT NULL CHECK(element <> ''),
  "kind" CHARACTER VARYING(64) NOT NULL CHECK(kind <> ''),
  "observed_start_time" TIMESTAMP WITHOUT TIME ZONE NOT NULL,
  "observed_end_time" TIMESTAMP WITHOUT TIME ZONE,
  "estimated_time" TSRANGE NOT NULL,
  "storage_location" CHARACTER VARYING(1024),
  "assets" CHARACTER VARYING(1024)[] NOT NULL,
  "s2cells" BIGINT[] NOT NULL,
  EXCLUDE USING GIST (element WITH =, estimated_time WITH &&),
  PRIMARY KEY ("instance_id")
);

ALTER TABLE IF EXISTS "element_instances"
  ALTER COLUMN "storage_location" DROP NOT NULL,
  ALTER COLUMN "generation" SET NOT NULL,
  DROP COLUMN IF EXISTS "instance_name" CASCADE,
  ADD COLUMN IF NOT EXISTS "kind" CHARACTER VARYING(64) NOT NULL CHECK(kind
<> '');

The second command should not change anything; however, then if you run:
SELECT con.conname, con.consrc
  FROM pg_catalog.pg_constraint con
  INNER JOIN pg_catalog.pg_class rel
  ON rel.oid = con.conrelid
  WHERE rel.relname = "element_instances"
  ORDER BY con.conname;

you will see:

...
element_instances_kind_check, "((kind)::text <> ''::text)"
element_instances_kind_check1, "((kind)::text <> ''::text)"
...

A duplicate constraint has been added!

Thanks,

Alfred

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

* Re: Bug with "CHECK" when part of ALTER TABLE ... ADD COLUMN IF NOT EXISTS ... CHECK ...
  2019-09-24 18:37 Bug with "CHECK" when part of ALTER TABLE ... ADD COLUMN IF NOT EXISTS ... CHECK ... Alfred R. Fuller <alfred.fuller@gmail.com>
@ 2019-09-24 20:27 ` Fabrízio de Royes Mello <fabriziomello@gmail.com>
  1 sibling, 0 replies; 5+ messages in thread

From: Fabrízio de Royes Mello @ 2019-09-24 20:27 UTC (permalink / raw)
  To: Alfred R. Fuller <alfred.fuller@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org

On Tue, Sep 24, 2019 at 5:15 PM Alfred R. Fuller <alfred.fuller@gmail.com>
wrote:

> Hi,
>
> I ran into what seems to be a bug with this command.
>
> Postgres version: PostgreSQL 11.5 (Debian 11.5-1.pgdg90+1) on
> x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0
> 20170516, 64-bit
>
> Expected behavior:
> If the column exists nothing is altered.
>
> Actual behavior:
> The check is always added regardless if the column exists or not.
>
> Reproduction steps:
>
> CREATE TABLE "element_instances" (
>   "instance_id" UUID NOT NULL DEFAULT uuid_generate_v4(),
>   "generation" INTEGER NOT NULL DEFAULT 1,
>   "element" CHARACTER VARYING(1024) NOT NULL CHECK(element <> ''),
>   "kind" CHARACTER VARYING(64) NOT NULL CHECK(kind <> ''),
>   "observed_start_time" TIMESTAMP WITHOUT TIME ZONE NOT NULL,
>   "observed_end_time" TIMESTAMP WITHOUT TIME ZONE,
>   "estimated_time" TSRANGE NOT NULL,
>   "storage_location" CHARACTER VARYING(1024),
>   "assets" CHARACTER VARYING(1024)[] NOT NULL,
>   "s2cells" BIGINT[] NOT NULL,
>   EXCLUDE USING GIST (element WITH =, estimated_time WITH &&),
>   PRIMARY KEY ("instance_id")
> );
>
> ALTER TABLE IF EXISTS "element_instances"
>   ALTER COLUMN "storage_location" DROP NOT NULL,
>   ALTER COLUMN "generation" SET NOT NULL,
>   DROP COLUMN IF EXISTS "instance_name" CASCADE,
>   ADD COLUMN IF NOT EXISTS "kind" CHARACTER VARYING(64) NOT NULL
> CHECK(kind <> '');
>
> The second command should not change anything; however, then if you run:
> SELECT con.conname, con.consrc
>   FROM pg_catalog.pg_constraint con
>   INNER JOIN pg_catalog.pg_class rel
>   ON rel.oid = con.conrelid
>   WHERE rel.relname = "element_instances"
>   ORDER BY con.conname;
>
> you will see:
>
> ...
> element_instances_kind_check, "((kind)::text <> ''::text)"
> element_instances_kind_check1, "((kind)::text <> ''::text)"
> ...
>
> A duplicate constraint has been added!
>
>
I'll take a look on it?

-- 
   Fabrízio de Royes Mello         Timbira - http://www.timbira.com.br/
   PostgreSQL: Consultoria, Desenvolvimento, Suporte 24x7 e Treinamento

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

* Re: Bug with "CHECK" when part of ALTER TABLE ... ADD COLUMN IF NOT EXISTS ... CHECK ...
  2019-09-24 18:37 Bug with "CHECK" when part of ALTER TABLE ... ADD COLUMN IF NOT EXISTS ... CHECK ... Alfred R. Fuller <alfred.fuller@gmail.com>
@ 2019-09-24 20:27 ` Tom Lane <tgl@sss.pgh.pa.us>
  2019-09-24 20:31   ` Re: Bug with "CHECK" when part of ALTER TABLE ... ADD COLUMN IF NOT EXISTS ... CHECK ... Fabrízio de Royes Mello <fabriziomello@gmail.com>
  1 sibling, 1 reply; 5+ messages in thread

From: Tom Lane @ 2019-09-24 20:27 UTC (permalink / raw)
  To: Alfred R. Fuller <alfred.fuller@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org

"Alfred R. Fuller" <alfred.fuller@gmail.com> writes:
> Expected behavior:
> If the column exists nothing is altered.
> Actual behavior:
> The check is always added regardless if the column exists or not.

Yeah, this is yet another variant of the theme that ADD COLUMN IF EXISTS
only conditionalizes the creation of the column proper, and not other
subsidiary subcommands such as adding indexes or constraints.  I've been
poking at a fix [1], and hopefully we'll get it dealt with in v13, but
for sure it'll be a bigger change than we'd risk back-patching.

			regards, tom lane

[1] https://www.postgresql.org/message-id/flat/10365.1558909428@sss.pgh.pa.us





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

* Re: Bug with "CHECK" when part of ALTER TABLE ... ADD COLUMN IF NOT EXISTS ... CHECK ...
  2019-09-24 18:37 Bug with "CHECK" when part of ALTER TABLE ... ADD COLUMN IF NOT EXISTS ... CHECK ... Alfred R. Fuller <alfred.fuller@gmail.com>
  2019-09-24 20:27 ` Re: Bug with "CHECK" when part of ALTER TABLE ... ADD COLUMN IF NOT EXISTS ... CHECK ... Tom Lane <tgl@sss.pgh.pa.us>
@ 2019-09-24 20:31   ` Fabrízio de Royes Mello <fabriziomello@gmail.com>
  2019-09-24 21:50     ` Re: Bug with "CHECK" when part of ALTER TABLE ... ADD COLUMN IF NOT EXISTS ... CHECK ... Alfred R. Fuller <alfred.fuller@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Fabrízio de Royes Mello @ 2019-09-24 20:31 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Alfred R. Fuller <alfred.fuller@gmail.com>; pgsql-bugs@lists.postgresql.org

On Tue, Sep 24, 2019 at 5:27 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> "Alfred R. Fuller" <alfred.fuller@gmail.com> writes:
> > Expected behavior:
> > If the column exists nothing is altered.
> > Actual behavior:
> > The check is always added regardless if the column exists or not.
>
> Yeah, this is yet another variant of the theme that ADD COLUMN IF EXISTS
> only conditionalizes the creation of the column proper, and not other
> subsidiary subcommands such as adding indexes or constraints.  I've been
> poking at a fix [1], and hopefully we'll get it dealt with in v13, but
> for sure it'll be a bigger change than we'd risk back-patching.
>

Nice... I didn't see it... thanks

--
   Fabrízio de Royes Mello         Timbira - http://www.timbira.com.br/
   PostgreSQL: Consultoria, Desenvolvimento, Suporte 24x7 e Treinamento

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

* Re: Bug with "CHECK" when part of ALTER TABLE ... ADD COLUMN IF NOT EXISTS ... CHECK ...
  2019-09-24 18:37 Bug with "CHECK" when part of ALTER TABLE ... ADD COLUMN IF NOT EXISTS ... CHECK ... Alfred R. Fuller <alfred.fuller@gmail.com>
  2019-09-24 20:27 ` Re: Bug with "CHECK" when part of ALTER TABLE ... ADD COLUMN IF NOT EXISTS ... CHECK ... Tom Lane <tgl@sss.pgh.pa.us>
  2019-09-24 20:31   ` Re: Bug with "CHECK" when part of ALTER TABLE ... ADD COLUMN IF NOT EXISTS ... CHECK ... Fabrízio de Royes Mello <fabriziomello@gmail.com>
@ 2019-09-24 21:50     ` Alfred R. Fuller <alfred.fuller@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Alfred R. Fuller @ 2019-09-24 21:50 UTC (permalink / raw)
  To: fabriziomello@gmail.com; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-bugs@lists.postgresql.org

Thanks for the quick response! Do you have any suggestion on work arounds?

On Tue, Sep 24, 2019 at 1:31 PM Fabrízio de Royes Mello <
fabriziomello@gmail.com> wrote:

>
> On Tue, Sep 24, 2019 at 5:27 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> >
> > "Alfred R. Fuller" <alfred.fuller@gmail.com> writes:
> > > Expected behavior:
> > > If the column exists nothing is altered.
> > > Actual behavior:
> > > The check is always added regardless if the column exists or not.
> >
> > Yeah, this is yet another variant of the theme that ADD COLUMN IF EXISTS
> > only conditionalizes the creation of the column proper, and not other
> > subsidiary subcommands such as adding indexes or constraints.  I've been
> > poking at a fix [1], and hopefully we'll get it dealt with in v13, but
> > for sure it'll be a bigger change than we'd risk back-patching.
> >
>
> Nice... I didn't see it... thanks
>
> --
>    Fabrízio de Royes Mello         Timbira - http://www.timbira.com.br/
>    PostgreSQL: Consultoria, Desenvolvimento, Suporte 24x7 e Treinamento
>

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


end of thread, other threads:[~2019-09-24 21:50 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-09-24 18:37 Bug with "CHECK" when part of ALTER TABLE ... ADD COLUMN IF NOT EXISTS ... CHECK ... Alfred R. Fuller <alfred.fuller@gmail.com>
2019-09-24 20:27 ` Fabrízio de Royes Mello <fabriziomello@gmail.com>
2019-09-24 20:27 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-09-24 20:31   ` Fabrízio de Royes Mello <fabriziomello@gmail.com>
2019-09-24 21:50     ` Alfred R. Fuller <alfred.fuller@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