1999-04-25 05:19:27 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* pqformat.c
|
|
|
|
* Routines for formatting and parsing frontend/backend messages
|
|
|
|
*
|
|
|
|
* Outgoing messages are built up in a StringInfo buffer (which is expansible)
|
|
|
|
* and then sent in a single call to pq_putmessage. This module provides data
|
|
|
|
* formatting/conversion routines that are needed to produce valid messages.
|
|
|
|
* Note in particular the distinction between "raw data" and "text"; raw data
|
|
|
|
* is message protocol characters and binary values that are not subject to
|
2003-04-19 02:02:30 +02:00
|
|
|
* character set conversion, while text is converted by character encoding
|
|
|
|
* rules.
|
1999-04-25 05:19:27 +02:00
|
|
|
*
|
2003-04-19 02:02:30 +02:00
|
|
|
* Incoming messages are similarly read into a StringInfo buffer, via
|
|
|
|
* pq_getmessage, and then parsed and converted from that using the routines
|
|
|
|
* in this module.
|
1999-04-25 05:19:27 +02:00
|
|
|
*
|
2003-05-09 17:44:42 +02:00
|
|
|
* These same routines support reading and writing of external binary formats
|
|
|
|
* (typsend/typreceive routines). The conversion routines for individual
|
|
|
|
* data types are exactly the same, only initialization and completion
|
|
|
|
* are different.
|
|
|
|
*
|
|
|
|
*
|
2020-01-01 18:21:45 +01:00
|
|
|
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1999-04-25 05:19:27 +02:00
|
|
|
*
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/libpq/pqformat.c
|
1999-04-25 05:19:27 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* INTERFACE ROUTINES
|
1999-04-25 23:50:58 +02:00
|
|
|
* Message assembly and output:
|
1999-05-25 18:15:34 +02:00
|
|
|
* pq_beginmessage - initialize StringInfo buffer
|
1999-04-25 05:19:27 +02:00
|
|
|
* pq_sendbyte - append a raw byte to a StringInfo buffer
|
|
|
|
* pq_sendint - append a binary integer to a StringInfo buffer
|
2003-05-09 17:44:42 +02:00
|
|
|
* pq_sendint64 - append a binary 8-byte int to a StringInfo buffer
|
2003-05-09 23:19:50 +02:00
|
|
|
* pq_sendfloat4 - append a float4 to a StringInfo buffer
|
|
|
|
* pq_sendfloat8 - append a float8 to a StringInfo buffer
|
1999-04-25 05:19:27 +02:00
|
|
|
* pq_sendbytes - append raw data to a StringInfo buffer
|
2003-05-09 17:44:42 +02:00
|
|
|
* pq_sendcountedtext - append a counted text string (with character set conversion)
|
|
|
|
* pq_sendtext - append a text string (with conversion)
|
2002-09-03 23:45:44 +02:00
|
|
|
* pq_sendstring - append a null-terminated text string (with conversion)
|
2009-03-02 22:18:43 +01:00
|
|
|
* pq_send_ascii_string - append a null-terminated text string (without conversion)
|
1999-04-25 05:19:27 +02:00
|
|
|
* pq_endmessage - send the completed message to the frontend
|
|
|
|
* Note: it is also possible to append data to the StringInfo buffer using
|
|
|
|
* the regular StringInfo routines, but this is discouraged since required
|
2002-09-03 23:45:44 +02:00
|
|
|
* character set conversion may not occur.
|
1999-04-25 05:19:27 +02:00
|
|
|
*
|
2003-05-09 17:44:42 +02:00
|
|
|
* typsend support (construct a bytea value containing external binary data):
|
|
|
|
* pq_begintypsend - initialize StringInfo buffer
|
|
|
|
* pq_endtypsend - return the completed string as a "bytea*"
|
|
|
|
*
|
1999-04-25 23:50:58 +02:00
|
|
|
* Special-case message output:
|
2002-09-03 23:45:44 +02:00
|
|
|
* pq_puttextmessage - generate a character set-converted message in one step
|
2003-04-22 02:08:07 +02:00
|
|
|
* pq_putemptymessage - convenience routine for message with empty body
|
1999-04-25 23:50:58 +02:00
|
|
|
*
|
2003-04-19 02:02:30 +02:00
|
|
|
* Message parsing after input:
|
|
|
|
* pq_getmsgbyte - get a raw byte from a message buffer
|
|
|
|
* pq_getmsgint - get a binary integer from a message buffer
|
2003-05-09 17:44:42 +02:00
|
|
|
* pq_getmsgint64 - get a binary 8-byte int from a message buffer
|
2003-08-04 02:43:34 +02:00
|
|
|
* pq_getmsgfloat4 - get a float4 from a message buffer
|
|
|
|
* pq_getmsgfloat8 - get a float8 from a message buffer
|
2003-04-19 02:02:30 +02:00
|
|
|
* pq_getmsgbytes - get raw data from a message buffer
|
2003-08-04 02:43:34 +02:00
|
|
|
* pq_copymsgbytes - copy raw data from a message buffer
|
2003-05-09 17:44:42 +02:00
|
|
|
* pq_getmsgtext - get a counted text string (with conversion)
|
2003-08-04 02:43:34 +02:00
|
|
|
* pq_getmsgstring - get a null-terminated text string (with conversion)
|
Fix several mistakes around parallel workers and client_encoding.
Previously, workers sent data to the leader using the client encoding.
That mostly worked, but the leader the converted the data back to the
server encoding. Since not all encoding conversions are reversible,
that could provoke failures. Fix by using the database encoding for
all communication between worker and leader.
Also, while temporary changes to GUC settings, as from the SET clause
of a function, are in general OK for parallel query, changing
client_encoding this way inside of a parallel worker is not OK.
Previously, that would have confused the leader; with these changes,
it would not confuse the leader, but it wouldn't do anything either.
So refuse such changes in parallel workers.
Also, the previous code naively assumed that when it received a
NotifyResonse from the worker, it could pass that directly back to the
user. But now that worker-to-leader communication always uses the
database encoding, that's clearly no longer correct - though,
actually, the old way was always broken for V2 clients. So
disassemble and reconstitute the message instead.
Issues reported by Peter Eisentraut. Patch by me, reviewed by
Peter Eisentraut.
2016-07-01 00:35:32 +02:00
|
|
|
* pq_getmsgrawstring - get a null-terminated text string - NO conversion
|
2003-04-19 02:02:30 +02:00
|
|
|
* pq_getmsgend - verify message fully consumed
|
1999-04-25 05:19:27 +02:00
|
|
|
*/
|
2000-07-08 07:30:33 +02:00
|
|
|
|
1999-04-25 05:19:27 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2000-07-08 07:30:33 +02:00
|
|
|
#include <sys/param.h>
|
|
|
|
|
1999-04-25 05:19:27 +02:00
|
|
|
#include "libpq/libpq.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "libpq/pqformat.h"
|
|
|
|
#include "mb/pg_wchar.h"
|
2017-10-02 00:36:14 +02:00
|
|
|
#include "port/pg_bswap.h"
|
1999-04-25 05:19:27 +02:00
|
|
|
|
|
|
|
|
2003-04-22 02:08:07 +02:00
|
|
|
/* --------------------------------
|
|
|
|
* pq_beginmessage - initialize for sending a message
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_beginmessage(StringInfo buf, char msgtype)
|
|
|
|
{
|
|
|
|
initStringInfo(buf);
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2003-04-22 02:08:07 +02:00
|
|
|
/*
|
|
|
|
* We stash the message type into the buffer's cursor field, expecting
|
2005-10-15 04:49:52 +02:00
|
|
|
* that the pq_sendXXX routines won't touch it. We could alternatively
|
|
|
|
* make it the first byte of the buffer contents, but this seems easier.
|
2003-04-22 02:08:07 +02:00
|
|
|
*/
|
|
|
|
buf->cursor = msgtype;
|
|
|
|
}
|
|
|
|
|
1999-04-25 05:19:27 +02:00
|
|
|
/* --------------------------------
|
2017-10-12 01:01:52 +02:00
|
|
|
|
|
|
|
* pq_beginmessage_reuse - initialize for sending a message, reuse buffer
|
|
|
|
*
|
2018-03-29 21:18:53 +02:00
|
|
|
* This requires the buffer to be allocated in a sufficiently long-lived
|
2017-10-12 01:01:52 +02:00
|
|
|
* memory context.
|
1999-04-25 05:19:27 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2017-10-12 01:01:52 +02:00
|
|
|
pq_beginmessage_reuse(StringInfo buf, char msgtype)
|
1999-04-25 05:19:27 +02:00
|
|
|
{
|
2017-10-12 01:01:52 +02:00
|
|
|
resetStringInfo(buf);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We stash the message type into the buffer's cursor field, expecting
|
|
|
|
* that the pq_sendXXX routines won't touch it. We could alternatively
|
|
|
|
* make it the first byte of the buffer contents, but this seems easier.
|
|
|
|
*/
|
|
|
|
buf->cursor = msgtype;
|
1999-04-25 05:19:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
|
|
|
* pq_sendbytes - append raw data to a StringInfo buffer
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_sendbytes(StringInfo buf, const char *data, int datalen)
|
|
|
|
{
|
2017-10-12 01:01:52 +02:00
|
|
|
/* use variant that maintains a trailing null-byte, out of caution */
|
1999-04-25 05:19:27 +02:00
|
|
|
appendBinaryStringInfo(buf, data, datalen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
2003-05-09 17:44:42 +02:00
|
|
|
* pq_sendcountedtext - append a counted text string (with character set conversion)
|
1999-04-25 05:19:27 +02:00
|
|
|
*
|
1999-04-25 21:27:47 +02:00
|
|
|
* The data sent to the frontend by this routine is a 4-byte count field
|
2014-05-06 18:12:18 +02:00
|
|
|
* followed by the string. The count includes itself or not, as per the
|
2003-05-08 20:16:37 +02:00
|
|
|
* countincludesself flag (pre-3.0 protocol requires it to include itself).
|
1999-04-25 21:27:47 +02:00
|
|
|
* The passed text string need not be null-terminated, and the data sent
|
|
|
|
* to the frontend isn't either.
|
1999-04-25 05:19:27 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2003-05-08 20:16:37 +02:00
|
|
|
pq_sendcountedtext(StringInfo buf, const char *str, int slen,
|
|
|
|
bool countincludesself)
|
1999-04-25 05:19:27 +02:00
|
|
|
{
|
2003-05-08 20:16:37 +02:00
|
|
|
int extra = countincludesself ? 4 : 0;
|
1999-09-12 00:28:11 +02:00
|
|
|
char *p;
|
1999-05-25 18:15:34 +02:00
|
|
|
|
2005-09-24 19:53:28 +02:00
|
|
|
p = pg_server_to_client(str, slen);
|
1999-04-25 21:27:47 +02:00
|
|
|
if (p != str) /* actual conversion has been done? */
|
|
|
|
{
|
1999-09-12 00:28:11 +02:00
|
|
|
slen = strlen(p);
|
2017-10-12 01:01:52 +02:00
|
|
|
pq_sendint32(buf, slen + extra);
|
2017-10-12 01:01:52 +02:00
|
|
|
appendBinaryStringInfoNT(buf, p, slen);
|
1999-09-12 00:28:11 +02:00
|
|
|
pfree(p);
|
1999-04-25 21:27:47 +02:00
|
|
|
}
|
2003-05-08 20:16:37 +02:00
|
|
|
else
|
|
|
|
{
|
2017-10-12 01:01:52 +02:00
|
|
|
pq_sendint32(buf, slen + extra);
|
2017-10-12 01:01:52 +02:00
|
|
|
appendBinaryStringInfoNT(buf, str, slen);
|
2003-05-08 20:16:37 +02:00
|
|
|
}
|
1999-04-25 05:19:27 +02:00
|
|
|
}
|
|
|
|
|
2003-05-09 17:44:42 +02:00
|
|
|
/* --------------------------------
|
|
|
|
* pq_sendtext - append a text string (with conversion)
|
|
|
|
*
|
|
|
|
* The passed text string need not be null-terminated, and the data sent
|
|
|
|
* to the frontend isn't either. Note that this is not actually useful
|
|
|
|
* for direct frontend transmissions, since there'd be no way for the
|
|
|
|
* frontend to determine the string length. But it is useful for binary
|
|
|
|
* format conversions.
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_sendtext(StringInfo buf, const char *str, int slen)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
2005-09-24 19:53:28 +02:00
|
|
|
p = pg_server_to_client(str, slen);
|
2003-05-09 17:44:42 +02:00
|
|
|
if (p != str) /* actual conversion has been done? */
|
|
|
|
{
|
|
|
|
slen = strlen(p);
|
|
|
|
appendBinaryStringInfo(buf, p, slen);
|
|
|
|
pfree(p);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
appendBinaryStringInfo(buf, str, slen);
|
|
|
|
}
|
|
|
|
|
1999-04-25 05:19:27 +02:00
|
|
|
/* --------------------------------
|
2002-09-03 23:45:44 +02:00
|
|
|
* pq_sendstring - append a null-terminated text string (with conversion)
|
1999-04-25 05:19:27 +02:00
|
|
|
*
|
1999-04-25 21:27:47 +02:00
|
|
|
* NB: passed text string must be null-terminated, and so is the data
|
|
|
|
* sent to the frontend.
|
1999-04-25 05:19:27 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
1999-04-25 21:27:47 +02:00
|
|
|
pq_sendstring(StringInfo buf, const char *str)
|
1999-04-25 05:19:27 +02:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
int slen = strlen(str);
|
1999-09-12 00:28:11 +02:00
|
|
|
char *p;
|
1999-05-25 18:15:34 +02:00
|
|
|
|
2005-09-24 19:53:28 +02:00
|
|
|
p = pg_server_to_client(str, slen);
|
1999-04-25 21:27:47 +02:00
|
|
|
if (p != str) /* actual conversion has been done? */
|
|
|
|
{
|
1999-09-12 00:28:11 +02:00
|
|
|
slen = strlen(p);
|
2017-10-12 01:01:52 +02:00
|
|
|
appendBinaryStringInfoNT(buf, p, slen + 1);
|
1999-09-12 00:28:11 +02:00
|
|
|
pfree(p);
|
1999-04-25 21:27:47 +02:00
|
|
|
}
|
2003-05-09 17:44:42 +02:00
|
|
|
else
|
2017-10-12 01:01:52 +02:00
|
|
|
appendBinaryStringInfoNT(buf, str, slen + 1);
|
1999-04-25 05:19:27 +02:00
|
|
|
}
|
|
|
|
|
2009-03-02 22:18:43 +01:00
|
|
|
/* --------------------------------
|
|
|
|
* pq_send_ascii_string - append a null-terminated text string (without conversion)
|
|
|
|
*
|
|
|
|
* This function intentionally bypasses encoding conversion, instead just
|
|
|
|
* silently replacing any non-7-bit-ASCII characters with question marks.
|
|
|
|
* It is used only when we are having trouble sending an error message to
|
|
|
|
* the client with normal localization and encoding conversion. The caller
|
|
|
|
* should already have taken measures to ensure the string is just ASCII;
|
|
|
|
* the extra work here is just to make certain we don't send a badly encoded
|
|
|
|
* string to the client (which might or might not be robust about that).
|
|
|
|
*
|
|
|
|
* NB: passed text string must be null-terminated, and so is the data
|
|
|
|
* sent to the frontend.
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_send_ascii_string(StringInfo buf, const char *str)
|
|
|
|
{
|
|
|
|
while (*str)
|
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
char ch = *str++;
|
2009-03-02 22:18:43 +01:00
|
|
|
|
|
|
|
if (IS_HIGHBIT_SET(ch))
|
|
|
|
ch = '?';
|
|
|
|
appendStringInfoCharMacro(buf, ch);
|
|
|
|
}
|
|
|
|
appendStringInfoChar(buf, '\0');
|
|
|
|
}
|
|
|
|
|
2003-05-09 23:19:50 +02:00
|
|
|
/* --------------------------------
|
|
|
|
* pq_sendfloat4 - append a float4 to a StringInfo buffer
|
|
|
|
*
|
|
|
|
* The point of this routine is to localize knowledge of the external binary
|
|
|
|
* representation of float4, which is a component of several datatypes.
|
|
|
|
*
|
|
|
|
* We currently assume that float4 should be byte-swapped in the same way
|
|
|
|
* as int4. This rule is not perfect but it gives us portability across
|
|
|
|
* most IEEE-float-using architectures.
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_sendfloat4(StringInfo buf, float4 f)
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
float4 f;
|
|
|
|
uint32 i;
|
|
|
|
} swap;
|
2003-05-09 23:19:50 +02:00
|
|
|
|
|
|
|
swap.f = f;
|
2017-10-12 01:01:52 +02:00
|
|
|
pq_sendint32(buf, swap.i);
|
2003-05-09 23:19:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
|
|
|
* pq_sendfloat8 - append a float8 to a StringInfo buffer
|
|
|
|
*
|
|
|
|
* The point of this routine is to localize knowledge of the external binary
|
|
|
|
* representation of float8, which is a component of several datatypes.
|
|
|
|
*
|
|
|
|
* We currently assume that float8 should be byte-swapped in the same way
|
|
|
|
* as int8. This rule is not perfect but it gives us portability across
|
|
|
|
* most IEEE-float-using architectures.
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_sendfloat8(StringInfo buf, float8 f)
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
float8 f;
|
|
|
|
int64 i;
|
|
|
|
} swap;
|
2003-05-09 23:19:50 +02:00
|
|
|
|
|
|
|
swap.f = f;
|
|
|
|
pq_sendint64(buf, swap.i);
|
|
|
|
}
|
|
|
|
|
1999-04-25 05:19:27 +02:00
|
|
|
/* --------------------------------
|
|
|
|
* pq_endmessage - send the completed message to the frontend
|
|
|
|
*
|
|
|
|
* The data buffer is pfree()d, but if the StringInfo was allocated with
|
|
|
|
* makeStringInfo then the caller must still pfree it.
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_endmessage(StringInfo buf)
|
|
|
|
{
|
2003-04-22 02:08:07 +02:00
|
|
|
/* msgtype was saved in cursor field */
|
|
|
|
(void) pq_putmessage(buf->cursor, buf->data, buf->len);
|
2001-04-16 03:46:57 +02:00
|
|
|
/* no need to complain about any failure, since pqcomm.c already did */
|
1999-04-25 05:19:27 +02:00
|
|
|
pfree(buf->data);
|
|
|
|
buf->data = NULL;
|
|
|
|
}
|
|
|
|
|
2017-10-12 01:01:52 +02:00
|
|
|
/* --------------------------------
|
|
|
|
* pq_endmessage_reuse - send the completed message to the frontend
|
|
|
|
*
|
|
|
|
* The data buffer is *not* freed, allowing to reuse the buffer with
|
2019-06-17 09:13:16 +02:00
|
|
|
* pq_beginmessage_reuse.
|
2017-10-12 01:01:52 +02:00
|
|
|
--------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
pq_endmessage_reuse(StringInfo buf)
|
|
|
|
{
|
|
|
|
/* msgtype was saved in cursor field */
|
|
|
|
(void) pq_putmessage(buf->cursor, buf->data, buf->len);
|
|
|
|
}
|
|
|
|
|
2003-05-09 17:44:42 +02:00
|
|
|
|
|
|
|
/* --------------------------------
|
|
|
|
* pq_begintypsend - initialize for constructing a bytea result
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_begintypsend(StringInfo buf)
|
|
|
|
{
|
|
|
|
initStringInfo(buf);
|
|
|
|
/* Reserve four bytes for the bytea length word */
|
|
|
|
appendStringInfoCharMacro(buf, '\0');
|
|
|
|
appendStringInfoCharMacro(buf, '\0');
|
|
|
|
appendStringInfoCharMacro(buf, '\0');
|
|
|
|
appendStringInfoCharMacro(buf, '\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
|
|
|
* pq_endtypsend - finish constructing a bytea result
|
|
|
|
*
|
|
|
|
* The data buffer is returned as the palloc'd bytea value. (We expect
|
|
|
|
* that it will be suitably aligned for this because it has been palloc'd.)
|
|
|
|
* We assume the StringInfoData is just a local variable in the caller and
|
|
|
|
* need not be pfree'd.
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
bytea *
|
|
|
|
pq_endtypsend(StringInfo buf)
|
|
|
|
{
|
|
|
|
bytea *result = (bytea *) buf->data;
|
|
|
|
|
|
|
|
/* Insert correct length into bytea length word */
|
|
|
|
Assert(buf->len >= VARHDRSZ);
|
2007-02-28 00:48:10 +01:00
|
|
|
SET_VARSIZE(result, buf->len);
|
2003-05-09 17:44:42 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-04-25 23:50:58 +02:00
|
|
|
/* --------------------------------
|
2002-09-03 23:45:44 +02:00
|
|
|
* pq_puttextmessage - generate a character set-converted message in one step
|
1999-04-25 23:50:58 +02:00
|
|
|
*
|
|
|
|
* This is the same as the pqcomm.c routine pq_putmessage, except that
|
2002-09-03 23:45:44 +02:00
|
|
|
* the message body is a null-terminated string to which encoding
|
1999-04-25 23:50:58 +02:00
|
|
|
* conversion applies.
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
2003-04-22 02:08:07 +02:00
|
|
|
void
|
1999-04-25 23:50:58 +02:00
|
|
|
pq_puttextmessage(char msgtype, const char *str)
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
int slen = strlen(str);
|
1999-09-12 00:28:11 +02:00
|
|
|
char *p;
|
1999-05-25 18:15:34 +02:00
|
|
|
|
2005-09-24 19:53:28 +02:00
|
|
|
p = pg_server_to_client(str, slen);
|
1999-04-25 23:50:58 +02:00
|
|
|
if (p != str) /* actual conversion has been done? */
|
|
|
|
{
|
2003-04-22 02:08:07 +02:00
|
|
|
(void) pq_putmessage(msgtype, p, strlen(p) + 1);
|
1999-09-12 00:28:11 +02:00
|
|
|
pfree(p);
|
2003-04-22 02:08:07 +02:00
|
|
|
return;
|
1999-04-25 23:50:58 +02:00
|
|
|
}
|
2003-04-22 02:08:07 +02:00
|
|
|
(void) pq_putmessage(msgtype, str, slen + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* --------------------------------
|
|
|
|
* pq_putemptymessage - convenience routine for message with empty body
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_putemptymessage(char msgtype)
|
|
|
|
{
|
|
|
|
(void) pq_putmessage(msgtype, NULL, 0);
|
1999-04-25 23:50:58 +02:00
|
|
|
}
|
|
|
|
|
2003-04-19 02:02:30 +02:00
|
|
|
|
1999-04-25 05:19:27 +02:00
|
|
|
/* --------------------------------
|
2003-04-19 02:02:30 +02:00
|
|
|
* pq_getmsgbyte - get a raw byte from a message buffer
|
1999-04-25 05:19:27 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
int
|
2003-04-19 02:02:30 +02:00
|
|
|
pq_getmsgbyte(StringInfo msg)
|
|
|
|
{
|
|
|
|
if (msg->cursor >= msg->len)
|
2003-07-22 21:00:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROTOCOL_VIOLATION),
|
|
|
|
errmsg("no data left in message")));
|
2003-04-19 02:02:30 +02:00
|
|
|
return (unsigned char) msg->data[msg->cursor++];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
|
|
|
* pq_getmsgint - get a binary integer from a message buffer
|
|
|
|
*
|
|
|
|
* Values are treated as unsigned.
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
unsigned int
|
|
|
|
pq_getmsgint(StringInfo msg, int b)
|
1999-04-25 05:19:27 +02:00
|
|
|
{
|
2003-04-19 02:02:30 +02:00
|
|
|
unsigned int result;
|
1999-05-25 18:15:34 +02:00
|
|
|
unsigned char n8;
|
|
|
|
uint16 n16;
|
|
|
|
uint32 n32;
|
1999-04-25 05:19:27 +02:00
|
|
|
|
|
|
|
switch (b)
|
|
|
|
{
|
|
|
|
case 1:
|
2003-04-19 02:02:30 +02:00
|
|
|
pq_copymsgbytes(msg, (char *) &n8, 1);
|
|
|
|
result = n8;
|
1999-04-25 05:19:27 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
2003-04-19 02:02:30 +02:00
|
|
|
pq_copymsgbytes(msg, (char *) &n16, 2);
|
2017-10-02 00:36:14 +02:00
|
|
|
result = pg_ntoh16(n16);
|
1999-04-25 05:19:27 +02:00
|
|
|
break;
|
|
|
|
case 4:
|
2003-04-19 02:02:30 +02:00
|
|
|
pq_copymsgbytes(msg, (char *) &n32, 4);
|
2017-10-02 00:36:14 +02:00
|
|
|
result = pg_ntoh32(n32);
|
1999-04-25 05:19:27 +02:00
|
|
|
break;
|
|
|
|
default:
|
2003-07-22 21:00:12 +02:00
|
|
|
elog(ERROR, "unsupported integer size %d", b);
|
2003-04-19 02:02:30 +02:00
|
|
|
result = 0; /* keep compiler quiet */
|
1999-04-25 05:19:27 +02:00
|
|
|
break;
|
|
|
|
}
|
2003-04-19 02:02:30 +02:00
|
|
|
return result;
|
1999-04-25 05:19:27 +02:00
|
|
|
}
|
|
|
|
|
2003-05-09 17:44:42 +02:00
|
|
|
/* --------------------------------
|
|
|
|
* pq_getmsgint64 - get a binary 8-byte int from a message buffer
|
|
|
|
*
|
|
|
|
* It is tempting to merge this with pq_getmsgint, but we'd have to make the
|
|
|
|
* result int64 for all data widths --- that could be a big performance
|
|
|
|
* hit on machines where int64 isn't efficient.
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
int64
|
|
|
|
pq_getmsgint64(StringInfo msg)
|
|
|
|
{
|
2017-10-02 00:36:14 +02:00
|
|
|
uint64 n64;
|
2003-05-09 17:44:42 +02:00
|
|
|
|
2017-10-02 00:36:14 +02:00
|
|
|
pq_copymsgbytes(msg, (char *) &n64, sizeof(n64));
|
2003-05-09 17:44:42 +02:00
|
|
|
|
2017-10-02 00:36:14 +02:00
|
|
|
return pg_ntoh64(n64);
|
2003-05-09 17:44:42 +02:00
|
|
|
}
|
|
|
|
|
2003-05-09 23:19:50 +02:00
|
|
|
/* --------------------------------
|
2003-08-04 02:43:34 +02:00
|
|
|
* pq_getmsgfloat4 - get a float4 from a message buffer
|
2003-05-09 23:19:50 +02:00
|
|
|
*
|
|
|
|
* See notes for pq_sendfloat4.
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
float4
|
|
|
|
pq_getmsgfloat4(StringInfo msg)
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
float4 f;
|
|
|
|
uint32 i;
|
|
|
|
} swap;
|
2003-05-09 23:19:50 +02:00
|
|
|
|
|
|
|
swap.i = pq_getmsgint(msg, 4);
|
|
|
|
return swap.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
2003-08-04 02:43:34 +02:00
|
|
|
* pq_getmsgfloat8 - get a float8 from a message buffer
|
2003-05-09 23:19:50 +02:00
|
|
|
*
|
|
|
|
* See notes for pq_sendfloat8.
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
float8
|
|
|
|
pq_getmsgfloat8(StringInfo msg)
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
float8 f;
|
|
|
|
int64 i;
|
|
|
|
} swap;
|
2003-05-09 23:19:50 +02:00
|
|
|
|
|
|
|
swap.i = pq_getmsgint64(msg);
|
|
|
|
return swap.f;
|
|
|
|
}
|
|
|
|
|
1999-04-25 05:19:27 +02:00
|
|
|
/* --------------------------------
|
2003-04-19 02:02:30 +02:00
|
|
|
* pq_getmsgbytes - get raw data from a message buffer
|
1999-04-25 05:19:27 +02:00
|
|
|
*
|
2003-04-19 02:02:30 +02:00
|
|
|
* Returns a pointer directly into the message buffer; note this
|
|
|
|
* may not have any particular alignment.
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
pq_getmsgbytes(StringInfo msg, int datalen)
|
|
|
|
{
|
|
|
|
const char *result;
|
|
|
|
|
2003-05-08 20:16:37 +02:00
|
|
|
if (datalen < 0 || datalen > (msg->len - msg->cursor))
|
2003-07-22 21:00:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROTOCOL_VIOLATION),
|
|
|
|
errmsg("insufficient data left in message")));
|
2003-04-19 02:02:30 +02:00
|
|
|
result = &msg->data[msg->cursor];
|
|
|
|
msg->cursor += datalen;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------
|
2003-08-04 02:43:34 +02:00
|
|
|
* pq_copymsgbytes - copy raw data from a message buffer
|
1999-04-25 05:19:27 +02:00
|
|
|
*
|
2003-04-19 02:02:30 +02:00
|
|
|
* Same as above, except data is copied to caller's buffer.
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_copymsgbytes(StringInfo msg, char *buf, int datalen)
|
|
|
|
{
|
2003-05-08 20:16:37 +02:00
|
|
|
if (datalen < 0 || datalen > (msg->len - msg->cursor))
|
2003-07-22 21:00:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROTOCOL_VIOLATION),
|
|
|
|
errmsg("insufficient data left in message")));
|
2003-04-19 02:02:30 +02:00
|
|
|
memcpy(buf, &msg->data[msg->cursor], datalen);
|
|
|
|
msg->cursor += datalen;
|
|
|
|
}
|
|
|
|
|
2003-05-09 17:44:42 +02:00
|
|
|
/* --------------------------------
|
|
|
|
* pq_getmsgtext - get a counted text string (with conversion)
|
|
|
|
*
|
|
|
|
* Always returns a pointer to a freshly palloc'd result.
|
|
|
|
* The result has a trailing null, *and* we return its strlen in *nbytes.
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes)
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
char *str;
|
|
|
|
char *p;
|
2003-05-09 17:44:42 +02:00
|
|
|
|
|
|
|
if (rawbytes < 0 || rawbytes > (msg->len - msg->cursor))
|
2003-07-22 21:00:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROTOCOL_VIOLATION),
|
|
|
|
errmsg("insufficient data left in message")));
|
2003-05-09 17:44:42 +02:00
|
|
|
str = &msg->data[msg->cursor];
|
|
|
|
msg->cursor += rawbytes;
|
|
|
|
|
2005-09-24 19:53:28 +02:00
|
|
|
p = pg_client_to_server(str, rawbytes);
|
2003-05-09 17:44:42 +02:00
|
|
|
if (p != str) /* actual conversion has been done? */
|
|
|
|
*nbytes = strlen(p);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p = (char *) palloc(rawbytes + 1);
|
|
|
|
memcpy(p, str, rawbytes);
|
|
|
|
p[rawbytes] = '\0';
|
|
|
|
*nbytes = rawbytes;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2003-04-19 02:02:30 +02:00
|
|
|
/* --------------------------------
|
2003-08-04 02:43:34 +02:00
|
|
|
* pq_getmsgstring - get a null-terminated text string (with conversion)
|
2002-09-05 01:31:35 +02:00
|
|
|
*
|
2003-04-19 02:02:30 +02:00
|
|
|
* May return a pointer directly into the message buffer, or a pointer
|
|
|
|
* to a palloc'd conversion result.
|
1999-04-25 05:19:27 +02:00
|
|
|
* --------------------------------
|
|
|
|
*/
|
2003-04-19 02:02:30 +02:00
|
|
|
const char *
|
|
|
|
pq_getmsgstring(StringInfo msg)
|
1999-04-25 05:19:27 +02:00
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
char *str;
|
|
|
|
int slen;
|
1999-04-25 05:19:27 +02:00
|
|
|
|
2003-04-19 02:02:30 +02:00
|
|
|
str = &msg->data[msg->cursor];
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2003-04-19 02:02:30 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* It's safe to use strlen() here because a StringInfo is guaranteed to
|
|
|
|
* have a trailing null byte. But check we found a null inside the
|
2003-08-04 02:43:34 +02:00
|
|
|
* message.
|
2003-04-19 02:02:30 +02:00
|
|
|
*/
|
|
|
|
slen = strlen(str);
|
|
|
|
if (msg->cursor + slen >= msg->len)
|
2003-07-22 21:00:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROTOCOL_VIOLATION),
|
|
|
|
errmsg("invalid string in message")));
|
2003-04-19 02:02:30 +02:00
|
|
|
msg->cursor += slen + 1;
|
1999-04-25 05:19:27 +02:00
|
|
|
|
2005-09-24 19:53:28 +02:00
|
|
|
return pg_client_to_server(str, slen);
|
2003-04-19 02:02:30 +02:00
|
|
|
}
|
1999-04-25 05:19:27 +02:00
|
|
|
|
Fix several mistakes around parallel workers and client_encoding.
Previously, workers sent data to the leader using the client encoding.
That mostly worked, but the leader the converted the data back to the
server encoding. Since not all encoding conversions are reversible,
that could provoke failures. Fix by using the database encoding for
all communication between worker and leader.
Also, while temporary changes to GUC settings, as from the SET clause
of a function, are in general OK for parallel query, changing
client_encoding this way inside of a parallel worker is not OK.
Previously, that would have confused the leader; with these changes,
it would not confuse the leader, but it wouldn't do anything either.
So refuse such changes in parallel workers.
Also, the previous code naively assumed that when it received a
NotifyResonse from the worker, it could pass that directly back to the
user. But now that worker-to-leader communication always uses the
database encoding, that's clearly no longer correct - though,
actually, the old way was always broken for V2 clients. So
disassemble and reconstitute the message instead.
Issues reported by Peter Eisentraut. Patch by me, reviewed by
Peter Eisentraut.
2016-07-01 00:35:32 +02:00
|
|
|
/* --------------------------------
|
|
|
|
* pq_getmsgrawstring - get a null-terminated text string - NO conversion
|
|
|
|
*
|
|
|
|
* Returns a pointer directly into the message buffer.
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
pq_getmsgrawstring(StringInfo msg)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
int slen;
|
|
|
|
|
|
|
|
str = &msg->data[msg->cursor];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It's safe to use strlen() here because a StringInfo is guaranteed to
|
|
|
|
* have a trailing null byte. But check we found a null inside the
|
|
|
|
* message.
|
|
|
|
*/
|
|
|
|
slen = strlen(str);
|
|
|
|
if (msg->cursor + slen >= msg->len)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROTOCOL_VIOLATION),
|
|
|
|
errmsg("invalid string in message")));
|
|
|
|
msg->cursor += slen + 1;
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2003-04-19 02:02:30 +02:00
|
|
|
/* --------------------------------
|
|
|
|
* pq_getmsgend - verify message fully consumed
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pq_getmsgend(StringInfo msg)
|
|
|
|
{
|
|
|
|
if (msg->cursor != msg->len)
|
2003-07-22 21:00:12 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROTOCOL_VIOLATION),
|
|
|
|
errmsg("invalid message format")));
|
1999-04-25 05:19:27 +02:00
|
|
|
}
|