Re: Single Row Table?

Lists: pgsql-general
From: "Yateen Joshi" <yjoshi(at)starentnetworks(dot)com>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Single Row Table?
Date: 2004-08-27 08:02:07
Message-ID: FF851C7EEB75954F9BCFB5CA117AB1EC0104F251@delta.nulinkinc.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hi,

I have got a table which is supposed to contain only one row. It does
not have any primary keys defined.
So, essentially, when a new insert happens in that table, I would like
it (the insert) to fail if there is already a row existing in that
table.
How can I do that? Can I add any constraints? Or do I need to write a
separate trigger for the same?

Thanks and regards,

Yateen V. Joshi


From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Yateen Joshi <yjoshi(at)starentnetworks(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Single Row Table?
Date: 2004-08-29 21:30:44
Message-ID: 20040829213044.GA15047@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Fri, Aug 27, 2004 at 13:32:07 +0530,
Yateen Joshi <yjoshi(at)starentnetworks(dot)com> wrote:
> Hi,
>
> I have got a table which is supposed to contain only one row. It does
> not have any primary keys defined.
> So, essentially, when a new insert happens in that table, I would like
> it (the insert) to fail if there is already a row existing in that
> table.
> How can I do that? Can I add any constraints? Or do I need to write a
> separate trigger for the same?

A simple way to force this is to add a primary key and a constraint
that forces the primary key to be a particular value.


From: "Scott Marlowe" <smarlowe(at)qwest(dot)net>
To: "Bruno Wolff III" <bruno(at)wolff(dot)to>
Cc: "Yateen Joshi" <yjoshi(at)starentnetworks(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Single Row Table?
Date: 2004-08-29 21:38:45
Message-ID: 1093815524.5493.31.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Sun, 2004-08-29 at 15:30, Bruno Wolff III wrote:
> On Fri, Aug 27, 2004 at 13:32:07 +0530,
> Yateen Joshi <yjoshi(at)starentnetworks(dot)com> wrote:
> > Hi,
> >
> > I have got a table which is supposed to contain only one row. It does
> > not have any primary keys defined.
> > So, essentially, when a new insert happens in that table, I would like
> > it (the insert) to fail if there is already a row existing in that
> > table.
> > How can I do that? Can I add any constraints? Or do I need to write a
> > separate trigger for the same?
>
> A simple way to force this is to add a primary key and a constraint
> that forces the primary key to be a particular value.

Is it reasonable / possible to add a check constraint something like
select count(*) from table <=1?


From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Scott Marlowe <smarlowe(at)qwest(dot)net>
Cc: Yateen Joshi <yjoshi(at)starentnetworks(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Single Row Table?
Date: 2004-08-30 13:20:19
Message-ID: 20040830132019.GA28435@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Sun, Aug 29, 2004 at 15:38:45 -0600,
Scott Marlowe <smarlowe(at)qwest(dot)net> wrote:
> On Sun, 2004-08-29 at 15:30, Bruno Wolff III wrote:
> > On Fri, Aug 27, 2004 at 13:32:07 +0530,
> > Yateen Joshi <yjoshi(at)starentnetworks(dot)com> wrote:
> > > Hi,
> > >
> > > I have got a table which is supposed to contain only one row. It does
> > > not have any primary keys defined.
> > > So, essentially, when a new insert happens in that table, I would like
> > > it (the insert) to fail if there is already a row existing in that
> > > table.
> > > How can I do that? Can I add any constraints? Or do I need to write a
> > > separate trigger for the same?
> >
> > A simple way to force this is to add a primary key and a constraint
> > that forces the primary key to be a particular value.
>
> Is it reasonable / possible to add a check constraint something like
> select count(*) from table <=1?

It should be possible to do that with an after trigger, but it is a bit
more work to set up. As long as the table gets vacuumed, either way
is probably reasonably fast.


From: Tim Penhey <tim(at)penhey(dot)net>
To: Yateen Joshi <yjoshi(at)starentnetworks(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Single Row Table?
Date: 2004-08-30 21:12:12
Message-ID: 4133982C.7090709@penhey.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Yateen Joshi wrote:

> Hi,
>
>
>
> I have got a table which is supposed to contain only one row. It does
> not have any primary keys defined.
>
> So, essentially, when a new insert happens in that table, I would like
> it (the insert) to fail if there is already a row existing in that table.
>
> How can I do that? Can I add any constraints? Or do I need to write a
> separate trigger for the same?
>
>
>
> Thanks and regards,
>
>
>
> Yateen V. Joshi
>
>
>
You could try:

id INT PRIMARY KEY NOT NULL DEFAULT(1) CHECK (id = 1),

Tim


From: Jeff Eckermann <jeff_eckermann(at)yahoo(dot)com>
To: Scott Marlowe <smarlowe(at)qwest(dot)net>, Bruno Wolff III <bruno(at)wolff(dot)to>
Cc: Yateen Joshi <yjoshi(at)starentnetworks(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Single Row Table?
Date: 2004-08-31 15:32:30
Message-ID: 20040831153230.50523.qmail@web20824.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


--- Scott Marlowe <smarlowe(at)qwest(dot)net> wrote:

> On Sun, 2004-08-29 at 15:30, Bruno Wolff III wrote:
> > On Fri, Aug 27, 2004 at 13:32:07 +0530,
> > Yateen Joshi <yjoshi(at)starentnetworks(dot)com> wrote:
> > > Hi,
> > >
> > > I have got a table which is supposed to contain
> only one row. It does
> > > not have any primary keys defined.
> > > So, essentially, when a new insert happens in
> that table, I would like
> > > it (the insert) to fail if there is already a
> row existing in that
> > > table.
> > > How can I do that? Can I add any constraints? Or
> do I need to write a
> > > separate trigger for the same?
> >
> > A simple way to force this is to add a primary key
> and a constraint
> > that forces the primary key to be a particular
> value.
>
> Is it reasonable / possible to add a check
> constraint something like
> select count(*) from table <=1?

ISTM most natural to do this with a rule, e.g.:

CREATE RULE my_insert_rule AS ON INSERT TO my_table DO
INSTEAD NOTHING;

Which will cause all inserts to be silently dropped.
If you want to return a message to the application,
you could use a statement trigger, which I believe we
don't have yet, or you could use a rule like:

CREATE RULE my_insert_rule AS ON INSERT TO my_table DO
INSTEAD SELECT 'Inserts to my_table not allowed!';

Although the application may not be expecting a return
message, and might not handle it.

>
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 9: the planner will ignore your desire to choose
> an index scan if your
> joining column's datatypes do not match
>


_______________________________
Do you Yahoo!?
Win 1 of 4,000 free domain names from Yahoo! Enter now.
http://promotions.yahoo.com/goldrush


From: Mike Nolan <nolan(at)gw(dot)tssi(dot)com>
To: jeff_eckermann(at)yahoo(dot)com (Jeff Eckermann)
Cc: smarlowe(at)qwest(dot)net (Scott Marlowe), bruno(at)wolff(dot)to (Bruno Wolff III), yjoshi(at)starentnetworks(dot)com (Yateen Joshi), pgsql-general(at)postgresql(dot)org
Subject: Re: Single Row Table?
Date: 2004-08-31 15:47:46
Message-ID: 200408311547.i7VFlkxK012921@gw.tssi.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

> ISTM most natural to do this with a rule, e.g.:
>
> CREATE RULE my_insert_rule AS ON INSERT TO my_table DO
> INSTEAD NOTHING;
>
> Which will cause all inserts to be silently dropped.

This strikes me as bad programming practice. Errors should be reported,
not silently ignored. If the application is doing an insert when it doesn't
need to, then the application is flawed as well.
--
Mike Nolan


From: Thomas Kellerer <spam_eater(at)gmx(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Single Row Table?
Date: 2004-08-31 19:12:47
Message-ID: ch2ijf$iem$1@sea.gmane.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Tim Penhey wrote on 30.08.2004 23:12:

>> I have got a table which is supposed to contain only one row. It does
>> not have any primary keys defined.
>>
>> So, essentially, when a new insert happens in that table, I would like
>> it (the insert) to fail if there is already a row existing in that table.
>>
>>
> You could try:
>
> id INT PRIMARY KEY NOT NULL DEFAULT(1) CHECK (id = 1),
>

I like that approach :)

But should you also prevent DELETE's from that table? Otherwise you could
wind up with no rows at all. I guess that would have to be done using a rule...

Thomas


From: Mike Nolan <nolan(at)gw(dot)tssi(dot)com>
To: spam_eater(at)gmx(dot)net (Thomas Kellerer)
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Single Row Table?
Date: 2004-08-31 19:46:30
Message-ID: 200408311946.i7VJkV4E016628@gw.tssi.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

> But should you also prevent DELETE's from that table? Otherwise you could
> wind up with no rows at all. I guess that would have to be done using a rule...

Why not just revoke the delete privilege?
--
Mike Nolan


From: Thomas Kellerer <spam_eater(at)gmx(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Single Row Table?
Date: 2004-08-31 20:35:37
Message-ID: ch2nep$3rj$1@sea.gmane.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Mike Nolan wrote on 31.08.2004 21:46:

>>But should you also prevent DELETE's from that table? Otherwise you could
>>wind up with no rows at all. I guess that would have to be done using a rule...
>
>
> Why not just revoke the delete privilege?

That was one of my first guesses as well, but then I'm not sure if you can
revoke DELETE and INSERT privilege from the owner of the table...

Thomas


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Thomas Kellerer <spam_eater(at)gmx(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Single Row Table?
Date: 2004-08-31 20:59:04
Message-ID: 15132.1093985944@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Thomas Kellerer <spam_eater(at)gmx(dot)net> writes:
> That was one of my first guesses as well, but then I'm not sure if you can
> revoke DELETE and INSERT privilege from the owner of the table...

In Postgres you can do this, although I think it's contrary to the
restrictions of the SQL spec, so it might not work in other DBs.

The reason is that Postgres treats the owner's ordinary privileges
as having been granted by the owner to himself; therefore he can revoke
'em too. The SQL spec treats the owner's privileges as having been
granted by the mystical entity "_SYSTEM"; not being "_SYSTEM", the owner
cannot revoke them.

Since it's obviously useful to be able to make a read-only table,
I think that the spec has missed a bet here.

regards, tom lane


From: Thomas Kellerer <spam_eater(at)gmx(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Single Row Table?
Date: 2004-08-31 21:09:30
Message-ID: ch2pea$ad9$1@sea.gmane.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Tom Lane wrote on 31.08.2004 22:59:

> In Postgres you can do this, although I think it's contrary to the
> restrictions of the SQL spec, so it might not work in other DBs.
>
> The reason is that Postgres treats the owner's ordinary privileges
> as having been granted by the owner to himself; therefore he can revoke
> 'em too. The SQL spec treats the owner's privileges as having been
> granted by the mystical entity "_SYSTEM"; not being "_SYSTEM", the owner
> cannot revoke them.
>
> Since it's obviously useful to be able to make a read-only table,
> I think that the spec has missed a bet here.
>

Thanks for the explanation. And yes I agree with you that making a table
read only by revoking the grants is something *very* useful.

Cheers
Thomas


From: Mike Nolan <nolan(at)gw(dot)tssi(dot)com>
To: spam_eater(at)gmx(dot)net (Thomas Kellerer)
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Single Row Table?
Date: 2004-08-31 21:15:23
Message-ID: 200408312115.i7VLFNWT018132@gw.tssi.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

> > Why not just revoke the delete privilege?
>
> That was one of my first guesses as well, but then I'm not sure if you can
> revoke DELETE and INSERT privilege from the owner of the table...

I just did a quick test on 7.4.5. Yes the table owner can revoke (and
re-grant) delete privileges on a table he owns, but of course I was not
able to revoke delete privileges from a superuser, since by definition
a superuser bypasses all access controls restrictions.

I assume the rule approach would apply to the superuser as well as to
other users. That makes it better able to handle this situation, whether
or not that approach has downsides is an internals question I can't answer.
--
Mike Nolan


From: Mike Nolan <nolan(at)gw(dot)tssi(dot)com>
To: spam_eater(at)gmx(dot)net (Thomas Kellerer)
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Single Row Table?
Date: 2004-08-31 21:24:00
Message-ID: 200408312124.i7VLO034018263@gw.tssi.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

> Thanks for the explanation. And yes I agree with you that making a table
> read only by revoking the grants is something *very* useful.

IIRC, Oracle supports read-only tablespaces. The reason for doing it at
the tablespace level probably has to with the overall concept of tablespaces
being linked to physical storage issues. Thus a read-only tablespace
could be stored on CD-ROM or some other non-writeable medium.
--
Mike Nolan