Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit12590c5

Browse files
committed
Fix unsafe reference into relcache in constructed CommentStmt.
The CommentStmt made by RebuildConstraintComment() has to pstrdup therelation name, else it will contain a dangling pointer after thatrelcache entry is flushed. (I'm less sure that pstrdup'ing connameis necessary, but let's be safe.) Failure to do this leads to weirderrors or crashes, as reported by Marko Elezovic.Bug introduced by commite42375f, so back-patch to 9.5 as that was.Fix by David Rowley, regression test by Michael PaquierDiscussion:https://postgr.es/m/DB6PR03MB30775D58E732D4EB0C13725B9AE00@DB6PR03MB3077.eurprd03.prod.outlook.com
1 parentf8dc198 commit12590c5

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

‎src/backend/commands/tablecmds.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9764,8 +9764,8 @@ RebuildConstraintComment(AlteredTableInfo *tab, int pass, Oid objid,
97649764
cmd=makeNode(CommentStmt);
97659765
cmd->objtype=OBJECT_TABCONSTRAINT;
97669766
cmd->object= (Node*)list_make3(makeString(get_namespace_name(RelationGetNamespace(rel))),
9767-
makeString(RelationGetRelationName(rel)),
9768-
makeString(conname));
9767+
makeString(pstrdup(RelationGetRelationName(rel))),
9768+
makeString(pstrdup(conname)));
97699769
cmd->comment=comment_str;
97709770

97719771
/* Append it to list of commands */

‎src/test/regress/expected/alter_table.out

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2788,6 +2788,39 @@ SELECT conname as constraint, obj_description(oid, 'pg_constraint') as comment F
27882788
comment_test_positive_col_check | CHECK constraint on comment_test.positive_col
27892789
(2 rows)
27902790

2791+
-- Check compatibility for foreign keys and comments. This is done
2792+
-- separately as rebuilding the column type of the parent leads
2793+
-- to an error and would reduce the test scope.
2794+
CREATE TABLE comment_test_child (
2795+
id text CONSTRAINT comment_test_child_fk REFERENCES comment_test);
2796+
CREATE INDEX comment_test_child_fk ON comment_test_child(id);
2797+
COMMENT ON COLUMN comment_test_child.id IS 'Column ''id'' on comment_test_child';
2798+
COMMENT ON INDEX comment_test_child_fk IS 'Index backing the FOREIGN KEY of comment_test_child';
2799+
COMMENT ON CONSTRAINT comment_test_child_fk ON comment_test_child IS 'FOREIGN KEY constraint of comment_test_child';
2800+
-- Change column type of parent
2801+
ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE text;
2802+
ALTER TABLE comment_test ALTER COLUMN id SET DATA TYPE int USING id::integer;
2803+
ERROR: foreign key constraint "comment_test_child_fk" cannot be implemented
2804+
DETAIL: Key columns "id" and "id" are of incompatible types: text and integer.
2805+
-- Comments should be intact
2806+
SELECT col_description('comment_test_child'::regclass, 1) as comment;
2807+
comment
2808+
-----------------------------------
2809+
Column 'id' on comment_test_child
2810+
(1 row)
2811+
2812+
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;
2813+
index | comment
2814+
-----------------------+-----------------------------------------------------
2815+
comment_test_child_fk | Index backing the FOREIGN KEY of comment_test_child
2816+
(1 row)
2817+
2818+
SELECT conname as constraint, obj_description(oid, 'pg_constraint') as comment FROM pg_constraint where conrelid = 'comment_test_child'::regclass ORDER BY 1, 2;
2819+
constraint | comment
2820+
-----------------------+----------------------------------------------
2821+
comment_test_child_fk | FOREIGN KEY constraint of comment_test_child
2822+
(1 row)
2823+
27912824
-- Check that we map relation oids to filenodes and back correctly. Only
27922825
-- display bad mappings so the test output doesn't change all the time. A
27932826
-- filenode function call can return NULL for a relation dropped concurrently

‎src/test/regress/sql/alter_table.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,6 +1786,24 @@ SELECT col_description('comment_test'::regclass, 1) as comment;
17861786
SELECT indexrelid::regclass::textas index, obj_description(indexrelid,'pg_class')as commentFROM pg_indexwhere indrelid='comment_test'::regclassORDER BY1,2;
17871787
SELECT connameasconstraint, obj_description(oid,'pg_constraint')as commentFROM pg_constraintwhere conrelid='comment_test'::regclassORDER BY1,2;
17881788

1789+
-- Check compatibility for foreign keys and comments. This is done
1790+
-- separately as rebuilding the column type of the parent leads
1791+
-- to an error and would reduce the test scope.
1792+
CREATETABLEcomment_test_child (
1793+
idtextCONSTRAINT comment_test_child_fkREFERENCES comment_test);
1794+
CREATEINDEXcomment_test_child_fkON comment_test_child(id);
1795+
COMMENT ON COLUMN comment_test_child.id IS'Column''id'' on comment_test_child';
1796+
COMMENT ON INDEX comment_test_child_fk IS'Index backing the FOREIGN KEY of comment_test_child';
1797+
COMMENT ON CONSTRAINT comment_test_child_fk ON comment_test_child IS'FOREIGN KEY constraint of comment_test_child';
1798+
1799+
-- Change column type of parent
1800+
ALTERTABLE comment_test ALTER COLUMN idSET DATA TYPEtext;
1801+
ALTERTABLE comment_test ALTER COLUMN idSET DATA TYPEint USING id::integer;
1802+
1803+
-- Comments should be intact
1804+
SELECT col_description('comment_test_child'::regclass,1)as comment;
1805+
SELECT indexrelid::regclass::textas index, obj_description(indexrelid,'pg_class')as commentFROM pg_indexwhere indrelid='comment_test_child'::regclassORDER BY1,2;
1806+
SELECT connameasconstraint, obj_description(oid,'pg_constraint')as commentFROM pg_constraintwhere conrelid='comment_test_child'::regclassORDER BY1,2;
17891807

17901808
-- Check that we map relation oids to filenodes and back correctly. Only
17911809
-- display bad mappings so the test output doesn't change all the time. A

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp