PostgreSQL Source Code  git master
proto.c File Reference
#include "postgres.h"
#include "access/sysattr.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_type.h"
#include "libpq/pqformat.h"
#include "replication/logicalproto.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
Include dependency graph for proto.c:

Go to the source code of this file.

Macros

#define LOGICALREP_IS_REPLICA_IDENTITY   1
 
#define TRUNCATE_CASCADE   (1<<0)
 
#define TRUNCATE_RESTART_SEQS   (1<<1)
 

Functions

static void logicalrep_write_attrs (StringInfo out, Relation rel)
 
static void logicalrep_write_tuple (StringInfo out, Relation rel, HeapTuple tuple, bool binary)
 
static void logicalrep_read_attrs (StringInfo in, LogicalRepRelation *rel)
 
static void logicalrep_read_tuple (StringInfo in, LogicalRepTupleData *tuple)
 
static void logicalrep_write_namespace (StringInfo out, Oid nspid)
 
static const char * logicalrep_read_namespace (StringInfo in)
 
void logicalrep_write_begin (StringInfo out, ReorderBufferTXN *txn)
 
void logicalrep_read_begin (StringInfo in, LogicalRepBeginData *begin_data)
 
void logicalrep_write_commit (StringInfo out, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
void logicalrep_read_commit (StringInfo in, LogicalRepCommitData *commit_data)
 
void logicalrep_write_origin (StringInfo out, const char *origin, XLogRecPtr origin_lsn)
 
char * logicalrep_read_origin (StringInfo in, XLogRecPtr *origin_lsn)
 
void logicalrep_write_insert (StringInfo out, Relation rel, HeapTuple newtuple, bool binary)
 
LogicalRepRelId logicalrep_read_insert (StringInfo in, LogicalRepTupleData *newtup)
 
void logicalrep_write_update (StringInfo out, Relation rel, HeapTuple oldtuple, HeapTuple newtuple, bool binary)
 
LogicalRepRelId logicalrep_read_update (StringInfo in, bool *has_oldtuple, LogicalRepTupleData *oldtup, LogicalRepTupleData *newtup)
 
void logicalrep_write_delete (StringInfo out, Relation rel, HeapTuple oldtuple, bool binary)
 
LogicalRepRelId logicalrep_read_delete (StringInfo in, LogicalRepTupleData *oldtup)
 
void logicalrep_write_truncate (StringInfo out, int nrelids, Oid relids[], bool cascade, bool restart_seqs)
 
Listlogicalrep_read_truncate (StringInfo in, bool *cascade, bool *restart_seqs)
 
void logicalrep_write_rel (StringInfo out, Relation rel)
 
LogicalRepRelationlogicalrep_read_rel (StringInfo in)
 
void logicalrep_write_typ (StringInfo out, Oid typoid)
 
void logicalrep_read_typ (StringInfo in, LogicalRepTyp *ltyp)
 

Macro Definition Documentation

◆ LOGICALREP_IS_REPLICA_IDENTITY

#define LOGICALREP_IS_REPLICA_IDENTITY   1

Definition at line 26 of file proto.c.

Referenced by logicalrep_read_attrs(), and logicalrep_write_attrs().

◆ TRUNCATE_CASCADE

#define TRUNCATE_CASCADE   (1<<0)

Definition at line 28 of file proto.c.

Referenced by logicalrep_read_truncate(), and logicalrep_write_truncate().

◆ TRUNCATE_RESTART_SEQS

#define TRUNCATE_RESTART_SEQS   (1<<1)

Definition at line 29 of file proto.c.

Referenced by logicalrep_read_truncate(), and logicalrep_write_truncate().

Function Documentation

◆ logicalrep_read_attrs()

static void logicalrep_read_attrs ( StringInfo  in,
LogicalRepRelation rel 
)
static

Definition at line 652 of file proto.c.

References LogicalRepRelation::attkeys, LogicalRepRelation::attnames, LogicalRepRelation::atttyps, bms_add_member(), i, LOGICALREP_IS_REPLICA_IDENTITY, LogicalRepRelation::natts, palloc(), pq_getmsgbyte(), pq_getmsgint(), pq_getmsgstring(), and pstrdup().

Referenced by logicalrep_read_rel().

653 {
654  int i;
655  int natts;
656  char **attnames;
657  Oid *atttyps;
658  Bitmapset *attkeys = NULL;
659 
660  natts = pq_getmsgint(in, 2);
661  attnames = palloc(natts * sizeof(char *));
662  atttyps = palloc(natts * sizeof(Oid));
663 
664  /* read the attributes */
665  for (i = 0; i < natts; i++)
666  {
667  uint8 flags;
668 
669  /* Check for replica identity column */
670  flags = pq_getmsgbyte(in);
671  if (flags & LOGICALREP_IS_REPLICA_IDENTITY)
672  attkeys = bms_add_member(attkeys, i);
673 
674  /* attribute name */
675  attnames[i] = pstrdup(pq_getmsgstring(in));
676 
677  /* attribute type id */
678  atttyps[i] = (Oid) pq_getmsgint(in, 4);
679 
680  /* we ignore attribute mode for now */
681  (void) pq_getmsgint(in, 4);
682  }
683 
684  rel->attnames = attnames;
685  rel->atttyps = atttyps;
686  rel->attkeys = attkeys;
687  rel->natts = natts;
688 }
const char * pq_getmsgstring(StringInfo msg)
Definition: pqformat.c:581
char * pstrdup(const char *in)
Definition: mcxt.c:1186
unsigned char uint8
Definition: c.h:372
unsigned int Oid
Definition: postgres_ext.h:31
Bitmapset * attkeys
Definition: logicalproto.h:65
#define LOGICALREP_IS_REPLICA_IDENTITY
Definition: proto.c:26
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:401
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
void * palloc(Size size)
Definition: mcxt.c:949
int i
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417

◆ logicalrep_read_begin()

void logicalrep_read_begin ( StringInfo  in,
LogicalRepBeginData begin_data 
)

Definition at line 59 of file proto.c.

References LogicalRepBeginData::committime, elog, ERROR, LogicalRepBeginData::final_lsn, InvalidXLogRecPtr, pq_getmsgint(), pq_getmsgint64(), and LogicalRepBeginData::xid.

Referenced by apply_handle_begin().

60 {
61  /* read fields */
62  begin_data->final_lsn = pq_getmsgint64(in);
63  if (begin_data->final_lsn == InvalidXLogRecPtr)
64  elog(ERROR, "final_lsn not set in begin message");
65  begin_data->committime = pq_getmsgint64(in);
66  begin_data->xid = pq_getmsgint(in, 4);
67 }
TransactionId xid
Definition: logicalproto.h:81
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define ERROR
Definition: elog.h:43
XLogRecPtr final_lsn
Definition: logicalproto.h:79
#define elog(elevel,...)
Definition: elog.h:214
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:455
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
TimestampTz committime
Definition: logicalproto.h:80

◆ logicalrep_read_commit()

void logicalrep_read_commit ( StringInfo  in,
LogicalRepCommitData commit_data 
)

Definition at line 94 of file proto.c.

References LogicalRepCommitData::commit_lsn, LogicalRepCommitData::committime, elog, LogicalRepCommitData::end_lsn, ERROR, pq_getmsgbyte(), and pq_getmsgint64().

Referenced by apply_handle_commit().

95 {
96  /* read flags (unused for now) */
97  uint8 flags = pq_getmsgbyte(in);
98 
99  if (flags != 0)
100  elog(ERROR, "unrecognized flags %u in commit message", flags);
101 
102  /* read fields */
103  commit_data->commit_lsn = pq_getmsgint64(in);
104  commit_data->end_lsn = pq_getmsgint64(in);
105  commit_data->committime = pq_getmsgint64(in);
106 }
unsigned char uint8
Definition: c.h:372
#define ERROR
Definition: elog.h:43
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:401
#define elog(elevel,...)
Definition: elog.h:214
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:455
XLogRecPtr commit_lsn
Definition: logicalproto.h:86
TimestampTz committime
Definition: logicalproto.h:88

◆ logicalrep_read_delete()

LogicalRepRelId logicalrep_read_delete ( StringInfo  in,
LogicalRepTupleData oldtup 
)

Definition at line 275 of file proto.c.

References generate_unaccent_rules::action, elog, ERROR, logicalrep_read_tuple(), pq_getmsgbyte(), and pq_getmsgint().

Referenced by apply_handle_delete().

276 {
277  char action;
278  LogicalRepRelId relid;
279 
280  /* read the relation id */
281  relid = pq_getmsgint(in, 4);
282 
283  /* read and verify action */
284  action = pq_getmsgbyte(in);
285  if (action != 'K' && action != 'O')
286  elog(ERROR, "expected action 'O' or 'K', got %c", action);
287 
288  logicalrep_read_tuple(in, oldtup);
289 
290  return relid;
291 }
#define ERROR
Definition: elog.h:43
static void logicalrep_read_tuple(StringInfo in, LogicalRepTupleData *tuple)
Definition: proto.c:527
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:401
#define elog(elevel,...)
Definition: elog.h:214
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
uint32 LogicalRepRelId
Definition: logicalproto.h:51

◆ logicalrep_read_insert()

LogicalRepRelId logicalrep_read_insert ( StringInfo  in,
LogicalRepTupleData newtup 
)

Definition at line 158 of file proto.c.

References generate_unaccent_rules::action, elog, ERROR, logicalrep_read_tuple(), pq_getmsgbyte(), and pq_getmsgint().

Referenced by apply_handle_insert().

159 {
160  char action;
161  LogicalRepRelId relid;
162 
163  /* read the relation id */
164  relid = pq_getmsgint(in, 4);
165 
166  action = pq_getmsgbyte(in);
167  if (action != 'N')
168  elog(ERROR, "expected new tuple but got %d",
169  action);
170 
171  logicalrep_read_tuple(in, newtup);
172 
173  return relid;
174 }
#define ERROR
Definition: elog.h:43
static void logicalrep_read_tuple(StringInfo in, LogicalRepTupleData *tuple)
Definition: proto.c:527
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:401
#define elog(elevel,...)
Definition: elog.h:214
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
uint32 LogicalRepRelId
Definition: logicalproto.h:51

◆ logicalrep_read_namespace()

static const char * logicalrep_read_namespace ( StringInfo  in)
static

Definition at line 714 of file proto.c.

References pq_getmsgstring().

Referenced by logicalrep_read_rel(), and logicalrep_read_typ().

715 {
716  const char *nspname = pq_getmsgstring(in);
717 
718  if (nspname[0] == '\0')
719  nspname = "pg_catalog";
720 
721  return nspname;
722 }
const char * pq_getmsgstring(StringInfo msg)
Definition: pqformat.c:581

◆ logicalrep_read_origin()

char* logicalrep_read_origin ( StringInfo  in,
XLogRecPtr origin_lsn 
)

Definition at line 128 of file proto.c.

References pq_getmsgint64(), pq_getmsgstring(), and pstrdup().

129 {
130  /* fixed fields */
131  *origin_lsn = pq_getmsgint64(in);
132 
133  /* return origin */
134  return pstrdup(pq_getmsgstring(in));
135 }
const char * pq_getmsgstring(StringInfo msg)
Definition: pqformat.c:581
char * pstrdup(const char *in)
Definition: mcxt.c:1186
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:455

◆ logicalrep_read_rel()

LogicalRepRelation* logicalrep_read_rel ( StringInfo  in)

Definition at line 374 of file proto.c.

References logicalrep_read_attrs(), logicalrep_read_namespace(), LogicalRepRelation::nspname, palloc(), pq_getmsgbyte(), pq_getmsgint(), pq_getmsgstring(), pstrdup(), LogicalRepRelation::relname, LogicalRepRelation::remoteid, and LogicalRepRelation::replident.

Referenced by apply_handle_relation().

375 {
377 
378  rel->remoteid = pq_getmsgint(in, 4);
379 
380  /* Read relation name from stream */
382  rel->relname = pstrdup(pq_getmsgstring(in));
383 
384  /* Read the replica identity. */
385  rel->replident = pq_getmsgbyte(in);
386 
387  /* Get attribute description */
388  logicalrep_read_attrs(in, rel);
389 
390  return rel;
391 }
const char * pq_getmsgstring(StringInfo msg)
Definition: pqformat.c:581
char * pstrdup(const char *in)
Definition: mcxt.c:1186
LogicalRepRelId remoteid
Definition: logicalproto.h:57
static void logicalrep_read_attrs(StringInfo in, LogicalRepRelation *rel)
Definition: proto.c:652
static const char * logicalrep_read_namespace(StringInfo in)
Definition: proto.c:714
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:401
void * palloc(Size size)
Definition: mcxt.c:949
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417

◆ logicalrep_read_truncate()

List* logicalrep_read_truncate ( StringInfo  in,
bool cascade,
bool restart_seqs 
)

Definition at line 324 of file proto.c.

References i, lappend_oid(), NIL, pq_getmsgint(), TRUNCATE_CASCADE, and TRUNCATE_RESTART_SEQS.

Referenced by apply_handle_truncate().

326 {
327  int i;
328  int nrelids;
329  List *relids = NIL;
330  uint8 flags;
331 
332  nrelids = pq_getmsgint(in, 4);
333 
334  /* read and decode truncate flags */
335  flags = pq_getmsgint(in, 1);
336  *cascade = (flags & TRUNCATE_CASCADE) > 0;
337  *restart_seqs = (flags & TRUNCATE_RESTART_SEQS) > 0;
338 
339  for (i = 0; i < nrelids; i++)
340  relids = lappend_oid(relids, pq_getmsgint(in, 4));
341 
342  return relids;
343 }
#define NIL
Definition: pg_list.h:65
unsigned char uint8
Definition: c.h:372
List * lappend_oid(List *list, Oid datum)
Definition: list.c:357
int i
#define TRUNCATE_CASCADE
Definition: proto.c:28
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
Definition: pg_list.h:50
#define TRUNCATE_RESTART_SEQS
Definition: proto.c:29

◆ logicalrep_read_tuple()

static void logicalrep_read_tuple ( StringInfo  in,
LogicalRepTupleData tuple 
)
static

Definition at line 527 of file proto.c.

References LogicalRepTupleData::colstatus, LogicalRepTupleData::colvalues, StringInfoData::cursor, StringInfoData::data, elog, ERROR, i, StringInfoData::len, LOGICALREP_COLUMN_BINARY, LOGICALREP_COLUMN_NULL, LOGICALREP_COLUMN_TEXT, LOGICALREP_COLUMN_UNCHANGED, StringInfoData::maxlen, LogicalRepTupleData::ncols, palloc(), palloc0(), pq_copymsgbytes(), pq_getmsgbyte(), pq_getmsgint(), and value.

Referenced by logicalrep_read_delete(), logicalrep_read_insert(), and logicalrep_read_update().

528 {
529  int i;
530  int natts;
531 
532  /* Get number of attributes */
533  natts = pq_getmsgint(in, 2);
534 
535  /* Allocate space for per-column values; zero out unused StringInfoDatas */
536  tuple->colvalues = (StringInfoData *) palloc0(natts * sizeof(StringInfoData));
537  tuple->colstatus = (char *) palloc(natts * sizeof(char));
538  tuple->ncols = natts;
539 
540  /* Read the data */
541  for (i = 0; i < natts; i++)
542  {
543  char kind;
544  int len;
545  StringInfo value = &tuple->colvalues[i];
546 
547  kind = pq_getmsgbyte(in);
548  tuple->colstatus[i] = kind;
549 
550  switch (kind)
551  {
553  /* nothing more to do */
554  break;
556  /* we don't receive the value of an unchanged column */
557  break;
559  len = pq_getmsgint(in, 4); /* read length */
560 
561  /* and data */
562  value->data = palloc(len + 1);
563  pq_copymsgbytes(in, value->data, len);
564  value->data[len] = '\0';
565  /* make StringInfo fully valid */
566  value->len = len;
567  value->cursor = 0;
568  value->maxlen = len;
569  break;
571  len = pq_getmsgint(in, 4); /* read length */
572 
573  /* and data */
574  value->data = palloc(len + 1);
575  pq_copymsgbytes(in, value->data, len);
576  /* not strictly necessary but per StringInfo practice */
577  value->data[len] = '\0';
578  /* make StringInfo fully valid */
579  value->len = len;
580  value->cursor = 0;
581  value->maxlen = len;
582  break;
583  default:
584  elog(ERROR, "unrecognized data representation type '%c'", kind);
585  }
586  }
587 }
#define LOGICALREP_COLUMN_NULL
Definition: logicalproto.h:46
#define LOGICALREP_COLUMN_TEXT
Definition: logicalproto.h:48
StringInfoData * colvalues
Definition: logicalproto.h:37
#define LOGICALREP_COLUMN_BINARY
Definition: logicalproto.h:49
#define LOGICALREP_COLUMN_UNCHANGED
Definition: logicalproto.h:47
#define ERROR
Definition: elog.h:43
void * palloc0(Size size)
Definition: mcxt.c:980
static struct @143 value
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:401
void pq_copymsgbytes(StringInfo msg, char *buf, int datalen)
Definition: pqformat.c:530
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:214
int i
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417

◆ logicalrep_read_typ()

void logicalrep_read_typ ( StringInfo  in,
LogicalRepTyp ltyp 
)

Definition at line 426 of file proto.c.

References logicalrep_read_namespace(), LogicalRepTyp::nspname, pq_getmsgint(), pq_getmsgstring(), pstrdup(), LogicalRepTyp::remoteid, and LogicalRepTyp::typname.

Referenced by apply_handle_type().

427 {
428  ltyp->remoteid = pq_getmsgint(in, 4);
429 
430  /* Read type name from stream */
432  ltyp->typname = pstrdup(pq_getmsgstring(in));
433 }
char * typname
Definition: logicalproto.h:73
const char * pq_getmsgstring(StringInfo msg)
Definition: pqformat.c:581
char * pstrdup(const char *in)
Definition: mcxt.c:1186
static const char * logicalrep_read_namespace(StringInfo in)
Definition: proto.c:714
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
char * nspname
Definition: logicalproto.h:72

◆ logicalrep_read_update()

LogicalRepRelId logicalrep_read_update ( StringInfo  in,
bool has_oldtuple,
LogicalRepTupleData oldtup,
LogicalRepTupleData newtup 
)

Definition at line 209 of file proto.c.

References generate_unaccent_rules::action, elog, ERROR, logicalrep_read_tuple(), pq_getmsgbyte(), and pq_getmsgint().

Referenced by apply_handle_update().

212 {
213  char action;
214  LogicalRepRelId relid;
215 
216  /* read the relation id */
217  relid = pq_getmsgint(in, 4);
218 
219  /* read and verify action */
220  action = pq_getmsgbyte(in);
221  if (action != 'K' && action != 'O' && action != 'N')
222  elog(ERROR, "expected action 'N', 'O' or 'K', got %c",
223  action);
224 
225  /* check for old tuple */
226  if (action == 'K' || action == 'O')
227  {
228  logicalrep_read_tuple(in, oldtup);
229  *has_oldtuple = true;
230 
231  action = pq_getmsgbyte(in);
232  }
233  else
234  *has_oldtuple = false;
235 
236  /* check for new tuple */
237  if (action != 'N')
238  elog(ERROR, "expected action 'N', got %c",
239  action);
240 
241  logicalrep_read_tuple(in, newtup);
242 
243  return relid;
244 }
#define ERROR
Definition: elog.h:43
static void logicalrep_read_tuple(StringInfo in, LogicalRepTupleData *tuple)
Definition: proto.c:527
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:401
#define elog(elevel,...)
Definition: elog.h:214
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
uint32 LogicalRepRelId
Definition: logicalproto.h:51

◆ logicalrep_write_attrs()

static void logicalrep_write_attrs ( StringInfo  out,
Relation  rel 
)
static

Definition at line 593 of file proto.c.

References bms_free(), bms_is_member(), FirstLowInvalidHeapAttributeNumber, i, INDEX_ATTR_BITMAP_IDENTITY_KEY, LOGICALREP_IS_REPLICA_IDENTITY, NameStr, TupleDescData::natts, pq_sendbyte(), pq_sendint16(), pq_sendint32(), pq_sendstring(), RelationData::rd_rel, RelationGetDescr, RelationGetIndexAttrBitmap(), and TupleDescAttr.

Referenced by logicalrep_write_rel().

594 {
595  TupleDesc desc;
596  int i;
597  uint16 nliveatts = 0;
598  Bitmapset *idattrs = NULL;
599  bool replidentfull;
600 
601  desc = RelationGetDescr(rel);
602 
603  /* send number of live attributes */
604  for (i = 0; i < desc->natts; i++)
605  {
606  if (TupleDescAttr(desc, i)->attisdropped || TupleDescAttr(desc, i)->attgenerated)
607  continue;
608  nliveatts++;
609  }
610  pq_sendint16(out, nliveatts);
611 
612  /* fetch bitmap of REPLICATION IDENTITY attributes */
613  replidentfull = (rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL);
614  if (!replidentfull)
615  idattrs = RelationGetIndexAttrBitmap(rel,
617 
618  /* send the attributes */
619  for (i = 0; i < desc->natts; i++)
620  {
621  Form_pg_attribute att = TupleDescAttr(desc, i);
622  uint8 flags = 0;
623 
624  if (att->attisdropped || att->attgenerated)
625  continue;
626 
627  /* REPLICA IDENTITY FULL means all columns are sent as part of key. */
628  if (replidentfull ||
630  idattrs))
632 
633  pq_sendbyte(out, flags);
634 
635  /* attribute name */
636  pq_sendstring(out, NameStr(att->attname));
637 
638  /* attribute type id */
639  pq_sendint32(out, (int) att->atttypid);
640 
641  /* attribute mode */
642  pq_sendint32(out, att->atttypmod);
643  }
644 
645  bms_free(idattrs);
646 }
static void pq_sendint16(StringInfo buf, uint16 i)
Definition: pqformat.h:137
#define RelationGetDescr(relation)
Definition: rel.h:482
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
unsigned char uint8
Definition: c.h:372
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
void pq_sendstring(StringInfo buf, const char *str)
Definition: pqformat.c:197
Form_pg_class rd_rel
Definition: rel.h:109
#define LOGICALREP_IS_REPLICA_IDENTITY
Definition: proto.c:26
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
unsigned short uint16
Definition: c.h:373
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
int i
Bitmapset * RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
Definition: relcache.c:4937
#define NameStr(name)
Definition: c.h:622
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427

◆ logicalrep_write_begin()

void logicalrep_write_begin ( StringInfo  out,
ReorderBufferTXN txn 
)

Definition at line 45 of file proto.c.

References ReorderBufferTXN::commit_time, ReorderBufferTXN::final_lsn, pq_sendbyte(), pq_sendint32(), pq_sendint64(), and ReorderBufferTXN::xid.

Referenced by pgoutput_begin_txn().

46 {
47  pq_sendbyte(out, 'B'); /* BEGIN */
48 
49  /* fixed fields */
50  pq_sendint64(out, txn->final_lsn);
51  pq_sendint64(out, txn->commit_time);
52  pq_sendint32(out, txn->xid);
53 }
TimestampTz commit_time
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:153
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
XLogRecPtr final_lsn
TransactionId xid

◆ logicalrep_write_commit()

void logicalrep_write_commit ( StringInfo  out,
ReorderBufferTXN txn,
XLogRecPtr  commit_lsn 
)

Definition at line 74 of file proto.c.

References ReorderBufferTXN::commit_time, ReorderBufferTXN::end_lsn, pq_sendbyte(), and pq_sendint64().

Referenced by pgoutput_commit_txn().

76 {
77  uint8 flags = 0;
78 
79  pq_sendbyte(out, 'C'); /* sending COMMIT */
80 
81  /* send the flags field (unused for now) */
82  pq_sendbyte(out, flags);
83 
84  /* send fields */
85  pq_sendint64(out, commit_lsn);
86  pq_sendint64(out, txn->end_lsn);
87  pq_sendint64(out, txn->commit_time);
88 }
TimestampTz commit_time
unsigned char uint8
Definition: c.h:372
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:153
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161
XLogRecPtr end_lsn

◆ logicalrep_write_delete()

void logicalrep_write_delete ( StringInfo  out,
Relation  rel,
HeapTuple  oldtuple,
bool  binary 
)

Definition at line 250 of file proto.c.

References Assert, logicalrep_write_tuple(), pq_sendbyte(), pq_sendint32(), RelationData::rd_rel, and RelationGetRelid.

Referenced by pgoutput_change().

251 {
252  Assert(rel->rd_rel->relreplident == REPLICA_IDENTITY_DEFAULT ||
253  rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL ||
254  rel->rd_rel->relreplident == REPLICA_IDENTITY_INDEX);
255 
256  pq_sendbyte(out, 'D'); /* action DELETE */
257 
258  /* use Oid as relation identifier */
259  pq_sendint32(out, RelationGetRelid(rel));
260 
261  if (rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL)
262  pq_sendbyte(out, 'O'); /* old tuple follows */
263  else
264  pq_sendbyte(out, 'K'); /* old key follows */
265 
266  logicalrep_write_tuple(out, rel, oldtuple, binary);
267 }
static void logicalrep_write_tuple(StringInfo out, Relation rel, HeapTuple tuple, bool binary)
Definition: proto.c:439
Form_pg_class rd_rel
Definition: rel.h:109
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
#define Assert(condition)
Definition: c.h:745
#define RelationGetRelid(relation)
Definition: rel.h:456

◆ logicalrep_write_insert()

void logicalrep_write_insert ( StringInfo  out,
Relation  rel,
HeapTuple  newtuple,
bool  binary 
)

Definition at line 141 of file proto.c.

References logicalrep_write_tuple(), pq_sendbyte(), pq_sendint32(), and RelationGetRelid.

Referenced by pgoutput_change().

142 {
143  pq_sendbyte(out, 'I'); /* action INSERT */
144 
145  /* use Oid as relation identifier */
146  pq_sendint32(out, RelationGetRelid(rel));
147 
148  pq_sendbyte(out, 'N'); /* new tuple follows */
149  logicalrep_write_tuple(out, rel, newtuple, binary);
150 }
static void logicalrep_write_tuple(StringInfo out, Relation rel, HeapTuple tuple, bool binary)
Definition: proto.c:439
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
#define RelationGetRelid(relation)
Definition: rel.h:456

◆ logicalrep_write_namespace()

static void logicalrep_write_namespace ( StringInfo  out,
Oid  nspid 
)
static

Definition at line 694 of file proto.c.

References elog, ERROR, get_namespace_name(), pq_sendbyte(), and pq_sendstring().

Referenced by logicalrep_write_rel(), and logicalrep_write_typ().

695 {
696  if (nspid == PG_CATALOG_NAMESPACE)
697  pq_sendbyte(out, '\0');
698  else
699  {
700  char *nspname = get_namespace_name(nspid);
701 
702  if (nspname == NULL)
703  elog(ERROR, "cache lookup failed for namespace %u",
704  nspid);
705 
706  pq_sendstring(out, nspname);
707  }
708 }
void pq_sendstring(StringInfo buf, const char *str)
Definition: pqformat.c:197
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161
#define ERROR
Definition: elog.h:43
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3191
#define elog(elevel,...)
Definition: elog.h:214

◆ logicalrep_write_origin()

void logicalrep_write_origin ( StringInfo  out,
const char *  origin,
XLogRecPtr  origin_lsn 
)

Definition at line 112 of file proto.c.

References pq_sendbyte(), pq_sendint64(), and pq_sendstring().

Referenced by pgoutput_begin_txn().

114 {
115  pq_sendbyte(out, 'O'); /* ORIGIN */
116 
117  /* fixed fields */
118  pq_sendint64(out, origin_lsn);
119 
120  /* origin string */
121  pq_sendstring(out, origin);
122 }
void pq_sendstring(StringInfo buf, const char *str)
Definition: pqformat.c:197
static void pq_sendint64(StringInfo buf, uint64 i)
Definition: pqformat.h:153
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161

◆ logicalrep_write_rel()

void logicalrep_write_rel ( StringInfo  out,
Relation  rel 
)

Definition at line 349 of file proto.c.

References logicalrep_write_attrs(), logicalrep_write_namespace(), pq_sendbyte(), pq_sendint32(), pq_sendstring(), RelationData::rd_rel, RelationGetNamespace, RelationGetRelationName, RelationGetRelid, and relname.

Referenced by send_relation_and_attrs().

350 {
351  char *relname;
352 
353  pq_sendbyte(out, 'R'); /* sending RELATION */
354 
355  /* use Oid as relation identifier */
356  pq_sendint32(out, RelationGetRelid(rel));
357 
358  /* send qualified relation name */
360  relname = RelationGetRelationName(rel);
361  pq_sendstring(out, relname);
362 
363  /* send replica identity */
364  pq_sendbyte(out, rel->rd_rel->relreplident);
365 
366  /* send the attribute info */
367  logicalrep_write_attrs(out, rel);
368 }
static void logicalrep_write_namespace(StringInfo out, Oid nspid)
Definition: proto.c:694
void pq_sendstring(StringInfo buf, const char *str)
Definition: pqformat.c:197
Form_pg_class rd_rel
Definition: rel.h:109
NameData relname
Definition: pg_class.h:38
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
static void logicalrep_write_attrs(StringInfo out, Relation rel)
Definition: proto.c:593
#define RelationGetRelationName(relation)
Definition: rel.h:490
#define RelationGetRelid(relation)
Definition: rel.h:456
#define RelationGetNamespace(relation)
Definition: rel.h:497

◆ logicalrep_write_truncate()

void logicalrep_write_truncate ( StringInfo  out,
int  nrelids,
Oid  relids[],
bool  cascade,
bool  restart_seqs 
)

Definition at line 297 of file proto.c.

References i, pq_sendbyte(), pq_sendint32(), pq_sendint8(), TRUNCATE_CASCADE, and TRUNCATE_RESTART_SEQS.

Referenced by pgoutput_truncate().

301 {
302  int i;
303  uint8 flags = 0;
304 
305  pq_sendbyte(out, 'T'); /* action TRUNCATE */
306 
307  pq_sendint32(out, nrelids);
308 
309  /* encode and send truncate flags */
310  if (cascade)
311  flags |= TRUNCATE_CASCADE;
312  if (restart_seqs)
313  flags |= TRUNCATE_RESTART_SEQS;
314  pq_sendint8(out, flags);
315 
316  for (i = 0; i < nrelids; i++)
317  pq_sendint32(out, relids[i]);
318 }
unsigned char uint8
Definition: c.h:372
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
int i
#define TRUNCATE_CASCADE
Definition: proto.c:28
static void pq_sendint8(StringInfo buf, uint8 i)
Definition: pqformat.h:129
#define TRUNCATE_RESTART_SEQS
Definition: proto.c:29

◆ logicalrep_write_tuple()

static void logicalrep_write_tuple ( StringInfo  out,
Relation  rel,
HeapTuple  tuple,
bool  binary 
)
static

Definition at line 439 of file proto.c.

References elog, enlargeStringInfo(), ERROR, GETSTRUCT, heap_deform_tuple(), HeapTupleIsValid, i, LOGICALREP_COLUMN_BINARY, LOGICALREP_COLUMN_NULL, LOGICALREP_COLUMN_TEXT, LOGICALREP_COLUMN_UNCHANGED, MaxTupleAttributeNumber, TupleDescData::natts, ObjectIdGetDatum, OidIsValid, OidOutputFunctionCall(), OidSendFunctionCall(), pfree(), pq_sendbyte(), pq_sendbytes(), pq_sendcountedtext(), pq_sendint(), pq_sendint16(), RelationGetDescr, ReleaseSysCache(), SearchSysCache1(), HeapTupleData::t_len, TupleDescAttr, TYPEOID, values, VARATT_IS_EXTERNAL_ONDISK, VARDATA, VARHDRSZ, and VARSIZE.

Referenced by logicalrep_write_delete(), logicalrep_write_insert(), and logicalrep_write_update().

440 {
441  TupleDesc desc;
443  bool isnull[MaxTupleAttributeNumber];
444  int i;
445  uint16 nliveatts = 0;
446 
447  desc = RelationGetDescr(rel);
448 
449  for (i = 0; i < desc->natts; i++)
450  {
451  if (TupleDescAttr(desc, i)->attisdropped || TupleDescAttr(desc, i)->attgenerated)
452  continue;
453  nliveatts++;
454  }
455  pq_sendint16(out, nliveatts);
456 
457  /* try to allocate enough memory from the get-go */
458  enlargeStringInfo(out, tuple->t_len +
459  nliveatts * (1 + 4));
460 
461  heap_deform_tuple(tuple, desc, values, isnull);
462 
463  /* Write the values */
464  for (i = 0; i < desc->natts; i++)
465  {
466  HeapTuple typtup;
467  Form_pg_type typclass;
468  Form_pg_attribute att = TupleDescAttr(desc, i);
469  char *outputstr;
470 
471  if (att->attisdropped || att->attgenerated)
472  continue;
473 
474  if (isnull[i])
475  {
477  continue;
478  }
479 
480  if (att->attlen == -1 && VARATT_IS_EXTERNAL_ONDISK(values[i]))
481  {
482  /*
483  * Unchanged toasted datum. (Note that we don't promise to detect
484  * unchanged data in general; this is just a cheap check to avoid
485  * sending large values unnecessarily.)
486  */
488  continue;
489  }
490 
491  typtup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(att->atttypid));
492  if (!HeapTupleIsValid(typtup))
493  elog(ERROR, "cache lookup failed for type %u", att->atttypid);
494  typclass = (Form_pg_type) GETSTRUCT(typtup);
495 
496  /*
497  * Send in binary if requested and type has suitable send function.
498  */
499  if (binary && OidIsValid(typclass->typsend))
500  {
501  bytea *outputbytes;
502  int len;
503 
505  outputbytes = OidSendFunctionCall(typclass->typsend, values[i]);
506  len = VARSIZE(outputbytes) - VARHDRSZ;
507  pq_sendint(out, len, 4); /* length */
508  pq_sendbytes(out, VARDATA(outputbytes), len); /* data */
509  pfree(outputbytes);
510  }
511  else
512  {
514  outputstr = OidOutputFunctionCall(typclass->typoutput, values[i]);
515  pq_sendcountedtext(out, outputstr, strlen(outputstr), false);
516  pfree(outputstr);
517  }
518 
519  ReleaseSysCache(typtup);
520  }
521 }
static void pq_sendint16(StringInfo buf, uint16 i)
Definition: pqformat.h:137
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: postgres.h:314
#define VARDATA(PTR)
Definition: postgres.h:302
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
static void pq_sendint(StringInfo buf, uint32 i, int b)
Definition: pqformat.h:172
#define MaxTupleAttributeNumber
Definition: htup_details.h:33
#define RelationGetDescr(relation)
Definition: rel.h:482
#define VARSIZE(PTR)
Definition: postgres.h:303
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define VARHDRSZ
Definition: c.h:568
#define LOGICALREP_COLUMN_NULL
Definition: logicalproto.h:46
#define LOGICALREP_COLUMN_TEXT
Definition: logicalproto.h:48
#define OidIsValid(objectId)
Definition: c.h:651
#define LOGICALREP_COLUMN_BINARY
Definition: logicalproto.h:49
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161
#define LOGICALREP_COLUMN_UNCHANGED
Definition: logicalproto.h:47
unsigned short uint16
Definition: c.h:373
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
bytea * OidSendFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1676
uint32 t_len
Definition: htup.h:64
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:283
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
void pq_sendcountedtext(StringInfo buf, const char *str, int slen, bool countincludesself)
Definition: pqformat.c:142
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_type * Form_pg_type
Definition: pg_type.h:255
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:1249
static Datum values[MAXATTR]
Definition: bootstrap.c:167
void pq_sendbytes(StringInfo buf, const char *data, int datalen)
Definition: pqformat.c:125
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1657
#define elog(elevel,...)
Definition: elog.h:214
int i
Definition: c.h:562

