I am curios about capabilities of relational DBMS to avoid extra copying while selecting from columns of fixed size types (e.g. long, timestamps, double).
These arrays are used as-is by libraries such as numpy.
create table t(d float8, s text, ts timestamp);
from multiprocessing.managers import SharedMemoryManager
from multiprocessing.shared_memory import SharedMemory
from typing import Tuple
import numpy as np
import psycopg2
conn = psycopg2.connect("dbname=test user=postgres")
cur = conn.cursor()
with SharedMemoryManager() as smm:
shared_mem = smm.SharedMemory(size = 1000 * sizeof(double))
arr = create_np_array_from_shared_mem(shared_mem, dtype('float64'), (1000,))
cur.execute(
"select d from t where ts between now() - interval '1 month' and now() limit 1000"
)
cur.fetchAsIsIntoMemory(shared_mem.buf)
arr = np.frombuffer(shared_mem.buf, dtype=dtype('float64'))
arr = arr.reshape((1000,))
print("SUCCESS!!!")
I expect fetchAsIsIntoMemory makes the driver to use memory from the pointer as a network buffer.