pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Commenting a FK crashes ALTER TABLE statements
5+ messages / 4 participants
[nested] [flat]

Commits from this thread:
  2017-05-15 12590c5d33 b35cce914c 53a1aa9f9a  Fix unsafe reference into relcache in constructed CommentStmt.
* Commenting a FK crashes ALTER TABLE statements
@ 2017-05-14 15:43 Marko Elezovic <marko.elezovic@oradian.com>
  2017-05-15 04:05 ` Re: Commenting a FK crashes ALTER TABLE statements David Rowley <david.rowley@2ndquadrant.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Marko Elezovic @ 2017-05-14 15:43 UTC (permalink / raw)
  To: pgsql-bugs

Affected versions: 9.5.0 up to 9.6.3 (regression from 9.4.12)
Affected OS: All

Script to reproduce:
  psql -Upostgres -c"DROP DATABASE IF EXISTS cod;" postgres
  psql -Upostgres -c"CREATE DATABASE cod;" postgres
  psql -Upostgres -c"CREATE TABLE foo(id int PRIMARY KEY);" cod
  psql -Upostgres -c"CREATE TABLE bar(id int CONSTRAINT baz REFERENCES foo);" cod
  psql -Upostgres -c"COMMENT ON CONSTRAINT baz ON bar IS 'Fubar';" cod
  psql -Upostgres -c"ALTER TABLE foo ALTER COLUMN id TYPE int;" cod

*IMPORTANT NOTE*: The example above is 100% reproducible if executed verbatim, from command line of 9.5+ while on either Windows/Linux
On the other hand, when ran from psql by pasting all the commands or running them within the same script it is not always reproducible.
I also haven't been able to reproduce the issue by adding a new test into src/test/regress/sql/comments.sql

Expected output after last alter should be a message with random garbage instead of foreign key name:

  ERROR:  relation "public.¬" does not exist
  ERROR:  relation "public." does not exist

I've traced the issue into Re-adding an existing comment (starting in tablecmds.c):

case AT_ReAddComment:     /* Re-add existing comment */
address = CommentObject((CommentStmt *) cmd->def);

... which fails in pg_constraint.c while traversing the constraints:

scan = systable_beginscan(pg_constraint, ConstraintRelidIndexId, true,
NULL, 1, skey);

       while (HeapTupleIsValid(tuple = systable_getnext(scan)))

Here tuple sometimes gets garbage memory addresses, so it explains at least 50% of the craziness I've seen while reproducing.

The same example also has a chance to produce these as well:

  ERROR:  constraint "baz" for table "foo" does not exist <-- due to not being found in the above while loop
  ERROR:  "foo_pkey" is an index                          <-- when trying to open the heap for pkey constraint

  ERROR:  relation "public.pg_class_tblspc_relfilenode_index" does not exist <-- ??? Heisenbugs, frequently visible in production SQL migrations
  ERROR:  relation "public.pg_constraint_conname_nsp_index" does not exist   <-- ??? but could never reproduce when in debug mode / tracing

I'll be happy to continue the tracing effort if need be, but hoping this is reproducible on your machines

With heartfelt regards,
Marko


Oradian Ltd., Head of Technology
marko.elezovic@oradian.com<mailto:marko.elezovic@oradian.com>
+385 91 2646 342

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

* Re: Commenting a FK crashes ALTER TABLE statements
  2017-05-14 15:43 Commenting a FK crashes ALTER TABLE statements Marko Elezovic <marko.elezovic@oradian.com>
@ 2017-05-15 04:05 ` David Rowley <david.rowley@2ndquadrant.com>
  2017-05-15 04:19   ` Re: Commenting a FK crashes ALTER TABLE statements Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: David Rowley @ 2017-05-15 04:05 UTC (permalink / raw)
  To: Marko Elezovic <marko.elezovic@oradian.com>; +Cc: pgsql-bugs

On 15 May 2017 at 03:43, Marko Elezovic <marko.elezovic@oradian.com> wrote:
> Script to reproduce:
>
>   psql -Upostgres -c"DROP DATABASE IF EXISTS cod;" postgres
>
>   psql -Upostgres -c"CREATE DATABASE cod;" postgres
>
>   psql -Upostgres -c"CREATE TABLE foo(id int PRIMARY KEY);" cod
>
>   psql -Upostgres -c"CREATE TABLE bar(id int CONSTRAINT baz REFERENCES
> foo);" cod
>
>   psql -Upostgres -c"COMMENT ON CONSTRAINT baz ON bar IS 'Fubar';" cod
>
>   psql -Upostgres -c"ALTER TABLE foo ALTER COLUMN id TYPE int;" cod

Thanks for detailing out the method to reproduce.

It can be simplified a bit to become:

CREATE TABLE foo(id int PRIMARY KEY);
CREATE TABLE bar(id int CONSTRAINT baz REFERENCES foo);
COMMENT ON CONSTRAINT baz ON bar IS 'Fubar';
\c
ALTER TABLE foo ALTER COLUMN id TYPE int;

It seems there's just some missing pstrdup() calls in
RebuildConstraintComment().

The attached should fix it.

-- 
 David Rowley                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

recomment_fix.patch (application/octet-stream, 709B) download diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index cdcb949..29b0544 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9746,8 +9746,8 @@ RebuildConstraintComment(AlteredTableInfo *tab, int pass, Oid objid, cmd = makeNode(CommentStmt); cmd->objtype = OBJECT_TABCONSTRAINT; cmd->object = (Node *) list_make3(makeString(get_namespace_name(RelationGetNamespace(rel))), - makeString(RelationGetRelationName(rel)), - makeString(conname)); + makeString(pstrdup(RelationGetRelationName(rel))), + makeString(pstrdup(conname))); cmd->comment = comment_str; /* Append it to list of commands */
^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: Commenting a FK crashes ALTER TABLE statements
  2017-05-14 15:43 Commenting a FK crashes ALTER TABLE statements Marko Elezovic <marko.elezovic@oradian.com>
  2017-05-15 04:05 ` Re: Commenting a FK crashes ALTER TABLE statements David Rowley <david.rowley@2ndquadrant.com>
