Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get array base type in postgres via jdbc

How to get base type / dimension of an array column in Postgres by Java?

I have a tables that contain arrays like int[][] and text[].

When I traverse the metadata from JDBC I can only get type as java.sql.Array. Even in information_schema.columns it stored simply as ARRAY.

How can I know the base type and its dimension?

I'm now working on tool to dump table info.

like image 605
user1192878 Avatar asked Mar 13 '26 00:03

user1192878


2 Answers

For the array base type, DatabaseMetaData.getColumns() returns a ResultSet containing column metadata. One of the columns returned is TYPE_NAME. This appears to contain the name of the array base type, prefixed with an underscore. For example, _int4 or _text. There is some additional information about the type in pg_type that may be helpful.

For the dimensions, it appears unlikely that they will be in the metadata. From the documentation:

However, the current implementation ignores any supplied array size limits, i.e., the behavior is the same as for arrays of unspecified length.

The current implementation does not enforce the declared number of dimensions either. Arrays of a particular element type are all considered to be of the same type, regardless of size or number of dimensions. So, declaring the array size or number of dimensions in CREATE TABLE is simply documentation; it does not affect run-time behavior.

The array_dims function will return the current dimensions of an array value. But since this could be different for every row in the table, I doubt this will help you.

UPDATE: It appears the dimensions are available in the metadata. See @a_horse_with_no_name's answer.

like image 127
Joe F Avatar answered Mar 14 '26 13:03

Joe F


You can query pg_attributes directly:

select att.attname, 
       att.attndims, 
       pg_catalog.format_type(atttypid, NULL) as display_type 
from pg_attribute att 
  join pg_class tbl on tbl.oid = att.attrelid  
  join pg_namespace ns on tbl.relnamespace = ns.oid  
where tbl.relname = 'your_table_name'  
  and ns.nspname = 'table_schema'

SQLFiddle example: http://sqlfiddle.com/#!12/50301/1

Note that format_type() will actually return integer[] even though the column was defined as int[][] but the attndims column will carry the information you want.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!