问题
i am trying to use "unnest" function in insert,
while doing so, the serial skips a number for each insert,
plz help me to solve this...
mydb=# \d tab1
Table "public.tab1"
Column | Type | Modifiers
--------+---------+---------------------------------------------------
id | integer | not null default nextval('tab1_id_seq'::regclass)
col1 | integer |
col2 | integer |
Indexes:
"tab1_pkey" PRIMARY KEY, btree (id)
mydb=# insert into tab1(id,col1,col2) values (nextval('tab1_id_seq'),1,unnest(array[4,5]));
INSERT 0 2
mydb=# select * from tab1;
id | col1 | col2
----+------+------
1 | 1 | 4
2 | 1 | 5
(2 rows)
mydb=# insert into tab1(id,col1,col2) values (nextval('tab1_id_seq'),2,unnest(array[4,5]));
INSERT 0 2
mydb=# select * from tab1;
id | col1 | col2
----+------+------
1 | 1 | 4
2 | 1 | 5
4 | 2 | 4
5 | 2 | 5
(4 rows)
mydb=# insert into tab1(col1,col2) values(2,unnest(array[4,5]));
INSERT 0 2
mydb=# select * from tab1;
id | col1 | col2
----+------+------
1 | 1 | 4
2 | 1 | 5
4 | 2 | 4
5 | 2 | 5
7 | 2 | 4
8 | 2 | 5
(6 rows)
mydb=# insert into tab1(col2) values(unnest(array[4,5]));
INSERT 0 2
mydb=# select * from tab1;
id | col1 | col2
----+------+------
1 | 1 | 4
2 | 1 | 5
4 | 2 | 4
5 | 2 | 5
7 | 2 | 4
8 | 2 | 5
10 | | 4
11 | | 5
(8 rows)
mydb=# select nextval('tab1_id_seq');
nextval
---------
13
(1 row)
for every insert it skips a number in id column, help me solve this plz...
回答1:
unnest
returns multiple rows, so using it inside a single row of VALUES
is a bit of a hack. Although it does work, it appears that the nextval
call is somehow evaluated twice.
You can write an insert as INSERT INTO ... SELECT ...
rather than INSERT INTO ... VALUES
: in PostgreSQL, VALUES
is just a row constructor. So consider writing something like this:
insert into tab1(col1, col2) select 1, unnest(array[4,5])
回答2:
If you use the unnest function in the from clause of an insert ... select
it doesn't skip the extra IDs.
As:
insert into tab1(col1,col2)
select
1, u.val
from
unnest(array[4,5]) as u(val);
I think by putting it in a values clause you're running foul of a deprecated postgresql specific extension. The Postgresql Documentation says about this:
Currently, functions returning sets can also be called in the select list of a query. For each row that the query generates by itself, the function returning set is invoked, and an output row is generated for each element of the function's result set. Note, however, that this capability is deprecated and might be removed in future releases.
来源:https://stackoverflow.com/questions/9529364/insert-with-unnest-function-skips-a-number-in-serial-column