@ 2017-05-15 04:19   ` Michael Paquier <michael.paquier@gmail.com>
  2017-05-15 14:37     ` Re: Commenting a FK crashes ALTER TABLE statements Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 5+ messages in thread

From: Michael Paquier @ 2017-05-15 04:19 UTC (permalink / raw)
  To: David Rowley <david.rowley@2ndquadrant.com>; +Cc: Marko Elezovic <marko.elezovic@oradian.com>; pgsql-bugs

On Mon, May 15, 2017 at 1:05 PM, David Rowley
<david.rowley@2ndquadrant.com> wrote:
> It can be simplified a bit to become:
>
> CREATE TABLE foo(id int PRIMARY KEY);
> CREATE TABLE bar(id int CONSTRAINT baz REFERENCES foo);
> COMMENT ON CONSTRAINT baz ON bar IS 'Fubar';
> \c
> ALTER TABLE foo ALTER COLUMN id TYPE int;
>
> It seems there's just some missing pstrdup() calls in
> RebuildConstraintComment().
>
> The attached should fix it.

I was just finishing to debug it :)
It is surprising that we have not caught this earlier as comment
re-creation handling in ALTER TABLE has been reworked some time ago
already. This means that we did not stress this code enough. Attached
is the previous fix, completed with a set of regression tests.
-- 
Michael

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

