Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let's say I have a table with fields A and B. I make regular queries on A+B, so I created a composite index on (A,B). Would queries on only A also be fully optimized by the composite index?

Additionally, I created an index on A, but Postgres still uses the composite index for queries on only A. If the previous answer is positive, I guess it doesn't really matter, but why does it select the composite index by default, if the single A index is available?

share|improve this question
I tried to set up a small test for this. In my case, however, the two-column index was used only when I dropped the single-column one, unrelated to which one was created first. It is interesting that if I created the two-column index first, the initial plan used a bitmap heap scan. If I created the one-column index, then run the query (used index scan) and dropped the newly created index, the plan involving the two-column index switched to index scan. See the steps on SQLFiddle – dezso Oct 23 '12 at 19:38
@dezso Interesting. Where are the costs for each query? – Luciano Oct 23 '12 at 20:05
Bitmap index scan cost: 107.98, 43 ms execution time. Index scan one-column: cost 8.69, two-column: 43.69. Execution times do not differ significantly (the fluctuation is bigger than the difference between the two). – dezso Oct 23 '12 at 20:10
@Luciano Can you show the explain analyze and the query text? – Craig Ringer Oct 23 '12 at 22:32

It certainly is. We discussed that in great detail under this related question.

It also depends on the data types of the indexed columns. An index on, say, two integer columns is exactly as big as an index on one. Space is typically allocated in chunks of 8 bytes. For one integer (4 bytes) that's another 4 bytes of padding.

The keywords here are data alignment and MAXALIGN: usually 4 Bytes on a 32-bit OS or 8 Bytes on a 64-bit OS. If you are not sure, check out pg_controldata.

In such a case there is really no downside for the planner to use an index on (a,b) - compared to an index on just (a). And it is generally preferable for multiple queries to use the same index. The chance for it to reside in the cache already (or at least parts of it) grows when shared.

If you already maintain an index on (a,b), then it doesn't make sense to create another index on just (a) - unless it is substantially smaller. The same is not true for (b,a) vs. (a). Follow the link in the first line for more on that.

On the other hand, there is a potential downside to including an additional column in the index, even if that only utilizes space otherwise wasted for padding. If the additional column is updated, the index needs an update, too. Also prevents potential HOT (Heap Only Tuple) updates on the table when this column is involved. More on HOT updates in this related answer on SO.

More on how to measure object sizes in this related answer.

share|improve this answer
This caching effect was my target in the small test described above - what I got I think is 'negative'. – dezso Oct 24 '12 at 6:22
@dezso: Did you maybe forget to ANALYZE the newly created table? I can't make sense of the results you describe in the above comment otherwise. – Erwin Brandstetter Oct 24 '12 at 19:37
Say I already had an index on column a. If I now realize that I need an index on (a, b), should I drop the single-column index on a? – Zoltán Jun 19 '13 at 14:58
@Zoltán: It depends. For very small columns (like two integer) the index on (a,b) would make an additional index on (a) pointless. For bigger columns it may be different. Start a question if you need more details. – Erwin Brandstetter Jun 19 '13 at 16:58
@WAF: Similar in MySQL, except for gory details:…,… – Erwin Brandstetter Dec 11 '14 at 22:43

It is in the case if you just use just first in the predicate.

It will do scan if you use first columns of composite key and non-key column of composite key.

To trick it you can just dummy predicates like this and then non-key column:

[A,B] is your index, [C] - another column

To utilize index you write as:

share|improve this answer
@dezso It will use index only in the case if there are one or two predicates [A] Or [A],[B]. It will not use it in the order [B],[A] or [A],[C]. To be able to utilize index with additional column [C], you need to enforce index by ordering predicates as [A],[B] and [C]. – Farfarak Oct 24 '12 at 9:52
Did I miss something or your answer really does not address the question ("why does it select the composite index by default, if the single A index is available?") ? – dezso Oct 24 '12 at 9:53
Of course. But this still does not explain why the single column index was not used even when only that column was searched. – dezso Oct 24 '12 at 9:55
What exactly do you achieve with B=B? I think you achieve nothing so I'm voting down absent any evidence this isn't just ignored by the optimizer – Jack Douglas Oct 24 '12 at 9:57
B=B is effectively the same as B IS NOT NULL, which seems uncalled for. Certainly not needed to use an index on (a,b). – Erwin Brandstetter May 27 '14 at 15:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.