Skip to main content

Views

A view is a stored SELECT that is read like a table: through SQL, GraphQL and, when it has a geometry column, as a map layer through the OGC services. A materialized view stores the result of its SELECT instead of running it on every read, and is brought up to date by a refresh.

The View API has two resources:

ResourceWhat it is
/api/v4/schemas/{schema}/viewsThe views and materialized views of a schema: create them, read their definitions and replace the definition of a plain view.
/api/v4/views/refreshesRefresh jobs for materialized views: queue a refresh, follow its status and cancel it.

Access model:

  • Both resources are for the database owner (super user) only and require an Authorization: Bearer header. A sub-user gets 403 (SUPER_USER_ONLY).
  • A view is also a relation in the Table API: dropping, renaming, comments, columns and privileges are handled there, on the same name. The View API has no DELETE.

Create a view​

POST takes a name and a definition, which is one SELECT statement. Schema-qualify the relations in it. The response is 201 Created with a Location header pointing to the new view:

Request
POST https://api.gc2.io/api/v4/schemas/rockhall/views HTTP/1.1
Content-Type: application/json
Authorization: Bearer abc123

{
"name": "guitarists",
"definition": "SELECT id, name FROM rockhall.inductees WHERE instrument = 'guitar'"
}
Response
HTTP/1.1 201 Created
Location: /api/v4/schemas/rockhall/views/guitarists

The name can hold letters, digits and underscores, is at most 63 characters and cannot start with a digit; anything else is 400 (INVALID_NAME). If a table or view of that name already exists in the schema, the request is rejected with 409 (RELATION_EXISTS).

The definition is run as one statement. A second statement in it is refused, and whatever PostgreSQL rejects comes back as 400 (INVALID_DEFINITION) with PostgreSQL's message.

Create several views​

An array creates the views in the order given and in one transaction: a later view may select from an earlier one, and if one fails none is created.

Request
POST https://api.gc2.io/api/v4/schemas/rockhall/views HTTP/1.1
Content-Type: application/json
Authorization: Bearer abc123

[
{
"name": "guitarists",
"definition": "SELECT id, name FROM rockhall.inductees WHERE instrument = 'guitar'"
},
{
"name": "guitarist_count",
"definition": "SELECT count(*) AS total FROM rockhall.guitarists"
}
]
Response
HTTP/1.1 201 Created
Location: /api/v4/schemas/rockhall/views/guitarists,guitarist_count

Get views​

GET without a name returns all views and materialized views of the schema as an array. With one name the response is a single object; with a comma-separated list it is an array. An unknown schema, or a name that is not a view in it, is 404 (VIEW_NOT_FOUND).

Request
GET https://api.gc2.io/api/v4/schemas/rockhall/views/guitarists HTTP/1.1
Accept: application/json; charset=utf-8
Authorization: Bearer abc123
Response
{
"name": "guitarists",
"definition": " SELECT id,\n name\n FROM rockhall.inductees\n WHERE ((instrument)::text = 'guitar'::text);",
"materialized": false,
"populated": null,
"_links": {
"self": "/api/v4/schemas/rockhall/views/guitarists",
"table": "/api/v4/schemas/rockhall/tables/guitarists"
}
}

definition is returned as PostgreSQL stores it, not as the text that was sent. _links.table is the same relation in the Table API.

Replace the definition​

PATCH with a new definition replaces the SELECT of a plain view. The response is 303 See Other with a Location header pointing back to the view.

Request
PATCH https://api.gc2.io/api/v4/schemas/rockhall/views/guitarists HTTP/1.1
Content-Type: application/json
Authorization: Bearer abc123

{
"definition": "SELECT id, name, year FROM rockhall.inductees WHERE instrument = 'guitar'"
}

The replacement is a CREATE OR REPLACE VIEW, so the new SELECT must keep the existing columns with the same names, types and order; new columns go at the end. A definition that drops, renames or retypes a column is 400 (INVALID_DEFINITION) — drop the view and create it again instead.

The definition of a materialized view cannot be replaced: the request is rejected with 409 (MATERIALIZED_VIEW_NOT_REPLACEABLE). Drop it and create it again, then refresh it.

Drop and rename a view​

Use the Table API with the view's name: DELETE /api/v4/schemas/{schema}/tables/{view} drops it and PATCH renames it or sets its comment. Columns and privileges of a view are handled as for a table.

