pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedQuery to retrieve the index columns when a function is used.
3+ messages / 3 participants
[nested] [flat]
* Query to retrieve the index columns when a function is used.
@ 2020-03-09 13:15 Sterpu Victor <victor@caido.ro>
2020-03-11 21:09 ` Re: Query to retrieve the index columns when a function is used. David Rowley <dgrowleyml@gmail.com>
0 siblings, 1 reply; 3+ messages in thread
From: Sterpu Victor @ 2020-03-09 13:15 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Hello
I'm testing on Postgresql 12.1 and I have a index like this:
"check_dates_gist" EXCLUDE USING gist (id_test1 WITH =, id_test2 WITH =,
tsrange(valid_from::timestamp without time zone, valid_to::timestamp
without time zone) WITH &&)
When I run this query:
"select pc.relname, pi.indisunique, pi.indisprimary,
array_agg(a.attname) as attname
FROM pg_class pc
JOIN pg_index pi ON pc.oid = pi.indexrelid AND pc.oid IN
(SELECT indexrelid FROM pg_index, pg_class WHERE pg_class.relname='test'
AND pg_class.oid=pg_index.indrelid)
JOIN pg_attribute a ON a.attrelid = pc.oid
GROUP BY pc.relname,pi.indisunique, pi.indisprimary;"
I retrieve the index but there is no detail about the columns valid_from
and valid_to.
How can I retrieve this detail?
Thank you.
--
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Query to retrieve the index columns when a function is used.
2020-03-09 13:15 Query to retrieve the index columns when a function is used. Sterpu Victor <victor@caido.ro>
@ 2020-03-11 21:09 ` David Rowley <dgrowleyml@gmail.com>
2020-03-11 22:29 ` Re: Query to retrieve the index columns when a function is used. Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 3+ messages in thread
From: David Rowley @ 2020-03-11 21:09 UTC (permalink / raw)
To: Sterpu Victor <victor@caido.ro>; +Cc: pgsql-general@lists.postgresql.org
On Tue, 10 Mar 2020 at 02:16, Sterpu Victor <victor@caido.ro> wrote:
> I'm testing on Postgresql 12.1 and I have a index like this:
> "check_dates_gist" EXCLUDE USING gist (id_test1 WITH =, id_test2 WITH =, tsrange(valid_from::timestamp without time zone, valid_to::timestamp without time zone) WITH &&)
>
> When I run this query:
> "select pc.relname, pi.indisunique, pi.indisprimary, array_agg(a.attname) as attname
> FROM pg_class pc
> JOIN pg_index pi ON pc.oid = pi.indexrelid AND pc.oid IN
> (SELECT indexrelid FROM pg_index, pg_class WHERE pg_class.relname='test' AND pg_class.oid=pg_index.indrelid)
> JOIN pg_attribute a ON a.attrelid = pc.oid
> GROUP BY pc.relname,pi.indisunique, pi.indisprimary;"
> I retrieve the index but there is no detail about the columns valid_from and valid_to.
>
> How can I retrieve this detail?
Those details are in the indexprs column. There's an item there for
each 0 valued indkey. It's not going to be particularly easy for you
to parse that from SQL. Internally in PostgreSQL, we have functions
that could give you that information easily, but to access those from
SQL you'd need to write something in C. The C function is named
pull_varattnos(). That will give you a Bitmapset for each of the
columns that are found. You'd need to write a set-returning function
to return those values then join that to pg_attribute.
Alternatively, it likely wouldn't be impossible to do at the SQL level
with regexp_matches(), but building an expression to reliably extract
what you want might not be an easy task. On a quick test, I see that:
select indexrelid, unnest(r.e) from pg_index i, lateral
regexp_matches(i.indexprs, 'VAR :varno 1 :varattno (\d{1,})', 'g')
r(e) where i.indexprs is not null;
does kick out the correct results for the expression indexes I have in
my database, but there may be some more complex expressions that break
it.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Query to retrieve the index columns when a function is used.
2020-03-09 13:15 Query to retrieve the index columns when a function is used. Sterpu Victor <victor@caido.ro>
2020-03-11 21:09 ` Re: Query to retrieve the index columns when a function is used. David Rowley <dgrowleyml@gmail.com>
@ 2020-03-11 22:29 ` Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 3+ messages in thread
From: Tom Lane @ 2020-03-11 22:29 UTC (permalink / raw)
To: David Rowley <dgrowleyml@gmail.com>; +Cc: Sterpu Victor <victor@caido.ro>; pgsql-general@lists.postgresql.org
David Rowley <dgrowleyml@gmail.com> writes:
> On Tue, 10 Mar 2020 at 02:16, Sterpu Victor <victor@caido.ro> wrote:
>> How can I retrieve this detail?
> Those details are in the indexprs column. There's an item there for
> each 0 valued indkey. It's not going to be particularly easy for you
> to parse that from SQL. Internally in PostgreSQL, we have functions
> that could give you that information easily, but to access those from
> SQL you'd need to write something in C.
You could use pg_get_indexdef(). I agree that parsing indexprs from
SQL is not to be recommended --- aside from being very complex, it
would break on a regular basis, because we change those structs often.
regards, tom lane
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2020-03-11 22:29 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-03-09 13:15 Query to retrieve the index columns when a function is used. Sterpu Victor <victor@caido.ro>
2020-03-11 21:09 ` David Rowley <dgrowleyml@gmail.com>
2020-03-11 22:29 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox