Navigation trail:Torque

Question:Can I use Torque with PostgreSQL ?

Answer: Yes, it would appear that several people are using Torque and PostgreSQL together successfully. You do however need to be aware of the issues outlined in the rest of this FAQ.

Question:I have problems using idMethod='native'. What's up?

Answer: Postgres creates a sequence with a special name for the autoincrement. In order to get Torque to interact gracefully with this sequence, you need to tell it the sequence name. In addition, Postgres restricts the sequence name to 30 characters, which means that the names get truncated strangely. Thus, to get everything to work, you basically need to do the following:

  1. include a <id-method-parameter name="seqName" value="Tablename_columnname_seq"/> in every table that has an "autoincrement" column.

  2. If the "tablename_columnname" combination is longer than 26 letters, it will get truncated to 26 letters by Postgres . You need to figure out the truncated name. The best way to find it is:
    • create the database via the usual Torque methods.
    • go into psql and execute "\ds tablename" - this will show you the name of the sequence.
    • go back to your schema file and set the value parameter to the name of the sequence.
    • recreate the database via the usual Torque methods. Now everything should work.

An example of a table definition (without truncation issues) is:

  <table name="TypeCategory" createLogTable="true"> 
    <column name="TypeCategoryId" autoIncrement="true" required="true"  

{{{ <column name="Name" required="true" size="32" type="VARCHAR"/>

An example of a table definition with truncation issues is:

  <table name="AssociateLogin"> 
    <column name="AssociateLoginId" required="true"  

{{{ <column name="AssociateKey" type="INTEGER"/>

    <column name="Password" size="250"  

{{{ <column name="CreateTime" type="TIMESTAMP" />

The situation with PostgreSQL 7.3 would appear to be quite different. It appears that in SQL it is enough to simply declare a column as "serial" and the necessary sequence will be created with the correct name (truncation to 26 characters no longer occurs) and will be dropped automatically when the table is dropped. This would mean that Tourque could be updated to not generate the code that creates and drops the sequence and the the id-method-parameter element is probably no longer necessary (if it is used as part of the id generation code, and I do not think it is, then the necessary value can be derrived). -- ScottEade 2003-02-06

It's a valid point. The above code works for 7.2 and Torque 3.0b3, and may be different for other versions. I think the <id-method-parameter> element is necessary because the Torque model expects the sequence name to be "tablename_seq" but Postgresql creates a sequence which is called "tablename_columnname_seq". I think once an object is inserted into the database, Torque needs a method to find out the ID of the object. It does this by fetching the current value of the sequence (which should be equal to the last value inserted into the database.) If Torque cannot figure out the sequence name correctly, then it cannot find the appropriate ID and everything goes south. (I might be incorrect about this - it may be only an issue of making sure the sequence is created and dropped, in which case Scott is absolutely right.) --PeterHamlen 2003-02-06

So this is basically a bug in Torque - i.e. it should generate "tablename_columnname_seq" rather than "tablename_seq" and perhaps still allow for the <id-method-parameter> element to allow for versions of PostgreSQL that truncate the sequence name to 26 characters. -- ScottEade 2003-02-07

To get sequences to work I added the <id-method-parameter> elements mentioned above, but then had to remove the sql that drops/creates the sequences that was generated by torque:sql before executing it with torque:insert-sql. -- ScottEade 2003-10-14

<em><strong>Question:</strong>Do I need to patch the Postgres driver to work with Torque?</em>

You do not need to apply any patches to the Postgres driver in order to use Torque <strong>unless</strong> you want to use "large objects" (CLOBS/BLOBS).

The "patch issue" dates from the time when Torque was tightly coupled with Turbine. It arose primarily because Turbine stored a hashmap of user information directly in the database. This data was stored as a BLOB. Now that Torque is decoupled from Turbine, this issue is only relevant if you use BLOBs into your schema (ie, use the datatypes BLOB or CLOB or LONGVARBINARY or LONGVARCHAR).

<em><strong>Question:</strong> So what if I am using BLOBS, etc. ?</em>

  1. You will need to patch the driver.
  2. The JDBC "BLOB" type should map to the PostgreSQL "oid" column type, but the JDBC driver does not support this automatically. The JDBC driver returns metadata that indicates that "oid" columns have JDBC type Types.INTEGER (the type of the oid ID pointer itself), so Torque (actually, the underlying Village API) doesn't treat the column properly. This can be fixed by patching the JDBC driver, as mentioned in the ["PostgreSQL" Howto].

  3. Also, for PostgreSQL 7.2 and higher, it is necessary to specify the parameter "compatible=7.1" on the JDBC URL for PreparedStatement?.setBytes to work correctly on oid columns. Note that this will also prevent the VARBINARY (bytea) types from working. Although BLOBs and best for large binary content, the LONGVARBINARY (bytea) types seem to be more portable between databases and JDBC drivers.

This seems out of date since the file has none of the code indicated. A recent, or better fix is where? --rnh

<em><strong>Question:</strong> What other issues are there?</em>

TorqueProjectPages/PostgreSQLFAQ (last edited 2009-09-20 23:04:16 by localhost)