Hi Hackers,

When I tried to select a big amount of rows, psql complains a error "Cannot add 
cell to table content: total cell count of 905032704 exceeded."

Here are the reproduce steps:
```
interma=# select version();
                                         version
-----------------------------------------------------------------------------------------
 PostgreSQL 12.13 on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 
8.3.0, 64-bit
(1 row)

interma=# create table t26(a int,b int,c int,d int,e int,f int,g int,h int,i 
int,j int,k int,l int,m int,n int,o int,p int,q int,r int,s int,t int ,u int,v 
int,w int,x int,y int,z int);
CREATE TABLE
interma=# insert into t26 select generate_series(1,200000000);
INSERT 0 200000000
interma=# select * from t26;
Cannot add cell to table content: total cell count of 905032704 exceeded.
```

I checked the related code, and root cause is clear:
```
// in printTableAddCell()
if (content->cellsadded >= content->ncolumns * content->nrows)
      report this error and exit

// cellsadded is long type, but ncolumns and nrows are int
// so, it's possible overflow the int value here.

// using a test program to verify:
int rows = 200000000;
int cols = 26;
printf("%d*%d = %d\n", rows,cols, rows*cols);

output:
      2,0000,0000*26 = 9,0503,2704 // overflow and be truncated into int value 
here
```

Based on it, I think it's a bug. We should use long for ncolumns and nrows and 
give a more obvious error message here.

My version is 12.13, and I think the latest code also exists this issue: issue: 
https://github.com/postgres/postgres/blob/1a4fd77db85abac63e178506335aee74625f6499/src/fe_utils/print.c#L3259

Any thoughts? or some other hidden reasons?
Thanks.


Reply via email to