rum icon indicating copy to clipboard operation
rum copied to clipboard

indexing inherited tables

Open IvanKonevJr opened this issue 5 years ago • 14 comments

Hi! We urgently need to index all inherited tables by parent - tell me what to do for this?

IvanKonevJr avatar Apr 09 '19 03:04 IvanKonevJr

Hello,

Can you describe your task more detailed? Do you mean inherited tables or partitions? Do you mean creation of indexes for each table or creation of one global index for all childs?

Currently PostgreSQL doesn't support cascade creation of indexes for inherited tables, and you need to do it manually. But it recursively creates indexes for partitions.

za-arthur avatar Apr 09 '19 08:04 za-arthur

Creating one global index for all child inherited tables. Need full-text search in all child tables in one query for max performance, besides whether there are no ways to combine several relevant queries.

"But it recursively creates indexes for partitions." Do you mean that it is possible to use one index per query on the parent table, where the result affects all child rows?

IvanKonevJr avatar Apr 09 '19 08:04 IvanKonevJr

Unfortunately currently it isn't possible to create a global index. It is an issue not only of RUM, but also of PostgreSQL itself.

"But it recursively creates indexes for partitions." Do you mean that it is possible to use one index per query on the parent table, where the result affects all child rows?

No, it creates separate indexes for each partition.

za-arthur avatar Apr 09 '19 09:04 za-arthur

Well, then let me discuss - can you help me to expand the source code of RUM to include all the rows of children in the index - because and since the select on the parent table returns all rows of children tables with unique id.

IvanKonevJr avatar Apr 09 '19 09:04 IvanKonevJr

I'm not sure that I understood you correctly. Can you give an example of queries and tables?

za-arthur avatar Apr 09 '19 09:04 za-arthur

On Tue, Apr 9, 2019 at 12:20 PM Lao Tsing [email protected] wrote:

Well, then let me discuss - can you help me to expand the source code of RUM to include all the rows of children in the index - becouse and since the select on the parent table returns all rows of children tables with unique id.

Did you think about creating view, which effectively is select from all children tables ?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/postgrespro/rum/issues/55#issuecomment-481172771, or mute the thread https://github.com/notifications/unsubscribe-auth/AGFI4q6ihmsWK5SabSTnRsVWq6MmZGoGks5vfFtdgaJpZM4cjiZh .

-- Postgres Professional: http://www.postgrespro.com The Russian Postgres Company

obartunov avatar Apr 09 '19 09:04 obartunov

Well, for example, we have electronics reestr and furniture reestr, in each table there are more than a million rows - we are interested in finding an employee who performed some actions on items in both tables through a nice WebQuery DSL.

p.s. materialized view with a huge tables get 2x database size and a lot of cost

IvanKonevJr avatar Apr 09 '19 09:04 IvanKonevJr

and what if, during the generation of the rum index with the presence of children's tables, to do it in a loop on each table, then the index will be complete and consistent, since of query on the parent table return all rows of children? this idea is simple, how can you add it to the source code? I would become tester =)

IvanKonevJr avatar Apr 09 '19 09:04 IvanKonevJr

I think you can use UNION statement here. And also you can use here a view (not materialized) as Oleg wrote. Does this work for you?

za-arthur avatar Apr 09 '19 09:04 za-arthur

Is it possible to build a full-text ranked query for a not materialized view? In the documentation: "CREATE INDEX constructs an index on the specified column(s) of the specified relation, which can be a table or a materialized view."

IvanKonevJr avatar Apr 09 '19 09:04 IvanKonevJr

the generation of the rum index with the presence of children's tables, to do it in a loop on each table, then the index will be complete and consistent, since of query on the parent table return all rows of children

is this idea not easy to implement? i think it solves the problem of full-text search on inherited tables and allows in most cases to refuse ElasticSearch

IvanKonevJr avatar Apr 09 '19 09:04 IvanKonevJr

the generation of the rum index with the presence of children's tables, to do it in a loop on each table, then the index will be complete and consistent, since of query on the parent table return all rows of children

Unfortunately we haven't such plans. It won't bring much gain, but it requires much more complex changes into RUM. Alternatively you can create inherited indexes manually. Or you may propose a patch in pgsql-hackers.

za-arthur avatar Apr 10 '19 10:04 za-arthur

Arthur, can created inherited indexes manually on each children tables to build ranked full-text result?

IvanKonevJr avatar Apr 11 '19 02:04 IvanKonevJr

Yes. Let's say we have two tables:

=# \d+ test_rum
 Column |   Type
--------+----------
 t      | text     
 a      | tsvector 
Indexes:
    "rumidx" rum (a)
Child tables: test_rum_2
=# \d+ test_rum_2
 Column |   Type   
--------+----------
 t      | text     
 a      | tsvector 
Indexes:
    "rumidx_2" rum (a)
Inherits: test_rum

The following query uses both indexes:

=# explain (costs off)                                                
SELECT * FROM test_rum WHERE a @@ to_tsquery('pg_catalog.english', 'ever|wrote')
ORDER BY a <=> to_tsquery('pg_catalog.english', 'ever|wrote');
                                  QUERY PLAN                                  
------------------------------------------------------------------------------
 Sort
   Sort Key: ((test_rum.a <=> '''ever'' | ''wrote'''::tsquery))
   ->  Result
         ->  Append
               ->  Bitmap Heap Scan on test_rum
                     Recheck Cond: (a @@ '''ever'' | ''wrote'''::tsquery)
                     ->  Bitmap Index Scan on rumidx
                           Index Cond: (a @@ '''ever'' | ''wrote'''::tsquery)
               ->  Bitmap Heap Scan on test_rum_2
                     Recheck Cond: (a @@ '''ever'' | ''wrote'''::tsquery)
                     ->  Bitmap Index Scan on rumidx_2
                           Index Cond: (a @@ '''ever'' | ''wrote'''::tsquery)

za-arthur avatar Apr 11 '19 08:04 za-arthur