问题
My objective is to get a primary key field automatically inserted when inserting new row in the table.
How to get a sequence going from session to session in PostgreSQL?
doubleemploi@hanbei:/home/yves$ psql -d test
Mot de passe :
psql (8.4.13)
Saisissez « help » pour l''aide.
test=> create sequence test001 start 10;
CREATE SEQUENCE
test=> select currval('test001');
ERREUR: la valeur courante (currval) de la séquence « test00 » n''est pas encore définie dans cette session
--- current value not yet defined this session (???)
test=> select setval('test001', 10);
setval
--------
10
(1 ligne)
test=> select currval('test00');
currval
---------
10
(1 ligne)
test=> \q
test@hanbei:/home/yves$ psql -d test
Mot de passe :
psql (8.4.13)
Saisissez « help » pour l''aide.
test=> select currval('test001');
ERREUR: la valeur courante (currval) de la séquence « test00 » n''est pas encore définie dans cette session
回答1:
This may be simpler than you think ...
My objective is to get a primary key field automatically inserted when inserting new row in the table.
Just set the default value of the column:
ALTER TABLE tbl ALTER COLUMN tbl_id SET DEFAULT nextval('my_seq'::regclass);
Or simpler yet, create the table with a serial type for primary key to begin with:
CREATE TABLE tbl(
tbl_id serial PRIMARY KEY
,col1 txt
-- more columns
);
It creates a dedicated sequence and sets the default for tbl_id automatically.
This way tbl_id
is assigned the next value from the attached sequence automatically if you don't mention it in the INSERT
. Works with any session, concurrent or not.
INSERT INTO tbl(col1) VALUES ('foo');
If you want the new tbl_id
back to do something with it:
INSERT INTO tbl(col1) VALUES ('foo') RETURNING tbl_id;
回答2:
The currval
will return the last value generated for the sequence within the current session. So if another session generates a new value for the sequence you still can retrieve the last value generated by YOUR session, avoiding errors.
But, to get the last generated value on any sessions, you can use the above:
SELECT last_value FROM your_sequence_name;
Be careful, if the value was used by other session with an uncommited (or aborted) transaction and you use this value as a reference, you may get an error. Generally people just need the currval
or even the return of setval
.
回答3:
I will give a practical answer for this matter. My database server is used by my programs and my psql terminal; so there are multiple sessions. currently I am in my psql terminal:
fooserver=> select currval('fusion_id_seq');
ERROR: currval of sequence "fusion_id_seq" is not yet defined in this session
fooserver=> select nextval('fusion_id_seq');
nextval
---------
320032
(1 row)
fooserver=> select currval('fusion_id_seq');
currval
---------
320032
(1 row)
It looks that you can only see the values in your own session. This will also affect the currval of another session. This is probably related to multi-threading of the server to isolate different session. The counter (serial in psql) is a shared object. In my opinion, this session should be able to get the current value of the counter as long as the counter is properly locked to ensure only a single thread (session) can increment it (atomic operation). But I could be wrong here (not an expert on database server writer).
回答4:
Actually nextval will advance sequence and return the new value, so that would be the answer for your question.
currval will return the value most recently obtained with nextval for specified sequence (This though might fail if there wasn't nextval used in current session).
回答5:
This issue seems to be intermittent , For consistency use CTE to get inserted sequence for current session
WITH inserted AS ( INSERT INTO notifn_main (notifn_dt,stat_id) SELECT now(),22 FROM notifn RETURNING id ) SELECT id from inserted INTO tmp_id;
来源:https://stackoverflow.com/questions/12481448/currval-has-not-yet-been-defined-this-session-how-to-get-multi-session-sequence