Skip to content

v1.18.0 - query_array and faster row parsing

Latest

Choose a tag to compare

@leafo leafo released this 11 Aug 14:59
· 3 commits to master since this release
v1.18.0

What's new

postgres:query_array(query_string, ...)

Identical to query, but each row in the result is an array of values ordered by column position instead of a table keyed by field name. The ordered list of field names is stored in the fields key of each result.

local res = assert(pg:query_array("select 1 as a, 'two' as b"))

res.fields --> { "a", "b" }
res[1] --> { 1, "two" }

Unlike query, NULL values are always included in rows using the postgres.NULL sentinel value, regardless of the convert_null setting, so values always align with their position in fields. Because rows are not keyed by field name, this interface also supports queries that return multiple columns with the same name.

Array rows are cheaper to construct than the hash tables returned by query (about half the allocations), making this interface a good fit for queries that return a large number of rows.

Faster row parsing

The result parsing path has been optimized: type converters are resolved once per column from the row description instead of being dispatched for every value, integer fields in protocol messages are decoded with byte arithmetic instead of substring allocations, and row tables are preallocated with table.new when available. Queries returning many rows parse about 10% faster with query, and up to 1.4x faster when combined with query_array.

Other changes

  • receive_message now returns an error instead of raising when reading the message body from the socket fails
  • Rewrote the benchmark script, run with make bench (OpenResty) or make bench_luasocket

Full Changelog: v1.17.0...v1.18.0