-
Hello, I'm developing a multi-tenant application that creates a schema for each tenant. CREATE SCHEMA t_123;
CREATE TABLE t_123.users (
user_id TEXT PRIMARY KEY,
display_name TEXT NOT NULL,
primary_email TEXT NOT NULL,
); I wanted to write an efficient query for this table, so tried to use var users []*User
b := &pgx.Batch{}
b.Queue("SET search_path TO " + tenantId)
b.Queue("SELECT user_id, display_name FROM users").Query(...)
err := tx.SendBatch(ctx, b).Close() The code worked fine if it is run sequentially, but when I tried to run tests for the function
If I change the default query execution mode to Since all schemas have exactly the same tables, I wonder why |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
The query exec modes that fail all use the extended protocol to prepare / describe the queries before they are executed. The batch system minimizes the number of round trips by sending all prepares / describes at once and then sending all queries at once. So in your example the equivalent of A similar problem occurs when creating and then using temporary tables in the same batch. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
QueryExecModeExec
does use the extended protocol, but it does not describe the query before it is executed. Instead of asking PostgreSQL what types the parameters are, it tells PostgreSQL the type the arguments are encoded as. In the vast majority of cases, this works fine. However, there are cases such as when the underlying type is json/jsonb where this means pgx will not know that it needs to encode as JSON.