/*
  * Set the state of a subscription table.
  *
+ * If update_only is true and the record for given table doesn't exist, do
+ * nothing.  This can be used to avoid inserting a new record that was deleted
+ * by someone else.  Generally, subscription DDL commands should use false,
+ * workers should use true.
+ *
  * The insert-or-update logic in this function is not concurrency safe so it
  * might raise an error in rare circumstances.  But if we took a stronger lock
  * such as ShareRowExclusiveLock, we would risk more deadlocks.
  */
 Oid
 SetSubscriptionRelState(Oid subid, Oid relid, char state,
-                       XLogRecPtr sublsn)
+                       XLogRecPtr sublsn, bool update_only)
 {
    Relation    rel;
    HeapTuple   tup;
-   Oid         subrelid;
+   Oid         subrelid = InvalidOid;
    bool        nulls[Natts_pg_subscription_rel];
    Datum       values[Natts_pg_subscription_rel];
 
     * If the record for given table does not exist yet create new record,
     * otherwise update the existing one.
     */
-   if (!HeapTupleIsValid(tup))
+   if (!HeapTupleIsValid(tup) && !update_only)
    {
        /* Form the tuple. */
        memset(values, 0, sizeof(values));
 
        heap_freetuple(tup);
    }
-   else
+   else if (HeapTupleIsValid(tup))
    {
        bool        replaces[Natts_pg_subscription_rel];
 
 
                                         rv->schemaname, rv->relname);
 
                SetSubscriptionRelState(subid, relid, table_state,
-                                       InvalidXLogRecPtr);
+                                       InvalidXLogRecPtr, false);
            }
 
            ereport(NOTICE,
        {
            SetSubscriptionRelState(sub->oid, relid,
                          copy_data ? SUBREL_STATE_INIT : SUBREL_STATE_READY,
-                                   InvalidXLogRecPtr);
+                                   InvalidXLogRecPtr, false);
            ereport(NOTICE,
                    (errmsg("added subscription for table %s.%s",
                            quote_identifier(rv->schemaname),
 
        SetSubscriptionRelState(MyLogicalRepWorker->subid,
                                MyLogicalRepWorker->relid,
                                MyLogicalRepWorker->relstate,
-                               MyLogicalRepWorker->relstate_lsn);
+                               MyLogicalRepWorker->relstate_lsn,
+                               true);
 
        walrcv_endstreaming(wrconn, &tli);
        finish_sync_worker();
                }
                SetSubscriptionRelState(MyLogicalRepWorker->subid,
                                        rstate->relid, rstate->state,
-                                       rstate->lsn);
+                                       rstate->lsn, true);
            }
        }
        else
                SetSubscriptionRelState(MyLogicalRepWorker->subid,
                                        MyLogicalRepWorker->relid,
                                        MyLogicalRepWorker->relstate,
-                                       MyLogicalRepWorker->relstate_lsn);
+                                       MyLogicalRepWorker->relstate_lsn,
+                                       true);
                CommitTransactionCommand();
                pgstat_report_stat(false);
 
                    SetSubscriptionRelState(MyLogicalRepWorker->subid,
                                            MyLogicalRepWorker->relid,
                                            SUBREL_STATE_SYNCDONE,
-                                           *origin_startpos);
+                                           *origin_startpos,
+                                           true);
                    finish_sync_worker();
                }
                break;
 
 } SubscriptionRelState;
 
 extern Oid SetSubscriptionRelState(Oid subid, Oid relid, char state,
-                       XLogRecPtr sublsn);
+                       XLogRecPtr sublsn, bool update_only);
 extern char GetSubscriptionRelState(Oid subid, Oid relid,
                        XLogRecPtr *sublsn, bool missing_ok);
 extern void RemoveSubscriptionRel(Oid subid, Oid relid);