1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: postgresql-socket-api.lisp
6 ;;;; Purpose: Low-level PostgreSQL interface using sockets
7 ;;;; Programmers: Kevin M. Rosenberg based on
8 ;;;; Original code by Pierre R. Mai
10 ;;;; Date Started: Feb 2002
14 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
15 ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai
17 ;;;; CLSQL users are granted the rights to distribute and use this software
18 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
19 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
20 ;;;; *************************************************************************
23 ;;;; Changes by Kevin Rosenberg
24 ;;;; - Added socket open functions for Allegro and Lispworks
25 ;;;; - Changed CMUCL FFI to UFFI
26 ;;;; - Added necessary (force-output) for socket streams on
27 ;;;; Allegro and Lispworks
28 ;;;; - Added initialization variable
29 ;;;; - Added field type processing
32 (in-package #:postgresql-socket)
34 (uffi:def-enum pgsql-ftype
42 (defmethod clsql-base-sys:database-type-library-loaded ((database-type
43 (eql :postgresql-socket)))
44 "T if foreign library was able to be loaded successfully. Always true for
48 (defmethod clsql-base-sys:database-type-load-foreign ((database-type (eql :postgresql-socket)))
54 (defmacro define-message-constants (description &rest clauses)
55 (assert (evenp (length clauses)))
56 (loop with seen-characters = nil
57 for (name char) on clauses by #'cddr
58 for char-code = (char-code char)
59 for doc-string = (format nil "~A (~:C): ~A" description char name)
60 if (member char seen-characters)
61 do (error "Duplicate message type ~@C for group ~A" char description)
64 `(defconstant ,name ,char-code ,doc-string)
66 and do (push char seen-characters)
68 (return `(progn ,@result-clauses))))
70 (eval-when (:compile-toplevel :load-toplevel :execute)
71 (define-message-constants "Backend Message Constants"
72 +ascii-row-message+ #\D
73 +authentication-message+ #\R
74 +backend-key-message+ #\K
75 +binary-row-message+ #\B
76 +completed-response-message+ #\C
77 +copy-in-response-message+ #\G
78 +copy-out-response-message+ #\H
79 +cursor-response-message+ #\P
80 +empty-query-response-message+ #\I
81 +error-response-message+ #\E
82 +function-response-message+ #\V
83 +notice-response-message+ #\N
84 +notification-response-message+ #\A
85 +ready-for-query-message+ #\Z
86 +row-description-message+ #\T))
89 (declaim (inline read-byte write-byte))
91 (defun send-socket-value-int32 (socket value)
92 (declare (type stream socket)
93 (type (unsigned-byte 32) value))
94 (write-byte (ldb (byte 8 24) value) socket)
95 (write-byte (ldb (byte 8 16) value) socket)
96 (write-byte (ldb (byte 8 8) value) socket)
97 (write-byte (ldb (byte 8 0) value) socket)
100 (defun send-socket-value-int16 (socket value)
101 (declare (type stream socket)
102 (type (unsigned-byte 16) value))
103 (write-byte (ldb (byte 8 8) value) socket)
104 (write-byte (ldb (byte 8 0) value) socket)
107 (defun send-socket-value-int8 (socket value)
108 (declare (type stream socket)
109 (type (unsigned-byte 8) value))
110 (write-byte (ldb (byte 8 0) value) socket)
113 (defun send-socket-value-char-code (socket value)
114 (declare (type stream socket)
115 (type character value))
116 (write-byte (ldb (byte 8 0) (char-code value)) socket)
119 (defun send-socket-value-string (socket value)
120 (declare (type stream socket)
122 (loop for char across value
123 for code = (char-code char)
124 do (write-byte code socket)
125 finally (write-byte 0 socket))
128 (defun send-socket-value-limstring (socket value limit)
129 (declare (type stream socket)
132 (let ((length (length value)))
133 (dotimes (i (min length limit))
134 (let ((code (char-code (char value i))))
135 (write-byte code socket)))
136 (dotimes (i (- limit length))
137 (write-byte 0 socket)))
141 (defun read-socket-value-int32 (socket)
142 (declare (type stream socket))
143 (declare (optimize (speed 3)))
145 (declare (type (unsigned-byte 32) result))
146 (setf (ldb (byte 8 24) result) (read-byte socket))
147 (setf (ldb (byte 8 16) result) (read-byte socket))
148 (setf (ldb (byte 8 8) result) (read-byte socket))
149 (setf (ldb (byte 8 0) result) (read-byte socket))
152 (defun read-socket-value-int16 (socket)
153 (declare (type stream socket))
155 (declare (type (unsigned-byte 16) result))
156 (setf (ldb (byte 8 8) result) (read-byte socket))
157 (setf (ldb (byte 8 0) result) (read-byte socket))
160 (defun read-socket-value-int8 (socket)
161 (declare (type stream socket))
164 (defun read-socket-value-string (socket)
165 (declare (type stream socket))
166 (with-output-to-string (out)
167 (loop for code = (read-byte socket)
169 do (write-char (code-char code) out))))
172 (defmacro define-message-sender (name (&rest args) &rest clauses)
173 (let ((socket-var (gensym))
175 (dolist (clause clauses)
176 (let* ((type (first clause))
177 (fn (intern (concatenate 'string (symbol-name '#:send-socket-value-)
178 (symbol-name type)))))
179 (push `(,fn ,socket-var ,@(rest clause)) body)))
180 `(defun ,name (,socket-var ,@args)
183 (define-message-sender send-startup-message
184 (database user &optional (command-line "") (backend-tty ""))
186 (int32 #x00020000) ; Version 2.0
187 (limstring database 64)
189 (limstring command-line 64)
190 (limstring "" 64) ; Unused
191 (limstring backend-tty 64))
193 (define-message-sender send-terminate-message ()
196 (define-message-sender send-unencrypted-password-message (password)
197 (int32 (+ 5 (length password)))
200 (define-message-sender send-query-message (query)
204 (define-message-sender send-encrypted-password-message (crypted-password)
205 (int32 (+ 5 (length crypted-password)))
206 (string crypted-password))
208 (define-message-sender send-cancel-request (pid key)
210 (int32 80877102) ; Magic
215 (defun read-socket-sequence (string stream)
216 "KMR -- Added to support reading from binary stream into a string"
217 (declare (string string)
219 (optimize (speed 3) (safety 0)))
220 (dotimes (i (length string))
222 (setf (char string i) (code-char (read-byte stream))))
226 ;;; Support for encrypted password transmission
229 (eval-when (compile eval load)
230 (defvar *crypt-library-loaded* nil)
232 (unless *crypt-library-loaded*
233 (uffi:load-foreign-library
234 (uffi:find-foreign-library "libcrypt"
235 '("/usr/lib/" "/usr/local/lib/" "/lib/"))
236 :supporting-libraries '("c"))
237 (setq *crypt-library-loaded* t)))
239 (in-package :postgresql-socket)
241 (uffi:def-function "crypt"
246 (defun crypt-password (password salt)
247 "Encrypt a password for transmission to a PostgreSQL server."
248 (uffi:with-cstring (password-cstring password)
249 (uffi:with-cstring (salt-cstring salt)
250 (uffi:convert-from-cstring
251 (crypt password-cstring salt-cstring)))))
254 ;;;; Condition hierarchy
256 (define-condition postgresql-condition (condition)
257 ((connection :initarg :connection :reader postgresql-condition-connection)
258 (message :initarg :message :reader postgresql-condition-message))
261 (format stream "~@<~A occurred on connection ~A. ~:@_Reason: ~A~:@>"
263 (postgresql-condition-connection c)
264 (postgresql-condition-message c)))))
266 (define-condition postgresql-error (error postgresql-condition)
269 (define-condition postgresql-fatal-error (postgresql-error)
272 (define-condition postgresql-login-error (postgresql-fatal-error)
275 (define-condition postgresql-warning (warning postgresql-condition)
278 (define-condition postgresql-notification (postgresql-condition)
282 (format stream "~@<Asynchronous notification on connection ~A: ~:@_~A~:@>"
283 (postgresql-condition-connection c)
284 (postgresql-condition-message c)))))
288 (defstruct postgresql-connection
300 (defstruct postgresql-cursor
307 (defconstant +postgresql-server-default-port+ 5432
308 "Default port of PostgreSQL server.")
310 (defvar *postgresql-server-socket-timeout* 60
311 "Timeout in seconds for reads from the PostgreSQL server.")
314 (defun open-postgresql-socket (host port)
317 ;; Directory to unix-domain socket
318 (ext:connect-to-unix-socket
320 (make-pathname :name ".s.PGSQL" :type (princ-to-string port)
323 (ext:connect-to-inet-socket host port))))
326 (defun open-postgresql-socket (host port)
329 ;; Directory to unix-domain socket
330 (sb-bsd-sockets:socket-connect
332 (make-pathname :name ".s.PGSQL" :type (princ-to-string port)
335 (let ((sock (make-instance 'sb-bsd-sockets:inet-socket
338 (sb-bsd-sockets:socket-connect
340 (sb-bsd-sockets:host-ent-address
341 (sb-bsd-sockets:get-host-by-name host))
346 (defun open-postgresql-socket-stream (host port)
347 (system:make-fd-stream
348 (open-postgresql-socket host port)
349 :input t :output t :element-type '(unsigned-byte 8)
351 :timeout *postgresql-server-socket-timeout*))
355 (defun open-postgresql-socket-stream (host port)
356 (sb-bsd-sockets:socket-make-stream
357 (open-postgresql-socket host port) :input t :output t
358 :element-type '(unsigned-byte 8)))
362 (defun open-postgresql-socket-stream (host port)
365 (let ((path (namestring
366 (make-pathname :name ".s.PGSQL" :type (princ-to-string port)
368 (socket:make-socket :type :stream :address-family :file
370 :remote-filename path :local-filename path)))
372 (socket:with-pending-connect
373 (mp:with-timeout (*postgresql-server-socket-timeout* (error "connect failed"))
374 (socket:make-socket :type :stream :address-family :internet
375 :remote-port port :remote-host host
376 :connect :active :nodelay t))))))
379 (defun open-postgresql-socket-stream (host port)
382 (error "File sockets not supported on Lispworks."))
384 (comm:open-tcp-stream host port :direction :io :element-type '(unsigned-byte 8)
385 :read-timeout *postgresql-server-socket-timeout*))
388 ;;; Interface Functions
390 (defun open-postgresql-connection (&key (host (cmucl-compat:required-argument))
391 (port +postgresql-server-default-port+)
392 (database (cmucl-compat:required-argument))
393 (user (cmucl-compat:required-argument))
394 options tty password)
395 "Open a connection to a PostgreSQL server with the given parameters.
396 Note that host, database and user arguments must be supplied.
398 If host is a pathname, it is assumed to name a directory containing
399 the local unix-domain sockets of the server, with port selecting which
400 of those sockets to open. If host is a string, it is assumed to be
401 the name of the host running the PostgreSQL server. In that case a
402 TCP connection to the given port on that host is opened in order to
403 communicate with the server. In either case the port argument
404 defaults to `+postgresql-server-default-port+'.
406 Password is the clear-text password to be passed in the authentication
407 phase to the server. Depending on the server set-up, it is either
408 passed in the clear, or encrypted via crypt and a server-supplied
409 salt. In that case the alien function specified by `*crypt-library*'
410 and `*crypt-function-name*' is used for encryption.
412 Note that all the arguments (including the clear-text password
413 argument) are stored in the `postgresql-connection' structure, in
414 order to facilitate automatic reconnection in case of communication
416 (reopen-postgresql-connection
417 (make-postgresql-connection :host host :port port
418 :options (or options "") :tty (or tty "")
419 :database database :user user
420 :password (or password ""))))
422 (defun encrypt-md5 (plaintext salt)
424 (format nil "~{~2,'0X~}"
425 (coerce (md5:md5sum-sequence (concatenate 'string plaintext salt)) 'list))))
427 (defun reopen-postgresql-connection (connection)
428 "Reopen the given PostgreSQL connection. Closes any existing
429 connection, if it is still open."
430 (when (postgresql-connection-open-p connection)
431 (close-postgresql-connection connection))
432 (let ((socket (open-postgresql-socket-stream
433 (postgresql-connection-host connection)
434 (postgresql-connection-port connection))))
437 (setf (postgresql-connection-socket connection) socket)
438 (send-startup-message socket
439 (postgresql-connection-database connection)
440 (postgresql-connection-user connection)
441 (postgresql-connection-options connection)
442 (postgresql-connection-tty connection))
443 (force-output socket)
445 (case (read-socket-value-int8 socket)
446 (#.+authentication-message+
447 (case (read-socket-value-int32 socket)
450 (error 'postgresql-login-error
451 :connection connection
453 "Postmaster expects unsupported Kerberos authentication."))
455 (send-unencrypted-password-message
457 (postgresql-connection-password connection)))
459 (let ((salt (make-string 2)))
460 (read-socket-sequence salt socket)
461 (send-encrypted-password-message
464 (postgresql-connection-password connection) salt))))
466 (let ((salt (make-string 4)))
467 (read-socket-sequence salt socket)
468 (let* ((pwd2 (encrypt-md5 (postgresql-connection-password connection)
469 (postgresql-connection-user connection)))
470 (pwd (encrypt-md5 pwd2 salt)))
471 (send-encrypted-password-message
473 (concatenate 'string "md5" pwd)))))
475 (error 'postgresql-login-error
476 :connection connection
478 "Postmaster expects unknown authentication method."))))
479 (#.+error-response-message+
480 (let ((message (read-socket-value-string socket)))
481 (error 'postgresql-login-error
482 :connection connection :message message)))
484 (error 'postgresql-login-error
485 :connection connection
487 "Received garbled message from Postmaster"))))
488 ;; Start backend communication
489 (force-output socket)
491 (case (read-socket-value-int8 socket)
492 (#.+backend-key-message+
493 (setf (postgresql-connection-pid connection)
494 (read-socket-value-int32 socket)
495 (postgresql-connection-key connection)
496 (read-socket-value-int32 socket)))
497 (#.+ready-for-query-message+
500 (#.+error-response-message+
501 (let ((message (read-socket-value-string socket)))
502 (error 'postgresql-login-error
503 :connection connection
505 (#.+notice-response-message+
506 (let ((message (read-socket-value-string socket)))
507 (warn 'postgresql-warning :connection connection
510 (error 'postgresql-login-error
511 :connection connection
513 "Received garbled message from Postmaster")))))
517 (defun close-postgresql-connection (connection &optional abort)
520 (send-terminate-message (postgresql-connection-socket connection))))
521 (close (postgresql-connection-socket connection)))
523 (defun postgresql-connection-open-p (connection)
524 (let ((socket (postgresql-connection-socket connection)))
525 (and socket (streamp socket) (open-stream-p socket))))
527 (defun ensure-open-postgresql-connection (connection)
528 (unless (postgresql-connection-open-p connection)
529 (reopen-postgresql-connection connection)))
531 (defun process-async-messages (connection)
532 (assert (postgresql-connection-open-p connection))
533 ;; Process any asnychronous messages
534 (loop with socket = (postgresql-connection-socket connection)
535 while (listen socket)
537 (case (read-socket-value-int8 socket)
538 (#.+notice-response-message+
539 (let ((message (read-socket-value-string socket)))
540 (warn 'postgresql-warning :connection connection
542 (#.+notification-response-message+
543 (let ((pid (read-socket-value-int32 socket))
544 (message (read-socket-value-string socket)))
545 (when (= pid (postgresql-connection-pid connection))
546 (signal 'postgresql-notification :connection connection
549 (close-postgresql-connection connection)
550 (error 'postgresql-fatal-error :connection connection
551 :message "Received garbled message from backend")))))
553 (defun start-query-execution (connection query)
554 (ensure-open-postgresql-connection connection)
555 (process-async-messages connection)
556 (send-query-message (postgresql-connection-socket connection) query)
557 (force-output (postgresql-connection-socket connection)))
559 (defun wait-for-query-results (connection)
560 (assert (postgresql-connection-open-p connection))
561 (let ((socket (postgresql-connection-socket connection))
565 (case (read-socket-value-int8 socket)
566 (#.+completed-response-message+
567 (return (values :completed (read-socket-value-string socket))))
568 (#.+cursor-response-message+
569 (setq cursor-name (read-socket-value-string socket)))
570 (#.+row-description-message+
571 (let* ((count (read-socket-value-int16 socket))
576 (read-socket-value-string socket)
577 (read-socket-value-int32 socket)
578 (read-socket-value-int16 socket)
579 (read-socket-value-int32 socket)))))
582 (make-postgresql-cursor :connection connection
585 (#.+copy-in-response-message+
587 (#.+copy-out-response-message+
589 (#.+ready-for-query-message+
593 (#.+error-response-message+
594 (let ((message (read-socket-value-string socket)))
596 (make-condition 'postgresql-error
597 :connection connection :message message))))
598 (#.+notice-response-message+
599 (let ((message (read-socket-value-string socket)))
600 (warn 'postgresql-warning
601 :connection connection :message message)))
602 (#.+notification-response-message+
603 (let ((pid (read-socket-value-int32 socket))
604 (message (read-socket-value-string socket)))
605 (when (= pid (postgresql-connection-pid connection))
606 (signal 'postgresql-notification :connection connection
609 (close-postgresql-connection connection)
610 (error 'postgresql-fatal-error :connection connection
611 :message "Received garbled message from backend"))))))
613 (defun read-null-bit-vector (socket count)
614 (let ((result (make-array count :element-type 'bit)))
615 (dotimes (offset (ceiling count 8))
616 (loop with byte = (read-byte socket)
617 for index from (* offset 8) below (min count (* (1+ offset) 8))
618 for weight downfrom 7
619 do (setf (aref result index) (ldb (byte 1 weight) byte))))
623 (defun read-field (socket type)
624 (let ((length (- (read-socket-value-int32 socket) 4)))
627 (read-integer-from-socket socket length))
629 (read-double-from-socket socket length))
631 (let ((result (make-string length)))
632 (read-socket-sequence result socket)
635 (uffi:def-constant +char-code-zero+ (char-code #\0))
636 (uffi:def-constant +char-code-minus+ (char-code #\-))
637 (uffi:def-constant +char-code-plus+ (char-code #\+))
638 (uffi:def-constant +char-code-period+ (char-code #\.))
639 (uffi:def-constant +char-code-lower-e+ (char-code #\e))
640 (uffi:def-constant +char-code-upper-e+ (char-code #\E))
642 (defun read-integer-from-socket (socket length)
643 (declare (fixnum length))
647 (first-char (read-byte socket))
649 (declare (fixnum first-char))
650 (decf length) ;; read first char
652 ((= first-char +char-code-minus+)
654 ((= first-char +char-code-plus+)
657 (setq val (- first-char +char-code-zero+))))
663 (- (read-byte socket) +char-code-zero+))))
668 (defmacro ascii-digit (int)
669 (let ((offset (gensym)))
670 `(let ((,offset (- ,int +char-code-zero+)))
671 (declare (fixnum ,int ,offset))
672 (if (and (>= ,offset 0)
677 (defun read-double-from-socket (socket length)
678 (declare (fixnum length))
679 (let ((before-decimal 0)
686 (char (read-byte socket)))
687 (declare (fixnum char exponent decimal-count))
688 (decf length) ;; already read first character
690 ((= char +char-code-minus+)
692 ((= char +char-code-plus+)
694 ((= char +char-code-period+)
697 (setq before-decimal (ascii-digit char))
698 (unless before-decimal
699 (error "Unexpected value"))))
703 (setq char (read-byte socket))
704 ;; (format t "~&len:~D, i:~D, char:~D, minusp:~A, decimalp:~A" length i char minusp decimalp)
705 (let ((weight (ascii-digit char)))
707 ((and weight (not decimalp)) ;; before decimal point
708 (setq before-decimal (+ weight (* 10 before-decimal))))
709 ((and weight decimalp) ;; after decimal point
710 (setq after-decimal (+ weight (* 10 after-decimal)))
711 (incf decimal-count))
712 ((and (= char +char-code-period+))
714 ((or (= char +char-code-lower-e+) ;; E is for exponent
715 (= char +char-code-upper-e+))
716 (setq exponent (read-integer-from-socket socket (- length i 1)))
717 (setq exponent (or exponent 0))
720 (break "Unexpected value"))
723 (setq result (* (+ (coerce before-decimal 'double-float)
725 (expt 10 (- decimal-count))))
733 (defun read-double-from-socket (socket length)
734 (let ((result (make-string length)))
735 (read-socket-sequence result socket)
736 (let ((*read-default-float-format* 'double-float))
737 (read-from-string result))))
739 (defun read-cursor-row (cursor types)
740 (let* ((connection (postgresql-cursor-connection cursor))
741 (socket (postgresql-connection-socket connection))
742 (fields (postgresql-cursor-fields cursor)))
743 (assert (postgresql-connection-open-p connection))
745 (let ((code (read-socket-value-int8 socket)))
747 (#.+ascii-row-message+
749 (loop with count = (length fields)
750 with null-vector = (read-null-bit-vector socket count)
752 for null-bit across null-vector
754 for null-p = (zerop null-bit)
759 (read-field socket (nth i types)))))
760 (#.+binary-row-message+
762 (#.+completed-response-message+
763 (return (values nil (read-socket-value-string socket))))
764 (#.+error-response-message+
765 (let ((message (read-socket-value-string socket)))
766 (error 'postgresql-error
767 :connection connection :message message)))
768 (#.+notice-response-message+
769 (let ((message (read-socket-value-string socket)))
770 (warn 'postgresql-warning
771 :connection connection :message message)))
772 (#.+notification-response-message+
773 (let ((pid (read-socket-value-int32 socket))
774 (message (read-socket-value-string socket)))
775 (when (= pid (postgresql-connection-pid connection))
776 (signal 'postgresql-notification :connection connection
779 (close-postgresql-connection connection)
780 (error 'postgresql-fatal-error :connection connection
781 :message "Received garbled message from backend")))))))
783 (defun map-into-indexed (result-seq func seq)
784 (dotimes (i (length seq))
786 (setf (elt result-seq i)
787 (funcall func (elt seq i) i)))
790 (defun copy-cursor-row (cursor sequence types)
791 (let* ((connection (postgresql-cursor-connection cursor))
792 (socket (postgresql-connection-socket connection))
793 (fields (postgresql-cursor-fields cursor)))
794 (assert (= (length fields) (length sequence)))
796 (let ((code (read-socket-value-int8 socket)))
798 (#.+ascii-row-message+
801 (let* ((count (length sequence))
802 (null-vector (read-null-bit-vector socket count)))
805 (if (zerop (elt null-vector i))
806 (setf (elt sequence i) nil)
807 (let ((value (read-field socket (nth i types))))
808 (setf (elt sequence i) value)))))
811 #'(lambda (null-bit i)
814 (read-field socket (nth i types))))
815 (read-null-bit-vector socket (length sequence)))))
816 (#.+binary-row-message+
818 (#.+completed-response-message+
819 (return (values nil (read-socket-value-string socket))))
820 (#.+error-response-message+
821 (let ((message (read-socket-value-string socket)))
822 (error 'postgresql-error
823 :connection connection :message message)))
824 (#.+notice-response-message+
825 (let ((message (read-socket-value-string socket)))
826 (warn 'postgresql-warning
827 :connection connection :message message)))
828 (#.+notification-response-message+
829 (let ((pid (read-socket-value-int32 socket))
830 (message (read-socket-value-string socket)))
831 (when (= pid (postgresql-connection-pid connection))
832 (signal 'postgresql-notification :connection connection
835 (close-postgresql-connection connection)
836 (error 'postgresql-fatal-error :connection connection
837 :message "Received garbled message from backend")))))))
839 (defun skip-cursor-row (cursor)
840 (let* ((connection (postgresql-cursor-connection cursor))
841 (socket (postgresql-connection-socket connection))
842 (fields (postgresql-cursor-fields cursor)))
844 (let ((code (read-socket-value-int8 socket)))
846 (#.+ascii-row-message+
847 (loop for null-bit across
848 (read-null-bit-vector socket (length fields))
850 (unless (zerop null-bit)
851 (let* ((length (read-socket-value-int32 socket)))
852 (loop repeat (- length 4) do (read-byte socket)))))
854 (#.+binary-row-message+
856 (#.+completed-response-message+
857 (return (values nil (read-socket-value-string socket))))
858 (#.+error-response-message+
859 (let ((message (read-socket-value-string socket)))
860 (error 'postgresql-error
861 :connection connection :message message)))
862 (#.+notice-response-message+
863 (let ((message (read-socket-value-string socket)))
864 (warn 'postgresql-warning
865 :connection connection :message message)))
866 (#.+notification-response-message+
867 (let ((pid (read-socket-value-int32 socket))
868 (message (read-socket-value-string socket)))
869 (when (= pid (postgresql-connection-pid connection))
870 (signal 'postgresql-notification :connection connection
873 (close-postgresql-connection connection)
874 (error 'postgresql-fatal-error :connection connection
875 :message "Received garbled message from backend")))))))
877 (defun run-query (connection query &optional (result-types nil))
878 (start-query-execution connection query)
879 (multiple-value-bind (status cursor)
880 (wait-for-query-results connection)
881 (assert (eq status :cursor))
882 (loop for row = (read-cursor-row cursor result-types)
886 (wait-for-query-results connection))))
889 (declaim (ext:maybe-inline read-byte write-byte))