◆ logicalrep_write_typ()

void logicalrep_write_typ ( StringInfo  out,
Oid  typoid 
)

Definition at line 399 of file proto.c.

References elog, ERROR, getBaseType(), GETSTRUCT, HeapTupleIsValid, logicalrep_write_namespace(), NameStr, ObjectIdGetDatum, pq_sendbyte(), pq_sendint32(), pq_sendstring(), ReleaseSysCache(), SearchSysCache1(), and TYPEOID.

Referenced by send_relation_and_attrs().

400 {
401  Oid basetypoid = getBaseType(typoid);
402  HeapTuple tup;
403  Form_pg_type typtup;
404 
405  pq_sendbyte(out, 'Y'); /* sending TYPE */
406 
407  tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(basetypoid));
408  if (!HeapTupleIsValid(tup))
409  elog(ERROR, "cache lookup failed for type %u", basetypoid);
410  typtup = (Form_pg_type) GETSTRUCT(tup);
411 
412  /* use Oid as relation identifier */
413  pq_sendint32(out, typoid);
414 
415  /* send qualified type name */
416  logicalrep_write_namespace(out, typtup->typnamespace);
417  pq_sendstring(out, NameStr(typtup->typname));
418 
419  ReleaseSysCache(tup);
420 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
static void logicalrep_write_namespace(StringInfo out, Oid nspid)
Definition: proto.c:694
void pq_sendstring(StringInfo buf, const char *str)
Definition: pqformat.c:197
unsigned int Oid
Definition: postgres_ext.h:31
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_type * Form_pg_type
Definition: pg_type.h:255
#define elog(elevel,...)
Definition: elog.h:214
#define NameStr(name)
Definition: c.h:622
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2409

