[PATCH] Postgresql: warnings when inserting a backslash

Please report bugs here!

Moderator: Thorsten

Post Reply
bram
Posts: 19
Joined: Mon Aug 16, 2010 12:54 pm

[PATCH] Postgresql: warnings when inserting a backslash

Post by bram » Tue Aug 24, 2010 4:24 pm

Short version:

In the default postgres 'standard_conforming_strings' is set to off.
This causes pg_escape to change a string which contains a backslash to two backslashes. (That is: from ' foo\ bar' into 'foo \\ bar')
This then updates the database with the correct data but a warning is shown in the postgres logs:

Code: Select all

	WARNING:  nonstandard use of \\ in a string literal at character 8
	HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
The attached patch fixes this.



Long version:

postgresql < 8.1 interpreted backslashes in strings the same way as in C.
That is: 'foo \t bar' is interpreted as 'foo + tab + bar'
To insert a literal backslash followed by the letter t one has/had to use 'foo \\t bar'.

From postgres 8.1 new variables and new syntax are added:
- variable: 'standard_conforming_strings'
- variable: 'escape_string_warning'
- syntax: E''

When 'standard_conforming_strings' is set to off (the default) then strings are interpreted the same way as in C.
That is: 'foo \t bar' is interpreted as 'foo + tab + bar'
If one wants to insert a literal backslash then one needs to use 'foo \\t bar'.
The effect of this is:
a) database record inserted with 'foo + backslash + t + bar'
b) a warning is shown in the logfile

When 'standard_conforming_strings' is set to on then strings are not interpreted the same way as in C.
That is: 'foo \t bar' is interpreted as 'foo + backslash + t + bar'
If the string needs to be interpreted then one has to use E'foo \t bar' (which is interpreted as 'foo + tab + bar').

(Note: pg_escape correctly checks the value of standard_conforming_strings and behaves as expect when it is 'on' or 'off')
You do not have the required permissions to view the files attached to this post.

Thorsten
Posts: 14759
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Re: [PATCH] Postgresql: warnings when inserting a backslash

Post by Thorsten » Wed Aug 25, 2010 8:22 pm

Hi,

thanks for the patch. I added it to the 2.6 branch for the 2.6.8 release!

bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist

Post Reply