1999-04-25 05:19:27 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* pqformat.h
|
|
|
|
* Definitions for formatting and parsing frontend/backend messages
|
|
|
|
*
|
2021-01-02 19:06:25 +01:00
|
|
|
* Portions Copyright (c) 1996-2021, 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/include/libpq/pqformat.h
|
1999-04-25 05:19:27 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#ifndef PQFORMAT_H
|
|
|
|
#define PQFORMAT_H
|
|
|
|
|
|
|
|
#include "lib/stringinfo.h"
|
2017-10-12 01:01:52 +02:00
|
|
|
#include "mb/pg_wchar.h"
|
|
|
|
#include "port/pg_bswap.h"
|
1999-04-25 05:19:27 +02:00
|
|
|
|
2003-04-22 02:08:07 +02:00
|
|
|
extern void pq_beginmessage(StringInfo buf, char msgtype);
|
2017-10-12 01:01:52 +02:00
|
|
|
extern void pq_beginmessage_reuse(StringInfo buf, char msgtype);
|
|
|
|
extern void pq_endmessage(StringInfo buf);
|
|
|
|
extern void pq_endmessage_reuse(StringInfo buf);
|
|
|
|
|
1999-04-25 05:19:27 +02:00
|
|
|
extern void pq_sendbytes(StringInfo buf, const char *data, int datalen);
|
2003-05-08 20:16:37 +02:00
|
|
|
extern void pq_sendcountedtext(StringInfo buf, const char *str, int slen,
|
|
|
|
bool countincludesself);
|
2003-05-09 17:44:42 +02:00
|
|
|
extern void pq_sendtext(StringInfo buf, const char *str, int slen);
|
1999-04-25 21:27:47 +02:00
|
|
|
extern void pq_sendstring(StringInfo buf, const char *str);
|
2009-03-02 22:18:43 +01:00
|
|
|
extern void pq_send_ascii_string(StringInfo buf, const char *str);
|
2003-05-09 23:19:50 +02:00
|
|
|
extern void pq_sendfloat4(StringInfo buf, float4 f);
|
|
|
|
extern void pq_sendfloat8(StringInfo buf, float8 f);
|
2017-10-12 01:01:52 +02:00
|
|
|
|
|
|
|
/*
|
2018-06-27 08:40:32 +02:00
|
|
|
* Append a [u]int8 to a StringInfo buffer, which already has enough space
|
2017-10-12 01:01:52 +02:00
|
|
|
* preallocated.
|
|
|
|
*
|
2017-10-13 00:25:38 +02:00
|
|
|
* The use of pg_restrict allows the compiler to optimize the code based on
|
|
|
|
* the assumption that buf, buf->len, buf->data and *buf->data don't
|
2017-10-12 01:01:52 +02:00
|
|
|
* overlap. Without the annotation buf->len etc cannot be kept in a register
|
2017-10-13 17:46:05 +02:00
|
|
|
* over subsequent pq_writeintN calls.
|
2017-10-12 02:16:16 +02:00
|
|
|
*
|
|
|
|
* The use of StringInfoData * rather than StringInfo is due to MSVC being
|
|
|
|
* overly picky and demanding a * before a restrict.
|
2017-10-12 01:01:52 +02:00
|
|
|
*/
|
|
|
|
static inline void
|
2018-06-27 08:40:32 +02:00
|
|
|
pq_writeint8(StringInfoData *pg_restrict buf, uint8 i)
|
2017-10-12 01:01:52 +02:00
|
|
|
{
|
2018-06-27 08:40:32 +02:00
|
|
|
uint8 ni = i;
|
2017-10-12 01:01:52 +02:00
|
|
|
|
2018-06-27 08:40:32 +02:00
|
|
|
Assert(buf->len + (int) sizeof(uint8) <= buf->maxlen);
|
|
|
|
memcpy((char *pg_restrict) (buf->data + buf->len), &ni, sizeof(uint8));
|
|
|
|
buf->len += sizeof(uint8);
|
2017-10-12 01:01:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-06-27 08:40:32 +02:00
|
|
|
* Append a [u]int16 to a StringInfo buffer, which already has enough space
|
2017-10-12 01:01:52 +02:00
|
|
|
* preallocated.
|
|
|
|
*/
|
|
|
|
static inline void
|
2018-06-27 08:40:32 +02:00
|
|
|
pq_writeint16(StringInfoData *pg_restrict buf, uint16 i)
|
2017-10-12 01:01:52 +02:00
|
|
|
{
|
2018-06-27 08:40:32 +02:00
|
|
|
uint16 ni = pg_hton16(i);
|
2017-10-12 01:01:52 +02:00
|
|
|
|
2018-06-27 08:40:32 +02:00
|
|
|
Assert(buf->len + (int) sizeof(uint16) <= buf->maxlen);
|
|
|
|
memcpy((char *pg_restrict) (buf->data + buf->len), &ni, sizeof(uint16));
|
|
|
|
buf->len += sizeof(uint16);
|
2017-10-12 01:01:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-06-27 08:40:32 +02:00
|
|
|
* Append a [u]int32 to a StringInfo buffer, which already has enough space
|
2017-10-12 01:01:52 +02:00
|
|
|
* preallocated.
|
|
|
|
*/
|
|
|
|
static inline void
|
2018-06-27 08:40:32 +02:00
|
|
|
pq_writeint32(StringInfoData *pg_restrict buf, uint32 i)
|
2017-10-12 01:01:52 +02:00
|
|
|
{
|
2018-06-27 08:40:32 +02:00
|
|
|
uint32 ni = pg_hton32(i);
|
2017-10-12 01:01:52 +02:00
|
|
|
|
2018-06-27 08:40:32 +02:00
|
|
|
Assert(buf->len + (int) sizeof(uint32) <= buf->maxlen);
|
|
|
|
memcpy((char *pg_restrict) (buf->data + buf->len), &ni, sizeof(uint32));
|
|
|
|
buf->len += sizeof(uint32);
|
2017-10-12 01:01:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-06-27 08:40:32 +02:00
|
|
|
* Append a [u]int64 to a StringInfo buffer, which already has enough space
|
2017-10-12 01:01:52 +02:00
|
|
|
* preallocated.
|
|
|
|
*/
|
|
|
|
static inline void
|
2018-06-27 08:40:32 +02:00
|
|
|
pq_writeint64(StringInfoData *pg_restrict buf, uint64 i)
|
2017-10-12 01:01:52 +02:00
|
|
|
{
|
2018-06-27 08:40:32 +02:00
|
|
|
uint64 ni = pg_hton64(i);
|
2017-10-12 01:01:52 +02:00
|
|
|
|
2018-06-27 08:40:32 +02:00
|
|
|
Assert(buf->len + (int) sizeof(uint64) <= buf->maxlen);
|
|
|
|
memcpy((char *pg_restrict) (buf->data + buf->len), &ni, sizeof(uint64));
|
|
|
|
buf->len += sizeof(uint64);
|
2017-10-12 01:01:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Append a null-terminated text string (with conversion) to a buffer with
|
|
|
|
* preallocated space.
|
|
|
|
*
|
|
|
|
* NB: The pre-allocated space needs to be sufficient for the string after
|
|
|
|
* converting to client encoding.
|
|
|
|
*
|
|
|
|
* NB: passed text string must be null-terminated, and so is the data
|
|
|
|
* sent to the frontend.
|
|
|
|
*/
|
|
|
|
static inline void
|
2017-10-13 00:25:38 +02:00
|
|
|
pq_writestring(StringInfoData *pg_restrict buf, const char *pg_restrict str)
|
2017-10-12 01:01:52 +02:00
|
|
|
{
|
|
|
|
int slen = strlen(str);
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = pg_server_to_client(str, slen);
|
|
|
|
if (p != str) /* actual conversion has been done? */
|
|
|
|
slen = strlen(p);
|
|
|
|
|
|
|
|
Assert(buf->len + slen + 1 <= buf->maxlen);
|
|
|
|
|
2017-10-13 00:25:38 +02:00
|
|
|
memcpy(((char *pg_restrict) buf->data + buf->len), p, slen + 1);
|
2017-10-12 01:01:52 +02:00
|
|
|
buf->len += slen + 1;
|
|
|
|
|
|
|
|
if (p != str)
|
|
|
|
pfree(p);
|
|
|
|
}
|
|
|
|
|
2018-06-27 08:40:32 +02:00
|
|
|
/* append a binary [u]int8 to a StringInfo buffer */
|
2017-10-12 01:01:52 +02:00
|
|
|
static inline void
|
2018-06-27 08:40:32 +02:00
|
|
|
pq_sendint8(StringInfo buf, uint8 i)
|
2017-10-12 01:01:52 +02:00
|
|
|
{
|
2018-06-27 08:40:32 +02:00
|
|
|
enlargeStringInfo(buf, sizeof(uint8));
|
2017-10-12 01:01:52 +02:00
|
|
|
pq_writeint8(buf, i);
|
|
|
|
}
|
|
|
|
|
2018-06-27 08:40:32 +02:00
|
|
|
/* append a binary [u]int16 to a StringInfo buffer */
|
2017-10-12 01:01:52 +02:00
|
|
|
static inline void
|
2018-06-27 08:40:32 +02:00
|
|
|
pq_sendint16(StringInfo buf, uint16 i)
|
2017-10-12 01:01:52 +02:00
|
|
|
{
|
2018-06-27 08:40:32 +02:00
|
|
|
enlargeStringInfo(buf, sizeof(uint16));
|
2017-10-12 01:01:52 +02:00
|
|
|
pq_writeint16(buf, i);
|
|
|
|
}
|
|
|
|
|
2018-06-27 08:40:32 +02:00
|
|
|
/* append a binary [u]int32 to a StringInfo buffer */
|
2017-10-12 01:01:52 +02:00
|
|
|
static inline void
|
2018-06-27 08:40:32 +02:00
|
|
|
pq_sendint32(StringInfo buf, uint32 i)
|
2017-10-12 01:01:52 +02:00
|
|
|
{
|
2018-06-27 08:40:32 +02:00
|
|
|
enlargeStringInfo(buf, sizeof(uint32));
|
2017-10-12 01:01:52 +02:00
|
|
|
pq_writeint32(buf, i);
|
|
|
|
}
|
|
|
|
|
2018-06-27 08:40:32 +02:00
|
|
|
/* append a binary [u]int64 to a StringInfo buffer */
|
2017-10-12 01:01:52 +02:00
|
|
|
static inline void
|
2018-06-27 08:40:32 +02:00
|
|
|
pq_sendint64(StringInfo buf, uint64 i)
|
2017-10-12 01:01:52 +02:00
|
|
|
{
|
2018-06-27 08:40:32 +02:00
|
|
|
enlargeStringInfo(buf, sizeof(uint64));
|
2017-10-12 01:01:52 +02:00
|
|
|
pq_writeint64(buf, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* append a binary byte to a StringInfo buffer */
|
|
|
|
static inline void
|
2018-06-27 08:40:32 +02:00
|
|
|
pq_sendbyte(StringInfo buf, uint8 byt)
|
2017-10-12 01:01:52 +02:00
|
|
|
{
|
|
|
|
pq_sendint8(buf, byt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Append a binary integer to a StringInfo buffer
|
|
|
|
*
|
2017-10-13 17:46:05 +02:00
|
|
|
* This function is deprecated; prefer use of the functions above.
|
2017-10-12 01:01:52 +02:00
|
|
|
*/
|
|
|
|
static inline void
|
2018-06-27 08:40:32 +02:00
|
|
|
pq_sendint(StringInfo buf, uint32 i, int b)
|
2017-10-12 01:01:52 +02:00
|
|
|
{
|
|
|
|
switch (b)
|
|
|
|
{
|
|
|
|
case 1:
|
2018-06-27 08:40:32 +02:00
|
|
|
pq_sendint8(buf, (uint8) i);
|
2017-10-12 01:01:52 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
2018-06-27 08:40:32 +02:00
|
|
|
pq_sendint16(buf, (uint16) i);
|
2017-10-12 01:01:52 +02:00
|
|
|
break;
|
|
|
|
case 4:
|
2018-06-27 08:40:32 +02:00
|
|
|
pq_sendint32(buf, (uint32) i);
|
2017-10-12 01:01:52 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
elog(ERROR, "unsupported integer size %d", b);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-04-25 05:19:27 +02:00
|
|
|
|
2003-05-09 17:44:42 +02:00
|
|
|
extern void pq_begintypsend(StringInfo buf);
|
|
|
|
extern bytea *pq_endtypsend(StringInfo buf);
|
|
|
|
|
2003-04-22 02:08:07 +02:00
|
|
|
extern void pq_puttextmessage(char msgtype, const char *str);
|
|
|
|
extern void pq_putemptymessage(char msgtype);
|
1999-04-25 23:50:58 +02:00
|
|
|
|
2003-04-19 02:02:30 +02:00
|
|
|
extern int pq_getmsgbyte(StringInfo msg);
|
|
|
|
extern unsigned int pq_getmsgint(StringInfo msg, int b);
|
2003-05-09 17:44:42 +02:00
|
|
|
extern int64 pq_getmsgint64(StringInfo msg);
|
2003-05-09 23:19:50 +02:00
|
|
|
extern float4 pq_getmsgfloat4(StringInfo msg);
|
|
|
|
extern float8 pq_getmsgfloat8(StringInfo msg);
|
2003-04-19 02:02:30 +02:00
|
|
|
extern const char *pq_getmsgbytes(StringInfo msg, int datalen);
|
|
|
|
extern void pq_copymsgbytes(StringInfo msg, char *buf, int datalen);
|
2003-05-09 17:44:42 +02:00
|
|
|
extern char *pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes);
|
2003-04-19 02:02:30 +02:00
|
|
|
extern const char *pq_getmsgstring(StringInfo msg);
|
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
|
|
|
extern const char *pq_getmsgrawstring(StringInfo msg);
|
2003-04-19 02:02:30 +02:00
|
|
|
extern void pq_getmsgend(StringInfo msg);
|
2001-10-28 07:26:15 +01:00
|
|
|
|
1999-04-25 05:19:27 +02:00
|
|
|
#endif /* PQFORMAT_H */
|