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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:46:17+00:00 2026-06-06T13:46:17+00:00

I have a column of Int Array. I want to add another column to

  • 0

I have a column of Int Array. I want to add another column to the table, that always shows the number elements in that array for that row. It should update this value automatically. Is there a way to embedd a function as default value? If so, how would this function know where to pick its argument (the int array column/row number).

  • 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-06T13:46:19+00:00Added an answer on June 6, 2026 at 1:46 pm

    In a normalized table you would not include this functionally dependent and redundant information as a separate column.

    It is easy and fast enough to compute it on the fly:

    SELECT array_dims ('{1,2,3}'::int[]);
    

    Or:

    SELECT array_length('{1,2,3}'::int[], 1);
    

    array_length() has been introduced with PostgreSQL 8.4. Maybe an incentive to upgrade? 8.3 is going out of service soon.

    With Postgres 8.3 you can use:

    SELECT array_upper('{1,2,3}'::int[], 1);
    

    But that’s inferior, because the array index can start with any number, if entered explicitly. array_upper() would not tell the actual length then, you would have to subtract array_lower() first. Also note, that in PostgreSQL arrays can always contain multiple dimensions, regardless of how many dimensions have been declared. I quote the manual here:

    The current implementation does not enforce the declared number of
    dimensions either. Arrays of a particular element type are all
    considered to be of the same type, regardless of size or number of
    dimensions. So, declaring the array size or number of dimensions in
    CREATE TABLE is simply documentation; it does not affect run-time
    behavior.

    (True for 8.3 and 9.1 alike.) That’s why I mentioned array_dims() first, to give a complete picture.

    Details about array functions in the manual.


    You may want to create a view to include that functionally dependent column:

    CREATE VIEW v_tbl AS
    SELECT arr_col, array_length(arr_col, 1) AS arr_len
    FROM   tbl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a 2D-array that I want to sort based on the second column.
Assume I have a table like this: column A(int) column B(int) 1 2 2
I have this table: ID(INT) DATE(DATETIME) Under the DATE column there are a lot
I have a simple table with autoincrementing ID's and one text column. id(int), value(nvarchar)
I have a table that has 5 columns: AcctId (int), Address1 (varchar), Address2 (varchar),
I have a byte array that can be very big in size. I want
I have a gridview: A column ( type int ) How can I get
Hi I have written code like this @Id @Column(nullable=false) @GeneratedValue(strategy=GenerationType.AUTO) public int getUserID() {
I have a table [Users] with the following columns: INT SmallDateTime Bit Bit [UserId],
Imagine I have these columns in a table: id int NOT NULL IDENTITY PRIMARY

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.