Re: Chicken/egg problem with range types

From: Alban Hertroys <haramrae(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Scott Bailey <artacus72(at)gmail(dot)com>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Chicken/egg problem with range types
Date: 2012-07-18 17:48:32
Message-ID: 1EA42BB9-B372-4B41-A6DD-92D3FC507999@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 18 Jul 2012, at 16:15, Tom Lane wrote:

> Alban Hertroys <haramrae(at)gmail(dot)com> writes:
>> On 18 Jul 2012, at 5:08, Tom Lane wrote:
>>> I wonder whether we could improve this by postponing the no-shell-types
>>> check from creation to function runtime.
>
>> I don't suppose it'd be possible to treat it as a deferred constraint? Then the check would be moved to the end of the transaction.
>
> You mean, after we've already crashed, or allowed a security breach to
> happen? Doesn't sound very helpful. In any case, my concern is that
> there not be any added overhead, not about moving it around.

I did mean the transaction in which the dependency got created, not some later transaction in which it gets used. I'm fairly sure that'd be before any crashes or security breaches. Or is that not what you're implying?

Basically:
BEGIN;
CREATE TYPE dt_range AS (..., CANONICAL = dt_range_canonical); -- check for CANONICAL function deferred
CREATE FUNCTION dt_range_canonical(dt_range) ...; -- now it exists
COMMIT; -- here the check for the CANONICAL function is performed

I did make an assumption in there that the dependency doesn't get used before the creating transaction committed. Using the dependency in the same transaction that created it should probably not be allowed, or the check should be moved to function runtime if that happens (like in your original suggestion).

Another alternative, which would probably require a major effort to implement, would be to make CREATE FUNCTION inlinable (is that the right word?) by making the DDL statement return the function identifier of the function it just created.

With that, the dependency of the OP would be created somewhat like:

CREATE TYPE dt_range AS (..., CANONICAL = (CREATE FUNCTION dt_range_canonical(dt_range) ...));

Some languages use similar constructs, for example to assign methods to object prototypes in the case of Javascript. I know, SQL is not object oriented (but neither is Javascript, strictly speaking).

Alban Hertroys

--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bob Pawley 2012-07-18 18:07:34 Trouble with NEW
Previous Message Brian McNally 2012-07-18 17:42:21 Re: Problem running "ALTER TABLE...", ALTER TABLE waiting