Skip to content

Commit e1bf652

Browse files
author
Neil Conway
committed
Prevent a backend crash when processing CREATE TABLE commands with
more than 65K columns, or when the created table has more than 65K columns due to adding inherited columns from parent relations. Fix a similar crash when processing SELECT queries with more than 65K target list entries. In all three cases we would eventually detect the error and elog, but the check was being made too late.
1 parent 8a1821a commit e1bf652

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/backend/commands/tablecmds.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.139 2004/11/05 19:15:57 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.140 2004/11/16 23:34:22 neilc Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -680,6 +680,23 @@ MergeAttributes(List *schema, List *supers, bool istemp,
680680
* defaults */
681681
int child_attno;
682682

683+
/*
684+
* Check for and reject tables with too many columns. We perform
685+
* this check relatively early for two reasons: (a) we don't run
686+
* the risk of overflowing an AttrNumber in subsequent code (b) an
687+
* O(n^2) algorithm is okay if we're processing <= 1600 columns,
688+
* but could take minutes to execute if the user attempts to
689+
* create a table with hundreds of thousands of columns.
690+
*
691+
* Note that we also need to check that any we do not exceed this
692+
* figure after including columns from inherited relations.
693+
*/
694+
if (list_length(schema) > MaxHeapAttributeNumber)
695+
ereport(ERROR,
696+
(errcode(ERRCODE_TOO_MANY_COLUMNS),
697+
errmsg("tables can have at most %d columns",
698+
MaxHeapAttributeNumber)));
699+
683700
/*
684701
* Check for duplicate names in the explicit list of attributes.
685702
*
@@ -979,6 +996,16 @@ MergeAttributes(List *schema, List *supers, bool istemp,
979996
}
980997

981998
schema = inhSchema;
999+
1000+
/*
1001+
* Check that we haven't exceeded the legal # of columns after
1002+
* merging in inherited columns.
1003+
*/
1004+
if (list_length(schema) > MaxHeapAttributeNumber)
1005+
ereport(ERROR,
1006+
(errcode(ERRCODE_TOO_MANY_COLUMNS),
1007+
errmsg("tables can have at most %d columns",
1008+
MaxHeapAttributeNumber)));
9821009
}
9831010

9841011
/*

src/backend/parser/analyze.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.312 2004/09/27 04:12:02 neilc Exp $
9+
* $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.313 2004/11/16 23:34:26 neilc Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -396,6 +396,18 @@ transformStmt(ParseState *pstate, Node *parseTree,
396396
result->querySource = QSRC_ORIGINAL;
397397
result->canSetTag = true;
398398

399+
/*
400+
* Check that we did not produce too many resnos; at the very
401+
* least we cannot allow more than 2^16, since that would exceed
402+
* the range of a AttrNumber. It seems safest to use
403+
* MaxTupleAttributeNumber.
404+
*/
405+
if (pstate->p_next_resno - 1 > MaxTupleAttributeNumber)
406+
ereport(ERROR,
407+
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
408+
errmsg("target lists can have at most %d entries",
409+
MaxTupleAttributeNumber)));
410+
399411
return result;
400412
}
401413

0 commit comments

Comments
 (0)