quoting psql varible as identifier

Started by Pavel Stehuleover 16 years ago73 messageshackers
Jump to latest
#1Pavel Stehule
pavel.stehule@gmail.com

Hello

I am working on new patch. I see a problem with copy quote_ident on
client side. This function call ScanKeywordLookup function.

const ScanKeyword *keyword = ScanKeywordLookup(ident,

ScanKeywords,

NumScanKeywords);

so we cannot simply implement quote_ident on client side :(. So we
have to use a real query.

It is acceptable for you?

Pavel

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Pavel Stehule (#1)
Re: quoting psql varible as identifier

Pavel Stehule <pavel.stehule@gmail.com> writes:

so we cannot simply implement quote_ident on client side :(. So we
have to use a real query.

It is acceptable for you?

No, certainly not --- that adds a boatload of failure conditions.
Just quote it unconditionally.

regards, tom lane

#3Robert Haas
robertmhaas@gmail.com
In reply to: Pavel Stehule (#1)
Re: quoting psql varible as identifier

On Dec 29, 2009, at 8:57 AM, Pavel Stehule <pavel.stehule@gmail.com>
wrote:

Hello

I am working on new patch. I see a problem with copy quote_ident on
client side. This function call ScanKeywordLookup function.

const ScanKeyword *keyword = ScanKeywordLookup(ident,

ScanKeywords,

NumScanKeywords);

so we cannot simply implement quote_ident on client side :(. So we
have to use a real query.

It is acceptable for you?

No. It has to be client-side.

...Robert

#4Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#2)
Re: quoting psql varible as identifier

2009/12/29 Tom Lane <tgl@sss.pgh.pa.us>:

Pavel Stehule <pavel.stehule@gmail.com> writes:

so we cannot simply implement quote_ident on client side :(. So we
have to use a real query.

It is acceptable for you?

No, certainly not --- that adds a boatload of failure conditions.
Just quote it unconditionally.

ok

this function have to live in libpq - it needs some not exported
functionality. But, it would not be a problem.

Pavel

Show quoted text

                       regards, tom lane

#5Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Pavel Stehule (#4)
Re: quoting psql varible as identifier

Pavel Stehule escribi�:

2009/12/29 Tom Lane <tgl@sss.pgh.pa.us>:

Pavel Stehule <pavel.stehule@gmail.com> writes:

so we cannot simply implement quote_ident on client side :(. So we
have to use a real query.

It is acceptable for you?

No, certainly not --- that adds a boatload of failure conditions.
Just quote it unconditionally.

ok

this function have to live in libpq - it needs some not exported
functionality. But, it would not be a problem.

Can we use a trick similar to pg_dump's?

--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

#6Pavel Stehule
pavel.stehule@gmail.com
In reply to: Alvaro Herrera (#5)
Re: quoting psql varible as identifier

2009/12/29 Alvaro Herrera <alvherre@commandprompt.com>:

Pavel Stehule escribió:

2009/12/29 Tom Lane <tgl@sss.pgh.pa.us>:

Pavel Stehule <pavel.stehule@gmail.com> writes:

so we cannot simply implement quote_ident on client side :(. So we
have to use a real query.

It is acceptable for you?

No, certainly not --- that adds a boatload of failure conditions.
Just quote it unconditionally.

ok

this function have to live in libpq - it needs some not exported
functionality. But, it would not be a problem.

Can we use a trick similar to pg_dump's?

??

Pavel

Show quoted text

--
Alvaro Herrera                                http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

#7Pavel Stehule
pavel.stehule@gmail.com
In reply to: Alvaro Herrera (#5)
Re: quoting psql varible as identifier

2009/12/29 Alvaro Herrera <alvherre@commandprompt.com>:

Pavel Stehule escribió:

2009/12/29 Tom Lane <tgl@sss.pgh.pa.us>:

Pavel Stehule <pavel.stehule@gmail.com> writes:

so we cannot simply implement quote_ident on client side :(. So we
have to use a real query.

It is acceptable for you?

No, certainly not --- that adds a boatload of failure conditions.
Just quote it unconditionally.

ok

this function have to live in libpq - it needs some not exported
functionality. But, it would not be a problem.

Can we use a trick similar to pg_dump's?

I see it - we can move function (content) fmtId from dumputils.c to libpq.

Pavel

Show quoted text

--
Alvaro Herrera                                http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

#8Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Pavel Stehule (#6)
Re: quoting psql varible as identifier

Pavel Stehule escribi�:

2009/12/29 Alvaro Herrera <alvherre@commandprompt.com>:

Pavel Stehule escribi�:

2009/12/29 Tom Lane <tgl@sss.pgh.pa.us>:

Pavel Stehule <pavel.stehule@gmail.com> writes:

so we cannot simply implement quote_ident on client side :(. So we
have to use a real query.

It is acceptable for you?

No, certainly not --- that adds a boatload of failure conditions.
Just quote it unconditionally.

ok

this function have to live in libpq - it needs some not exported
functionality. But, it would not be a problem.

Can we use a trick similar to pg_dump's?

??

See src/bin/pg_dump/keywords.c and fmtId in dumputils.c

--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

#9Pavel Stehule
pavel.stehule@gmail.com
In reply to: Alvaro Herrera (#8)
Re: quoting psql varible as identifier

2009/12/29 Alvaro Herrera <alvherre@commandprompt.com>:

Pavel Stehule escribió:

2009/12/29 Alvaro Herrera <alvherre@commandprompt.com>:

Pavel Stehule escribió:

2009/12/29 Tom Lane <tgl@sss.pgh.pa.us>:

Pavel Stehule <pavel.stehule@gmail.com> writes:

so we cannot simply implement quote_ident on client side :(. So we
have to use a real query.

It is acceptable for you?

No, certainly not --- that adds a boatload of failure conditions.
Just quote it unconditionally.

ok

this function have to live in libpq - it needs some not exported
functionality. But, it would not be a problem.

Can we use a trick similar to pg_dump's?

??

See src/bin/pg_dump/keywords.c and fmtId in dumputils.c

I found it. It is maybe too complex for using in psql.

Pavel

Show quoted text

--
Alvaro Herrera                                http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

#10Tom Lane
tgl@sss.pgh.pa.us
In reply to: Pavel Stehule (#7)
Re: quoting psql varible as identifier

Pavel Stehule <pavel.stehule@gmail.com> writes:

2009/12/29 Alvaro Herrera <alvherre@commandprompt.com>:

Can we use a trick similar to pg_dump's?

I see it - we can move function (content) fmtId from dumputils.c to libpq.

This is not a good idea. pg_dump can be expected to be up-to-date with
the backend's keyword list, but libpq cannot.

Just quote the thing unconditionally. It's not worth working harder
than that anyway.

regards, tom lane

#11Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#10)
Re: quoting psql varible as identifier

2009/12/29 Tom Lane <tgl@sss.pgh.pa.us>:

Pavel Stehule <pavel.stehule@gmail.com> writes:

2009/12/29 Alvaro Herrera <alvherre@commandprompt.com>:

Can we use a trick similar to pg_dump's?

I see it - we can move function (content) fmtId from dumputils.c to libpq.

This is not a good idea.  pg_dump can be expected to be up-to-date with
the backend's keyword list, but libpq cannot.

Just quote the thing unconditionally.  It's not worth working harder
than that anyway.

I see it.

Pavel

Show quoted text

                       regards, tom lane

#12Pavel Stehule
pavel.stehule@gmail.com
In reply to: Pavel Stehule (#11)
Re: quoting psql varible as identifier

hello

here is patch

pavel@postgres:5432=# \set foo 'hello world'
pavel@postgres:5432=# SELECT :'foo' AS :"foo";
hello world
-------------
hello world
(1 row)

Regards
Pavel

2009/12/29 Pavel Stehule <pavel.stehule@gmail.com>:

Show quoted text

2009/12/29 Tom Lane <tgl@sss.pgh.pa.us>:

Pavel Stehule <pavel.stehule@gmail.com> writes:

2009/12/29 Alvaro Herrera <alvherre@commandprompt.com>:

Can we use a trick similar to pg_dump's?

I see it - we can move function (content) fmtId from dumputils.c to libpq.

This is not a good idea.  pg_dump can be expected to be up-to-date with
the backend's keyword list, but libpq cannot.

Just quote the thing unconditionally.  It's not worth working harder
than that anyway.

I see it.

Pavel

                       regards, tom lane

Attachments:

variable_escaping.difftext/x-patch; charset=US-ASCII; name=variable_escaping.diffDownload+209-8
#13Robert Haas
robertmhaas@gmail.com
In reply to: Pavel Stehule (#12)
Re: quoting psql varible as identifier

On Tue, Dec 29, 2009 at 3:19 PM, Pavel Stehule <pavel.stehule@gmail.com> wrote:

here is patch

The error handling in quote_literal() doesn't look right to me. The
documentation for PQescapeStringConn says that it stores an error
message in the conn object, but your code ignores that and prints out
a generic message instead. That doesn't seem right: but then it
further goes on to call exit(1), which seems like a considerable
overreaction to an encoding violation, which is apparently the only
class of error PQescapeStringConn() is documented to throw.

...Robert

#14Pavel Stehule
pavel.stehule@gmail.com
In reply to: Robert Haas (#13)
Re: quoting psql varible as identifier

2009/12/30 Robert Haas <robertmhaas@gmail.com>:

On Tue, Dec 29, 2009 at 3:19 PM, Pavel Stehule <pavel.stehule@gmail.com> wrote:

here is patch

The error handling in quote_literal() doesn't look right to me.  The
documentation for PQescapeStringConn says that it stores an error
message in the conn object, but your code ignores that and prints out
a generic message instead.  That doesn't seem right: but then it
further goes on to call exit(1), which seems like a considerable
overreaction to an encoding violation, which is apparently the only
class of error PQescapeStringConn() is documented to throw.

Actually I am not sure about the adequate solution. Now, the lexer
doesn't return any error. Any handled errors are fatal, and lexer (and
psql) is finished with exit(1) - so there are not checking status
after any lexer call. It is question if we have to do it. Because
error will be raised in next stage:

"Presently the only possible error conditions involve invalid
multibyte encoding in the source string. The output string is still
generated on error, but it can be expected that the server will reject
it as malformed. On error, a suitable message is stored in the conn
object, whether or not error is NULL."

http://www.postgresql.org/docs/8.4/static/libpq-exec.html#LIBPQ-EXEC-ESCAPE-STRING

so probably - we can quietly ignore this error without security or
functionality issues.

I see two solution:

a) print correct message and exit(1)
b) quietly ignore this error - it's more warning than error, because
error will be raised immediately

Third variant, checking lexer status over every call is maybe non
adequate to frequency of this error and an importance of this patch. I
am for a.

Regards, and happy new year
Pavel

Show quoted text

...Robert

#15Tom Lane
tgl@sss.pgh.pa.us
In reply to: Pavel Stehule (#14)
Re: quoting psql varible as identifier

Pavel Stehule <pavel.stehule@gmail.com> writes:

a) print correct message and exit(1)

Which part of "no, you're not doing that" wasn't clear to you?

The error handling in this function should be no different from that
in the existing escaping functions. exit(1) is completely unacceptable.

regards, tom lane

#16Tom Lane
tgl@sss.pgh.pa.us
In reply to: Pavel Stehule (#12)
Re: quoting psql varible as identifier

Pavel Stehule <pavel.stehule@gmail.com> writes:

here is patch

I looked at this patch a bit, and I think the real problem with it is
that it's not multibyte safe. You've copied backend code that is
allowed to assume it's in a safe encoding (ie, one where multibyte
characters can't contain non-high-bit-set bytes). This is not okay
on the client side, see SJIS and similar encodings.

Where you need to start out is by cloning PQescapeStringConn, which does
a similar type of transformation correctly even in unsafe encodings.
I think we'd agreed upthread that libpq should provide
PQescapeIdentifier functionality anyhow.

Once you've actually read that code, you'll realize that it's okay to
treat the error result as a warning, which resolves the other point
of concern. Just print the message and use the result anyway.

regards, tom lane

#17Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#15)
Re: quoting psql varible as identifier

2010/1/2 Tom Lane <tgl@sss.pgh.pa.us>:

Pavel Stehule <pavel.stehule@gmail.com> writes:

a) print correct message and exit(1)

Which part of "no, you're not doing that" wasn't clear to you?

The error handling in this function should be no different from that
in the existing escaping functions.  exit(1) is completely unacceptable.

Are we talking we about error handling in psql lexer?

What I know, in psql there are no any escaping function now. Yes,
exit(1) is maybe too hard - but it is safe. I didn't write
PQescapeStringConn function, and I am not able to speak, we can ignore
this error result. Full handling of this possible error, means to add
some CHECK over any lexer call.

Pavel

Show quoted text

                       regards, tom lane

#18Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#16)
Re: quoting psql varible as identifier

2010/1/2 Tom Lane <tgl@sss.pgh.pa.us>:

Pavel Stehule <pavel.stehule@gmail.com> writes:

here is patch

I looked at this patch a bit, and I think the real problem with it is
that it's not multibyte safe.  You've copied backend code that is
allowed to assume it's in a safe encoding (ie, one where multibyte
characters can't contain non-high-bit-set bytes).  This is not okay
on the client side, see SJIS and similar encodings.

this code is taken from pg_dump, so if I understand it well, this is
littl bit different case.

Where you need to start out is by cloning PQescapeStringConn, which does
a similar type of transformation correctly even in unsafe encodings.
I think we'd agreed upthread that libpq should provide
PQescapeIdentifier functionality anyhow.

ok

Once you've actually read that code, you'll realize that it's okay to
treat the error result as a warning, which resolves the other point
of concern.  Just print the message and use the result anyway.

ok.

Pavel

Show quoted text

                       regards, tom lane

#19Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#16)
Re: quoting psql varible as identifier

hello

2010/1/2 Tom Lane <tgl@sss.pgh.pa.us>:

Pavel Stehule <pavel.stehule@gmail.com> writes:

here is patch

I looked at this patch a bit, and I think the real problem with it is
that it's not multibyte safe.  You've copied backend code that is
allowed to assume it's in a safe encoding (ie, one where multibyte
characters can't contain non-high-bit-set bytes).  This is not okay
on the client side, see SJIS and similar encodings.

Where you need to start out is by cloning PQescapeStringConn, which does
a similar type of transformation correctly even in unsafe encodings.
I think we'd agreed upthread that libpq should provide
PQescapeIdentifier functionality anyhow.

I am looking on psql directory. Now I found, so in this directory is
linked dumputil.c - It could little bit to help us.

I have one question. If I understand well, the function fmtId isn't
multibyte safe? So why is possible to use it in pg_dump?

Pavel

Show quoted text

Once you've actually read that code, you'll realize that it's okay to
treat the error result as a warning, which resolves the other point
of concern.  Just print the message and use the result anyway.

                       regards, tom lane

#20Pavel Stehule
pavel.stehule@gmail.com
In reply to: Pavel Stehule (#19)
Re: quoting psql varible as identifier

Hello

I talked with Hitoshi Harada, and fmtId function is safe (minimally
for Japanese case). This function working without any errors, so we
must not duplicate a code.

Pavel

2010/1/4 Pavel Stehule <pavel.stehule@gmail.com>:

Show quoted text

hello

2010/1/2 Tom Lane <tgl@sss.pgh.pa.us>:

Pavel Stehule <pavel.stehule@gmail.com> writes:

here is patch

I looked at this patch a bit, and I think the real problem with it is
that it's not multibyte safe.  You've copied backend code that is
allowed to assume it's in a safe encoding (ie, one where multibyte
characters can't contain non-high-bit-set bytes).  This is not okay
on the client side, see SJIS and similar encodings.

Where you need to start out is by cloning PQescapeStringConn, which does
a similar type of transformation correctly even in unsafe encodings.
I think we'd agreed upthread that libpq should provide
PQescapeIdentifier functionality anyhow.

I am looking on psql directory. Now I found, so in this directory is
linked dumputil.c - It could little bit to help us.

I have one question. If I understand well, the function fmtId isn't
multibyte safe? So why is possible to use it in pg_dump?

Pavel

Once you've actually read that code, you'll realize that it's okay to
treat the error result as a warning, which resolves the other point
of concern.  Just print the message and use the result anyway.

                       regards, tom lane

Attachments:

variable_escaping.diffapplication/octet-stream; name=variable_escaping.diffDownload+142-8
#21Tom Lane
tgl@sss.pgh.pa.us
In reply to: Pavel Stehule (#19)
#22Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#21)
#23Robert Haas
robertmhaas@gmail.com
In reply to: Pavel Stehule (#22)
#24Tom Lane
tgl@sss.pgh.pa.us
In reply to: Robert Haas (#23)
#25Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#24)
#26Robert Haas
robertmhaas@gmail.com
In reply to: Pavel Stehule (#25)
#27Pavel Stehule
pavel.stehule@gmail.com
In reply to: Robert Haas (#26)
#28Pavel Stehule
pavel.stehule@gmail.com
In reply to: Robert Haas (#26)
#29Robert Haas
robertmhaas@gmail.com
In reply to: Pavel Stehule (#28)
#30Pavel Stehule
pavel.stehule@gmail.com
In reply to: Robert Haas (#29)
#31Robert Haas
robertmhaas@gmail.com
In reply to: Robert Haas (#29)
#32Pavel Stehule
pavel.stehule@gmail.com
In reply to: Robert Haas (#31)
#33Robert Haas
robertmhaas@gmail.com
In reply to: Pavel Stehule (#32)
#34Pavel Stehule
pavel.stehule@gmail.com
In reply to: Robert Haas (#33)
#35Robert Haas
robertmhaas@gmail.com
In reply to: Pavel Stehule (#34)
#36Pavel Stehule
pavel.stehule@gmail.com
In reply to: Robert Haas (#35)
#37Robert Haas
robertmhaas@gmail.com
In reply to: Pavel Stehule (#36)
#38Tom Lane
tgl@sss.pgh.pa.us
In reply to: Robert Haas (#37)
#39Robert Haas
robertmhaas@gmail.com
In reply to: Tom Lane (#38)
#40Pavel Stehule
pavel.stehule@gmail.com
In reply to: Robert Haas (#39)
#41Robert Haas
robertmhaas@gmail.com
In reply to: Pavel Stehule (#40)
#42Pavel Stehule
pavel.stehule@gmail.com
In reply to: Robert Haas (#41)
#43Robert Haas
robertmhaas@gmail.com
In reply to: Pavel Stehule (#42)
#44Tom Lane
tgl@sss.pgh.pa.us
In reply to: Robert Haas (#43)
#45Robert Haas
robertmhaas@gmail.com
In reply to: Tom Lane (#44)
#46Tom Lane
tgl@sss.pgh.pa.us
In reply to: Robert Haas (#45)
#47Pavel Stehule
pavel.stehule@gmail.com
In reply to: Robert Haas (#45)
#48Robert Haas
robertmhaas@gmail.com
In reply to: Tom Lane (#46)
#49Tom Lane
tgl@sss.pgh.pa.us
In reply to: Robert Haas (#48)
#50Robert Haas
robertmhaas@gmail.com
In reply to: Tom Lane (#49)
#51Tom Lane
tgl@sss.pgh.pa.us
In reply to: Robert Haas (#50)
#52Robert Haas
robertmhaas@gmail.com
In reply to: Tom Lane (#51)
#53Tom Lane
tgl@sss.pgh.pa.us
In reply to: Robert Haas (#52)
#54Robert Haas
robertmhaas@gmail.com
In reply to: Tom Lane (#53)
#55Robert Haas
robertmhaas@gmail.com
In reply to: Robert Haas (#54)
#56Pavel Stehule
pavel.stehule@gmail.com
In reply to: Robert Haas (#55)
#57Robert Haas
robertmhaas@gmail.com
In reply to: Pavel Stehule (#56)
#58Pavel Stehule
pavel.stehule@gmail.com
In reply to: Robert Haas (#57)
#59Robert Haas
robertmhaas@gmail.com
In reply to: Pavel Stehule (#58)
#60Pavel Stehule
pavel.stehule@gmail.com
In reply to: Robert Haas (#59)
#61Robert Haas
robertmhaas@gmail.com
In reply to: Pavel Stehule (#60)
#62Pavel Stehule
pavel.stehule@gmail.com
In reply to: Robert Haas (#61)
#63Pavel Stehule
pavel.stehule@gmail.com
In reply to: Pavel Stehule (#62)
#64Robert Haas
robertmhaas@gmail.com
In reply to: Pavel Stehule (#63)
#65Pavel Stehule
pavel.stehule@gmail.com
In reply to: Robert Haas (#64)
#66Pavel Stehule
pavel.stehule@gmail.com
In reply to: Robert Haas (#64)
#67Robert Haas
robertmhaas@gmail.com
In reply to: Pavel Stehule (#66)
#68Pavel Stehule
pavel.stehule@gmail.com
In reply to: Robert Haas (#67)
#69Robert Haas
robertmhaas@gmail.com
In reply to: Pavel Stehule (#68)
#70Pavel Stehule
pavel.stehule@gmail.com
In reply to: Robert Haas (#69)
#71Robert Haas
robertmhaas@gmail.com
In reply to: Pavel Stehule (#70)
#72Pavel Stehule
pavel.stehule@gmail.com
In reply to: Robert Haas (#71)
#73Robert Haas
robertmhaas@gmail.com
In reply to: Pavel Stehule (#72)