Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the maximum number of columns in a PostgreSQL select query

Do you know what the maximum number of columns that can be queried in Postgresql? I need to know this before I start my project.

like image 609
Luke101 Avatar asked Sep 26 '12 17:09

Luke101


People also ask

How many columns can be created in PostgreSQL table?

The maximum number of columns that can be accommodated in a PostgreSQL table depends on the configured block size and the type of the column. For the default block size of 8KB, at least 250 columns can be stored. This can rise to 1,600 columns if all of the columns are very simple fields, such as integer values.

Does PostgreSQL have limit?

PostgreSQL LIMIT is an optional clause of the SELECT statement that constrains the number of rows returned by the query. The statement returns row_count rows generated by the query. If row_count is zero, the query returns an empty set.


2 Answers

For others who might find this information useful the answer is 1663 depending on the types of columns occording to this post http://archives.postgresql.org/pgsql-admin/2008-05/msg00208.php

like image 41
Luke101 Avatar answered Sep 27 '22 23:09

Luke101


According to PostgreSQL Limits it's "250 - 1600 depending on column types". See note under the table. The column types affect it because in PostgreSQL rows may be at most 8kb (one page) wide, they cannot span pages. Big values in columns are OK because TOAST handles that, but there's a limit to how many columns you can fit in that depends on how wide the un-TOASTed data types used are.

(Strictly this refers to columns that can be stored in on-disk rows; queries might be able to use wider column sets than this. I don't recommend relying on it.)

If you're even thinking about approaching the column limits you're probably going to have issues.

Mapping spreadsheets to relational databases seems like the simplest thing in the world - map columns to columns, rows to rows, and go. Right? In reality, spreadsheets are huge freeform monsters that enforce no structure and can be really unweildy. Relational databases are designed to handle lots more rows, but at a cost; in the case of PostgreSQL part of that cost is a limitation to how wide it likes those rows to be. When facing spreadsheets created by Joe User this can be a real problem.

One "solution" is to decompose them into EAV, but that's unspeakably slow and ugly to work with. Better solutions are using arrays where possible, composite types, hstore, json, xml, etc.

In the end, though, sometimes the best answer is to analyse the spreadsheet using a spreadsheet.

like image 198
Craig Ringer Avatar answered Sep 23 '22 23:09

Craig Ringer