pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to enforce the use of the sequence for serial columns ?
9+ messages / 4 participants
[nested] [flat]

* How to enforce the use of the sequence for serial columns ?
@ 2006-12-13 11:46 Marc Mamin <M.Mamin@intershop.de>
  2006-12-13 12:08 ` Re: How to enforce the use of the sequence for serial columns ? Donald Fraser <postgres@kiwi-fraser.net>
  2006-12-13 14:08 ` Re: How to enforce the use of the sequence for serial columns ? Jerry Sievers <jerry@jerrysievers.com>
  0 siblings, 2 replies; 9+ messages in thread

From: Marc Mamin @ 2006-12-13 11:46 UTC (permalink / raw)
  To: pgsql-admin


I'd like to ensure that nobody provide the ID in an insert statement
when the id is linked to a sequence.
I tried it with a trigger, but the id value is fed before the "BEFORE
INSERT" test is performed (see below)...


Any Idea ?

Cheers,

Marc



CREATE FUNCTION serialtest() RETURNS trigger AS $serialtest$
    BEGIN
        -- Check that the id is provided
        IF NEW.id IS NOT NULL THEN
            RAISE EXCEPTION 'id will be set from a sequence; do not
provide it!';
        END IF;
        
        RETURN NEW;
    END;
$serialtest$ LANGUAGE plpgsql;


CREATE TABLE test_table
(
  id serial primary key,
  foo int
);


CREATE TRIGGER test BEFORE INSERT OR UPDATE ON test_table     
FOR EACH ROW EXECUTE PROCEDURE serialtest();


insert into test_table(foo)values(1);

ERROR: id will be set from a sequence; do not provide it!
SQL state: P0001

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: How to enforce the use of the sequence for serial columns ?
  2006-12-13 11:46 How to enforce the use of the sequence for serial columns ? Marc Mamin <M.Mamin@intershop.de>
@ 2006-12-13 12:08 ` Donald Fraser <postgres@kiwi-fraser.net>
  1 sibling, 0 replies; 9+ messages in thread

From: Donald Fraser @ 2006-12-13 12:08 UTC (permalink / raw)
  To: pgsql-admin

How to enforce the use of the sequence for serial columns ?There are many
ways, here are a couple to think about:
1)
Revoke all access to the table and then create a VIEW to access the table
where by you simply don't use column "id" in the VIEWs insert / update
statements.

2)
Manually retrieve the serial number from a trigger function and modify
column "id" in the function.


Regards
Donald Fraser

----- Original Message ----- 
From: Marc Mamin

I'd like to ensure that nobody provide the ID in an insert statement when
the id is linked to a sequence.
I tried it with a trigger, but the id value is fed before the "BEFORE
INSERT" test is performed (see below)...


Any Idea ?
Cheers,
Marc



CREATE FUNCTION serialtest() RETURNS trigger AS $serialtest$
    BEGIN
        -- Check that the id is provided
        IF NEW.id IS NOT NULL THEN
            RAISE EXCEPTION 'id will be set from a sequence; do not provide
it!';
        END IF;

        RETURN NEW;
    END;
$serialtest$ LANGUAGE plpgsql;


CREATE TABLE test_table
(
  id serial primary key,
  foo int
);


CREATE TRIGGER test BEFORE INSERT OR UPDATE ON test_table
FOR EACH ROW EXECUTE PROCEDURE serialtest();


insert into test_table(foo)values(1);
ERROR: id will be set from a sequence; do not provide it!
SQL state: P0001





^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: How to enforce the use of the sequence for serial columns ?
  2006-12-13 11:46 How to enforce the use of the sequence for serial columns ? Marc Mamin <M.Mamin@intershop.de>
@ 2006-12-13 14:08 ` Jerry Sievers <jerry@jerrysievers.com>
  1 sibling, 0 replies; 9+ messages in thread

From: Jerry Sievers @ 2006-12-13 14:08 UTC (permalink / raw)
  To: Marc Mamin <M.Mamin@intershop.de>; +Cc: pgsql-admin

"Marc Mamin" <M.Mamin@intershop.de> writes:

> I'd like to ensure that nobody provide the ID in an insert statement
> when the id is linked to a sequence.
> I tried it with a trigger, but the id value is fed before the "BEFORE
> INSERT" test is performed (see below)...
> 
> 
> Any Idea ?

Trigger based solution where same trig can be used for any number of
tables by changing the parameter.

Will throw one of 2 exceptions on failure to use sequence for the insert.

create table foo (a serial);
psql:q:2: NOTICE:  CREATE TABLE will create implicit sequence "foo_a_seq" for serial column "foo.a"
CREATE TABLE
create function footrig()
returns trigger
as $$
begin
	-- may throw currval not defined exception
	if new.a = currval(tg_argv[0]) then
		-- currval defined and equal new col value
		return new;		       
	end if;

	-- currval is defined but not used for this insert
	raise exception 'Not using default sequence';