Drop a view
DELETE https://api.gc2.io/api/v4/schemas/rockhall/tables/guitarist_count HTTP/1.1
Authorization: Bearer abc123

Materialized views​

Set materialized to true when creating. It can only be set on create.

Request
POST https://api.gc2.io/api/v4/schemas/rockhall/views HTTP/1.1
Content-Type: application/json
Authorization: Bearer abc123

{
"name": "inductees_per_year",
"definition": "SELECT year, count(*) AS total FROM rockhall.inductees GROUP BY year",
"materialized": true
}
Created without data

A materialized view is always created without data and cannot be read until it has been refreshed — SQL, WMS and WFS requests on it fail. populated is false until the first refresh has run. Queue a refresh and wait for it to succeed before using the view.

A materialized view is a stored copy: changes in the underlying tables reach it only through a new refresh.

Refresh jobs​

A refresh can take a long time, so it runs as a background job. A job object:

FieldDescription
uuidThe job id.
schema, viewThe materialized view.
concurrentlyWhether the view is refreshed without blocking readers.
statuspending, running, succeeded, failed or cancelled.
staletrue when a running job has not been heard from for five minutes. It is failed after ten.
errorWhy a failed job failed: PostgreSQL's message, worker stopped or stale: no heartbeat from <host>. Otherwise null.
usernameWho queued the job.
hostThe node that runs or ran the job.
created, started, finished, heartbeat, cancel_requestedTimestamps.
_linksself (the job) and view (the view).

Queue a refresh​

POST names the schema and the view and answers 202 Accepted with the job and a Location header. The request only queues the refresh: a worker on the server picks the job up within about a minute, so pending for that long is normal.

Request
POST https://api.gc2.io/api/v4/views/refreshes HTTP/1.1
Content-Type: application/json
Authorization: Bearer abc123

{
"schema": "rockhall",
"view": "inductees_per_year"
}
Response 202
{
"uuid": "0b8a3f0e-5a0c-4d5e-9d0b-2f1f6f0a7c11",
"schema": "rockhall",
"view": "inductees_per_year",
"concurrently": false,
"status": "pending",
"stale": false,
"error": null,
"username": "axl",
"host": null,
"created": "2026-12-04 09:15:00.482913+00",
"started": null,
"finished": null,
"heartbeat": null,
"cancel_requested": null,
"_links": {
"self": "/api/v4/views/refreshes/0b8a3f0e-5a0c-4d5e-9d0b-2f1f6f0a7c11",
"view": "/api/v4/schemas/rockhall/views/inductees_per_year"
}
}

The body can also be an array of such objects; the response is then an array of jobs, and the list is queued as a whole or not at all.

A view can have one queued or running refresh at a time: a second request is rejected with 409 (JOB_ACTIVE). An unknown relation is 404 (VIEW_NOT_FOUND), and a plain view or a table is 400 (NOT_MATERIALIZED).

Refresh concurrently​

Without concurrently the view is locked for reads while the refresh runs. "concurrently": true refreshes without blocking readers (REFRESH MATERIALIZED VIEW CONCURRENTLY). PostgreSQL requires two things for that:

  • a unique index on the view, and
  • a view that already holds data — so the first refresh cannot be concurrent.

A job that asks for a concurrent refresh that cannot be run that way is queued and then ends as failed, with PostgreSQL's reason in error.

Request
POST https://api.gc2.io/api/v4/views/refreshes HTTP/1.1
Content-Type: application/json
Authorization: Bearer abc123

{
"schema": "rockhall",
"view": "inductees_per_year",
"concurrently": true
}

Follow a refresh​

GET /api/v4/views/refreshes/{uuid} returns one job, or an array for a comma-separated list of uuids. Poll it until status is succeeded, failed or cancelled. An unknown uuid is 404 (JOB_NOT_FOUND).

Request
GET https://api.gc2.io/api/v4/views/refreshes/0b8a3f0e-5a0c-4d5e-9d0b-2f1f6f0a7c11 HTTP/1.1
Accept: application/json; charset=utf-8
Authorization: Bearer abc123

GET /api/v4/views/refreshes lists the jobs of the database, newest first. Filter with the query parameters status, schema and view; an unknown status is 400 (INVALID_REQUEST).