◆ logicalrep_write_update()

void logicalrep_write_update ( StringInfo  out,
Relation  rel,
HeapTuple  oldtuple,
HeapTuple  newtuple,
bool  binary 
)

Definition at line 180 of file proto.c.

References Assert, logicalrep_write_tuple(), pq_sendbyte(), pq_sendint32(), RelationData::rd_rel, and RelationGetRelid.

Referenced by pgoutput_change().

182 {
183  pq_sendbyte(out, 'U'); /* action UPDATE */
184 
185  Assert(rel->rd_rel->relreplident == REPLICA_IDENTITY_DEFAULT ||
186  rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL ||
187  rel->rd_rel->relreplident == REPLICA_IDENTITY_INDEX);
188 
189  /* use Oid as relation identifier */
190  pq_sendint32(out, RelationGetRelid(rel));
191 
192  if (oldtuple != NULL)
193  {
194  if (rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL)
195  pq_sendbyte(out, 'O'); /* old tuple follows */
196  else
197  pq_sendbyte(out, 'K'); /* old key follows */
198  logicalrep_write_tuple(out, rel, oldtuple, binary);
199  }
200 
201  pq_sendbyte(out, 'N'); /* new tuple follows */
202  logicalrep_write_tuple(out, rel, newtuple, binary);
203 }
static void logicalrep_write_tuple(StringInfo out, Relation rel, HeapTuple tuple, bool binary)
Definition: proto.c:439
Form_pg_class rd_rel
Definition: rel.h:109
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
#define Assert(condition)
Definition: c.h:745
#define RelationGetRelid(relation)
Definition: rel.h:456