Thread: PQfmod and varchars
Hi list, I'm trying to find out, from a client, how many characters will fit in a varchar field (http://pgfoundry.org/tracker/index.php?func=detail&aid=1000286&group_id=1000085&atid=394). Problem is that when I do "PQfmod" on a varchar field defined as "varchar(20)", PQfmod returns "24". Now, it's fairly easy for me to subtract 4 from each result I get from PQfmod, but I have a suspicion that this "4" is actually "pointer's length", which means that I need to detect a 64 bit server and subtract 8 instead. 1. Is this suspicion correct? 2. If so, how do I detect how much to subtract. 3. If not, what is the meaning of "4"? Thanks, Shachar -- Shachar Shemesh Lingnu Open Source Consulting ltd. Have you backed up today's work? http://www.lingnu.com/backup.html
On 2005-04-22, Shachar Shemesh <psql@shemesh.biz> wrote: > Hi list, > > I'm trying to find out, from a client, how many characters will fit in a > varchar field > Problem is that when I do "PQfmod" on a varchar field defined as > "varchar(20)", PQfmod returns "24". Interpreting PQfmod requires a rather intimate knowledge of the internal type implementations. For several types, including varchar, the typmod is rather arbitrarily the type's length limit plus the size of a varlena header (which appears to be 4 bytes on all platforms, even 64-bit ones). This is arbitrary because (thanks to multibyte characters) the value doesn't actually relate to the storage size; presumably it once did in the past. A quick summary of other types (accurate I believe as of 8.0): bit: typmod is the specified length exactly bpchar (i.e. char(n)): typmod is the length + VARHDRSZ numeric: this is ugly, the typmod is ((prec << 16) | scale) + VARHDRSZ, i.e. numeric(10,2) is ((10 << 16) | 2) + 4 interval: very complex due to attempts to support sql-standard intervals time, timestamp: typmod is the specified precision exactly varbit: typmod is the maximum bit length exactly varchar: typmod is the maximum length + VARHDRSZ -- Andrew, Supernews http://www.supernews.com - individual and corporate NNTP services
Andrew - Supernews <andrew+nonews@supernews.com> writes: > Interpreting PQfmod requires a rather intimate knowledge of the internal > type implementations. > For several types, including varchar, the typmod is rather arbitrarily > the type's length limit plus the size of a varlena header (which appears > to be 4 bytes on all platforms, even 64-bit ones). Right --- it's +4 regardless of sizeof(Pointer). If we had it to do over we'd surely have eliminated that and defined the typmod as exactly the externally supplied length number ... but changing it now would break way too much stuff. It is conceivable there will someday be a compile-time option to make the varlena overhead 8 bytes, to support field widths wider than 32 bits (but don't hold your breath waiting for this). I think though that we'd probably find it wise to continue to define typmod as length limit plus 4, to avoid breaking client code that would usually not know what the server's internal representation is. regards, tom lane