By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,660 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,660 IT Pros & Developers. It's quick & easy.

Is it safe to use DEFAULT currval()

P: n/a
I've got a table, and I'm trying to track changes to it, but can't do it via a
primary key including a revision-number (historical reasons).

CREATE TABLE foo (
id SERIAL,
blah text,
tracking int4 DEFAULT currval('foo_id_seq')
PRIMARY KEY (id)
);

I'm actually setting the DEFAULT on tracking using ALTER TABLE after the
event.

Now - it works, but is that a design feature or just luck?

It's convenient that tracking = id of the first entry, but not vital, so I can
always use a separate sequence if needs be.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 12 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Richard Huxton <de*@archonet.com> writes:
CREATE TABLE foo (
id SERIAL,
blah text,
tracking int4 DEFAULT currval('foo_id_seq')
PRIMARY KEY (id)
); Now - it works, but is that a design feature or just luck?


You're essentially assuming that the column expressions for an INSERT
will be evaluated left-to-right. That's true at the moment, and I don't
see any foreseeable reason why we'd change it, but it surely is an
implementation dependency that could bite you someday.

regards, tom lane

---------------------------(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

Nov 12 '05 #2

P: n/a
On Saturday 27 September 2003 18:40, Tom Lane wrote:
Richard Huxton <de*@archonet.com> writes:
CREATE TABLE foo (
id SERIAL,
blah text,
tracking int4 DEFAULT currval('foo_id_seq')
PRIMARY KEY (id)
);

Now - it works, but is that a design feature or just luck?


You're essentially assuming that the column expressions for an INSERT
will be evaluated left-to-right. That's true at the moment, and I don't
see any foreseeable reason why we'd change it, but it surely is an
implementation dependency that could bite you someday.


I feared as much. Ah well, use nextval(), a different sequence or write a
trigger I suppose.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.