Description
- asyncpg version: 0.8.4
- PostgreSQL version: 9.5
- Python version: 3.6
- Platform: Linux (Alpine, on Docker)
- Do you use pgbouncer?: no
- Do you use the wheel package or built locally
(if locally, which version of Cython was used)?: wheel - Can you the issue be reproduced when running under
uvloop?: yes
I'd like to override json's encode/decode to avoid the need to dumps/loads my data-structure myself. However, I don't see any examples of how to override an existing codec so I end using connection pool's setup flag (as it's the only way I see to call set_type_codec
). I end up with something like:
async def setup(conn):
await conn.set_type_codec(
'json', encoder=json.dumps, decoder=json.loads, schema='pg_catalog'
)
This doesn't seem to do anything. So I add the binary=True
flag and get:
cannot override codec for type 114
Since this runs on every .acquire
(and, I guess, since there's an existing codec). I'd need to somehow prevent that since I only need to call it once per connection creation. So, I tried using a weakvaluedictionary:
CODECS_SET = weakref.WeakValueDictionary()
async def setup(conn):
conn_id = id(conn)
if conn_id not in CODECS_SET:
await conn.set_type_codec(
'json', encoder=json.dumps, decoder=json.loads, schema='pg_catalog', binary=True
)
CODECS_SET[conn_id] = conn
But this isn't permitted since connections don't have a __weakref__
attribute due to the use of __slots__
:
https://docs.python.org/3.6/reference/datamodel.html#notes-on-using-slots
So this is really a few questions:
- How do I replace the JSON codec?
- How am I supposed to do something for connection instantiation rather than acquire? (Like this, or prepared statement preload, etc.)
- Where can I read more about how the various codecs are laid out in binary format?