Waiting for 9.2 – relative paths in psql

On 6th of July, Robert Haas committed patch:

Add \ir command to psql.
 
\ir is short for "include relative"; when used from a script, the
supplied pathname will be interpreted relative to the input file,
rather than to the current working directory.
 
Gurjeet Singh, reviewed by Josh Kupershmidt, with substantial further
cleanup by me.

Commit message is pretty clear, but let's just show simple example.

In my home directory, I created directory test, and in it two files:

=$ ls -l
total 8
-rw-r--r-- 1 depesz depesz  9 2011-07-08 14:52 a.sql
-rw-r--r-- 1 depesz depesz 17 2011-07-08 14:52 b.sql
 
=$ cat a.sql
\i b.sql
 
=$ cat b.sql
select version()

Of course this is pretty minimalistic example, but it will work. Now, if I'll run a.sql, when in test/ directory, it will work as expected:

=$ pwd
/home/depesz/test
 
=$ psql -f a.sql
                                                        version
------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 9.2devel on x86_64-unknown-linux-gnu, compiled by gcc-4.5.real (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2, 64-bit
(1 row)

But when I'll run it in different directory:

=$ pwd
/home/depesz
 
=$ psql -f test/a.sql
psql:test/a.sql:1: b.sql: No such file or directory

Reason is pretty simple – a.sql did \i b.sql, which tries to load b.sql from current directory – which is /home/depesz, and there is no /home/depesz/b.sql.

On the other hand if I'll change a.sql content to use \ir, it will work nicely:

=$ pwd
/home/depesz
 
=$ cat test/a.sql
\ir b.sql
 
=$ ls -l ./b.sql
ls: cannot access ./b.sql: No such file or directory
 
=$ cat test/b.sql
select version()
 
=$ psql -f test/a.sql
                                                        version
------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 9.2devel on x86_64-unknown-linux-gnu, compiled by gcc-4.5.real (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2, 64-bit
(1 row)

This in itself is not a really a big deal, but I'm always overly excited about improvements in psql, so I welcome them whole heartedly.