ecpg
Preprocess C code with embedded SQL
TLDR
Preprocess a specific file
Preprocess from stdin and output to stdout
Preprocess from stdin and write to a file
Preprocess and specify an output file
Preprocess a header file (.pgh extension)
Preprocess in a specific compatibility mode
Preprocess with autocommit enabled
SYNOPSIS
ecpg [option...] inputfile
PARAMETERS
-I directory
Add directory to path for searching INCLUDE files.
-L directory
Add directory to library search path.
-l library
Add library to link line (e.g., -lpq).
-o filename
Specify output file name (default: input.ecpg.c).
-v
Enable verbose mode for detailed processing info.
-t
Enable minimal runtime debugging.
-c
Enable compatibility mode for older ECPG syntax.
-E
Display errors only, suppress warnings.
-R {no|full|include|location|statement}
Control error reporting detail level.
-i
Ignore missing INCLUDE files.
--include-any
Allow includes from any directory.
--include-first
Search current directory before system paths.
--version
Print version information.
-? | --help
Show help message.
DESCRIPTION
ecpg is the embedded SQL preprocessor for PostgreSQL, transforming C source files with embedded SQL statements into standard C code.
It processes directives like EXEC SQL followed by SQL commands, replacing them with calls to the libpq library. This enables seamless integration of SQL queries, DDL, DML, transactions, cursors, and dynamic SQL within C programs.
Key features include support for host variables (using : prefix), indicator variables, error handling via WHENEVER clauses, and type conversion between SQL and C data types. The preprocessor generates an output file (default: input.ecpg.c) that can be compiled with a C compiler like gcc, linking against libecpg and libpq.
Typical workflow: Write C code with embedded SQL, run ecpg input.c, then gcc -I$(pg_config --includedir) input.ecpg.c -lecpg -lpq -o program. It enforces SQL standards (e.g., SQL:1999) and provides debugging options for verbose output and error reporting.
ecpg is essential for database-intensive C applications, offering performance benefits over string-based queries while maintaining portability within PostgreSQL environments. (187 words)
CAVEATS
Requires PostgreSQL development packages (e.g., libecpg-dev). Output must be compiled with PostgreSQL libs; mismatched versions cause linking errors.
Not thread-safe by default; use -t for debugging.
COMPILATION EXAMPLE
After ecpg foo.pgc:
gcc $(pg_config --cflags) foo.ecpg.c $(pg_config --libs) -lecpg -o foo
EMBEDDED SQL SYNTAX
Use EXEC SQL SELECT ... INTO :hostvar; with WHENEVER SQLERROR GOTO label; for control flow.
HISTORY
Introduced in PostgreSQL 6.3 (1998) as open-source ECPG, originally from Ingres. Evolved with SQL standards support; current versions align with PostgreSQL 16+ features like advanced cursors and error handling.


