PgCache 0.9.4

0.9.4 gives literals, casts and bound parameters the types the origin gives them, so a cached result matches in the cases where the two used to choose differently. It also checks its publication for changes made by anyone else, and forwards reads on tables it cannot cache without examining them again. The changelog lists every change.

The Publication Is PgCache’s Alone

PgCache recreates its publication at every start, then adds and removes tables as queries are cached and evicted. Nothing else is meant to change it. Before 0.9.4 nothing noticed when something did: a cached table taken out of the publication stopped sending its changes, and PgCache went on serving the rows it had.

PgCache now compares the publication with what it put there, every 5 seconds:

Change made by someone elseWhat PgCache does
A table or a schema addedRemoves it and keeps the cache. The entry only put more on the stream; no change was missed.
A cached table removedResets the cache.
A row filter or a column list put on a cached tableResets the cache.
The publish options changed, or the publication made FOR ALL TABLESResets the cache.
The publication droppedResets the cache.

A reset discards the cached data, recreates the publication and forwards reads to the origin until queries are cached again, as a lost schema-change signal does. Changes may have been missed before the check ran, so a read can be stale for up to 5 seconds after such a change.

The log says what was found. A reset logs publication changed outside pgcache, resetting cache with the reason; a removed entry logs the publication holds entries pgcache did not add: removing them.

A table that someone had already added to the publication also no longer blocks its queries from being cached. PgCache removes the entry and adds the table itself.

Values Typed as the Origin Types Them

A cached query is evaluated in two places. The cache database runs it, and the proxy checks each changed row against it to keep the result current. Both have to give a literal, a cast or a bound parameter the type the origin gives it. Where they chose differently, a cached result could gain or lose a row, or the cache database raised an error and the read was forwarded every time.

0.9.4 closes these gaps:

  • Decimal literals keep every digit. They were held as doubles, which rounded a long literal.
  • Integer literals are integer when they fit and bigint otherwise, whatever they are compared with. They used to take the column’s type, so smallint_col < 100000 raised “out of range” on the cache at every execution, and real_col = 16777217 rounded the literal and matched 16777216.
  • real columns are widened to double precision for a comparison with a decimal, so a stored 0.1 does not equal the literal 0.1, as on the origin.
  • Casts of text read the text by PostgreSQL’s input rules: surrounding whitespace, digit underscores and 0x, 0o and 0b prefixes for ::int and ::bigint; any prefix of true, false, yes and no for ::bool. A ::numeric cast is now checked in the proxy when a row changes, without a round trip to the cache database.
  • Bound parameters keep the type the client declared, in text and in binary format. real and double precision stay floats, bigint and smallint keep their width, and a bool or an array sent as text is read under its declared type.

You may see fewer handle_cached_query failed lines with SQLSTATE 22003 in the log, and hits on queries that were forwarded every time. No setting is involved.

Tables That Cannot Be Cached

PgCache does not cache a table without a primary key, a view, or a table whose row-level security policy it cannot apply, and forwards reads on them. Before 0.9.4 each such read was examined again, at the cost of a catalog lookup on the origin, one read at a time. That held these reads well below what the origin serves directly.

PgCache now remembers the refusal for the table, or for the role when a policy decides it, and forwards later reads at once. It forgets when the origin runs DDL, a policy changes or a role changes, so a table that gains a primary key is cached without a restart.

The log tells these apart from a missing table: table public.orders has no primary key, relation order_totals is not a table.

Also in This Release

  • A read after an insert the column converts includes the new row. When the column rounds, pads or truncates the inserted value, a read on the same connection could be served from the cache before the row arrived.
  • A bare table name resolves to the first relation of that name on the search_path, as on the origin, and resolves again after DDL. A view earlier on the path is no longer passed over for a table behind it, and a new table that shadows a cached one takes effect without a restart.