Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZaNix-0007V5-Th for pgsql-hackers@arkaria.postgresql.org; Fri, 11 Sep 2015 12:46:40 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZaNix-0008AJ-AV for pgsql-hackers@arkaria.postgresql.org; Fri, 11 Sep 2015 12:46:39 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZaNhd-0006gt-Dv for pgsql-hackers@postgresql.org; Fri, 11 Sep 2015 12:45:17 +0000 Received: from newmail.postgrespro.ru ([93.174.131.138] helo=mail.postgrespro.ru) by makus.postgresql.org with esmtp (Exim 4.84) (envelope-from ) id 1ZaNhT-0003Kk-21 for pgsql-hackers@postgresql.org; Fri, 11 Sep 2015 12:45:16 +0000 Received: from [192.168.27.79] (unknown [192.168.27.1]) by mail.postgrespro.ru (Postfix) with ESMTPSA id C628321C317E for ; Fri, 11 Sep 2015 15:45:04 +0300 (MSK) Subject: [PROPOSAL] Covering + unique indexes. References: <55F2C1BC.3010109@postgrespro.ru> To: pgsql-hackers From: Anastasia Lubennikova X-Forwarded-Message-Id: <55F2C1BC.3010109@postgrespro.ru> Message-ID: <55F2CCD0.7040608@postgrespro.ru> Date: Fri, 11 Sep 2015 15:45:04 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: <55F2C1BC.3010109@postgrespro.ru> Content-Type: multipart/alternative; boundary="------------030502020706030202000606" X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org This is a multi-part message in MIME format. --------------030502020706030202000606 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Hi, hackers! Use case: Index-only scans is a wonderful feature that allows to speed up select queries of indexed columns. Therefore some users want to create multicolumn indexes on columns which are queried often. But if there's unique constraint on some column, they have to maintain another unique index. Even if the column is already in covering index. This adds overhead to data manipulation operations and database size. I've started work on a patch that allows to combine covering and unique functionality. The main idea is to allow user create multicolumn indexes with a definite number of unique columns. For example (don't mind SQL syntax here, please): CREATE INDEX index ON table (c1, c2, c3) UNIQUE ON (c1, c2); Created index has three columns, but only two of them have unique constraint. This idea has obvious restriction. We can set unique only for first index columns. There is no clear way to maintain following index. CREATE INDEX index ON table (c1, c2, c3) UNIQUE ON (c1, c3); So I suggest following syntax: CREATE [UNIQUE {ON FIRST {COLUMN | n_unique_column COLUMNS}} INDEX ON table_name (column_name1, column_name2 ...); Examples: CREATE UNIQUE INDEX ON table_name (c1, c2, c3); // (c1,c2, c3) must be UNIQUE. That's how it works now. CREATE UNIQUE ON FIRST COLUMN INDEX ON table_name (c1, c2, c3); // (c1) must be UNIQUE CREATE UNIQUE ON FIRST 2 COLUMNS INDEX ON table_name (c1, c2, c3); // (c1,c2) must be UNIQUE CREATE UNIQUE ON FIRST 3 COLUMNS INDEX ON table_name (c1, c2, c3); // (c1,c2, c3) must be UNIQUE Next issue is pg_index changes. Now there's only a boolean flag * bool indisunique; /* is this a unique index? */ But new algorithm requires to store a single number * unit16n_unique_columns; /* number of first columns of index which has unique constrains. */ I think, that numbers of all attributes themselves are not needed. Is it right? I'd like to see your suggestions about syntax changes. And of course any other comments are welcome. -- Anastasia Lubennikova Postgres Professional:http://www.postgrespro.com The Russian Postgres Company --------------030502020706030202000606 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit Hi, hackers!

Use case:
Index-only scans is a wonderful feature that allows to speed up select queries of indexed columns.
Therefore some users want to create multicolumn indexes on columns which are queried often.
But if there's unique constraint on some column, they have to maintain another unique index.
Even if the column is already in covering index.
This adds overhead to data manipulation operations and database size.

I've started work on a patch that allows to combine covering and unique functionality.
The main idea is to allow user create multicolumn indexes with a definite number of unique columns.
For example (don't mind SQL syntax here, please):
CREATE INDEX index ON table (c1, c2, c3) UNIQUE ON (c1, c2);
Created index has three columns, but only two of them have unique constraint.

This idea has obvious restriction. We can set unique only for first index columns.
There is no clear way to maintain following index.
CREATE INDEX index ON table (c1, c2, c3) UNIQUE ON (c1, c3);

So I suggest following syntax:
CREATE [UNIQUE {ON FIRST {COLUMN | n_unique_column COLUMNS}} INDEX ON table_name (column_name1, column_name2 ...);

Examples:
CREATE UNIQUE INDEX ON table_name (c1, c2, c3); // (c1,c2, c3) must be UNIQUE. That's how it works now.

CREATE UNIQUE ON FIRST COLUMN INDEX ON table_name (c1, c2, c3); // (c1) must be UNIQUE
CREATE UNIQUE ON FIRST 2 COLUMNS INDEX ON table_name (c1, c2, c3); // (c1,c2) must be UNIQUE
CREATE UNIQUE ON FIRST 3 COLUMNS INDEX ON table_name (c1, c2, c3); // (c1,c2, c3) must be UNIQUE

Next issue is pg_index changes.
Now there's only a boolean flag
  • bool indisunique; /* is this a unique index? */
But new algorithm requires to store a single number
  • unit16 n_unique_columns; /* number of first columns of index which has unique constrains. */
I think, that numbers of all attributes themselves are not needed. Is it right?

I'd like to see your suggestions about syntax changes.
And of course any other comments are welcome.
-- 
Anastasia Lubennikova
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company
--------------030502020706030202000606--