# Incremental merge on Postgres

**URL:** <https://discourse.getdbt.com/t/incremental-merge-on-postgres/6303>\
**Category:** Help\
**Tags:** incremental, postgres\
**Created:** [January 23, 2023, 1:20pm UTC](https://discourse.getdbt.com/t/incremental-merge-on-postgres/6303 "2023-01-23T13:20:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![righner](https://avatars.discourse-cdn.com/v4/letter/r/7ea924/32.png) [@righner](https://discourse.getdbt.com/u/righner)\
**Post date:** [January 23, 2023, 1:20pm UTC](https://discourse.getdbt.com/t/incremental-merge-on-postgres/6303/1 "2023-01-23T13:20:21Z")

</div>

## The problem I’m having

I have a table core.table with historical data that is to be updated from a table stage.table.  
I want to merge the new data into the historical table. So it should update existing records or add new ones.  
Postgres only seems to support delete+insert and append. Since I also want to update existing records, I need to use delete+insert. (Also, I do not understand why merge is not supported, since it is an available command for postgres??)  
However, the problem is that is seems to simply delete ALL records in the historical table, instead of just the those that are to be updated…

I am not doing a --full-refresh every time…

## The context of why I’m trying to do this

Postgres 15  
dbt 1.3.1

## Some example code or error messages

```auto
{{
    config(
        materialized='incremental',
        unique_key="'GUID'", 
        on_schema_change='fail'
    )
}}

SELECT * FROM {{ source('stage', 'table') }}

```

---

<div class="post-metadata">

**Author:** ![a\_slack\_user](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/a_slack_user/32/1629_2.png) [@a\_slack\_user](https://discourse.getdbt.com/u/a_slack_user)\
**Post date:** [January 23, 2023, 1:29pm UTC](https://discourse.getdbt.com/t/incremental-merge-on-postgres/6303/2 "2023-01-23T13:29:54Z")

</div>

To your minor question: `merge` command is new to Postgres as of [v15.0](https://www.postgresql.org/docs/release/15.0/) (October 2022). I don’t see an enhancement request to add `merge` incremental strategy to Postgres in the [open dbt enhancement requests](https://github.com/dbt-labs/dbt-core/issues), but that would be an excellent submission.

To your major question: do you see the “delete all records” behavior each time you run your model?

Note: `@Owen` originally [posted this reply in Slack](https://getdbt.slack.com/archives/CBSQTAPLG/p1674480587509899?thread_ts=1674480053.735299&cid=CBSQTAPLG). It might not have transferred perfectly.

---

<div class="post-metadata">

**Author:** ![righner](https://avatars.discourse-cdn.com/v4/letter/r/7ea924/32.png) [@righner](https://discourse.getdbt.com/u/righner)\
**Post date:** [January 23, 2023, 1:34pm UTC](https://discourse.getdbt.com/t/incremental-merge-on-postgres/6303/3 "2023-01-23T13:34:19Z")

</div>

Hi a\_slack\_user!  
thank you for your fast answer. Yes, I tested it by staging different subsets of data and always only this subset ends up in the core table. So it basically behaves like a regular table materialization.
