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

The Archive Base Latest Questions

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

I’m currently trying to create a function that will create an index on a

  • 0

I’m currently trying to create a function that will create an index on a table in every schema in the data warehouse. This is the script I have so far:

create or replace function dwh.loan_type_id_indexing()
returns void language plpgsql AS
$PROC$
Declare
       myschema varchar;
               sql text;        
Begin 
    for myschema in 
        SELECT nspname
          FROM pg_catalog.pg_namespace 
         where nspname not in ('information_schema', 'pg_catalog', 'pg_temp_1',
                               'pg_temp_7', 'pg_toast', 'pg_toast_temp_1',
                               'pg_toast_temp_7','public', 'c1', 'dwh',
                               'users', 'c2'
                              )
         order by nspname
    loop        
        sql = 'CREATE INDEX '|| myschema || '_' ||'type_id ON '|| 
        myschema || '.' ||'.fact_tbl USING btree (loan_type_id)';

        execute sql;

    end loop;
END
$PROC$
volatile;

I know this isn’t correct but it will give you my thought process on what I’m trying to do.

  • 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-11T03:35:28+00:00Added an answer on June 11, 2026 at 3:35 am

    Instead of filtering out schemas and assuming every other one has the the table you want, query information_schema for them and loop over the resulting list:

    select t.table_schema
    from information_schema.tables t inner join information_schema.columns c 
      on (t.table_schema = c.table_schema and t.table_name = c.table_name) 
    where t.table_name = 'fact_loan' and c.column_name = 'loan_type_id'
      and t.table_schema NOT LIKE 'pg_%'
      and t.table_schema NOT IN ('information_schema', 'ad_delivery', 'dwh', 'users', 'wand');
    

    You now have everything you need to create the index with EXECUTE by looping over the records returned from the query.

    You might want to RAISE NOTICE 'Creating index on %s.fact_loan.loan_type_id', table_schema; to allow you to track progress, too, as index builds can take a while.

    If you were going to filter schemas, you’d be better off using schemaname NOT LIKE 'pg_%' AND lower(shemaname) <> 'information_schema' as shown above.

    BTW, I usually find this sort of job more convenient to do from a script outside the database where I have access to multiple connections, threading/multiprocessing, etc. A quick Python script with the psycopg2 driver for Pg would let you bang something like this together that does the index builds in parallel say 4 at a time; the right number would depend on your disk configuration.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I know there's a lot of other questions out there that deal with this
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

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.