Skip to content

pgtsql

provide Transact-SQL compatibility for PostgreSQL

pgtsql · provide Transact-SQL compatibility for PostgreSQL

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
100461pgtsqlpgtsql3.0LANGUnknownC

PostgreSQL · 11

Usage

Sources:

pgtsql 3.0 installs the pltsql procedural language and a small compatibility layer for Transact-SQL-style PostgreSQL functions and procedures. It is useful for porting limited procedural code that uses @ variables, T-SQL-like control flow, PRINT, and local # temporary tables. It is not a complete SQL Server compatibility layer or wire-protocol endpoint.

Core Workflow

Installing pgtsql requires a superuser because it registers a C language handler and validator. A minimal function can use T-SQL-style variable declarations and return syntax:

CREATE EXTENSION pgtsql;

CREATE FUNCTION tsql_value() RETURNS integer AS $$
DECLARE @value int = 120
BEGIN
    RETURN @value;
END
$$ LANGUAGE pltsql;

SELECT tsql_value();
SELECT sys.getdate(), sys.isnull(NULL::integer, 0);

Version 3.0 specifically targeted PostgreSQL 11 and added stored-procedure support. Pin this source and run its regression suite before trying it on a newer PostgreSQL release.

Installed Surface

  • Language: pltsql, backed by pltsql_call_handler and pltsql_validator.
  • Date/time helpers: sys.sysdatetime, sys.sysdatetimeoffset, sys.sysutcdatetime, sys.getdate, and sys.getutcdate.
  • Null helper: overloaded sys.isnull for a limited set of scalar types.
  • Catalog-like views: sys.tables, sys.views, and sys.objects expose selected PostgreSQL relations with SQL Server-shaped columns.

The parser supports selected conventions documented and tested upstream, including optional semicolons, @-prefixed variables, T-SQL-style IF/ELSE, PRINT, and # temporary-table rewriting. Test every migrated construct; unsupported T-SQL syntax and semantics are not emulated automatically.

Compatibility and Security Boundaries

Installation creates the sys schema unconditionally, so it can conflict with an existing schema. The extension grants PUBLIC usage on sys, selection from its compatibility views, and execution of its helper functions. Review those grants after installation.

The compatibility views contain placeholders for many SQL Server fields—for example epoch timestamps and constant flags—rather than equivalent server metadata. The project does not provide SQL Server authentication, TDS connectivity, full built-in coverage, SQL Server transaction behavior, or a complete system catalog. Treat pgtsql as a narrow PostgreSQL procedural-language porting aid and validate application behavior independently.

Table reader