Re: SQL:2011 application time
Paul A Jungwirth <pj@illuminatedcomputing.com>
Commits
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Rename gist stratnum support function
- 32edf732e8dc 18.0 landed
-
Remove support for temporal RESTRICT foreign keys
- b83e8a2ca2eb 18.0 landed
-
Cache NO ACTION foreign keys separately from RESTRICT foreign keys
- 9926f854d077 18.0 landed
-
Fix NO ACTION temporal foreign keys when the referenced endpoints change
- 1772d554b089 18.0 landed
-
Improve whitespace in without_overlaps test
- 888d4523f0c2 18.0 landed
-
Tests for logical replication with temporal keys
- 939b0908c87a 18.0 landed
-
Support for GiST in get_equal_strategy_number()
- 74edabce7a33 18.0 landed
-
Make the conditions in IsIndexUsableForReplicaIdentityFull() more explicit
- 13544e790ef8 18.0 landed
-
Replace get_equal_strategy_number_for_am() by get_equal_strategy_number()
- a2a475b011cf 18.0 landed
-
Improve internal logical replication error for missing equality strategy
- 321c287351f7 18.0 landed
-
Simplify IsIndexUsableForReplicaIdentityFull()
- 7727049e8f66 18.0 landed
-
Fix ALTER TABLE / REPLICA IDENTITY for temporal tables
- 79b575d3bc09 18.0 landed
-
doc: Update pg_constraint.conexclop docs for WITHOUT OVERLAPS
- f683ba0867da 18.0 landed
-
doc: Add PERIOD to ALTER TABLE reference docs
- d56af4c882e2 18.0 landed
-
doc: Add WITHOUT OVERLAPS to ALTER TABLE reference docs
- bf621059500b 18.0 landed
-
Add temporal FOREIGN KEY contraints
- 89f908a6d0ac 18.0 landed
- 34768ee36165 17.0 landed
-
Add temporal PRIMARY KEY and UNIQUE constraints
- fc0438b4e805 18.0 landed
- 46a0cd4cefb4 17.0 landed
-
Add stratnum GiST support function
- 7406ab623fee 18.0 landed
- 6db4598fcb82 17.0 landed
-
Avoid crashing when a JIT-inlined backend function throws an error.
- 5d6c64d29097 17.0 cited
-
Revert temporal primary keys and foreign keys
- 8aee330af55d 17.0 landed
-
Fix ON CONFLICT DO NOTHING/UPDATE for temporal indexes
- 144c2ce0cc75 17.0 landed
-
Add test for REPLICA IDENTITY with a temporal key
- 482e108cd38d 17.0 landed
-
Use half-open interval notation in without_overlaps tests
- 5577a71fb0cc 17.0 landed
-
Use daterange and YMD in without_overlaps tests instead of tsrange.
- a88c800deb6f 17.0 landed
-
Rename pg_constraint.conwithoutoverlaps to conperiod
- 030e10ff1a36 17.0 landed
-
Fix comment on gist_stratnum_btree
- 86232a49a437 17.0 landed
-
Add missing TAP test name
- 1ab763fc22ad 16.0 cited
-
Improve error handling of HMAC computations
- 5513dc6a304d 15.0 cited
-
Rename functions to avoid future conflicts
- ee419607381d 15.0 landed
Attachments
- v33-0001-Add-temporal-FOREIGN-KEYs.patch (text/x-patch) patch v33-0001
- v33-0002-Support-multiranges-in-temporal-FKs.patch (text/x-patch) patch v33-0002
- v33-0003-Add-support-funcs-for-FOR-PORTION-OF.patch (text/x-patch) patch v33-0003
- v33-0004-Add-UPDATE-DELETE-FOR-PORTION-OF.patch (text/x-patch) patch v33-0004
- v33-0005-Add-CASCADE-SET-NULL-SET-DEFAULT-for-temporal-fo.patch (text/x-patch) patch v33-0005
- v33-0006-Add-PERIODs.patch (text/x-patch) patch v33-0006
v33 attached with minor changes.
On 3/22/24 20:02, jian he wrote:
> On Fri, Mar 22, 2024 at 11:49 PM Peter Eisentraut <peter@eisentraut.org> wrote:
>> But after reading the comment in the function about collations, I think
>> there could be trouble. As long as we are only comparing for equality
>> (and we don't support nondeterministic global collations), then we can
>> use any collation to compare for equality. But if we are doing
>> contained-by, then the collation does matter, so we would need to get
>> the actual collation somehow. So as written, this might not always work
>> correctly.
>>
>> I think it would be safer for now if we just kept using the equality
>> operation even for temporal foreign keys. If we did that, then in the
>> case that you update a key to a new value that is contained by the old
>> value, this function would say "not equal" and fire all the checks, even
>> though it wouldn't need to. This is kind of similar to the "false
>> negatives" that the comment already talks about.
>>
>> What do you think?
>>
>
> we don't need to worry about primary key and foreign key with
> different collation.
> because it will be error out as incompatible data type,
> foreign key constraint will not be created.
I agree with jian he here. Here is my own investigation:
Rangetypes themselves are never collatable (see DefineRange in commands/typecmds.c).
But rangetypes do store a collation for their base type. So you can say:
paul=# create type textrange as range (subtype = text, collation = "C");
CREATE TYPE
That is stored in pg_range.rngcollation, but pg_type.typcollation is always zero.
So putting a collection on a rangetype column is an error:
paul=# create table t (r1 textrange collate "en-US-x-icu");
ERROR: collations are not supported by type textrange
And so is using an ad hoc collation with an operator:
paul=# select '[J,J]'::textrange <@ '[a,z]'::textrange collate "en-US-x-icu";
ERROR: collations are not supported by type textrange
LINE 1: select '[J,J]'::textrange <@ '[a,z]'::textrange collate "en-...
Almost everything ranges do is built on range_cmp_bounds, which uses the base type's collation.
There is no way to use a different one.
So when ri_CompareWithCast calls `lhs <@ rhs`, it is using the collation for that range's base type.
Indexes will use the same collation.
You also can't mix different range types.
Our textrange puts (English) lowercase after uppercase:
paul=# select '[j,j]'::textrange <@ '[a,z]'::textrange;
?column?
----------
t
(1 row)
paul=# select '[J,J]'::textrange <@ '[a,z]'::textrange;
?column?
----------
f
(1 row)
We could create a rangetype that intermingles uppercase & lower:
paul=# create type itextrange as range (subtype = text, collation = "en-US-x-icu");
CREATE TYPE
paul=# select '[J,J]'::itextrange <@ '[a,z]'::itextrange;
?column?
----------
t
(1 row)
But you can't mix them:
paul=# select '[J,J]'::itextrange <@ '[a,z]'::textrange;
ERROR: operator does not exist: itextrange <@ textrange
LINE 1: select '[J,J]'::itextrange <@ '[a,z]'::textrange;
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
Even if I create casts, mixing still fails:
paul=# create cast (textrange as itextrange) without function as implicit;
CREATE CAST
paul=# create cast (itextrange as textrange) without function as implicit;
CREATE CAST
paul=# select '[J,J]'::itextrange <@ '[a,z]'::textrange;
ERROR: operator does not exist: itextrange <@ textrange
LINE 1: select '[J,J]'::itextrange <@ '[a,z]'::textrange;
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
That's because the operator parameters are anyrange, and in can_coerce_type we call
check_generic_type_consistency which doesn't use casts.
It just asks if all the concrete range types are the same (as with other polymorphic types).
Adding a foreign key runs the same check:
paul=# create table pk (id int4range, valid_at textrange, constraint pkpk primary key (id, valid_at
without overlaps));
CREATE TABLE
paul=# create table fk (id int4range, valid_at itextrange, parent_id int4range);
CREATE TABLE
paul=# alter table fk add constraint fkfk foreign key (parent_id, period valid_at) references pk;
ERROR: foreign key constraint "fkfk" cannot be implemented
DETAIL: Key columns "valid_at" and "valid_at" are of incompatible types: itextrange and textrange.
I guess the user could define their own `textrange <@ itextrange` operator, using the lhs collation.
We would choose that operator for pfeqop but not ppeqop or ffeqop.
And we use ffeqop here, which would allow us to skip a check that pfeqop would fail.
Is that an issue? It feels like the user is doing their best to get nonsense results at that point,
and it's not really about the collation per se.
Incidentally here is another separate issue with foreign keys and collations I noticed this morning:
https://www.postgresql.org/message-id/78d824e0-b21e-480d-a252-e4b84bc2c24b%40illuminatedcomputing.com
That comes from nondeterministic collations, which feel like a troublesome thing here.
Probably foreign keys just weren't fully re-thought when we added them.
But we avoid the issue from 59a85cb4 (discussion at
https://www.postgresql.org/message-id/flat/3326fc2e-bc02-d4c5-e3e5-e54da466e89a@2ndquadrant.com)
about cascading changes when a PK experiences a not-binary-identical change that the collation
considers equal. These days we only call ri_CompareWithCast for changes on the FK side.
Now this is a long chain of reasoning to say rangetypes are safe. I added a comment. Note it doesn't
apply to arbitrary types, so if we support those eventually we should just require a recheck always,
or alternately use equals, not containedby. (That would require storing equals somewhere. It could
go in ffeqop, but that feels like a footgun since pfeqop and ppeqop need overlaps.)
On 3/21/24 22:33, jian he wrote:
> i think on update restrict, on delete restrict cannot be deferred,
> even if you set it DEFERRABLE INITIALLY DEFERRED.
> based on this idea, I made minor change on
> v32-0002-Support-multiranges-in-temporal-FKs.patch
Okay, added those tests too. Thanks!
Rebased to 697f8d266c.
Yours,
--
Paul ~{:-)
pj@illuminatedcomputing.com