You can use RETURNING with several values:
psql=> create table t (id serial not null, x varchar not null); psql=> insert into t (x) values ('a'),('b'),('c') returning id; id ---- 1 2 3 (3 rows)
So you need something more:
INSERT INTO AutoKeyEntity (Name,Description,EntityKey) VALUES ('AutoKey 254e3c64-485e-42a4-b1cf-d2e1e629df6a','Testing 5/4/2011 8:59:43 AM',DEFAULT) returning EntityKey; INSERT INTO AutoKeyEntityListed (EntityKey,Listed,ItemIndex) VALUES (CURRVAL('autokeyentity_entityKey_seq'),'Test 1 AutoKey 254e3c64-485e-42a4-b1cf-d2e1e629df6a', 0), (CURRVAL('autokeyentity_entityKey_seq'),'Test 2 AutoKey 254e3c64-485e-42a4-b1cf-d2e1e629df6a', 1), (CURRVAL('autokeyentity_entityKey_seq'),'Test 3 AutoKey 254e3c64-485e-42a4-b1cf-d2e1e629df6a', 2) returning EntityKey;
And then you will need to collect the EntityKey return values from each transaction statement.
You can try to capture the current value of the sequence at the beginning and end of the transaction and use them to determine which values were used, but which are not reliable
In addition, although multiple sessions guarantee that different sequence values are highlighted, values can be obtained from the sequence when all sessions are considered. For example, from cache 10, session A can reserve the values 1.10 and return nextval=1 , then session B can reserve the values 11..20 and return nextval=11 before session A generated nextval = 2. Thus, from the cache, you can confidently assume that nextval values are generated sequentially; with a cache setting more than you should only assume that nextval values are different and not that they are generated purely sequentially. In addition, last_value will reflect the last value reserved for any session, regardless of whether it has not yet been returned by nextval .
So, even if your sequences have cache values of one, you can still have non-contiguous sequence values in your transaction. However, you can be safe if the sequence cache value matches the number of INSERTs in the transaction, but I assume it will be too large to make sense.
UPDATE . I noticed (thanks to the commenters on the comments) that there are two tables, a little lost in the wall of the text.
In this case, you should be able to use the current INSERTS:
INSERT INTO AutoKeyEntity (Name,Description,EntityKey) VALUES ('AutoKey 254e3c64-485e-42a4-b1cf-d2e1e629df6a','Testing 5/4/2011 8:59:43 AM',DEFAULT) returning EntityKey; INSERT INTO AutoKeyEntityListed (EntityKey,Listed,ItemIndex) VALUES (CURRVAL('autokeyentity_entityKey_seq'),'Test 1 AutoKey 254e3c64-485e-42a4-b1cf-d2e1e629df6a', 0), (CURRVAL('autokeyentity_entityKey_seq'),'Test 2 AutoKey 254e3c64-485e-42a4-b1cf-d2e1e629df6a', 1), (CURRVAL('autokeyentity_entityKey_seq'),'Test 3 AutoKey 254e3c64-485e-42a4-b1cf-d2e1e629df6a', 2);
And take the EntityKey values one at a time from INSERT to AutoEntityKey . It might take some script to process the RETURNING values. You can also wrap AutoKeyEntity and its associated AutoKeyEntityListed INSERT in a function, and then use INTO to get the EntityKey value and return it from the function
INSERT INTO AutoKeyEntity RETURNING EntityKey INTO ek; RETURN ek;