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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:31:53+00:00 2026-06-02T15:31:53+00:00

I have a table being created in a PostgreSQL ( version 9 ) database

  • 0

I have a table being created in a PostgreSQL ( version 9 ) database by a third party product and I need to change that table to add a new column then set the column in question to a standard value.

I have the following in my function:

CREATE FUNCTION alterscorecolumns()
  RETURNS void AS
$BODY$

ALTER TABLE "hi_scores" ADD "total_score" integer;

UPDATE "hi_scores" SET total_score = score1+score2+score3;

$BODY$

However, I’m not allowed to do this because it doesn’t know that the total_score field exists. I just get the message ERROR: column "total_score" of relation "hi_scores" does not exist.

I am guessing there is some execution-plan related reason for this and that maybe I need to tell it to run the ALTER TABLE before it tries to perform the update, but I can’t seem to figure out what I need 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-02T15:31:56+00:00Added an answer on June 2, 2026 at 3:31 pm

    You can’t do it that way. The SQL in the function is parsed when you create the function. At the time of the creation of the function the column is not there, so you get the error message.

    You will need to use dynamic SQL to run the UPDATE statement.

    Something like:

    CREATE FUNCTION alterscorecolumns()
      RETURNS void AS
    $BODY$
    begin
      execute 'ALTER TABLE hi_scores ADD total_score integer';
      execute 'UPDATE hi_scores SET total_score = score1+score2+score3';
    $BODY$
    language plpgsql;
    

    (Not tested, so there might be syntax errors in there)

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

Sidebar

Related Questions

So I have a local SQLite table breadcrumbs being created on the Android device.
I have a table of data that is being sorted alphabetically, and it's a
I currently have a table with an id column that is being autoincremented and
I have a table of 3-4 columns, the central one being task names that
I have an html table containing values that are being generated from javascript. How
I have a MySQL table with about 5,000,000 rows that are being constantly updated
I have a database in production with one table that has grown extremely large
I have a backend process that maintains state in a PostgreSQL database, which needs
This (I think), is a little complicated. I have a table being created from
So I have a simple database with a table that has multiple column, most

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.