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

  • Home
  • SEARCH
  • 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 8877907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:35:00+00:00 2026-06-14T19:35:00+00:00

Is there some way to do a INSERT INTO t1 SELECT * FROM… such

  • 0

Is there some way to do a INSERT INTO t1 SELECT * FROM... such that it fails if the column names do not coincide?

I’m using Postgresql 9.x The columns names are not known in advance.

Motivation: I’m doing a periodic refresh of materialized views by the (quite standard) PL/pgSQL procedure:

CREATE OR REPLACE FUNCTION matview_refresh(name) RETURNS void AS 
$BODY$
DECLARE 
    matview ALIAS FOR $1;
    entry matviews%ROWTYPE;
BEGIN
    SELECT * INTO entry FROM matviews WHERE mv_name = matview;
    IF NOT FOUND THEN
        RAISE EXCEPTION 'Materialized view % does not exist.', matview;
    END IF;

    EXECUTE 'TRUNCATE TABLE ' || matview;
    EXECUTE 'INSERT INTO ' || matview  || ' SELECT * FROM ' || entry.v_name;

    UPDATE matviews SET last_refresh=CURRENT_TIMESTAMP WHERE mv_name=matview;
    RETURN;
END

I preferred a TRUNCATE followed by a SELECT * INTO instead of a DROP/CREATE because it seemed more light and concurrent-friendly. It would fail if someone adds/remove columns from the view (then I would do the DROP/CREATE) but, it doesn’t matter, in that case the refresh would not complete and we would catch the problem soon. What does matter is what happened today: someone changed the order of two columns of the view (of the same type), and the refresh inserted bogus data.

  • 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-14T19:35:02+00:00Added an answer on June 14, 2026 at 7:35 pm

    Build this into your plpgsql function to verify that view and table share the same column names in the same sequence exactly:

    IF EXISTS (
        SELECT 1
        FROM (
           SELECT *
           FROM   pg_attribute
           WHERE  attrelid = matview::regclass
           AND    attisdropped = FALSE
           AND    attnum > 0
           ) t
        FULL OUTER JOIN (
           SELECT *
           FROM   pg_attribute
           WHERE  attrelid = entry.v_name::regclass
           AND    attisdropped = FALSE
           AND    attnum > 0
           ) v USING (attnum, attname) -- atttypid to check for type, too
        WHERE t.attname IS NULL
        OR    v.attname IS NULL
       ) THEN 
       RAISE EXCEPTION 'Mismatch between table and view!';
    END IF;
    

    The FULL OUTER JOIN adds a row with NULL values for any mismatch between the list of column names. So, if EXISTS finds a row, something is off.

    And the cast to ::regclass would raise an exception right away if either table or view do not exist (or is out of scope – not in the search_path and not schema-qualified).

    If you also want to check data types of the columns, just add atttypid to the USING clause.

    As an aside: Querying pg_catalog tables is regularly faster by an order of magnitude than querying the bloated views int information_schema – information_schema is only good for SQL standard compliance and portability of code. Since you are writing 100 % Postgres-specific code, neither is relevant here.

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

Sidebar

Related Questions

I'm converting some data in SQL Server: INSERT INTO MYTABLE (AllowEdit) (Select PreventEdit from
Is there some way when sending this message to specify that I rather have
Is there a way to make views that use SELECT * stay in sync
since SELECT INTO NEW_TABLE FROM QUERY creates NEW_TABLE the new table will not have
I have an insert statement that pulls some data into a few table variables
Is there some way to define an abstract type as a parameter in an
Is there some way to use Tuning Advisor for SQL Express? Is there some
Is there some way to compile (with the CSC task) a WPF Application with
is there some way to force the gridview's pager to show up, even when
Is there some way I can catch when a method generates a write to

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.