SQL Server data to PostgreSQL on insert sql trigger

352 Views Asked by At

I am trying to move data from MS SQL Server (2014) to PostgreSQL (12.6) by using the psqlodbc(12_02_0000-x64) odbc driver and Postgresql as linked server in SSMS on my local machine.

The goal is move data inserted in a SQL Server table to a PGSQL table by using an insert trigger on the sql server table. But I always get a MS Distributed Transaction Coordinator error:

Msg 8522, Level 18, State 1, Line 1 Microsoft Distributed Transaction Coordinator (MS DTC) has stopped this transaction.

A simple insert into .. select works fine:

INSERT INTO [POSTGRESQL].[MyPgDb].[MyPgSchema].[MyPgTable](Id, StartTime, EndTime, TestMessage)
(SELECT [Id], [StartTime], [EndTime], [TestMessage] FROM [MySqlDb].[dbo].[MySqlTable]);

This is the trigger I am using:

USE [MySqlDb]
GO
/****** Object:  Trigger [dbo].[TriggerInsertMsg]    Script Date: 02.07.2021 09:15:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[TriggerInsertMsg]
    ON [MySqlDb].[dbo].[MySqlTable]
    AFTER INSERT
    AS
    BEGIN TRAN
    SET NOCOUNT ON
    -- SET XACT_ABORT ON??
        INSERT INTO [POSTGRESQL].[MyPgDb].[MyPgSchema].[MyPgTable](Id, StartTime, EndTime, TestMessage)
        (SELECT [inserted].Id, [inserted].StartTime, [inserted].EndTime, [inserted].TestMessage FROM INSERTED)
    COMMIT;

I tried the following actions without any success:

  • Using BEGIN TRAN.. COMMIT; instead of BEGIN..END in the trigger
  • Starting MSDTC service
  • Enabling MSDTC in firewall
  • Enabling XA Transactions in dcomcnfg
  • using SET XACT_ABORT ON

The trigger works fine when I replace the linked pg server by an other SQL table. Do I have to adjust some PostgreSQL configuration to enable transactions coming from the SQL server and/or MS DTC? Where can I find the reference of the error (Msg 8522, Level 18, State 1, Line 1)?

0

There are 0 best solutions below