Bash extensions for Postgresql and SQLite SQL databases in Bash scripts.
Bash is the most widely used UNIX shell. Using SQL databases intensively in Bash scripts using commands like psql or sqlite3 is slow.
Since the CLI programs /usr/bin/psql or /usr/bin/sqlite3 are called for each query, there is a large overhead.
cg_sqlite and cg_psql are fast bash-builtins for SQLite and Postgresql with less overhead. The result is captured in a bash array to avoid expensive invocations of fork().
Status: Working. Already using cg_psql in production.
db=/tmp/my_test_sqlite3.db
result=''
cg_sqlite aa -D <database file> 'SQL_Statements;'")
echo "Results: ${#aa[@]} First: ${aa[0]:-} Second: ${aa[1]:-}"
-D <database file or connection info, respectively>
-d $'\t' Delimiter character of query result for columns
-d '|'
-d $'\t\n' ... and delimit records by 2nd character
-l <Max number of results> There is a default value DEFAULT_MAX_RESULTS for entries captured in a variable.
-V Print version. Can be used to check available of the builtin
-v Increase verbosity. Can be repeated.
./sqlite_test.sh
Run SQL_benchmark.sh without parameter for instructions.
10,000 simple SELECT queries - lower values are better:
/usr/bin/pgsql 811 seconds
/usr/bin/sqlite 59 seconds
cg_psql bashbuiltin 1.3 seconds
cg_sqlite bashbuiltin 0.5 seconds
First Install dependencies
The enclosed compile script generates shared object files with the ending .so.
compile_builtin.sh bashbuiltin_psql.c
or
compile_builtin.sh bashbuiltin_sqlite.c
Builtins need to be loaded into the Bash instance with enable. The following commands can be run in the current interactive shell or can be placed at the top of a Bash script.
enable -f ~/compiled/bashbuiltin_sqlite.so cg_sqlite
or
enable -f ~/compiled/bashbuiltin_psql.so cg_psql
When they are loaded successfully, the help command will print the full documentation
help cg_psql
or
help cg_sqlite
- Other DBs like MySQL and DB2 are not (yet) supported. You can send a request-for-feature.