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 8531305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:27:13+00:00 2026-06-11T09:27:13+00:00

I have a table with a SERIAL primary key, and also an ltree column,

  • 0

I have a table with a SERIAL primary key, and also an ltree column, whose value I want to be the concatenation of those primary keys. e.g.

id | path
----------
1    1
2    1.2
3    1.2.3
4    1.4
5    1.5

I’m curious if there’s a way to do such an insert in one query, e.g.

INSERT INTO foo (id, ltree) VALUES (DEFAULT, THIS.id::text)

I’m probably overreaching here and trying to do in one query what I should be doing in two (grouped in a transaction).

  • 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-06-11T09:27:14+00:00Added an answer on June 11, 2026 at 9:27 am

    You could use a CTE to retrieve the value from the sequence once and use it repeatedly:

    WITH cte AS (
       SELECT nextval('foo_id_seq') AS id
       )
    INSERT INTO foo (id, ltree)
    SELECT id, '1.' || id
    FROM   cte;
    

    The CTE with a data-modifying command requires Postgres 9.1 or later.

    If you are not sure about the name of the sequence, use
    pg_get_serial_sequence() instead:

    WITH i AS (
       SELECT nextval(pg_get_serial_sequence('foo', 'id')) AS id
       )
    INSERT INTO foo (id, ltree)
    SELECT id, '1.' || id
    FROM   i;
    

    If the table name “foo” might not be unique across all schemas in the DB, schema-qualify it. And if the spelling of any name is non-standard, you have to double-quote:

    pg_get_serial_sequence('"My_odd_Schema".foo', 'id')
    


    Quick tests indicated @Mark’s idea with lastval() might work too:

    INSERT INTO foo (ltree) VALUES ('1.' || lastval());
    
    • You can just leave id out of the query, the serial column will be assigned automatically. Makes no difference.

    • There shouldn’t be a race condition between rows. I quote the manual:

    currval

    Return the value most recently obtained by nextval for this sequence in the current session. (An error is reported if nextval has
    never been called for this sequence in this session.) Because this is
    returning a session-local value, it gives a predictable answer whether
    or not other sessions have executed nextval since the current session
    did.

    This function requires USAGE or SELECT privilege on the sequence.

    lastval

    Return the value most recently returned by nextval in the current session. This function is identical to currval, except that instead of
    taking the sequence name as an argument it refers to whichever
    sequence nextval was most recently applied to in the current session.
    It is an error to call lastval if nextval has not yet been called in
    the current session.

    This function requires USAGE or SELECT privilege on the last used sequence.

    Bold emphasis mine.

    But, as @Bernard commented, it can fail after all: there is no guarantee that the default value is filled (and nextval() called in the process) before lastval() is called to fill the 2nd column ltree. So stick with the first solution and nextval() to be sure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with Serial(primary key), text, date It get's inserted every so
Suppose I have the following table definition: CREATE TABLE x (i serial primary key,
I have a date_dimension table definition: CREATE TABLE date_dimension ( id integer primary key,
I've got three tables: CREATE TABLE credential_types ( id serial PRIMARY KEY, name varchar(32)
Consider the following tables: CREATE TABLE user_roles( pkey SERIAL PRIMARY KEY, bit_id BIGINT NOT
CREATE TABLE college ( id SERIAL PRIMARY KEY, SCHOOL VARCHAR(100), CColor VARCHAR(100), CCmascot VARCHAR(100)
With this schema: create table object ( obj_id serial primary key, name varchar(80) not
I have a table that looks like this: CREATE TABLE foobar ( id SERIAL
I have a table in my postgresql 8.4 database like this: id(serial), event_type_id(id, foreign
I have table and this table contain result column with some entries. I just

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.