varlena_logo Varlena
Consulting Support Training About Us Contact Us
Features
GeneralBits
eRe Reporting Tools

  Varlena (vahr-lee-nah)

A varlena is a variable length array. It is a key data structure used in the code for the database systems Postgres, Postgres95, PostgreSQL and Illustra Databases.

I first heard of varlena while working at Illustra in 1993. Rumor had it that mer's cat had been named varlena after that euphoniously named data structure. Working with gateways and then with the DataBlade API, I worked often with varlenas and data type conversions.

The name varlena originated at UCBerkeley with the Postgres project. In PostgreSQL code, the earliest reference is in .../src/include/postgres.h CVS version 1.1 imported October 31, 1996. On February 10, 2001, it migrated to .../src/include/c.h when postgres.h was separated into postgres.h and postgres_fe.h

There are some references to varlena in other google-able code occasionally, however, I suspect some Berkeley graduates were involved. There is also a reference to a character named varlena in EverQuest (tm) and I have the same suspicion. There is also the occassional use of varlena as a proper name. In fact people sometimes assume it is my name, but it is not. (My name is hard enough for most people.)

A varlena is the basis of variable length data throughout Postgres and is particularly key to the concept of Extensibility.

The elegantly simple data structure is defined in ...pgsql/src/include/c.h as:

	struct varlena
	{
		int32    vl_len;
		char     vl_dat[1];
	};
	
The first four bytes store the length and the fifth byte is the start of the variable length array. It looks like this when it has a value:
			[ 0 | 0 | 0 | 7 | V | a | r | l | e | n | a ]
		

The varlena structure is defined to hold any kind of data. The ability to map the bytes following the length word to any C data structure enables user defined data types, numerics, arrays, text types, bytea types, etc. to be stored and carried throughout the system in a uniform way.

Because any data can be stored in the data area, a varlena notoriously is not null terminated. In PostgreSQL some of the data types (e.g. cstring) that are based on varlenas do store null terminated strings in them. That is just an attribute of that particular data type. Don't count on it.


clover A. Elein Mustain Powered By Postgres