summaryrefslogtreecommitdiffstats
path: root/tests/scripts/pipeline-demo.py
blob: ec952293a18d79209ab7cce649935f9d596201fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"""Pipeline mode demo

This reproduces libpq_pipeline::pipelined_insert PostgreSQL test at
src/test/modules/libpq_pipeline/libpq_pipeline.c::test_pipelined_insert().

We do not fetch results explicitly (using cursor.fetch*()), this is
handled by execute() calls when pgconn socket is read-ready, which
happens when the output buffer is full.
"""
import argparse
import asyncio
import logging
from contextlib import contextmanager
from functools import partial
from typing import Any, Iterator, Optional, Sequence, Tuple

from psycopg import AsyncConnection, Connection
from psycopg import pq, waiting
from psycopg import errors as e
from psycopg.abc import PipelineCommand
from psycopg.generators import pipeline_communicate
from psycopg.pq import Format, DiagnosticField
from psycopg._compat import Deque

psycopg_logger = logging.getLogger("psycopg")
pipeline_logger = logging.getLogger("pipeline")
args: argparse.Namespace


class LoggingPGconn:
    """Wrapper for PGconn that logs fetched results."""

    def __init__(self, pgconn: pq.abc.PGconn, logger: logging.Logger):
        self._pgconn = pgconn
        self._logger = logger

        def log_notice(result: pq.abc.PGresult) -> None:
            def get_field(field: DiagnosticField) -> Optional[str]:
                value = result.error_field(field)
                return value.decode("utf-8", "replace") if value else None

            logger.info(
                "notice %s %s",
                get_field(DiagnosticField.SEVERITY),
                get_field(DiagnosticField.MESSAGE_PRIMARY),
            )

        pgconn.notice_handler = log_notice

        if args.trace:
            self._trace_file = open(args.trace, "w")
            pgconn.trace(self._trace_file.fileno())

    def __del__(self) -> None:
        if hasattr(self, "_trace_file"):
            self._pgconn.untrace()
            self._trace_file.close()

    def __getattr__(self, name: str) -> Any:
        return getattr(self._pgconn, name)

    def send_query(self, command: bytes) -> None:
        self._logger.warning("PQsendQuery broken in libpq 14.5")
        self._pgconn.send_query(command)
        self._logger.info("sent %s", command.decode())

    def send_query_params(
        self,
        command: bytes,
        param_values: Optional[Sequence[Optional[bytes]]],
        param_types: Optional[Sequence[int]] = None,
        param_formats: Optional[Sequence[int]] = None,
        result_format: int = Format.TEXT,
    ) -> None:
        self._pgconn.send_query_params(
            command, param_values, param_types, param_formats, result_format
        )
        self._logger.info("sent %s", command.decode())

    def send_query_prepared(
        self,
        name: bytes,
        param_values: Optional[Sequence[Optional[bytes]]],
        param_formats: Optional[Sequence[int]] = None,
        result_format: int = Format.TEXT,
    ) -> None:
        self._pgconn.send_query_prepared(
            name, param_values, param_formats, result_format
        )
        self._logger.info("sent prepared '%s' with %s", name.decode(), param_values)

    def send_prepare(
        self,
        name: bytes,
        command: bytes,
        param_types: Optional[Sequence[int]] = None,
    ) -> None:
        self._pgconn.send_prepare(name, command, param_types)
        self._logger.info("prepare %s as '%s'", command.decode(), name.decode())

    def get_result(self) -> Optional[pq.abc.PGresult]:
        r = self._pgconn.get_result()
        if r is not None:
            self._logger.info("got %s result", pq.ExecStatus(r.status).name)
        return r


@contextmanager
def prepare_pipeline_demo_pq(
    pgconn: LoggingPGconn, rows_to_send: int, logger: logging.Logger
) -> Iterator[Tuple[Deque[PipelineCommand], Deque[str]]]:
    """Set up pipeline demo with initial queries and yield commands and
    results queue for pipeline_communicate().
    """
    logger.debug("enter pipeline")
    pgconn.enter_pipeline_mode()

    setup_queries = [
        ("begin", "BEGIN TRANSACTION"),
        ("drop table", "DROP TABLE IF EXISTS pq_pipeline_demo"),
        (
            "create table",
            (
                "CREATE UNLOGGED TABLE pq_pipeline_demo("
                " id serial primary key,"
                " itemno integer,"
                " int8filler int8"
                ")"
            ),
        ),
        (
            "prepare",
            ("INSERT INTO pq_pipeline_demo(itemno, int8filler)" " VALUES ($1, $2)"),
        ),
    ]

    commands = Deque[PipelineCommand]()
    results_queue = Deque[str]()

    for qname, query in setup_queries:
        if qname == "prepare":
            pgconn.send_prepare(qname.encode(), query.encode())
        else:
            pgconn.send_query_params(query.encode(), None)
        results_queue.append(qname)

    committed = False
    synced = False

    while True:
        if rows_to_send:
            params = [f"{rows_to_send}".encode(), f"{1 << 62}".encode()]
            commands.append(partial(pgconn.send_query_prepared, b"prepare", params))
            results_queue.append(f"row {rows_to_send}")
            rows_to_send -= 1

        elif not committed:
            committed = True
            commands.append(partial(pgconn.send_query_params, b"COMMIT", None))
            results_queue.append("commit")

        elif not synced:

            def sync() -> None:
                pgconn.pipeline_sync()
                logger.info("pipeline sync sent")

            synced = True
            commands.append(sync)
            results_queue.append("sync")

        else:
            break

    try:
        yield commands, results_queue
    finally:
        logger.debug("exit pipeline")
        pgconn.exit_pipeline_mode()


