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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:41:37+00:00 2026-06-14T18:41:37+00:00

In postgres 9.1 I’d like to create a function that takes an index name,

  • 0

In postgres 9.1 I’d like to create a function that takes an index name, a table name and a variable number of columns, constructs an index, and then does some other things.

My current approach is to use plpgsql and construct a dynamic command to execute. However, I’m getting tripped up when trying to use quote_ident to protect all of the identifiers. Code thus far:

CREATE OR REPLACE FUNCTION my_create_index(indexname text, tablename text, VARIADIC arr text[]) RETURNS void AS $$
  DECLARE
    command_string text;
  BEGIN
    command_string := 'CREATE INDEX ' || quote_ident(indexname) || ' ON ' ||
         quote_ident(tablename) || ' (' ||
         format(repeat('%I ', array_length($3, 1)), VARIADIC $3) ||
         ')';

    -- display the string
    RAISE NOTICE '%', command_string;

    -- execute the string
    EXECUTE command_string;

    -- (do other stuff)
  END;
$$ LANGUAGE plpgsql;

The code appears to be successful when passing one column name, but with two or more I get the following error:

ERROR:  too few arguments for format

What am I doing wrong? (Presumably something with format or my use of VARIADIC.)

Thank you!

  • 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-14T18:41:39+00:00Added an answer on June 14, 2026 at 6:41 pm

    You don’t generally to use both format and quote_ident. format is smart enough to do identifer quoting its self; you show that yourself in part of your expression, while using unnecessary concatenation and quote_ident calls elsewhere.

    However, I’m seeing the same issue you are with calling format with a VARIADIC array argument, and I suspect you’ve found a bug.

    Here’s a workaround until I spot what’s going on:

    command_string := format('CREATE INDEX %I ON %I (%s)', 
        indexname, tablename, (
          SELECT string_agg(quote_ident(x), ', ') 
          FROM unnest($3) x
        )
    );
    

    Note that in your original code, '%I ' should’ve been '%I, '.

    Yes, bug confirmed, and it’s been reported before but looks like it never got checked up on. The same bug exists in concat and concat_ws. These functions fail to check for the VARIADIC argument flag.

    Observe:

    regress=> SELECT format('%I', VARIADIC ARRAY['b','c','d']);
      format   
    -----------
     "{b,c,d}"
    (1 row)
    
    regress=> SELECT format('%I', 'b','c','d');
     format 
    --------
     b
    (1 row)
    
    regress=> SELECT format('%I, %I, %I', VARIADIC ARRAY['b','c','d']);
    ERROR:  too few arguments for format
    

    The only workaround I can think of is either avoiding them (as shown above) or using EXECUTE to construct the function call dynamically. In the case of format that’s totally redundant, so I’d just use the above sub-query with unnest.

    I’ll try to chase this up on the PostgreSQL mailing lists, but I’m going to be insanely busy next week, so poke me or post on pgsql-bugs with a link back to this question yourself if you don’t see a follow-up by mid week-after-next.

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

Sidebar

Related Questions

I have a postgres table like this: CREATE SEQUENCE seq; CREATE TABLE tbl (id
I have a postgres table that looks in part like: Year | Month |
I need a Postgres function to return a virtual table (like in Oracle) with
I have a (Postgres) DB table that I'd like to add a manual 'sort'
postgres has an array data type, in this case a numeric array: CREATE TABLE
I've got a Postgres ORDER BY issue with the following table: em_code name EM001
I've a legacy postgres db that has date columns cast as character(50) fields (don't
I need help in Postgres triggers. I have table with 2 columns: sold boolean;
I'm using Postgres and would like to make a big update query that would
I have a Postgres database that has 2 columns that are not primary keys

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.