How to get array index when doing unnest in PGSQL
use the unnest()
function in the from
clause, then you can add the with ordinality
option which returns the array index
select t.id, u.name, t.status, u.idx
from testunnest t
cross join unnest(t.fname) with ordinality as u(name, idx);
In general it's recommended to use set-returning functions in the FROM clause anyway.