Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dWm4y-0001Bm-7w for pgsql-jdbc@arkaria.postgresql.org; Sun, 16 Jul 2017 16:07:32 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dWm4x-0005CF-LH for pgsql-jdbc@arkaria.postgresql.org; Sun, 16 Jul 2017 16:07:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dWm4S-0004D7-TC for pgsql-jdbc@postgresql.org; Sun, 16 Jul 2017 16:07:00 +0000 Received: from mail-it0-x22f.google.com ([2607:f8b0:4001:c0b::22f]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1dWm4O-0006U1-8F for pgsql-jdbc@postgresql.org; Sun, 16 Jul 2017 16:07:00 +0000 Received: by mail-it0-x22f.google.com with SMTP id l132so3561432ita.1 for ; Sun, 16 Jul 2017 09:06:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=seespotcode-net.20150623.gappssmtp.com; s=20150623; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=kfEwgdZoR02GiyOSUUc7oWtlyenpHYOlZbW61a1r9l0=; b=YzsK5bWEUTmu6W+7OSDFaa6Wr/xrcW6MVPjJQzSvZRLpg90f//Rc0s0MuPJ96dog6x kZ0c9RP/HDSMOZRRQhboBdRTjAtq8i0Z/PLgkuq2mRt9w9zteTL0eNtfw6LHDJz1ctDS HVbzCAIiBARgyZSIajfOKvNVuQPMw58ylWzTd/ychOlWhu4Cox+KRLE3iIJlE1slqzNj H98z68SbHRTeL09h6/Ke+TuUyBYTm0L+f+H+hPv3z+qaez9oEgdNrzSDuYCe5H/AxLEB Ki5vw5WHK/bYcpqrTNHqzKncg1AGpCmvwLh4UHr+y07wV1fdbf9HA3943+nUd6haO7R4 l7mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=kfEwgdZoR02GiyOSUUc7oWtlyenpHYOlZbW61a1r9l0=; b=TjnysnrWuTBNzy2BeRtcGn1iVuwItcBGUZRPh3OeFu7e341JCDTop812hZLQK8C0Sw M0j5SmQfA8eDEMEup/nL75ciz0XRz/vOrY5cLgrlTLyyp8e1ksEYkczzcytxW7UZtuB5 t6npWDfufn9+7dWAdNa5fD6XA7nS1mEmWbKFypEGxmAKZRJvsL1LziVmHT6e4peHVrZF ZqxJ/UnPNaAdVM2CGngp7PxYQqVUHW/Ql048dW+2RGZtsqAzDr+XOmdYqX1m0iTYG+kw I0uzUSDDYVtGyUyw5e7+wb2b/8/3ycIhd8rrlo8LQbb1KkmYzJotwt9dcSuyI/cy/nd/ VJ2g== X-Gm-Message-State: AIVw111qmQoWhD49ui3s6r4B4oECVf4Sgh0AzG6VkSqn9I3W/VSdu9zB kk3Xj3X4YlZzzj7i X-Received: by 10.36.82.77 with SMTP id d74mr2044960itb.119.1500221213608; Sun, 16 Jul 2017 09:06:53 -0700 (PDT) Received: from [192.168.1.4] (71-8-133-184.dhcp.stcd.mn.charter.com. [71.8.133.184]) by smtp.gmail.com with ESMTPSA id j21sm4560749itb.15.2017.07.16.09.06.52 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 16 Jul 2017 09:06:53 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2104\)) Subject: Re: TypeInfoCache.getPGType(pgTypeName) drops schema name when caching From: Michael Glaesemann In-Reply-To: Date: Sun, 16 Jul 2017 11:06:51 -0500 Cc: List Content-Transfer-Encoding: quoted-printable Message-Id: <7748FA42-7355-4B56-B9B7-F19ABE096A9A@seespotcode.net> References: <72F45272-226E-47FC-940F-F5E5FF446222@seespotcode.net> To: Vladimir Sitnikov X-Mailer: Apple Mail (2.2104) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org > On 2017-07-16, at 01:53, Vladimir Sitnikov = wrote: >=20 > Michael>_pgNameToOid.put(schema + "." + name, oid) > Michael>I think it makes sense to follow this behavior in getPGType(pgTyp= eName).=20 >=20 > The idea of type cache is to avoid pg_catalog SQLs on the hot path. > Consider a user issuing `createArrayOf`. Do you mean pgjdbc should query = the catalog on each invocation of createArrayOf? I'm not sure it would be t= he right thing, as it is basically the only way to create arrays. Sorry if I wasn't clear. I understand the purpose of the cache and agree th= at make sense to cache type information. What I'm trying to get at is the c= ached name values are different depending on whether getPGType(pgTypeName) = or getPGPGType(oid) are used resulting in inconsistent behavior. Here's an example that illustrates what I'm seeing. > cat GetPGTypeTest.java /** export CLASSPATH=3D"postgresql-42.1.3.jar:." javac GetPGTypeTest.java && java -Djdbc.drivers=3Dorg.postgresql.Driver = GetPGTypeTest export PGPORT=3D5496 creatdb get_pg_type_test; psql -d get_pg_type_test -c "CREATE SCHEMA cars;" psql -d get_pg_type_test -c "CREATE TYPE cars.color AS ENUM ('black')" Any customer can have a car painted any color that he wants so long as i= t is black. -- Henry Ford */ import java.sql.*; import org.postgresql.util.*; public class GetPGTypeTest { static String url =3D "jdbc:postgresql://localhost:5496/get_pg_type_tes= t"; static String sql =3D "SELECT CAST(? AS cars.color)"; static String color =3D "black"; static String colorType =3D "cars.color"; static String getColumnTypeName(PreparedStatement st) throws SQLExcepti= on { ResultSet rs =3D st.executeQuery(); ResultSetMetaData md =3D rs.getMetaData(); rs.close(); return md.getColumnTypeName(1); } static String getStringTypeName(Connection conn) throws SQLException { PreparedStatement st =3D conn.prepareStatement(sql); st.setString(1, color); return getColumnTypeName(st); } static String getObjectTypeName(Connection conn) throws SQLException { PreparedStatement st =3D conn.prepareStatement(sql); PGobject colorObject =3D new PGobject(); colorObject.setType(colorType); colorObject.setValue(color); st.setObject(1, colorObject); return getColumnTypeName(st); } public static void main(String[] args) { Connection conn =3D null; try { System.out.println("## setString first -- caching via getPGType= (oid)"); conn =3D DriverManager.getConnection(url); System.out.format("string: %s%n", getStringTypeName(conn)); System.out.format("object: %s%n", getObjectTypeName(conn)); System.out.println("## setObject first -- caching via getPGType= (pgTypeName)"); conn =3D DriverManager.getConnection(url); System.out.format("object: %s%n", getObjectTypeName(conn)); System.out.format("string: %s%n", getStringTypeName(conn)); } catch (SQLException e) { System.err.println(e.getMessage()); System.exit(1); } } } > export CLASSPATH=3D"postgresql-42.1.3.jar:." > javac GetPGTypeTest.java > java -Djdbc.drivers=3Dorg.postgresql.Driver GetPGTypeTest ## setString first -- caching via getPGType(oid) string: "cars"."color" object: "cars"."color" ## setObject first -- caching via getPGType(pgTypeName) object: color string: color The caching to the cars.colors type name is different depending on whether = getPGType(pgTypeName) and getPGType(oid) is doing the caching. getPGType(pg= TypeName) doesn't include the schema name when the type name is cached. Thi= s is clear from the differences in code used to generate the type names: In getPGType(pgTypeName): https://github.com/pgjdbc/pgjdbc/blob/cb3995b5a0311a2f5f7737fdfe83457680305= efb/pgjdbc/src/main/java/org/postgresql/jdbc/TypeInfoCache.java#L376-L377 String internalName =3D rs.getString(2); // typname, from getOidStatement _oidToPgName.put(oid, internalName); In getPGType(oid): https://github.com/pgjdbc/pgjdbc/blob/cb3995b5a0311a2f5f7737fdfe83457680305= efb/pgjdbc/src/main/java/org/postgresql/jdbc/TypeInfoCache.java#L412-L432 pgTypeName =3D "\"" + schema + "\".\"" + name + "\""; // ... _oidToPgName.put(oid, pgTypeName); The behavior is inconsistent and arguably wrong in the case of getPGType(pg= TypeName): if I have types with the same pg_type.typname but in different s= chemas, ResultSetMetaData.getColumnTypeName doesn't provide a way to distin= guish between them if getPGType(pgTypeName) is doing the caching. I hope this is a clearer explanation of the behavior. If it isn't, please l= et me know. Best, Michael Glaesemann grzm seespotcode net --=20 Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc