@@ -182,24 +182,34 @@ static u64 db_postgres_column_u64(struct db_stmt *stmt, int col)
182182
183183static s64 db_postgres_column_int (struct db_stmt * stmt , int col )
184184{
185- PGresult * res = (PGresult * )stmt -> inner_stmt ;
186- size_t actual = PQgetlength (res , stmt -> row , col );
187-
188- /* CockroachDB returns 8 bytes for INTEGER, PostgreSQL returns 4 */
189- if (actual == 4 ) {
190- be32 bin ;
191- memcpy (& bin , PQgetvalue (res , stmt -> row , col ), sizeof (bin ));
192- return be32_to_cpu (bin );
193- } else if (actual == 8 ) {
194- be64 bin ;
195- memcpy (& bin , PQgetvalue (res , stmt -> row , col ), sizeof (bin ));
196- return be64_to_cpu (bin );
197- }
198-
199- db_fatal (stmt -> db ,
200- "integer field size unexpected: expected 4 or 8, actual %zu\n" ,
201- actual );
202- return 0 ; /* Never reached, but silences compiler warning */
185+ PGresult * res = (PGresult * )stmt -> inner_stmt ;
186+ Oid col_type = PQftype (res , col ); // Get the column's type OID
187+
188+ if (col_type == INT4OID ) {
189+ /* This is a 32-bit integer (PG INT or CRDB INT4) */
190+ if (PQgetlength (res , stmt -> row , col ) != 4 ) {
191+ /* This check is now for safety/correctness */
192+ db_fatal (stmt -> db , "INT4 field size not 4 bytes\n" );
193+ }
194+ be32 bin ;
195+ memcpy (& bin , PQgetvalue (res , stmt -> row , col ), sizeof (bin ));
196+ return (s64 )be32_to_cpu (bin ); // Cast to s64 for return
197+ }
198+ else if (col_type == INT8OID ) {
199+ /* This is a 64-bit integer (PG BIGINT or CRDB INT) */
200+ if (PQgetlength (res , stmt -> row , col ) != 8 ) {
201+ db_fatal (stmt -> db , "INT8 field size not 8 bytes\n" );
202+ }
203+ be64 bin ;
204+ memcpy (& bin , PQgetvalue (res , stmt -> row , col ), sizeof (bin ));
205+ return be64_to_cpu (bin );
206+ }
207+
208+ /* This function was called on an unsupported column type */
209+ db_fatal (stmt -> db ,
210+ "integer field type unexpected: expected INT4 (23) or INT8 (20), actual %u\n" ,
211+ col_type );
212+ return 0 ;
203213}
204214
205215static size_t db_postgres_column_bytes (struct db_stmt * stmt , int col )
0 commit comments