ETLMCPData Conveyance Works
Log in Start free
Guides

Indexes for the etlmcp PostgreSQL table: faster loads and date filters

Which index to add to the table etlmcp loads in PostgreSQL, how to build it without blocking a run, and why it is safe for etlmcp's loads.

Add a B-tree index on (_etlmcp_pipeline_id, date) to the table etlmcp loads, and build it with CREATE INDEX CONCURRENTLY while logged in as the role that owns the table. etlmcp creates its table with no index at all, so once the table grows, every run's delete and every dashboard date filter reads the whole table. The index fixes both, and etlmcp's loads keep working with it in place.

What etlmcp creates

When the table does not exist yet, etlmcp creates it with the columns the rows deliver plus one more, _etlmcp_pipeline_id, a text column that marks which pipeline wrote each row. It adds no primary key and no index. See the PostgreSQL destination for how it names and types the columns.

Why the delete gets slow

Each run first deletes its own rows for the dates it covers, then inserts the new ones, in one transaction. The delete keeps to rows where _etlmcp_pipeline_id equals the pipeline's id and date falls within the run's date range, widened to cover every date in the delivered rows. Row replacement explains how that range is chosen.

Without an index, PostgreSQL has to read every row of the table to find the ones that match. On a table that holds years of daily rows for many pipelines, that happens on every run. etlmcp also sets a statement timeout on each session it opens, so a statement that runs too long fails instead of waiting for ever.

Which index to add

PostgreSQL's documentation explains that in a multicolumn B-tree index, equality conditions on the leading columns plus a range condition on the next column limit the part of the index that gets scanned. etlmcp's delete is exactly that shape: equality on the pipeline id, then a range on the date.

CREATE INDEX CONCURRENTLY etlmcp_pipeline_date_idx
    ON public.ad_spend (_etlmcp_pipeline_id, date);

Use your own schema and table name. Put _etlmcp_pipeline_id first: with date first, the range comes before the equality and the scan is not limited the same way.

Dashboards and BI tools usually filter only by date, without naming a pipeline. For those queries a single-column index on date is usually enough:

CREATE INDEX CONCURRENTLY etlmcp_date_idx ON public.ad_spend (date);

Build it without blocking a run

A plain CREATE INDEX lets reads continue but blocks inserts, updates and deletes until the build ends, which on a big table can take a long time. A run that starts meanwhile waits for it. CONCURRENTLY lets writes continue, at the cost of a slower build that scans the table twice.

  • It cannot run inside a transaction block. If your SQL client wraps statements in a transaction, turn that off for this statement.
  • If the build fails, it leaves an invalid index behind. PostgreSQL ignores it for queries but still updates it on every write. In psql, \d marks it INVALID. Drop it with DROP INDEX CONCURRENTLY and build it again, or rebuild it with REINDEX INDEX CONCURRENTLY.

Which role should run it

PostgreSQL makes the role that ran CREATE TABLE the table's owner, so the owner is etlmcp's login. Run the index build as that login, or as a role that is a member of it. Do not hand the table to another owner: when a platform delivers a new extra.* column, etlmcp adds it with ALTER TABLE, and PostgreSQL lets only the table's owner (or a superuser) run ALTER TABLE. If etlmcp loses ownership, a run that needs a new column fails with a missing-columns error. PostgreSQL privileges for the etlmcp user covers the grants.

Effect on etlmcp's loads

etlmcp learns a table's columns from information_schema.columns, which lists columns only, so your indexes do not change how it checks or loads the table. The cost is write work: PostgreSQL keeps every index in step with the table, so each one adds time to every insert and delete a run makes. Keep the indexes your queries use and drop the rest.

Related

Sources

  1. 11.1. Introduction — www.postgresql.org, checked
  2. 11.3. Multicolumn Indexes — www.postgresql.org, checked
  3. CREATE INDEX — www.postgresql.org, checked
  4. 5.9. Privileges — www.postgresql.org, checked
  5. ALTER TABLE — www.postgresql.org, checked