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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:33:07+00:00 2026-06-12T11:33:07+00:00

The PostgreSQL Documentation on arrays provides an example using [-1] to access what appears

  • 0

The PostgreSQL Documentation on arrays provides an example using [-1] to access what appears to be the last element of an array; however while SELECT arr[2:3]; produces {5,9}, arr[2:-1] results in {}.

How can the last element of an array be obtained in PostgreSQL?

Edit: Windows, PostgreSQL v9.2.1

  • 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-12T11:33:08+00:00Added an answer on June 12, 2026 at 11:33 am

    I think you’re misinterpreting the example. PostgreSQL arrays don’t have to be indexed from 1 to n, that’s just the default:

    By default PostgreSQL uses a one-based numbering convention for arrays, that is, an array of n elements starts with array[1] and ends with array[n].

    The example you’re looking at is this:

    SELECT f1[1][-2][3] AS e1, f1[1][-1][5] AS e2
     FROM (SELECT '[1:1][-2:-1][3:5]={{{1,2,3},{4,5,6}}}'::int[] AS f1) AS ss;
    

    But those negative numbers aren’t indexing from the end of the arrays as in languages such as Perl. In the FROM (SELECT ... part, they’re specifying the starting and ending indexes so the -1 in f1[1][-1][5] is just a plain old index. Consider this array_dims result:

    => SELECT array_dims('[1:1][-2:-1][3:5]={{{1,2,3},{4,5,6}}}'::int[]);
        array_dims     
    -------------------
     [1:1][-2:-1][3:5]
    

    If you’re using the default 1-based arrays then you can get the last element with a simple arr[array_length(arr, 1)]. If you’re not using the default [1:n] arrays then you’ll have to mess around with array_lower and array_upper to get the first and last elements; or, depending on the circumstances, you might be able to use unnest to unpack the array then work with the array as a rowset.

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

Sidebar

Related Questions

How to make this (PostgreSQL) using Rails syntax?: SELECT fld1, fld2 FROM ( SELECT
Using postgresql 9.1 with linux psql console and the windows pgadminIII the query: select
PostgreSQL documentation recommends using a CallableStatement to call stored procedures. In the case of
Using PostgreSQL triggers, is it possible to record the changes that have happened to
I'm using PostgreSQL 9.1, I thought it was supposed to support this XML integration
I'm using PostgreSQL 9.1.3 and the following functions: CREATE OR REPLACE FUNCTION cad(INOUT args
The documentation for PostgreSQL window functions seems to imply you can use the same
I was following the documentation on FullTextSearch in postgresql. I've created a tsvector column
I am trying to create a website using Symfony and PostgreSQL. I cant find
I am trying to setup PostgreSQL and allow only certain Windows users to access

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.