Rerunning a sync into PostgreSQL does not duplicate rows. Every row etlmcp writes carries the ID of the pipeline that wrote it in a column named _etlmcp_pipeline_id. Before a run inserts anything, it deletes the rows that carry its own pipeline ID and fall inside the run's date range, then inserts the fresh rows for that range. The delete and the insert happen in one transaction, so your table shows either the old rows or the new ones, never both. Because each run touches only its own pipeline's rows, several pipelines can write into the same table without overwriting each other.
What one run does
When a pipeline delivers to PostgreSQL, etlmcp works through these steps in a single database session:
- It reads the table's columns. If the table does not exist, it creates it, with one column per field in the rows plus
_etlmcp_pipeline_id. - It works out the date range to replace: the dates the run asked for, widened to cover any date that actually appears in the rows.
- It deletes the rows where
_etlmcp_pipeline_idis this pipeline's ID anddatefalls in that range. Rows of other pipelines, and this pipeline's rows outside the range, stay as they are. - It inserts the new rows and commits.
If anything fails before the commit, PostgreSQL rolls the whole transaction back and the rows from the last good run stay in place. A retry or a second run of the same dates therefore replaces what the earlier attempt delivered, even when that attempt stopped half-way. See retries for when etlmcp runs a failed sync again by itself.
A run that brings back no rows still clears its range when the table already has a date column, so a day whose figures dropped to nothing at the platform also disappears from your table.
Why etlmcp deletes instead of upserting
A plain PostgreSQL INSERT adds new rows and never replaces one already there. To update a matching row instead, you add an ON CONFLICT clause, and PostgreSQL only spots such a conflict through a unique index or constraint on the table. etlmcp does not rely on either: it deletes its own range first and then runs a plain insert. You therefore do not need a primary key or unique index on the table for reruns to stay clean, and you may add indexes of your own to speed up reporting queries.
Rows without a date
If the pipeline's rules remove the date column, for example a group rule that totals by campaign only, a run has no range to replace. In that case it deletes all of the pipeline's rows in the table and sends the totals for the whole stored history, so the table again holds one current set of rows for that pipeline.
Sharing one table between pipelines
You can point several pipelines, say one per ad account, at the same schema and table. Each run deletes only rows that carry its own pipeline ID, so a Meta pipeline rerunning last week never removes the Google rows for those days. To report on one pipeline, filter on _etlmcp_pipeline_id.
The pipelines must agree on columns. When a run brings a column the table lacks:
- A platform extra (a column whose name starts with
extra.) is added to the table as it appears. - A few columns that the pipeline's rules bring by themselves, such as the
roascolumn a group rule works out, are added when the login may alter the table, and left out of the rows when it may not. - Any other missing column, for example one a derive rule creates after the table was made, stops the run with
table {…}.{…} is missing columns: {…}. The same message appears when an extra cannot be added because the login does not own the table.
etlmcp also refuses to write to a table it did not create. A table without the _etlmcp_pipeline_id column fails with table {…}.{…} was not created by etlmcp; choose a new table name. Pick a new table name and let the first run create it. The column name _etlmcp_pipeline_id is reserved: a run whose rows carry a column of that name stops with column name {…} is reserved.
Column names longer than 63 bytes
PostgreSQL uses at most 63 bytes of a name in a default build and cuts anything longer, so two long names that start the same way could end up as one column. Platform extras keep their dotted names, such as extra. followed by the platform's field name, and you need double quotes around them in SQL. When an extra's name is longer than 63 bytes, etlmcp keeps the start of the name and ends it with an underscore and a short hash of the full name. The hash is the same on every run, so the column stays put, and two long extras that start the same way still get different names.
Other columns are not shortened. A column longer than the limit, for instance one a derive rule names, stops the run with column name too long (max {…}): {…}. Shorten the name in the rule.
Bytes are not characters: a name with accented letters or non-Latin script reaches the limit with fewer characters than an English one.