Обсуждение: Foreign key type checking patch

Поиск
Список
Период
Сортировка

Foreign key type checking patch

От
Fabien COELHO
Дата:
Hello again patchers,

Here is a proposed patch against 7.4.1 to check exact match
of foreign key types wrt the referenced keys, and to show
a warning if this is not the case.

This is an attempt to prevent stupid bugs such as :

  CREATE TABLE foo(id INT4 NOT NULL PRIMARY KEY);
  CREATE TABLE bla(id INT2 REFERENCES foo);

which may work at the beginning, and then fails later on.

I'm not at ease with postgresql internals, however this
implementation seems reasonnable to me, and in the spirit
of how the surrounding code works.

I could not find any simple way to tell the user about
what is being processed, as there is not real context information
and tell 'while processing this constraint'... However this
situation seems to be the normal case with any postgresql
messages, as far as I can tell from my use.

Have a nice day,

--
Fabien Coelho - coelho@cri.ensmp.fr

Вложения

Re: Foreign key type checking patch

От
Tom Lane
Дата:
Fabien COELHO <coelho@cri.ensmp.fr> writes:
> Here is a proposed patch against 7.4.1 to check exact match
> of foreign key types wrt the referenced keys, and to show
> a warning if this is not the case.

I think that this concern may be obsolete in CVS tip, at least for the
cases where we have indexable cross-type operators.  The correct way
to do this would be to look at the operator found by oper() and see
whether it's indexable.

            regards, tom lane

Re: Foreign key type checking patch

От
Fabien COELHO
Дата:
Dear Tom,

Thanks for your reply.

> > Here is a proposed patch against 7.4.1 to check exact match
> > of foreign key types wrt the referenced keys, and to show
> > a warning if this is not the case.
>
> I think that this concern may be obsolete in CVS tip,

I just get the current CVS and had a quick look at it.

> at least for the cases where we have indexable cross-type operators.
> The correct way to do this would be to look at the operator found by
> oper() and see whether it's indexable.

I must admit that I do not understand your point.

I wish I would have a WARNING if a foreign key is not declared exactly as
the key it references. I think that it is a desirable feature for stupid
users, including myself!

I cannot see why whether the "=" comparison version which is chosen is
indexable or not would lead to this information. It seems quite
reasonnable to look directly at the attribute types and compare them for
this purpose.

I noticed the compatible_oper() function which would return a no-coersion
binary operator between types. However that does not fit my purpose. For
instance, it seems to me that the IsBinaryCoercible returns true for
VARCHAR(12) and VARCHAR(16), as the type oid is the same, but I think a
warning makes sense anyway. So it is not the same issue.

So I can't see your point. Maybe some more lights would help?

--
Fabien.

Re: Foreign key type checking patch

От
Tom Lane
Дата:
Fabien COELHO <coelho@cri.ensmp.fr> writes:
> I must admit that I do not understand your point.

> I wish I would have a WARNING if a foreign key is not declared exactly as
> the key it references.

The reason why people want such a warning is that the RI checks tend to
be slow if it's not the case.  Accordingly, the warning should only
appear if the check is actually going to be slow.

You sound like you think it's a bug that Postgres supports cross-type FK
references at all.  I disagree.  It's a feature, albeit one whose
implementation could stand improvement.  The warning ought to come out
in cases where people are going to be exposed to the implementation
weaknesses.

            regards, tom lane

Re: Foreign key type checking patch

От
Stephan Szabo
Дата:
On Mon, 1 Mar 2004, Fabien COELHO wrote:

> > > Here is a proposed patch against 7.4.1 to check exact match
> > > of foreign key types wrt the referenced keys, and to show
> > > a warning if this is not the case.
> >
> > I think that this concern may be obsolete in CVS tip,
>
> I just get the current CVS and had a quick look at it.
>
> > at least for the cases where we have indexable cross-type operators.
> > The correct way to do this would be to look at the operator found by
> > oper() and see whether it's indexable.
>
> I must admit that I do not understand your point.
>
> I wish I would have a WARNING if a foreign key is not declared exactly as
> the key it references. I think that it is a desirable feature for stupid
> users, including myself!
>
> I cannot see why whether the "=" comparison version which is chosen is
> indexable or not would lead to this information. It seems quite
> reasonnable to look directly at the attribute types and compare them for
> this purpose.
>
> I noticed the compatible_oper() function which would return a no-coersion
> binary operator between types. However that does not fit my purpose. For
> instance, it seems to me that the IsBinaryCoercible returns true for
> VARCHAR(12) and VARCHAR(16), as the type oid is the same, but I think a
> warning makes sense anyway. So it is not the same issue.
>
> So I can't see your point. Maybe some more lights would help?

Currently the cross-type indexing issues cause us problems in performance
and so warning was considered reasonable so that it would warn people
about that problem.  Tom's suggestion has effectively the same effect,
warn when there might be an issue on the performance grounds.

I'm really not sure that it makes sense to warn for the fk cases where the
semantics should be correct (if they're not we need to fix it or make it
an error) but in which an error might have been made by the user because
the types are different given that it at least seems reasonable to me that
the fk type is allowable to be a subset of the referenced type.  I don't
think simply different types is sufficient to be warning material.

Re: Foreign key type checking patch

От
Tom Lane
Дата:
Stephan Szabo <sszabo@megazone.bigpanda.com> writes:
> I'm really not sure that it makes sense to warn for the fk cases where the
> semantics should be correct (if they're not we need to fix it or make it
> an error) but in which an error might have been made by the user because
> the types are different given that it at least seems reasonable to me that
> the fk type is allowable to be a subset of the referenced type.  I don't
> think simply different types is sufficient to be warning material.

I can think of several cases where it might be reasonable for the types
to be different.  One case in particular that needs some thought is
where the FK and referenced PK are domains on a common base type.

            regards, tom lane

Re: Foreign key type checking patch

От
Fabien COELHO
Дата:
> > I wish I would have a WARNING if a foreign key is not declared exactly as
> > the key it references.
>
> The reason why people want such a warning is that the RI checks tend to
> be slow if it's not the case.  Accordingly, the warning should only
> appear if the check is actually going to be slow.

Ok. I understand this, and this is another issue.

> You sound like you think it's a bug that Postgres supports cross-type FK
> references at all.

I'm sorry I sound like that, because this is not what I really mean.
Maybe my poor English could stand some improvement so as to be able
to convey my thinking clearly to others.

> I disagree.

I totally agree with you.

This is not my point. I do not want to "disallow" cross-type foreign keys.

My point is just to *** WARN *** the stupid user (me) about potential
would-be bugs if the type does not match. A warning is not an error. It
just means "hey man, did you notice that? maybe that is not what you
meant, maybe it is what you meant...". Well, this is my idea of a warning.

Consider this example :

CREATE TABLE foo(fid VARCHAR(2) NOT NULL PRIMARY KEY);
CREATE TABLE bla(fid VARCHAR(4) REFERENCES foo);

Although the "fid" attribute is declared VARCHAR(4) in bla, you will
never be able to put more that a VARCHAR(2) value inside as it must
reference the foo table.

Now consider this other example :

CREATE TABLE foo(fid VARCHAR(2) NOT NULL PRIMARY KEY);
CREATE TABLE bla(fid VARCHAR(1) REFERENCES foo);

Some key of foo will never be able to be referenced by bla.
Maybe it is the intent of the user, maybe not.

Anyway, IMVHO, both these cases deserve a warning, even of there is no
actual cpu cost. Hence my patch proposal.

> It's a feature, albeit one whose implementation could stand improvement.
> The warning ought to come out in cases where people are going to be
> exposed to the implementation weaknesses.

I agree with the feature, I just wish there would be a warning.

The "implementation weaknesses" is another issue that I do understand
but that I did not try to address in the submitted patch.

--
Fabien.

Re: Foreign key type checking patch

От
Fabien COELHO
Дата:
> I can think of several cases where it might be reasonable for the types
> to be different.

Sure. It's all about a warning, not about an error.


--
Fabien Coelho - coelho@cri.ensmp.fr