end
$$ language plpgsql;
CREATE FUNCTION
create trigger footrig
before insert on foo
for each row
execute procedure footrig('foo_a_seq');
CREATE TRIGGER
You are now connected to database "jerry".
insert into foo values (1000);
psql:q:25: ERROR:  currval of sequence "foo_a_seq" is not yet defined in this session
CONTEXT:  PL/pgSQL function "footrig" line 3 at if
insert into foo values (default);
INSERT 0 1
insert into foo values (1000);
psql:q:27: ERROR:  Not using default sequence
select * from foo;
 a 
---
 1
(1 row)


> Cheers,
> 
> Marc
> 
> 
> 
> CREATE FUNCTION serialtest() RETURNS trigger AS $serialtest$
>     BEGIN
>         -- Check that the id is provided
>         IF NEW.id IS NOT NULL THEN
>             RAISE EXCEPTION 'id will be set from a sequence; do not
> provide it!';
>         END IF;
>         
>         RETURN NEW;
>     END;
> $serialtest$ LANGUAGE plpgsql;
> 
> 
> CREATE TABLE test_table
> (
>   id serial primary key,
>   foo int
> );
> 
> 
> CREATE TRIGGER test BEFORE INSERT OR UPDATE ON test_table     
> FOR EACH ROW EXECUTE PROCEDURE serialtest();
> 
> 
> insert into test_table(foo)values(1);
> 
> ERROR: id will be set from a sequence; do not provide it!
> SQL state: P0001

