title: Is it possible to set a Postgres array value by specified index disregarding the length?
body: <p>I have the next JSONB data structure in my <code>data</code> column:</p>
  
  <pre><code>data = {
    field1,
    field2,
    ...,
    items: [id1, id2, ...id3],
  }
  </code></pre>
  
  <p>And there is a necessity to add an item in the <code>items</code> array specifying its index.
  There can be <strong>several parallel requests to the API, each one providing an ID and its index</strong>, so it's impossible to just replace an old <code>data</code> with a new one.
  Because in the API code we don't know what is the current number of items.</p>
  
  <p>Help me, please.
  How can we handle this situation using Postgres?</p>
  
  <p>For example, it's great to use something like <code>splice</code> in the Javascript, when you can set a value by index and fill the rest with <code>null</code> values.
  But is anything similar possible in Postgres?</p>
  
  <p>What results am I expecting (example):</p>
  
  <ol>
  <li>first request, index = 5, array in the db = [null, null, null, null, null, id]</li>
  <li>second request, index = 2, array in the db = [null, null, id, null, null, id]</li>
  <li>third request, index = 0, array in the db = [id, null, id, null, null, id]</li>
  <li>fourth request, index = 3, array in the db = [id, null, id, id, null, id]</li>
  <li>fifth request, index = 2, array in the db = [id, null, id, id, id, id]</li>
  <li>sixth request, index = 1, array in the db = [id, id, id, id, id, id]</li>
  </ol>
  
  <p>And so on.</p>