Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7575119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:36:25+00:00 2026-05-30T16:36:25+00:00

I am trying to use the unnest function in insert . While doing so,

  • 0

I am trying to use the unnest function in insert. While doing so, the serial skips a number for each insert.

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 the id column.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:36:26+00:00Added an answer on May 30, 2026 at 4:36 pm

    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])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying use a javascript function while passing php variables in it. For
I am trying use a javascript function while passing php variables in it. For
How do I use an array to insert to a table in a function?
I have been trying use the edit_post_link() function to contain an image. All of
Trying to use records with the cursor and later index by table in the
I am trying use the jQuery table sorter plugin for a table that is
I am trying use the onClick() function in an activity for an android app.
When I have a column with separated values, I can use the unnest() function:
I was trying use a set of filter functions to run the appropriate routine,
I'm trying use self-signed certificate (c#): X509Certificate2 cert = new X509Certificate2( Server.MapPath(~/App_Data/myhost.pfx), pass); on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.