-- 
-------------------------------------------------------------------------------
Jerry Sievers   305 854-3001 (home)     Production Database Administrator
                305 321-1144 (mobil	WWW E-Commerce Consultant



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: How to enforce the use of the sequence for serial columns
@ 2006-12-13 14:54 Ben K. <bkim@coe.tamu.edu>
  0 siblings, 0 replies; 9+ messages in thread

From: Ben K. @ 2006-12-13 14:54 UTC (permalink / raw)
  To: pgsql-admin

"Marc Mamin" <M.Mamin@intershop.de> writes:
> I'd like to ensure that nobody provide the ID in an insert statement
> when the id is linked to a sequence.
> I tried it with a trigger, but the id value is fed before the "BEFORE
> INSERT" test is performed (see below)...

If you have the luxury of re-creating the table (dump, edit schema, 
reload), you might want to try this:

create table mytable (myid serial primary key constraint mytable_myid_chk check (myid = 
currval('mytable_myid_seq'), mydata varchar(255), ...);

This will prevent "serial" id field from being arbitrarily manipulated
outside the sequence..

There may be an equivalent way using alter table, but on a first look, currval
cannot be used to check existing values.


Regards

Ben K.
Developer
http://benix.tamu.edu






^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: How to enforce the use of the sequence for serial columns ?
@ 2006-12-14 07:58 Marc Mamin <M.Mamin@intershop.de>
  0 siblings, 0 replies; 9+ messages in thread

From: Marc Mamin @ 2006-12-14 07:58 UTC (permalink / raw)
  To: jerry@jerrysievers.com; +Cc: pgsql-admin

 

>Trigger based solution where same trig can be used for any number of
tables by changing the parameter.

>Will throw one of 2 exceptions on failure to use sequence for the
insert.


Many thanks,
This seems to be the simplest solution,

Marc



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: How to enforce the use of the sequence for serial columns
@ 2006-12-14 11:57 Ben K. <bkim@coe.tamu.edu>
  2006-12-14 12:30 ` Re: How to enforce the use of the sequence for serial columns Jerry Sievers <jerry@jerrysievers.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Ben K. @ 2006-12-14 11:57 UTC (permalink / raw)
  To: pgsql-admin



> create table mytable (myid serial primary key constraint 
mytable_myid_chk check 
(myid = currval('mytable_myid_seq'), mydata varchar(255), ...);


I'd like to clarify that this will not be a full solution, since it will 
not allow update of the table unless nextval has been used in the same 
sequence already.

There seems to be a hack in case of oracle that allows using currval 
without nextval, but it's a hack and I don't know if there's an equivalent 
in postgresql (http://rootshell.be/~yong321/computer/sequence.txt). 
(Oracle seems to have "disable" option when adding check constraint by 
alter table, but the context seem a bit different from ours so may not be 
useful in our case, at any rate.)




Regards

Ben K.
Developer
http://benix.tamu.edu




---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: How to enforce the use of the sequence for serial columns
  2006-12-14 11:57 Re: How to enforce the use of the sequence for serial columns Ben K. <bkim@coe.tamu.edu>
@ 2006-12-14 12:30 ` Jerry Sievers <jerry@jerrysievers.com>
  2006-12-14 14:10   ` Re: How to enforce the use of the sequence for serial columns Ben K. <bkim@coe.tamu.edu>
  0 siblings, 1 reply; 9+ messages in thread

From: Jerry Sievers @ 2006-12-14 12:30 UTC (permalink / raw)
  To: Ben K. <bkim@coe.tamu.edu>; +Cc: pgsql-admin

"Ben K." <bkim@coe.tamu.edu> writes:

> > create table mytable (myid serial primary key constraint
> mytable_myid_chk check (myid = currval('mytable_myid_seq'), mydata
> varchar(255), ...);
> 
> 
> I'd like to clarify that this will not be a full solution, since it
> will not allow update of the table unless nextval has been used in the
> same sequence already.

It's not a full solution anyway since it prevents any kind of update
on the table due to check constraints firing  even if target field not
updated.  See below;


jerry@jerry#
= create table foo (a serial check (a = currval('foo_a_seq')), b int);
NOTICE:  CREATE TABLE will create implicit sequence "foo_a_seq" for serial column "foo.a"
CREATE TABLE
jerry@jerry#
= insert into foo values (default, 1);
INSERT 0 1
jerry@jerry#
= insert into foo values (default, 1);
INSERT 0 1
jerry@jerry#
= insert into foo values (default, 1);
INSERT 0 1
jerry@jerry#
= insert into foo values (default, 1);
INSERT 0 1
jerry@jerry#
= select * from foo;
 a | b 
---+---
 1 | 1
 2 | 1
 3 | 1
 4 | 1
(4 rows)

jerry@jerry#
= update foo set b=2;
ERROR:  new row for relation "foo" violates check constraint "foo_a_check"
jerry@jerry#
= 


> 
> There seems to be a hack in case of oracle that allows using currval
> without nextval, but it's a hack and I don't know if there's an
> equivalent in postgresql
> (http://rootshell.be/~yong321/computer/sequence.txt). (Oracle seems to
> have "disable" option when adding check constraint by alter table, but
> the context seem a bit different from ours so may not be useful in our
> case, at any rate.)
> 
> 
> 
> 
> Regards
> 
> Ben K.
> Developer
> http://benix.tamu.edu
> 
> 
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 2: Don't 'kill -9' the postmaster
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 5: don't forget to increase your free space map settings
> 

-- 
-------------------------------------------------------------------------------
Jerry Sievers   305 854-3001 (home)     Production Database Administrator
                305 321-1144 (mobil	WWW E-Commerce Consultant



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: How to enforce the use of the sequence for serial columns
  2006-12-14 11:57 Re: How to enforce the use of the sequence for serial columns Ben K. <bkim@coe.tamu.edu>
  2006-12-14 12:30 ` Re: How to enforce the use of the sequence for serial columns Jerry Sievers <jerry@jerrysievers.com>
@ 2006-12-14 14:10   ` Ben K. <bkim@coe.tamu.edu>
  0 siblings, 0 replies; 9+ messages in thread

From: Ben K. @ 2006-12-14 14:10 UTC (permalink / raw)
  To: Jerry Sievers <jerry@jerrysievers.com>; +Cc: pgsql-admin

> It's not a full solution anyway since it prevents any kind of update
> on the table due to check constraints firing  even if target field not
> updated.  See below;

Right. It was not a valid idea ...


Regards,

Ben K.
Developer
http://benix.tamu.edu



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: How to enforce the use of the sequence for serial columns
@ 2006-12-14 12:45 Marc Mamin <M.Mamin@intershop.de>
  0 siblings, 0 replies; 9+ messages in thread

From: Marc Mamin @ 2006-12-14 12:45 UTC (permalink / raw)
  To: Jerry Sievers <jerry@jerrysievers.com>; Ben K. <bkim@coe.tamu.edu>; +Cc: pgsql-admin


> It's not a full solution anyway since it prevents any kind of update
on the table due to check constraints firing  even if target field not
updated.  >

Well, we can extend the check within the trigger:

if (coalesce old.a=new.a -- Update
    OR 
    new.a = currval(tg_argv[0] -- Insert
) then
  return new;		       
end if;

Marc





^ permalink  raw  reply  [nested|flat] 9+ messages in thread


end of thread, other threads:[~2006-12-14 14:10 UTC | newest]

Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-12-13 11:46 How to enforce the use of the sequence for serial columns ? Marc Mamin <M.Mamin@intershop.de>
2006-12-13 12:08 ` Donald Fraser <postgres@kiwi-fraser.net>
2006-12-13 14:08 ` Jerry Sievers <jerry@jerrysievers.com>
2006-12-13 14:54 Re: How to enforce the use of the sequence for serial columns Ben K. <bkim@coe.tamu.edu>
2006-12-14 07:58 Re: How to enforce the use of the sequence for serial columns ? Marc Mamin <M.Mamin@intershop.de>
2006-12-14 11:57 Re: How to enforce the use of the sequence for serial columns Ben K. <bkim@coe.tamu.edu>
2006-12-14 12:30 ` Re: How to enforce the use of the sequence for serial columns Jerry Sievers <jerry@jerrysievers.com>
2006-12-14 14:10   ` Re: How to enforce the use of the sequence for serial columns Ben K. <bkim@coe.tamu.edu>
2006-12-14 12:45 Re: How to enforce the use of the sequence for serial columns Marc Mamin <M.Mamin@intershop.de>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox