ETLMCPData Conveyance Works
Log in Start free
Guides

dbt incremental models on top of etlmcp tables

How far back a dbt incremental model over an etlmcp PostgreSQL table must reprocess, which strategy to use, and why unique_key columns must not be null.

A dbt incremental model built on an etlmcp table must reprocess, on every run, a trailing window of dates at least as long as the window etlmcp re-pulls for the platforms in that table. etlmcp rewrites recent days on every routine sync because ad platforms keep correcting them, so a model that only picks up dates newer than its own latest date keeps the first, unfinished figures for ever. Reprocess by date with the delete+insert strategy, and if you key on row columns instead, make sure none of the unique_key columns can be null.

What etlmcp changes under your model

Each routine sync pulls the last few days again and replaces them in your PostgreSQL table. The number of days depends on the platform: the default is 7 days, and a platform that restates figures for longer gets its longer window.

Platform Days each routine sync re-pulls
Google Ads 31
Microsoft Advertising 31
LinkedIn 31
Meta 29
TikTok 29
GA4 13
Shopify 58
Pinterest 7
Snapchat 7

The trailing sync window explains where these come from, and each connector page says whether that connector is available on your server. Two cases reach further back than the table shows. After a source needed reconnecting, the next sync starts from where the last good sync's own window started, so the days it missed are covered too. And a sync you start by hand for an older range, a backfill, rewrites whatever dates you asked for.

A PostgreSQL run deletes the rows that carry its pipeline's ID in the _etlmcp_pipeline_id column and fall in the run's date range, then inserts the new rows. A row for an ad with no figures left on a day is gone after the sync, and a run that returns no rows still clears its range. How PostgreSQL rows are replaced has the details.

Pick the lookback

Use the longest window among the platforms that write into the table your model reads. A table fed only by Meta needs at least 29 days; a shared table that also takes Shopify needs at least 58. Add a day or two of margin: etlmcp counts its window back from the pipeline's local date, in the pipeline's own timezone, and your dbt job may run on a different calendar day.

Anchor the filter on the latest date already in your model ({{ this }}) rather than on today. If dbt skips a few days, the window then still starts before the first day it missed.

Use delete+insert keyed on the date

dbt's Postgres adapter supports the append, merge, delete+insert and microbatch strategies. When you set a unique_key and no strategy, it uses delete+insert. That strategy removes the target rows whose key appears in the new batch and then inserts the batch. dbt's strategy page notes that it also suits a unique_key that is not truly unique. Keying on the pipeline and the date mirrors what etlmcp does, so each date in the window is replaced as a whole and rows that vanished at the source vanish from your model too.

{{ config(
    materialized='incremental',
    incremental_strategy='delete+insert',
    unique_key=['_etlmcp_pipeline_id', 'date']
) }}

select
    _etlmcp_pipeline_id,
    date,
    network,
    account_id,
    campaign_id,
    campaign_name,
    coalesce(adgroup_id, '') as adgroup_id,
    coalesce(ad_id, '') as ad_id,
    impressions,
    clicks,
    cost,
    conversions,
    conversion_value,
    currency
from {{ source('etlmcp', 'ad_performance') }}
{% if is_incremental() %}
where date >= (select max(date) from {{ this }}) - 31
{% endif %}

Replace ad_performance with the table name you gave the destination, and the number of days with the longest window for your table. Neither key column should be null: etlmcp creates _etlmcp_pipeline_id as NOT NULL, and the Google Ads connector, for one, refuses a report row without a date. In PostgreSQL, subtracting a whole number from a date gives a date, so the filter needs no interval. Platform extras keep dotted column names such as extra. followed by the field name; select them in double quotes.

One gap remains: if a whole date loses every row of a pipeline, the batch holds no key for that date and the old rows stay in your model. A periodic dbt run --full-refresh, which drops the model's table and builds it again, clears that, and it is also the simplest way to pick up a backfill older than your lookback.

If you key on rows: no nulls in unique_key

You may prefer merge with a row-level key such as ['_etlmcp_pipeline_id', 'date', 'campaign_id', 'adgroup_id', 'ad_id']. dbt's incremental guide recommends passing several columns as a list rather than a concatenated string, and warns that none of them may contain nulls, otherwise rows fail to match and duplicates appear. etlmcp's ID columns do hold nulls: a Google Ads row reported at campaign level, for instance, has no ad group or ad, and the connector writes those IDs as null rather than empty text. Wrap each such column in coalesce(…, '') in your select, as the example above does, or build a single surrogate key, as dbt suggests.

A merge also never deletes. An ad that drops out of a day at the platform keeps its old row in your model until a full refresh. Prefer delete+insert by date.

Related

Sources

  1. Configure incremental models — docs.getdbt.com, checked
  2. About incremental strategy — docs.getdbt.com, checked
  3. Postgres configurations — docs.getdbt.com, checked
  4. 9.9. Date/Time Functions and Operators — www.postgresql.org, checked