forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit9321d2f
committed
Fix collation handling for foreign keys
Allowing foreign keys where the referenced and the referencing columnshave collations with different notions of equality is problematic.This can only happen when using nondeterministic collations, forexample, if the referencing column is case-insensitive and thereferenced column is not, or vice versa. It does not happen if bothcollations are deterministic.To show one example: CREATE COLLATION case_insensitive (provider = icu, deterministic = false, locale = 'und-u-ks-level2'); CREATE TABLE pktable (x text COLLATE "C" PRIMARY KEY); CREATE TABLE fktable (x text COLLATE case_insensitive REFERENCES pktable ON UPDATE CASCADE ON DELETE CASCADE); INSERT INTO pktable VALUES ('A'), ('a'); INSERT INTO fktable VALUES ('A'); BEGIN; DELETE FROM pktable WHERE x = 'a'; TABLE fktable; ROLLBACK; BEGIN; DELETE FROM pktable WHERE x = 'A'; TABLE fktable; ROLLBACK;Both of these DELETE statements delete the one row from fktable. Sothis means that one row from fktable references two rows in pktable,which should not happen. (That's why a primary key or uniqueconstraint is required on pktable.)When nondeterministic collations were implemented, the SQL standardavailable to yours truly said that referential integrity checks shouldbe performed with the collation of the referenced column, and sothat's how we implemented it. But this turned out to be a mistake inthe SQL standard, for the same reasons as above, that was later(SQL:2016) fixed to require both collations to be the same. So that'swhat we are aiming for here.We don't have to be quite so strict. We can allow differentcollations if they are both deterministic. This is also good forbackward compatibility.So the new rule is that the collations either have to be the same orboth deterministic. Or in other words, if one of them isnondeterministic, then both have to be the same.Users upgrading from before that have affected setups will need tomake changes to their schemas (i.e., change one or both collations inaffected foreign-key relationships) before the upgrade will succeed.Some of the nice test cases for the previous situation incollate.icu.utf8.sql are now obsolete. They are changed to just checkthe error checking of the new rule. Note that collate.sql alreadycontained a test for foreign keys with different deterministiccollations.A bunch of code in ri_triggers.c that added a COLLATE clause toenforce the referenced column's collation can be removed, because bothcolumns now have to have the same notion of equality, so it doesn'tmatter which one to use.Reported-by: Paul Jungwirth <pj@illuminatedcomputing.com>Reviewed-by: Jian He <jian.universality@gmail.com>Discussion:https://www.postgresql.org/message-id/flat/78d824e0-b21e-480d-a252-e4b84bc2c24b@illuminatedcomputing.com1 parent90bcc7c commit9321d2f
File tree
5 files changed
+105
-178
lines changed- doc/src/sgml/ref
- src
- backend
- commands
- utils/adt
- test/regress
- expected
- sql
5 files changed
+105
-178
lines changedLines changed: 7 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1205 | 1205 |
| |
1206 | 1206 |
| |
1207 | 1207 |
| |
| 1208 | + | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
1208 | 1215 |
| |
1209 | 1216 |
| |
1210 | 1217 |
| |
|
Lines changed: 68 additions & 16 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
398 | 398 |
| |
399 | 399 |
| |
400 | 400 |
| |
401 |
| - | |
| 401 | + | |
402 | 402 |
| |
403 | 403 |
| |
404 |
| - | |
| 404 | + | |
405 | 405 |
| |
406 | 406 |
| |
407 | 407 |
| |
| |||
9705 | 9705 |
| |
9706 | 9706 |
| |
9707 | 9707 |
| |
| 9708 | + | |
| 9709 | + | |
9708 | 9710 |
| |
9709 | 9711 |
| |
9710 | 9712 |
| |
| |||
9801 | 9803 |
| |
9802 | 9804 |
| |
9803 | 9805 |
| |
9804 |
| - | |
| 9806 | + | |
9805 | 9807 |
| |
9806 | 9808 |
| |
9807 | 9809 |
| |
9808 |
| - | |
| 9810 | + | |
9809 | 9811 |
| |
9810 | 9812 |
| |
9811 | 9813 |
| |
| |||
9814 | 9816 |
| |
9815 | 9817 |
| |
9816 | 9818 |
| |
9817 |
| - | |
| 9819 | + | |
9818 | 9820 |
| |
9819 | 9821 |
| |
9820 | 9822 |
| |
| |||
9823 | 9825 |
| |
9824 | 9826 |
| |
9825 | 9827 |
| |
9826 |
| - | |
| 9828 | + | |
| 9829 | + | |
9827 | 9830 |
| |
9828 | 9831 |
| |
9829 | 9832 |
| |
9830 | 9833 |
| |
9831 | 9834 |
| |
9832 |
| - | |
| 9835 | + | |
9833 | 9836 |
| |
9834 | 9837 |
| |
9835 | 9838 |
| |
| |||
9842 | 9845 |
| |
9843 | 9846 |
| |
9844 | 9847 |
| |
9845 |
| - | |
| 9848 | + | |
9846 | 9849 |
| |
9847 | 9850 |
| |
9848 | 9851 |
| |
| |||
9944 | 9947 |
| |
9945 | 9948 |
| |
9946 | 9949 |
| |
| 9950 | + | |
| 9951 | + | |
9947 | 9952 |
| |
9948 | 9953 |
| |
9949 | 9954 |
| |
| |||
10086 | 10091 |
| |
10087 | 10092 |
| |
10088 | 10093 |
| |
| 10094 | + | |
| 10095 | + | |
| 10096 | + | |
| 10097 | + | |
| 10098 | + | |
| 10099 | + | |
| 10100 | + | |
| 10101 | + | |
| 10102 | + | |
| 10103 | + | |
| 10104 | + | |
| 10105 | + | |
| 10106 | + | |
| 10107 | + | |
| 10108 | + | |
| 10109 | + | |
| 10110 | + | |
| 10111 | + | |
| 10112 | + | |
| 10113 | + | |
| 10114 | + | |
| 10115 | + | |
| 10116 | + | |
| 10117 | + | |
| 10118 | + | |
| 10119 | + | |
| 10120 | + | |
| 10121 | + | |
| 10122 | + | |
| 10123 | + | |
| 10124 | + | |
| 10125 | + | |
| 10126 | + | |
| 10127 | + | |
| 10128 | + | |
10089 | 10129 |
| |
10090 | 10130 |
| |
10091 | 10131 |
| |
| |||
10106 | 10146 |
| |
10107 | 10147 |
| |
10108 | 10148 |
| |
| 10149 | + | |
| 10150 | + | |
10109 | 10151 |
| |
10110 | 10152 |
| |
10111 | 10153 |
| |
| |||
10121 | 10163 |
| |
10122 | 10164 |
| |
10123 | 10165 |
| |
| 10166 | + | |
| 10167 | + | |
| 10168 | + | |
10124 | 10169 |
| |
10125 | 10170 |
| |
10126 | 10171 |
| |
| |||
10144 | 10189 |
| |
10145 | 10190 |
| |
10146 | 10191 |
| |
10147 |
| - | |
10148 |
| - | |
10149 |
| - | |
| 10192 | + | |
| 10193 | + | |
| 10194 | + | |
| 10195 | + | |
10150 | 10196 |
| |
10151 | 10197 |
| |
10152 | 10198 |
| |
| |||
10156 | 10202 |
| |
10157 | 10203 |
| |
10158 | 10204 |
| |
10159 |
| - | |
| 10205 | + | |
| 10206 | + | |
| 10207 | + | |
10160 | 10208 |
| |
10161 | 10209 |
| |
10162 | 10210 |
| |
| |||
12092 | 12140 |
| |
12093 | 12141 |
| |
12094 | 12142 |
| |
12095 |
| - | |
| 12143 | + | |
| 12144 | + | |
12096 | 12145 |
| |
12097 | 12146 |
| |
12098 | 12147 |
| |
| |||
12101 | 12150 |
| |
12102 | 12151 |
| |
12103 | 12152 |
| |
12104 |
| - | |
| 12153 | + | |
12105 | 12154 |
| |
12106 | 12155 |
| |
12107 | 12156 |
| |
| |||
12132 | 12181 |
| |
12133 | 12182 |
| |
12134 | 12183 |
| |
| 12184 | + | |
| 12185 | + | |
12135 | 12186 |
| |
12136 | 12187 |
| |
12137 | 12188 |
| |
| |||
12142 | 12193 |
| |
12143 | 12194 |
| |
12144 | 12195 |
| |
12145 |
| - | |
| 12196 | + | |
12146 | 12197 |
| |
12147 | 12198 |
| |
12148 | 12199 |
| |
| |||
12155 | 12206 |
| |
12156 | 12207 |
| |
12157 | 12208 |
| |
12158 |
| - | |
| 12209 | + | |
12159 | 12210 |
| |
12160 | 12211 |
| |
12161 | 12212 |
| |
| |||
12229 | 12280 |
| |
12230 | 12281 |
| |
12231 | 12282 |
| |
| 12283 | + | |
12232 | 12284 |
| |
12233 | 12285 |
| |
12234 | 12286 |
| |
|
Lines changed: 19 additions & 38 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
207 | 207 |
| |
208 | 208 |
| |
209 | 209 |
| |
210 |
| - | |
| 210 | + | |
211 | 211 |
| |
212 | 212 |
| |
213 | 213 |
| |
| |||
776 | 776 |
| |
777 | 777 |
| |
778 | 778 |
| |
779 |
| - | |
780 |
| - | |
781 | 779 |
| |
782 | 780 |
| |
783 | 781 |
| |
| |||
786 | 784 |
| |
787 | 785 |
| |
788 | 786 |
| |
789 |
| - | |
790 |
| - | |
791 | 787 |
| |
792 | 788 |
| |
793 | 789 |
| |
| |||
881 | 877 |
| |
882 | 878 |
| |
883 | 879 |
| |
884 |
| - | |
885 |
| - | |
886 | 880 |
| |
887 | 881 |
| |
888 | 882 |
| |
| |||
891 | 885 |
| |
892 | 886 |
| |
893 | 887 |
| |
894 |
| - | |
895 |
| - | |
896 | 888 |
| |
897 | 889 |
| |
898 | 890 |
| |
| |||
996 | 988 |
| |
997 | 989 |
| |
998 | 990 |
| |
999 |
| - | |
1000 |
| - | |
1001 | 991 |
| |
1002 | 992 |
| |
1003 | 993 |
| |
| |||
1009 | 999 |
| |
1010 | 1000 |
| |
1011 | 1001 |
| |
1012 |
| - | |
1013 |
| - | |
1014 | 1002 |
| |
1015 | 1003 |
| |
1016 | 1004 |
| |
| |||
1232 | 1220 |
| |
1233 | 1221 |
| |
1234 | 1222 |
| |
1235 |
| - | |
1236 |
| - | |
1237 | 1223 |
| |
1238 | 1224 |
| |
1239 | 1225 |
| |
| |||
1243 | 1229 |
| |
1244 | 1230 |
| |
1245 | 1231 |
| |
1246 |
| - | |
1247 |
| - | |
1248 | 1232 |
| |
1249 | 1233 |
| |
1250 | 1234 |
| |
| |||
1998 | 1982 |
| |
1999 | 1983 |
| |
2000 | 1984 |
| |
2001 |
| - | |
2002 |
| - | |
2003 |
| - | |
2004 |
| - | |
2005 |
| - | |
2006 |
| - | |
2007 |
| - | |
2008 |
| - | |
2009 |
| - | |
2010 |
| - | |
2011 |
| - | |
2012 |
| - | |
2013 |
| - | |
| 1985 | + | |
| 1986 | + | |
| 1987 | + | |
| 1988 | + | |
| 1989 | + | |
| 1990 | + | |
| 1991 | + | |
| 1992 | + | |
| 1993 | + | |
| 1994 | + | |
| 1995 | + | |
2014 | 1996 |
| |
2015 | 1997 |
| |
2016 | 1998 |
| |
| |||
2952 | 2934 |
| |
2953 | 2935 |
| |
2954 | 2936 |
| |
2955 |
| - | |
| 2937 | + | |
2956 | 2938 |
| |
2957 | 2939 |
| |
2958 | 2940 |
| |
| |||
2968 | 2950 |
| |
2969 | 2951 |
| |
2970 | 2952 |
| |
| 2953 | + | |
2971 | 2954 |
| |
2972 | 2955 |
| |
2973 | 2956 |
| |
2974 | 2957 |
| |
2975 |
| - | |
| 2958 | + | |
2976 | 2959 |
| |
2977 | 2960 |
| |
2978 | 2961 |
| |
| |||
2998 | 2981 |
| |
2999 | 2982 |
| |
3000 | 2983 |
| |
3001 |
| - | |
3002 |
| - | |
3003 |
| - | |
| 2984 | + | |
| 2985 | + | |
| 2986 | + | |
3004 | 2987 |
| |
3005 | 2988 |
| |
3006 | 2989 |
| |
3007 | 2990 |
| |
3008 | 2991 |
| |
3009 | 2992 |
| |
3010 | 2993 |
| |
3011 |
| - | |
3012 |
| - | |
3013 |
| - | |
| 2994 | + | |
3014 | 2995 |
| |
3015 | 2996 |
| |
3016 | 2997 |
| |
|
0 commit comments
Comments
(0)