Re: Querying database for table pk - better way?

From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Josh Trutwin <josh(at)trutwins(dot)homeip(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Querying database for table pk - better way?
Date: 2007-09-06 03:22:40
Message-ID: 20070906032240.GR6102@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Josh Trutwin wrote:

> > SELECT nspname as schema, c2.oid as tableoid, c2.relname as
> > table, substring(pg_catalog.pg_get_indexdef(i.indexrelid, 0, true)
> > from E'\\((.*)\\)')

> 2.) Can you explain that substring line? What in the world is "from
> E'\\((.*)\\)')" doing? Somehow it gets the column name....

The function is
substring(foo from bar)
so the E'' stuff you see is just the second operand. The E'' syntax
allows backslash escapes; if you used '' without the E it would complain
about the backslashes, because the SQL standard requires them to not
mean escape characters, but in this case they are being treated as
escapes. The first pair of backslashes are there to protect the
parentheses from being turned them into capturing parens; they are
doubled because the SQL parser eats one of them, so the regex engine
really gets \((.*(\) which means "parens, then .*, then parens, but
capture whatever is matched by the .*".

Phew, I think this is a very confuse explanation. Hope it helps :-)

--
Alvaro Herrera http://www.flickr.com/photos/alvherre/
"Aprender sin pensar es inútil; pensar sin aprender, peligroso" (Confucio)

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ow Mun Heng 2007-09-06 04:00:21 Re: Max File size per Table (1G limit??)
Previous Message Liam Slusser 2007-09-06 02:45:25 Re: pg_dump doesn¹t dump everything?