Running refreshes in a schema
GET https://api.gc2.io/api/v4/views/refreshes?status=running&schema=rockhall HTTP/1.1
Accept: application/json; charset=utf-8
Authorization: Bearer abc123

Finished jobs are kept for 30 days by default.

Cancel a refresh​

DELETE /api/v4/views/refreshes/{uuid} (or a comma-separated list) cancels:

  • A pending job is cancelled at once and the response is 204 No Content. A job that has already finished is left as it is, also with 204.
  • A running refresh is stopped by its worker within seconds. The response is 202 Accepted — poll the job until its status is cancelled. The view keeps the data it had.

The job itself stays in the list.

Request
DELETE https://api.gc2.io/api/v4/views/refreshes/0b8a3f0e-5a0c-4d5e-9d0b-2f1f6f0a7c11 HTTP/1.1
Authorization: Bearer abc123
Response 202 — a running refresh is being stopped
{
"success": true,
"message": "Stopping",
"uuid": ["0b8a3f0e-5a0c-4d5e-9d0b-2f1f6f0a7c11"],
"_links": {
"self": "/api/v4/views/refreshes/0b8a3f0e-5a0c-4d5e-9d0b-2f1f6f0a7c11"
}
}
Self-hosting

Refresh jobs are run by a cron worker (app/scripts/view_refresh_worker.php). The viewRefresher settings are maxConcurrent (runs per node, default 1), maxHours (default 12; a longer run is stopped and fails) and keepDays (default 30).

Using the SDK​

The @mapcentia/gc2-sdk exposes both resources through the Views class. Errors are thrown as CentiaApiError with .status and .code:

import { createCentiaClient, Views } from '@mapcentia/gc2-sdk'

const views = new Views(createCentiaClient({ baseUrl, auth: { getAccessToken } }))

// Create one view, or several in one transaction (created in order)
await views.postView('rockhall', {
name: 'guitarists',
definition: "SELECT id, name FROM rockhall.inductees WHERE instrument = 'guitar'",
})

// All views of a schema (array), one view (object) or several (array)
const all = await views.getView('rockhall')
const one = await views.getView('rockhall', 'guitarists')
const some = await views.getView('rockhall', ['guitarists', 'guitarist_count'])

// Replace the definition of a plain view; existing columns must be kept
await views.patchView('rockhall', 'guitarists', {
definition: "SELECT id, name, year FROM rockhall.inductees WHERE instrument = 'guitar'",
})

// A materialized view is created without data: queue a refresh and poll it
await views.postView('rockhall', {
name: 'inductees_per_year',
definition: 'SELECT year, count(*) AS total FROM rockhall.inductees GROUP BY year',
materialized: true,
})
const job = await views.postViewRefresh({ schema: 'rockhall', view: 'inductees_per_year' })
const current = await views.getViewRefresh(job.uuid) // status: pending | running | succeeded | failed | cancelled

// List jobs, optionally filtered by status, schema and view
const running = await views.getViewRefreshes({ status: 'running', schema: 'rockhall' })

// Cancel: null when cancelled outright or already finished (204),
// the stopping info when a running refresh is being stopped (202)
await views.deleteViewRefresh(job.uuid)

Fields​

FieldDescription
nameView name, without schema. Letters, digits and underscore, at most 63 characters, not starting with a digit. Required when creating.
definitionThe SELECT the view is made of. Required when creating and on PATCH. On read: as PostgreSQL stores it.
materializedtrue for a materialized view. Default false. Set on create only.
populatedRead-only. null for a plain view; for a materialized view false until its first refresh has run.
_linksRead-only. self (the view) and table (the same relation in the Table API).

Status codes​

StatusMeaning
400INVALID_NAME — the name is not a valid identifier; INVALID_DEFINITION — PostgreSQL rejected the definition (its message is included); INPUT_VALIDATION_ERROR — malformed body; NOT_MATERIALIZED — a refresh of a relation that is not a materialized view; INVALID_REQUEST — a malformed uuid or an unknown status filter.
403SUPER_USER_ONLY — the caller is not the database owner.
404VIEW_NOT_FOUND — unknown schema, or a name that is not a view in it; JOB_NOT_FOUND — unknown refresh job.
409RELATION_EXISTS — a relation of that name exists in the schema; MATERIALIZED_VIEW_NOT_REPLACEABLE — PATCH on a materialized view; JOB_ACTIVE — the view already has a queued or running refresh.