Docs

postgres/fragments.sql

fragments.sql — shared {{ define }} blocks for all postgres history templates

fragments.sql — shared {{ define }} blocks for all postgres history templates

Context fields  

  • .BusinessCols
  • .CompareScope
  • .EventTimeCol
  • .HasEventTime
  • .HistoryTable
  • .PKColumns
  • .RunID
  • .RunStartedAt

Template  

{{- /* fragments.sql — shared {{ define }} blocks for all postgres history templates */ -}}

{{- define "business_col_list" -}}
{{- range $i, $col := .BusinessCols }}{{ if $i }}, {{ end }}"{{ $col }}"{{ end -}}
{{- end -}}

{{- define "valid_from_expr" -}}
{{- if .HasEventTime -}}stg."{{ .EventTimeCol }}"::timestamptz
{{- else -}}'{{ .RunStartedAt }}'::timestamptz
{{- end -}}
{{- end -}}

{{- /* Universal amf_* system columns — used by all history modes (DESIGN-012). */ -}}
{{- define "amf_system_cols" -}}
amf_run_id, amf_inserted_at, amf_updated_at, amf_valid_from, amf_valid_to, amf_hash_primary_key, amf_hash_source_row_key
{{- end -}}

{{- define "amf_system_vals" -}}
{{ .RunID }}, NOW(), NOW(), {{ template "valid_from_expr" . }}, NULL, stg.amf_hash_primary_key, stg.amf_hash_source_row_key
{{- end -}}

{{- define "pk_join_condition" -}}
{{- range $i, $col := .PKColumns }}{{ if $i }} AND {{ end }}stg."{{ $col }}" = hst."{{ $col }}"{{ end -}}
{{- end -}}

{{- /* Compare scope (ADR-161). compare_source is the SDK's relation of compare rows: the
     _transform view under full, read where it stands as before. scd2 and snapshot read
     the compare twice, to close and then to insert, so under any other scope it is
     computed once per apply into a temporary table: compare_prepare fills it, compare_rows
     names it and compare_release drops it. The table is filled inside a DO block, which
     counts as neither inserted nor updated: the SDK counts the writes by statement kind. */ -}}
{{- define "compare_prepare" -}}
{{- if ne .CompareScope "full" -}}
DO $compare$
BEGIN
    DROP TABLE IF EXISTS pg_temp."{{ .HistoryTable }}_compare";
    CREATE TEMP TABLE "{{ .HistoryTable }}_compare" AS
    SELECT * FROM {{ template "compare_source" . }} cmp;
    ANALYZE pg_temp."{{ .HistoryTable }}_compare";
END
$compare$;

{{ end -}}
{{- end -}}

{{- define "compare_rows" -}}
{{- if eq .CompareScope "full" -}}{{ template "compare_source" . }}
{{- else -}}pg_temp."{{ .HistoryTable }}_compare"
{{- end -}}
{{- end -}}

{{- define "compare_release" -}}
{{- if ne .CompareScope "full" }};

DROP TABLE pg_temp."{{ .HistoryTable }}_compare"
{{- end -}}
{{- end -}}

Provenance  

Platformpostgres
Filepostgres/fragments.sql
Versionv1.3.1
Lines57
SHA-2569b5ea33dfeb0edecf75d34d2c6e2765a1d500d54b723138b703604a0e8607aa4

Last updated on October 11, 2026