recomment_fix_v2.patch (application/octet-stream, 5.3K) download diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index b94db89b25..cda9f0970a 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9764,8 +9764,8 @@ RebuildConstraintComment(AlteredTableInfo *tab, int pass, Oid objid, cmd = makeNode(CommentStmt); cmd->objtype = OBJECT_TABCONSTRAINT; cmd->object = (Node *) list_make3(makeString(get_namespace_name(RelationGetNamespace(rel))), - makeString(RelationGetRelationName(rel)), - makeString(conname)); + makeString(pstrdup(RelationGetRelationName(rel))), + makeString(pstrdup(conname))); cmd->comment = comment_str; /* Append it to list of commands */ diff --git a/src/test/regress/expected/alter_table.out b/src/test/regress/expected/alter_table.out index 41df9f0759..6ebebb4a03 100644 --- a/src/test/regress/expected/alter_table.out +++ b/src/test/regress/expected/alter_table.out @@ -2788,6 +2788,39 @@ SELECT conname as constraint, obj_description(oid, 'pg_constraint') as comment F comment_test_positive_col_check | CHECK constraint on comment_test.positive_col (2 rows) +-- Check compatibility for foreign keys and comments. This is done +-- separately as rebuilding the column type of the parent leads +-- to an error and would reduce the test scope. +CREATE TABLE comment_test_child ( + id text CONSTRAINT comment_test_child_fk REFERENCES comment_test); +CREATE INDEX comment_test_child_fk ON comment_test_child(id); +COMMENT ON COLUMN comment_test_child.id IS 'Column ''id'' on comment_test_child'; +COMMENT ON INDEX comment_test_child_fk IS 'Index backing the FOREIGN KEY of comment_test_child'; +COMMENT ON CONSTRAINT comment_test_child_fk ON comment_test_child IS 'FOREIGN KEY constraint of comment_test_child'; +-- Change column type of parent +ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE text; +ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE int USING id::integer; +ERROR: foreign key constraint "comment_test_child_fk" cannot be implemented +DETAIL: Key columns "id" and "id" are of incompatible types: text and integer. +-- Comments should be intact +SELECT col_description('comment_test_child'::regclass, 1) as comment; + comment +----------------------------------- + Column 'id' on comment_test_child +(1 row) + +SELECT indexrelid::regclass::text as index, obj_description(indexrelid, 'pg_class') as comment FROM pg_index where indrelid = 'comment_test_child'::regclass ORDER BY 1, 2; + index | comment +-----------------------+----------------------------------------------------- + comment_test_child_fk | Index backing the FOREIGN KEY of comment_test_child +(1 row) + +SELECT conname as constraint, obj_description(oid, 'pg_constraint') as comment FROM pg_constraint where conrelid = 'comment_test_child'::regclass ORDER BY 1, 2; + constraint | comment +-----------------------+---------------------------------------------- + comment_test_child_fk | FOREIGN KEY constraint of comment_test_child +(1 row) + -- Check that we map relation oids to filenodes and back correctly. Only -- display bad mappings so the test output doesn't change all the time. A -- filenode function call can return NULL for a relation dropped concurrently diff --git a/src/test/regress/sql/alter_table.sql b/src/test/regress/sql/alter_table.sql index 24d1d4d23b..f014675628 100644 --- a/src/test/regress/sql/alter_table.sql +++ b/src/test/regress/sql/alter_table.sql @@ -1786,6 +1786,24 @@ SELECT col_description('comment_test'::regclass, 1) as comment; SELECT indexrelid::regclass::text as index, obj_description(indexrelid, 'pg_class') as comment FROM pg_index where indrelid = 'comment_test'::regclass ORDER BY 1, 2; SELECT conname as constraint, obj_description(oid, 'pg_constraint') as comment FROM pg_constraint where conrelid = 'comment_test'::regclass ORDER BY 1, 2; +-- Check compatibility for foreign keys and comments. This is done +-- separately as rebuilding the column type of the parent leads +-- to an error and would reduce the test scope. +CREATE TABLE comment_test_child ( + id text CONSTRAINT comment_test_child_fk REFERENCES comment_test); +CREATE INDEX comment_test_child_fk ON comment_test_child(id); +COMMENT ON COLUMN comment_test_child.id IS 'Column ''id'' on comment_test_child'; +COMMENT ON INDEX comment_test_child_fk IS 'Index backing the FOREIGN KEY of comment_test_child'; +COMMENT ON CONSTRAINT comment_test_child_fk ON comment_test_child IS 'FOREIGN KEY constraint of comment_test_child'; + +-- Change column type of parent +ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE text; +ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE int USING id::integer; + +-- Comments should be intact +SELECT col_description('comment_test_child'::regclass, 1) as comment; +SELECT indexrelid::regclass::text as index, obj_description(indexrelid, 'pg_class') as comment FROM pg_index where indrelid = 'comment_test_child'::regclass ORDER BY 1, 2; +SELECT conname as constraint, obj_description(oid, 'pg_constraint') as comment FROM pg_constraint where conrelid = 'comment_test_child'::regclass ORDER BY 1, 2; -- Check that we map relation oids to filenodes and back correctly. Only -- display bad mappings so the test output doesn't change all the time. A
^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: Commenting a FK crashes ALTER TABLE statements
  2017-05-14 15:43 Commenting a FK crashes ALTER TABLE statements Marko Elezovic <marko.elezovic@oradian.com>
  2017-05-15 04:05 ` Re: Commenting a FK crashes ALTER TABLE statements David Rowley <david.rowley@2ndquadrant.com>
  2017-05-15 04:19   ` Re: Commenting a FK crashes ALTER TABLE statements Michael Paquier <michael.paquier@gmail.com>
