ETLMCPData Conveyance Works
Log in Start free
Guides

Querying the etlmcp table during a load: what dashboards see and what blocks

Why a dashboard never sees a half-loaded range from etlmcp, when it waits for a load, and which locks of yours can make a PostgreSQL run time out.

A dashboard that queries your etlmcp table during a run never sees a half-replaced day: etlmcp swaps the rows in one transaction, and PostgreSQL shows other sessions only committed data. Two things can still collide. A run that adds a column locks the table so hard that even a SELECT waits for the load to finish, and your own long queries or index builds can make a run wait until its statement timeout cancels it.

What a run does inside one transaction

etlmcp opens one session for a load, removes this pipeline's rows in the run's date range, inserts the new rows, and commits only at the end. The delete touches only rows that carry the pipeline's own ID, so other pipelines writing to the same table keep theirs. If any step fails, or the session runs past etlmcp's time limit, the connection is closed without a commit, PostgreSQL rolls the transaction back, and the rows from the last good run stay in place. See how rows are replaced for the date range a run covers.

What a dashboard sees

PostgreSQL uses Read Committed isolation unless you change it. At that level each query reads a snapshot taken when it starts and never sees another session's uncommitted changes. So while a run is between its delete and its commit, your dashboard still gets the old rows. After the commit, the next query gets the new ones. There is no moment in which a range is empty or half full. One caveat: two queries on the same page run at slightly different moments, so one tile can show the data before a commit and the next tile the data after it. Refresh once the run ends if the numbers must agree.

When a dashboard waits for a load

When the rows of a run carry an extra.* field the table does not have yet, etlmcp adds the column with ALTER TABLE ... ADD COLUMN IF NOT EXISTS, inside the same transaction as the load. It does the same for a few columns of its own making, such as id, when an older table lacks them. Most forms of ALTER TABLE take an ACCESS EXCLUSIVE lock, the only table lock that blocks a plain SELECT, and PostgreSQL keeps a table lock until the transaction ends. From that statement until the commit or rollback, every query on that table queues behind the load. A dashboard that hangs only on runs that bring a new field is most likely waiting on this lock. It resumes as soon as the run commits or fails, and etlmcp ends any session that goes past its time limit.

Runs that add no column take only the ROW EXCLUSIVE lock that DELETE and INSERT need. That lock does not block reads.

When your query makes a run fail

The wait works in the other direction too. If a long report query holds its read lock when etlmcp issues its ALTER TABLE, etlmcp's statement waits for the report to finish. etlmcp sets a statement_timeout on every connection, and PostgreSQL counts that timeout from the moment a statement arrives, so time spent waiting for a lock counts against it. When it expires, PostgreSQL cancels the statement, the transaction rolls back, and etlmcp treats the error as one that can pass and retries the run, up to 3 attempts in all.

If runs keep failing with a cancelled statement, look for queries that run for minutes against the etlmcp table, and move them to a copy or to hours when no pipeline runs.

Index builds and other maintenance

A plain CREATE INDEX takes a SHARE lock, which conflicts with the ROW EXCLUSIVE lock of the run's DELETE and INSERT. A run that starts during the build waits for it, and can hit the same statement timeout. Build indexes with CREATE INDEX CONCURRENTLY, which does not block writes, or do the work outside the hour your pipelines run. The same goes for VACUUM FULL, CLUSTER, TRUNCATE and most forms of your own ALTER TABLE, which take ACCESS EXCLUSIVE. See indexes for the etlmcp table for which ones help, and PostgreSQL destination errors for the messages a failed run shows.

Related

Sources

  1. PostgreSQL: Transaction Isolation — www.postgresql.org, checked
  2. PostgreSQL: Explicit Locking — www.postgresql.org, checked
  3. PostgreSQL: ALTER TABLE — www.postgresql.org, checked
  4. PostgreSQL: Client Connection Defaults — www.postgresql.org, checked