def pipeline_demo_pq(rows_to_send: int, logger: logging.Logger) -> None:
    pgconn = LoggingPGconn(Connection.connect().pgconn, logger)
    with prepare_pipeline_demo_pq(pgconn, rows_to_send, logger) as (
        commands,
        results_queue,
    ):
        while results_queue:
            fetched = waiting.wait(
                pipeline_communicate(
                    pgconn,  # type: ignore[arg-type]
                    commands,
                ),
                pgconn.socket,
            )
            assert not commands, commands
            for results in fetched:
                results_queue.popleft()
                for r in results:
                    if r.status in (
                        pq.ExecStatus.FATAL_ERROR,
                        pq.ExecStatus.PIPELINE_ABORTED,
                    ):
                        raise e.error_from_result(r)


async def pipeline_demo_pq_async(rows_to_send: int, logger: logging.Logger) -> None:
    pgconn = LoggingPGconn((await AsyncConnection.connect()).pgconn, logger)

    with prepare_pipeline_demo_pq(pgconn, rows_to_send, logger) as (
        commands,
        results_queue,
    ):
        while results_queue:
            fetched = await waiting.wait_async(
                pipeline_communicate(
                    pgconn,  # type: ignore[arg-type]
                    commands,
                ),
                pgconn.socket,
            )
            assert not commands, commands
            for results in fetched:
                results_queue.popleft()
                for r in results:
                    if r.status in (
                        pq.ExecStatus.FATAL_ERROR,
                        pq.ExecStatus.PIPELINE_ABORTED,
                    ):
                        raise e.error_from_result(r)


def pipeline_demo(rows_to_send: int, many: bool, logger: logging.Logger) -> None:
    """Pipeline demo using sync API."""
    conn = Connection.connect()
    conn.autocommit = True
    conn.pgconn = LoggingPGconn(conn.pgconn, logger)  # type: ignore[assignment]
    with conn.pipeline():
        with conn.transaction():
            conn.execute("DROP TABLE IF EXISTS pq_pipeline_demo")
            conn.execute(
                "CREATE UNLOGGED TABLE pq_pipeline_demo("
                " id serial primary key,"
                " itemno integer,"
                " int8filler int8"
                ")"
            )
            query = "INSERT INTO pq_pipeline_demo(itemno, int8filler) VALUES (%s, %s)"
            params = ((r, 1 << 62) for r in range(rows_to_send, 0, -1))
            if many:
                cur = conn.cursor()
                cur.executemany(query, list(params))
            else:
                for p in params:
                    conn.execute(query, p)


async def pipeline_demo_async(
    rows_to_send: int, many: bool, logger: logging.Logger
) -> None:
    """Pipeline demo using async API."""
    aconn = await AsyncConnection.connect()
    await aconn.set_autocommit(True)
    aconn.pgconn = LoggingPGconn(aconn.pgconn, logger)  # type: ignore[assignment]
    async with aconn.pipeline():
        async with aconn.transaction():
            await aconn.execute("DROP TABLE IF EXISTS pq_pipeline_demo")
            await aconn.execute(
                "CREATE UNLOGGED TABLE pq_pipeline_demo("
                " id serial primary key,"
                " itemno integer,"
                " int8filler int8"
                ")"
            )
            query = "INSERT INTO pq_pipeline_demo(itemno, int8filler) VALUES (%s, %s)"
            params = ((r, 1 << 62) for r in range(rows_to_send, 0, -1))
            if many:
                cur = aconn.cursor()
                await cur.executemany(query, list(params))
            else:
                for p in params:
                    await aconn.execute(query, p)


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-n",
        dest="nrows",
        metavar="ROWS",
        default=10_000,
        type=int,
        help="number of rows to insert",
    )
    parser.add_argument(
        "--pq", action="store_true", help="use low-level psycopg.pq API"
    )
    parser.add_argument(
        "--async", dest="async_", action="store_true", help="use async API"
    )
    parser.add_argument(
        "--many",
        action="store_true",
        help="use executemany() (not applicable for --pq)",
    )
    parser.add_argument("--trace", help="write trace info into TRACE file")
    parser.add_argument("-l", "--log", help="log file (stderr by default)")

    global args
    args = parser.parse_args()

    psycopg_logger.setLevel(logging.DEBUG)
    pipeline_logger.setLevel(logging.DEBUG)
    if args.log:
        psycopg_logger.addHandler(logging.FileHandler(args.log))
        pipeline_logger.addHandler(logging.FileHandler(args.log))
    else:
        psycopg_logger.addHandler(logging.StreamHandler())
        pipeline_logger.addHandler(logging.StreamHandler())

    if args.pq:
        if args.many:
            parser.error("--many cannot be used with --pq")
        if args.async_:
            asyncio.run(pipeline_demo_pq_async(args.nrows, pipeline_logger))
        else:
            pipeline_demo_pq(args.nrows, pipeline_logger)
    else:
        if pq.__impl__ != "python":
            parser.error(
                "only supported for Python implementation (set PSYCOPG_IMPL=python)"
            )
        if args.async_:
            asyncio.run(pipeline_demo_async(args.nrows, args.many, pipeline_logger))
        else:
            pipeline_demo(args.nrows, args.many, pipeline_logger)


if __name__ == "__main__":
    main()