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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:26:53+00:00 2026-06-12T09:26:53+00:00

First I am really new to pl/pgsql. Need it for a project. I am

  • 0

First I am really new to pl/pgsql. Need it for a project.

I am stuck with this (simplified) problem.

My db schema has a n to m relationship (author, books, author_books)

Now I want to have a pl/psgsql function insert_book. (I do know that all authors are definitely already in the author table, so I just want to pass their primary keys).

This function outline is what I have in mind.

 create or replace function insert_book(book_to_insert book, authors integer[])
  returns void as $$
begin
    -- insert book into table books
    -- for each author add an entry to author_books table
end;
 $$ language plpgsql;

As arguments I thought to pass a record of type book and the authors that wrote it. But how exactly would this work? I googled quite a bit and can’t seem to figure this out…

Question 1: Is the function outline “correct”/does it make sense?

Question 2: How to insert record book into table book? Do I have to go over all fields of book (title, isbn, publisher,…) and add them to an INSERT INTO statement or is there a “smarter” way?

Question 3: How would I call my function insert_book? I found this example here (http://dbaspot.com/postgresql/206142-passing-record-function-argument-pl-pgsql.html), but that doesn’t really help me. For testing purposes I am using the shell, but later on we will use Java with JDBC.

Thank you very much for your help.

  • 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-12T09:26:54+00:00Added an answer on June 12, 2026 at 9:26 am

    Using unnest() and a data-modifying CTE (requires Postgres 9.1 or later), this can be a simple SQL query:

    WITH x AS (SELECT '(1,foo_book)'::book AS _book
                    , '{1,2,3}'::int[]     AS _authors)
       , y AS (
       INSERT INTO book  -- no column list, correct due to composite type
       SELECT (x._book).*
       FROM   x
       RETURNING book_id
       )
    INSERT INTO author_book (book_id, author_id)
    SELECT y.book_id, unnest(x._authors)
    FROM   x,y;  -- CROSS JOIN ok, only 1 row for x and y
    

    The first CTE x is just for simplified data input and not strictly needed.

    SQL Fiddle.

    As to your questions:

    Question 1: Is the function outline "correct"/does it make sense?

    Might be easier to pass base types instead of the composite type book, but it is a perfectly valid approach. You have to know your way around the syntax for complex types, though. For instance, note the parenthesis around the name in my example: (x._book).*.

    A plpgsql function could look like this:

    CREATE OR REPLACE FUNCTION f_insert_book(_book book, _authors integer[])
       RETURNS void AS 
    $func$
    BEGIN
        WITH y AS (
            INSERT INTO book b
            SELECT (_book).*
            RETURNING b.book_id
            )
        INSERT INTO author_book (book_id, author_id)
        SELECT y.book_id, unnest(_authors)
        FROM   y;
    END
    $func$ LANGUAGE plpgsql;
    

    Question 2: How to insert record book into table book? (…) or is there a "smarter" way?

    The smarter way is to decompose the composite type with (variable_name).*.

    As the type is guaranteed to match the table (being derived from it), this is one of the rare cases, where it is perfectly ok, not to provide a column list for the INSERT command in persisted code.

    Question 3: How would I call my function insert_book? …

    SELECT f_insert_book('(1,foo_book)'::book, '{1,2,3}'::int[]);
    

    Within other plpgsql functions, use PERFORM instead of SELECT if you don’t provide a target (INTO foo) for the (non-existing) results.

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

Sidebar

Related Questions

at first i'm really new to ORM, nhibernate and FHN. i look into this
I'm really new to Regex and working hard, but this has gone beyond simple
I'm really new to Ruby (first day!) and I'm struggling with this here. I'm
I apologise if this question has already been asked. I'm really new to Python
I'm really new to xml, with this being my first dip into it. I'm
I'm really new to C++ and one of the first things that has me
This code has caused me lots of trouble so far but it's really first
I'm really new to silex and symfony. This is my first foray into silex.
I'm really new to Haskell and I'm stuck on trying to map the first
I am really new to database and this is my first program in database

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.