Postgres Arrays are One-Based
Today I was reminded that Postgres arrays are one-based. This realization came while using Ruby's times
method (zero-based index) to generate SQL targeting a Postgres array (one-based index).
Take this table:
name | pay_by_quarter
-------+---------------------------
Bill | {10000,10000,10000,10000}
Carol | {20000,25000,25000,25000}
(2 rows)
To select the first-quarter pay for all employees, use index 1
.
my_database=# select pay_by_quarter[1] from sal_emp;
pay_by_quarter
----------------
10000
20000
(2 rows)
Using your first Ruby index value 0
to produce pay_by_quarter[0]
returns a sad, empty column.
http://www.postgresql.org/docs/current/static/arrays.html
Tweet