How to push a JSON object to a nested array in a JSONB column
<p>I need to somehow push a JSON object to a nested array of potentionally existing JSON objects - see "pages" in the below JSON snippet.</p>

<pre><code>{
    "session_id": "someuuid",
    "visitor_ui": 1,
    "pages": [
        {
            "datetime": "2016-08-13T19:45:40.259Z",
            "duration,": 0,
            "device_id": 1,
            "url": {
                "path": "/"
            }
        },
        {
            "datetime": "2016-08-14T19:45:40.259Z",
            "duration,": 0,
            "device_id": 1,
            "url": {
                "path": "/test"
            }
        },
        // how can i push a new value (page) here??
    ]
    "visit_page_count": 2
}
</code></pre>

<p>I'm aware of the <code>jsonb_set(target jsonb, path text[], new_value jsonb[, create_missing boolean])</code> (although still finding it a bit hard to comprehend) but I guess using that, would require that I first <code>SELECT</code> the whole JSONB column, in order to find out how many elements inside "pages" already exists and what index to push it to using <code>jsonb_set</code>, right? I'm hoping theres a way in Postgres 9.5 / 9.6 to achieve the equivalent of what we know in programming languages eg. <code>pages.push({"key": "val"})</code>.</p>

<p>What would be the best and easiest way to do this with Postgresql 9.5 or 9.6?</p>