@ 2017-05-15 14:37     ` Tom Lane <tgl@sss.pgh.pa.us>
  2017-05-16 01:07       ` Re: Commenting a FK crashes ALTER TABLE statements Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Tom Lane @ 2017-05-15 14:37 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; +Cc: David Rowley <david.rowley@2ndquadrant.com>; Marko Elezovic <marko.elezovic@oradian.com>; pgsql-bugs

Michael Paquier <michael.paquier@gmail.com> writes:
> On Mon, May 15, 2017 at 1:05 PM, David Rowley
> <david.rowley@2ndquadrant.com> wrote:
>> It seems there's just some missing pstrdup() calls in
>> RebuildConstraintComment().
>> 
>> The attached should fix it.

> I was just finishing to debug it :)
> It is surprising that we have not caught this earlier as comment
> re-creation handling in ALTER TABLE has been reworked some time ago
> already. This means that we did not stress this code enough. Attached
> is the previous fix, completed with a set of regression tests.

Yeah, there should at least be a test case to cover the known crash.

I'll take this one, unless some other committer is already on it.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



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

* Re: Commenting a FK crashes ALTER TABLE statements
  2017-05-14 15:43 Commenting a FK crashes ALTER TABLE statements Marko Elezovic <marko.elezovic@oradian.com>
  2017-05-15 04:05 ` Re: Commenting a FK crashes ALTER TABLE statements David Rowley <david.rowley@2ndquadrant.com>
  2017-05-15 04:19   ` Re: Commenting a FK crashes ALTER TABLE statements Michael Paquier <michael.paquier@gmail.com>
  2017-05-15 14:37     ` Re: Commenting a FK crashes ALTER TABLE statements Tom Lane <tgl@sss.pgh.pa.us>
@ 2017-05-16 01:07       ` Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Michael Paquier @ 2017-05-16 01:07 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: David Rowley <david.rowley@2ndquadrant.com>; Marko Elezovic <marko.elezovic@oradian.com>; pgsql-bugs

On Mon, May 15, 2017 at 11:37 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Michael Paquier <michael.paquier@gmail.com> writes:
>> On Mon, May 15, 2017 at 1:05 PM, David Rowley
>> <david.rowley@2ndquadrant.com> wrote:
>>> It seems there's just some missing pstrdup() calls in
>>> RebuildConstraintComment().
>>>
>>> The attached should fix it.
>
>> I was just finishing to debug it :)
>> It is surprising that we have not caught this earlier as comment
>> re-creation handling in ALTER TABLE has been reworked some time ago
>> already. This means that we did not stress this code enough. Attached
>> is the previous fix, completed with a set of regression tests.
>
> Yeah, there should at least be a test case to cover the known crash.
>
> I'll take this one, unless some other committer is already on it.

Thanks. (committed as 12590c5d)
-- 
Michael


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



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


end of thread, other threads:[~2017-05-16 01:07 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-05-14 15:43 Commenting a FK crashes ALTER TABLE statements Marko Elezovic <marko.elezovic@oradian.com>
2017-05-15 04:05 ` David Rowley <david.rowley@2ndquadrant.com>
2017-05-15 04:19   ` Michael Paquier <michael.paquier@gmail.com>
2017-05-15 14:37     ` Tom Lane <tgl@sss.pgh.pa.us>
2017-05-16 01:07       ` Michael Paquier <michael.paquier@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