KB-4CC8
Appendix — Raw SQL / DOT Commands (2026-05-29)
9 min read Revision 1
iuevidenceappendixsqldotreproduce
Appendix — Raw SQL / DOT Commands (reproduce the evidence)
All read-only queries run via query_pg (role context_pack_readonly). All mutating scripts run via ssh contabo "docker exec -i postgres psql -U workflow_admin -d directus -v ON_ERROR_STOP=1" < file.sql and are wrapped in BEGIN … ROLLBACK.
Live function signatures (verified via pg_proc)
fn_iu_reconstruct_source(p_doc_code text) -> TABLE(source_position,sort_order,section_type,section_code,canonical_address,depth,gap_before,iu_id) [STABLE]
fn_iu_filter_axis_b(p_tag_key text DEFAULT NULL, p_sample_limit int DEFAULT 10) -> jsonb [STABLE]
fn_iu_filter_axis_c_subtree(p_root uuid, p_sample_limit int DEFAULT 100) -> jsonb [STABLE]
fn_iu_subtree(p_root uuid) -> TABLE(iu_id,canonical_address,depth,parent_id,relative_depth) [STABLE]
fn_iu_sql_link_resolve_all() -> jsonb [STABLE] fn_iu_sql_link_validate(p_link_id uuid) -> jsonb [STABLE]
fn_iu_compose(p_collection_key text,p_collection_kind text,p_title text,p_description text,p_pieces jsonb,p_actor text) -> jsonb
fn_iu_collection_add_piece(p_collection_id uuid,p_iu_id uuid,p_piece_order int,p_piece_role text,p_actor text) -> jsonb
fn_iu_collection_remove_piece(p_collection_id uuid,p_iu_id uuid,p_actor text) -> jsonb
fn_iu_collection_reorder_piece(p_collection_id uuid,p_iu_id uuid,p_new_order int,p_actor text) -> jsonb
fn_iu_collection_render(p_collection_id uuid) -> setof record [STABLE]
fn_iu_piece_split(p_source_canonical_address text,p_child_specs jsonb,p_actor text,p_review_decision_id uuid,p_change_set_id uuid,p_reason text,p_tool_revision text,p_dry_run boolean) -> jsonb
fn_iu_piece_merge(p_merged_spec jsonb,p_source_canonical_addresses text[],p_actor text,p_review_decision_id uuid,p_change_set_id uuid,p_reason text,p_tool_revision text,p_dry_run boolean) -> jsonb
fn_iu_emit_event(p_event_type text,p_canonical_address text,p_subject_ref uuid,p_actor_ref text,p_safe_payload jsonb DEFAULT '{}') -> uuid
fn_iu_route_worker_run(p_worker_name text DEFAULT 'iu_outbound_default',p_batch_limit int DEFAULT 100) -> jsonb
fn_iu_route_dead_letter_replay(p_dead_letter_id uuid) -> jsonb
fn_iu_gate_open(p_gate_key text,p_approval_id uuid,p_actor text,p_reason text,p_ttl_seconds int DEFAULT 300) -> jsonb
fn_iu_gate_close(p_gate_key text,p_actor text,p_reason text) -> jsonb
fn_iu_gate_verify_closed(p_? text) -> jsonb fn_iu_test_review_decision_create(p_actor text,p_reason text,p_manifest_tag text) -> jsonb
A — reconstruct (read-only)
SELECT * FROM fn_iu_reconstruct_source('knowledge/dev/laws/dieu37-governance-organization-law.md') ORDER BY source_position;
-- ordering integrity + deterministic digest:
WITH r AS (
SELECT 'DIEU-37' doc,* FROM fn_iu_reconstruct_source('knowledge/dev/laws/dieu37-governance-organization-law.md')
UNION ALL SELECT 'DIEU-35',* FROM fn_iu_reconstruct_source('DIEU-35')),
uv AS (SELECT DISTINCT ON (unit_id) unit_id,content_hash,version_seq FROM unit_version ORDER BY unit_id,version_seq DESC)
SELECT r.doc,count(*) pieces,count(uv.content_hash) pieces_with_hash,
md5(string_agg(r.canonical_address||':'||uv.content_hash,'|' ORDER BY r.source_position)) reconstruction_digest
FROM r JOIN uv ON uv.unit_id=r.iu_id GROUP BY r.doc;
B / C / F — filters & links (read-only)
SELECT fn_iu_filter_axis_b('legal_domain:knowledge_systems', 5);
SELECT fn_iu_filter_axis_c_subtree('cb211ee6-2b61-496e-b191-ef502ea28345', 100);
SELECT * FROM fn_iu_subtree('cb211ee6-2b61-496e-b191-ef502ea28345') ORDER BY relative_depth, canonical_address;
SELECT fn_iu_sql_link_resolve_all();
D — compose/split/merge governed transaction (skeleton)
BEGIN;
SET LOCAL statement_timeout='60s'; SET LOCAL idle_in_transaction_session_timeout='120s';
-- 1. test review_decision (Đ32 test-scope)
SELECT fn_iu_test_review_decision_create('iu_demo_harness','A-F demo D','TEST/iu-demo'); -- returns review_decision_id
-- 2. open gates with that approval_id
SELECT fn_iu_gate_open('iu_core.composer_enabled', :rd, 'iu_demo_harness','demo',300);
SELECT fn_iu_gate_open('iu_core.structure_ops_enabled', :rd, 'iu_demo_harness','demo',300);
-- 3. compose (1 existing law piece by iu_id + 3 minted demo bricks)
SELECT fn_iu_compose('TEST/iu-demo/compose-1','document','IU A-F Demo Composed Document','...',
jsonb_build_array(
jsonb_build_object('iu_id',(SELECT id FROM information_unit WHERE canonical_address='DIEU-37-v3.3#title'),'role','intro'),
jsonb_build_object('role','body','new_piece',jsonb_build_object('canonical_address','TEST/iu-demo/brick-alpha','title','Demo Brick Alpha','body','...','unit_kind','design_doc_section','section_type','paragraph')),
/* brick-beta, brick-gamma similarly */ ),
'iu_demo_harness');
-- 4. add / remove / reorder
SELECT fn_iu_collection_add_piece(:coll,(SELECT id FROM information_unit WHERE canonical_address='DIEU-37-v3.3#goal'),4,'appendix','iu_demo_harness');
SELECT fn_iu_collection_remove_piece(:coll,(SELECT id FROM information_unit WHERE canonical_address='DIEU-37-v3.3#title'),'iu_demo_harness');
SELECT fn_iu_collection_reorder_piece(:coll,(SELECT id FROM information_unit WHERE canonical_address='TEST/iu-demo/brick-beta'),0,'iu_demo_harness');
-- 5. split (children specs: {canonical_address,title,body,unit_kind,section_type} x2)
SELECT fn_iu_piece_split('TEST/iu-demo/brick-gamma', jsonb_build_array(/*gamma-1*/,/*gamma-2*/),'iu_demo_harness', :rd);
-- 6. merge (merged_spec with EXPLICIT body; 2 distinct sources)
SELECT fn_iu_piece_merge(jsonb_build_object('canonical_address','TEST/iu-demo/brick-merged','title','Merged Alpha+Beta','body','<explicit>','unit_kind','design_doc_section','section_type','paragraph'),
ARRAY['TEST/iu-demo/brick-alpha','TEST/iu-demo/brick-beta'],'iu_demo_harness', :rd);
-- 7. render
SELECT fn_iu_collection_render(:coll);
SELECT fn_iu_gate_close('iu_core.structure_ops_enabled','iu_demo_harness','done');
SELECT fn_iu_gate_close('iu_core.composer_enabled','iu_demo_harness','done');
ROLLBACK;
E — trigger in/out + DLQ governed transaction (skeleton)
BEGIN;
SET LOCAL statement_timeout='60s'; SET LOCAL idle_in_transaction_session_timeout='120s';
-- emit (refs-only payload)
SELECT fn_iu_emit_event('structure_piece_split','TEST/iu-demo/brick-gamma','<subject uuid>','iu_demo_harness',
jsonb_build_object('child_count',2,'child_refs',jsonb_build_array('...gamma-1','...gamma-2'),'note','refs-only'));
-- isolate a test worker to only the new event, then run it (dry_run route -> no delivery)
INSERT INTO iu_route_worker_cursor (worker_name,event_domain,last_created_at,last_event_id)
SELECT 'iu_demo_e_worker','iu',eo.created_at,eo.id FROM event_outbox eo
WHERE eo.event_domain='iu' AND eo.id<>'<emitted id>' ORDER BY eo.created_at DESC,eo.id DESC LIMIT 1;
SELECT fn_iu_route_worker_run('iu_demo_e_worker',10);
-- forced DLQ rows (one unrouted, one dry_run route) then replay each
INSERT INTO iu_route_dead_letter(worker_name,event_ref,event_domain,event_type,event_stream,route_code,idempotency_key,failure_code,failure_detail,attempts,event_snapshot) VALUES (...);
SELECT fn_iu_route_dead_letter_replay('<unrouted dlq id>'); -- decision=skipped, resolved=false
SELECT fn_iu_route_dead_letter_replay('<dryrun dlq id>'); -- decision=dry_run, resolved=true
ROLLBACK;
DOT command catalog (IU, used here)
dot_iu_filter_axis_b, dot_iu_subtree, dot_iu_sql_link_resolve, dot_iu_sql_link_validate (all category=read). Compose/structure DOT pairs: dot_iu_create_file_from_pieces, dot_iu_add_piece, dot_iu_remove_piece, dot_iu_reorder_piece, dot_iu_split_piece, dot_iu_merge_piece, dot_iu_render_file. Cut pipeline: dot_iu_cut_from_manifest, dot_iu_verify_cut_result. Test rd builder: dot_iu_test_review_decision_create.
Gate verification (read-only)
SELECT fn_iu_gate_verify_closed(NULL); -- {all_safe, never_flip_intact, all_governed_closed} + 10 per-gate
SELECT count(*) FROM pg_stat_activity WHERE state='idle in transaction';
SELECT count(*) FROM iu_gate_transition; -- 0 = no gate persisted open