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, TransactionId xid, Relation rel, HeapTuple newtuple, bool binary)
 
LogicalRepRelId logicalrep_read_insert (StringInfo in, LogicalRepTupleData *newtup)
 
void logicalrep_write_update (StringInfo out, TransactionId xid, 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, TransactionId xid, Relation rel, HeapTuple oldtuple, bool binary)
 
LogicalRepRelId logicalrep_read_delete (StringInfo in, LogicalRepTupleData *oldtup)
 
void logicalrep_write_truncate (StringInfo out, TransactionId xid, 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, TransactionId xid, Relation rel)
 
LogicalRepRelationlogicalrep_read_rel (StringInfo in)
 
void logicalrep_write_typ (StringInfo out, TransactionId xid, Oid typoid)
 
void logicalrep_read_typ (StringInfo in, LogicalRepTyp *ltyp)
 
void logicalrep_write_stream_start (StringInfo out, TransactionId xid, bool first_segment)
 
TransactionId logicalrep_read_stream_start (StringInfo in, bool *first_segment)
 
void logicalrep_write_stream_stop (StringInfo out)
 
void logicalrep_write_stream_commit (StringInfo out, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
TransactionId logicalrep_read_stream_commit (StringInfo in, LogicalRepCommitData *commit_data)
 
void logicalrep_write_stream_abort (StringInfo out, TransactionId xid, TransactionId subxid)
 
void logicalrep_read_stream_abort (StringInfo in, TransactionId *xid, TransactionId *subxid)
 

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 679 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().

680 {
681  int i;
682  int natts;
683  char **attnames;
684  Oid *atttyps;
685  Bitmapset *attkeys = NULL;
686 
687  natts = pq_getmsgint(in, 2);
688  attnames = palloc(natts * sizeof(char *));
689  atttyps = palloc(natts * sizeof(Oid));
690 
691  /* read the attributes */
692  for (i = 0; i < natts; i++)
693  {
694  uint8 flags;
695 
696  /* Check for replica identity column */
697  flags = pq_getmsgbyte(in);
698  if (flags & LOGICALREP_IS_REPLICA_IDENTITY)
699  attkeys = bms_add_member(attkeys, i);
700 
701  /* attribute name */
702  attnames[i] = pstrdup(pq_getmsgstring(in));
703 
704  /* attribute type id */
705  atttyps[i] = (Oid) pq_getmsgint(in, 4);
706 
707  /* we ignore attribute mode for now */
708  (void) pq_getmsgint(in, 4);
709  }
710 
711  rel->attnames = attnames;
712  rel->atttyps = atttyps;
713  rel->attkeys = attkeys;
714  rel->natts = natts;
715 }
const char * pq_getmsgstring(StringInfo msg)
Definition: pqformat.c:581
char * pstrdup(const char *in)
Definition: mcxt.c:1187
unsigned char uint8
Definition: c.h:427
unsigned int Oid
Definition: postgres_ext.h:31
Bitmapset * attkeys
Definition: logicalproto.h:98
#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:950
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:114
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define ERROR
Definition: elog.h:43
XLogRecPtr final_lsn
Definition: logicalproto.h:112
#define elog(elevel,...)
Definition: elog.h:228
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:113

◆ 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:427
#define ERROR
Definition: elog.h:43
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:401
#define elog(elevel,...)
Definition: elog.h:228
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:455
TimestampTz committime
Definition: logicalproto.h:121

◆ logicalrep_read_delete()

LogicalRepRelId logicalrep_read_delete ( StringInfo  in,
LogicalRepTupleData oldtup 
)

Definition at line 289 of file proto.c.

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

Referenced by apply_handle_delete().

290 {
291  char action;
292  LogicalRepRelId relid;
293 
294  /* read the relation id */
295  relid = pq_getmsgint(in, 4);
296 
297  /* read and verify action */
298  action = pq_getmsgbyte(in);
299  if (action != 'K' && action != 'O')
300  elog(ERROR, "expected action 'O' or 'K', got %c", action);
301 
302  logicalrep_read_tuple(in, oldtup);
303 
304  return relid;
305 }
#define ERROR
Definition: elog.h:43
static void logicalrep_read_tuple(StringInfo in, LogicalRepTupleData *tuple)
Definition: proto.c:554
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:401
#define elog(elevel,...)
Definition: elog.h:228
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
uint32 LogicalRepRelId
Definition: logicalproto.h:84

◆ logicalrep_read_insert()

LogicalRepRelId logicalrep_read_insert ( StringInfo  in,
LogicalRepTupleData newtup 
)

Definition at line 163 of file proto.c.

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

Referenced by apply_handle_insert().

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

◆ logicalrep_read_namespace()

static const char * logicalrep_read_namespace ( StringInfo  in)
static

Definition at line 741 of file proto.c.

References pq_getmsgstring().

Referenced by logicalrep_read_rel(), and logicalrep_read_typ().

742 {
743  const char *nspname = pq_getmsgstring(in);
744 
745  if (nspname[0] == '\0')
746  nspname = "pg_catalog";
747 
748  return nspname;
749 }
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:1187
int64 pq_getmsgint64(StringInfo msg)
Definition: pqformat.c:455

◆ logicalrep_read_rel()

LogicalRepRelation* logicalrep_read_rel ( StringInfo  in)

Definition at line 397 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().

398 {
400 
401  rel->remoteid = pq_getmsgint(in, 4);
402 
403  /* Read relation name from stream */
405  rel->relname = pstrdup(pq_getmsgstring(in));
406 
407  /* Read the replica identity. */
408  rel->replident = pq_getmsgbyte(in);
409 
410  /* Get attribute description */
411  logicalrep_read_attrs(in, rel);
412 
413  return rel;
414 }
const char * pq_getmsgstring(StringInfo msg)
Definition: pqformat.c:581
char * pstrdup(const char *in)
Definition: mcxt.c:1187
LogicalRepRelId remoteid
Definition: logicalproto.h:90
static void logicalrep_read_attrs(StringInfo in, LogicalRepRelation *rel)
Definition: proto.c:679
static const char * logicalrep_read_namespace(StringInfo in)
Definition: proto.c:741
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:401
void * palloc(Size size)
Definition: mcxt.c:950
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417

◆ logicalrep_read_stream_abort()

void logicalrep_read_stream_abort ( StringInfo  in,
TransactionId xid,
TransactionId subxid 
)

Definition at line 865 of file proto.c.

References Assert, and pq_getmsgint().

Referenced by apply_handle_stream_abort().

867 {
868  Assert(xid && subxid);
869 
870  *xid = pq_getmsgint(in, 4);
871  *subxid = pq_getmsgint(in, 4);
872 }
#define Assert(condition)
Definition: c.h:800
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417

◆ logicalrep_read_stream_commit()

TransactionId logicalrep_read_stream_commit ( StringInfo  in,
LogicalRepCommitData commit_data 
)

Definition at line 823 of file proto.c.

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

Referenced by apply_handle_stream_commit().

824 {
825  TransactionId xid;
826  uint8 flags;
827 
828  xid = pq_getmsgint(in, 4);
829 
830  /* read flags (unused for now) */
831  flags = pq_getmsgbyte(in);
832 
833  if (flags != 0)
834  elog(ERROR, "unrecognized flags %u in commit message", flags);
835 
836  /* read fields */
837  commit_data->commit_lsn = pq_getmsgint64(in);
838  commit_data->end_lsn = pq_getmsgint64(in);
839  commit_data->committime = pq_getmsgint64(in);
840 
841  return xid;
842 }
uint32 TransactionId
Definition: c.h:575
unsigned char uint8
Definition: c.h:427
#define ERROR
Definition: elog.h:43
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:401
#define elog(elevel,...)
Definition: elog.h:228
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:121

◆ logicalrep_read_stream_start()

TransactionId logicalrep_read_stream_start ( StringInfo  in,
bool first_segment 
)

Definition at line 773 of file proto.c.

References Assert, pq_getmsgbyte(), and pq_getmsgint().

Referenced by apply_handle_stream_start().

774 {
775  TransactionId xid;
776 
777  Assert(first_segment);
778 
779  xid = pq_getmsgint(in, 4);
780  *first_segment = (pq_getmsgbyte(in) == 1);
781 
782  return xid;
783 }
uint32 TransactionId
Definition: c.h:575
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:401
#define Assert(condition)
Definition: c.h:800
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 343 of file proto.c.

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

Referenced by apply_handle_truncate().

345 {
346  int i;
347  int nrelids;
348  List *relids = NIL;
349  uint8 flags;
350 
351  nrelids = pq_getmsgint(in, 4);
352 
353  /* read and decode truncate flags */
354  flags = pq_getmsgint(in, 1);
355  *cascade = (flags & TRUNCATE_CASCADE) > 0;
356  *restart_seqs = (flags & TRUNCATE_RESTART_SEQS) > 0;
357 
358  for (i = 0; i < nrelids; i++)
359  relids = lappend_oid(relids, pq_getmsgint(in, 4));
360 
361  return relids;
362 }
#define NIL
Definition: pg_list.h:65
unsigned char uint8
Definition: c.h:427
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 554 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().

555 {
556  int i;
557  int natts;
558 
559  /* Get number of attributes */
560  natts = pq_getmsgint(in, 2);
561 
562  /* Allocate space for per-column values; zero out unused StringInfoDatas */
563  tuple->colvalues = (StringInfoData *) palloc0(natts * sizeof(StringInfoData));
564  tuple->colstatus = (char *) palloc(natts * sizeof(char));
565  tuple->ncols = natts;
566 
567  /* Read the data */
568  for (i = 0; i < natts; i++)
569  {
570  char kind;
571  int len;
572  StringInfo value = &tuple->colvalues[i];
573 
574  kind = pq_getmsgbyte(in);
575  tuple->colstatus[i] = kind;
576 
577  switch (kind)
578  {
580  /* nothing more to do */
581  break;
583  /* we don't receive the value of an unchanged column */
584  break;
586  len = pq_getmsgint(in, 4); /* read length */
587 
588  /* and data */
589  value->data = palloc(len + 1);
590  pq_copymsgbytes(in, value->data, len);
591  value->data[len] = '\0';
592  /* make StringInfo fully valid */
593  value->len = len;
594  value->cursor = 0;
595  value->maxlen = len;
596  break;
598  len = pq_getmsgint(in, 4); /* read length */
599 
600  /* and data */
601  value->data = palloc(len + 1);
602  pq_copymsgbytes(in, value->data, len);
603  /* not strictly necessary but per StringInfo practice */
604  value->data[len] = '\0';
605  /* make StringInfo fully valid */
606  value->len = len;
607  value->cursor = 0;
608  value->maxlen = len;
609  break;
610  default:
611  elog(ERROR, "unrecognized data representation type '%c'", kind);
612  }
613  }
614 }
#define LOGICALREP_COLUMN_NULL
Definition: logicalproto.h:79
#define LOGICALREP_COLUMN_TEXT
Definition: logicalproto.h:81
StringInfoData * colvalues
Definition: logicalproto.h:70
#define LOGICALREP_COLUMN_BINARY
Definition: logicalproto.h:82
#define LOGICALREP_COLUMN_UNCHANGED
Definition: logicalproto.h:80
#define ERROR
Definition: elog.h:43
void * palloc0(Size size)
Definition: mcxt.c:981
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:950
#define elog(elevel,...)
Definition: elog.h:228
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 453 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().

454 {
455  ltyp->remoteid = pq_getmsgint(in, 4);
456 
457  /* Read type name from stream */
459  ltyp->typname = pstrdup(pq_getmsgstring(in));
460 }
const char * pq_getmsgstring(StringInfo msg)
Definition: pqformat.c:581
char * pstrdup(const char *in)
Definition: mcxt.c:1187
static const char * logicalrep_read_namespace(StringInfo in)
Definition: proto.c:741
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417

◆ logicalrep_read_update()

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

Definition at line 218 of file proto.c.

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

Referenced by apply_handle_update().

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

◆ logicalrep_write_attrs()

static void logicalrep_write_attrs ( StringInfo  out,
Relation  rel 
)
static

Definition at line 620 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().

621 {
622  TupleDesc desc;
623  int i;
624  uint16 nliveatts = 0;
625  Bitmapset *idattrs = NULL;
626  bool replidentfull;
627 
628  desc = RelationGetDescr(rel);
629 
630  /* send number of live attributes */
631  for (i = 0; i < desc->natts; i++)
632  {
633  if (TupleDescAttr(desc, i)->attisdropped || TupleDescAttr(desc, i)->attgenerated)
634  continue;
635  nliveatts++;
636  }
637  pq_sendint16(out, nliveatts);
638 
639  /* fetch bitmap of REPLICATION IDENTITY attributes */
640  replidentfull = (rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL);
641  if (!replidentfull)
642  idattrs = RelationGetIndexAttrBitmap(rel,
644 
645  /* send the attributes */
646  for (i = 0; i < desc->natts; i++)
647  {
648  Form_pg_attribute att = TupleDescAttr(desc, i);
649  uint8 flags = 0;
650 
651  if (att->attisdropped || att->attgenerated)
652  continue;
653 
654  /* REPLICA IDENTITY FULL means all columns are sent as part of key. */
655  if (replidentfull ||
657  idattrs))
659 
660  pq_sendbyte(out, flags);
661 
662  /* attribute name */
663  pq_sendstring(out, NameStr(att->attname));
664 
665  /* attribute type id */
666  pq_sendint32(out, (int) att->atttypid);
667 
668  /* attribute mode */
669  pq_sendint32(out, att->atttypmod);
670  }
671 
672  bms_free(idattrs);
673 }
static void pq_sendint16(StringInfo buf, uint16 i)
Definition: pqformat.h:137
#define RelationGetDescr(relation)
Definition: rel.h:483
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
unsigned char uint8
Definition: c.h:427
#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:110
#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:428
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:4945
#define NameStr(name)
Definition: c.h:677
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, LOGICAL_REP_MSG_BEGIN, pq_sendbyte(), pq_sendint32(), pq_sendint64(), and ReorderBufferTXN::xid.

Referenced by pgoutput_begin_txn().

46 {
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, LOGICAL_REP_MSG_COMMIT, pq_sendbyte(), and pq_sendint64().

Referenced by pgoutput_commit_txn().

76 {
77  uint8 flags = 0;
78 
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:427
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,
TransactionId  xid,
Relation  rel,
HeapTuple  oldtuple,
bool  binary 
)

Definition at line 259 of file proto.c.

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

Referenced by pgoutput_change().

261 {
262  Assert(rel->rd_rel->relreplident == REPLICA_IDENTITY_DEFAULT ||
263  rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL ||
264  rel->rd_rel->relreplident == REPLICA_IDENTITY_INDEX);
265 
267 
268  /* transaction ID (if not valid, we're not streaming) */
269  if (TransactionIdIsValid(xid))
270  pq_sendint32(out, xid);
271 
272  /* use Oid as relation identifier */
273  pq_sendint32(out, RelationGetRelid(rel));
274 
275  if (rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL)
276  pq_sendbyte(out, 'O'); /* old tuple follows */
277  else
278  pq_sendbyte(out, 'K'); /* old key follows */
279 
280  logicalrep_write_tuple(out, rel, oldtuple, binary);
281 }
static void logicalrep_write_tuple(StringInfo out, Relation rel, HeapTuple tuple, bool binary)
Definition: proto.c:466
Form_pg_class rd_rel
Definition: rel.h:110
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:800
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define RelationGetRelid(relation)
Definition: rel.h:457

◆ logicalrep_write_insert()

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

Definition at line 141 of file proto.c.

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

Referenced by pgoutput_change().

143 {
145 
146  /* transaction ID (if not valid, we're not streaming) */
147  if (TransactionIdIsValid(xid))
148  pq_sendint32(out, xid);
149 
150  /* use Oid as relation identifier */
151  pq_sendint32(out, RelationGetRelid(rel));
152 
153  pq_sendbyte(out, 'N'); /* new tuple follows */
154  logicalrep_write_tuple(out, rel, newtuple, binary);
155 }
static void logicalrep_write_tuple(StringInfo out, Relation rel, HeapTuple tuple, bool binary)
Definition: proto.c:466
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 TransactionIdIsValid(xid)
Definition: transam.h:41
#define RelationGetRelid(relation)
Definition: rel.h:457

◆ logicalrep_write_namespace()

static void logicalrep_write_namespace ( StringInfo  out,
Oid  nspid 
)
static

Definition at line 721 of file proto.c.

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

Referenced by logicalrep_write_rel(), and logicalrep_write_typ().

722 {
723  if (nspid == PG_CATALOG_NAMESPACE)
724  pq_sendbyte(out, '\0');
725  else
726  {
727  char *nspname = get_namespace_name(nspid);
728 
729  if (nspname == NULL)
730  elog(ERROR, "cache lookup failed for namespace %u",
731  nspid);
732 
733  pq_sendstring(out, nspname);
734  }
735 }
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:3196
#define elog(elevel,...)
Definition: elog.h:228

◆ logicalrep_write_origin()

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

Definition at line 112 of file proto.c.

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

Referenced by pgoutput_begin_txn(), and pgoutput_stream_start().

114 {
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,
TransactionId  xid,
Relation  rel 
)

Definition at line 368 of file proto.c.

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

Referenced by send_relation_and_attrs().

369 {
370  char *relname;
371 
373 
374  /* transaction ID (if not valid, we're not streaming) */
375  if (TransactionIdIsValid(xid))
376  pq_sendint32(out, xid);
377 
378  /* use Oid as relation identifier */
379  pq_sendint32(out, RelationGetRelid(rel));
380 
381  /* send qualified relation name */
383  relname = RelationGetRelationName(rel);
384  pq_sendstring(out, relname);
385 
386  /* send replica identity */
387  pq_sendbyte(out, rel->rd_rel->relreplident);
388 
389  /* send the attribute info */
390  logicalrep_write_attrs(out, rel);
391 }
static void logicalrep_write_namespace(StringInfo out, Oid nspid)
Definition: proto.c:721
void pq_sendstring(StringInfo buf, const char *str)
Definition: pqformat.c:197
Form_pg_class rd_rel
Definition: rel.h:110
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:620
#define RelationGetRelationName(relation)
Definition: rel.h:491
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define RelationGetRelid(relation)
Definition: rel.h:457
#define RelationGetNamespace(relation)
Definition: rel.h:498

◆ logicalrep_write_stream_abort()

void logicalrep_write_stream_abort ( StringInfo  out,
TransactionId  xid,
TransactionId  subxid 
)

Definition at line 849 of file proto.c.

References Assert, LOGICAL_REP_MSG_STREAM_ABORT, pq_sendbyte(), pq_sendint32(), and TransactionIdIsValid.

Referenced by pgoutput_stream_abort().

851 {
853 
855 
856  /* transaction ID */
857  pq_sendint32(out, xid);
858  pq_sendint32(out, subxid);
859 }
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:800
#define TransactionIdIsValid(xid)
Definition: transam.h:41

◆ logicalrep_write_stream_commit()

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

Definition at line 798 of file proto.c.

References Assert, ReorderBufferTXN::commit_time, ReorderBufferTXN::end_lsn, LOGICAL_REP_MSG_STREAM_COMMIT, pq_sendbyte(), pq_sendint32(), pq_sendint64(), TransactionIdIsValid, and ReorderBufferTXN::xid.

Referenced by pgoutput_stream_commit().

800 {
801  uint8 flags = 0;
802 
804 
806 
807  /* transaction ID */
808  pq_sendint32(out, txn->xid);
809 
810  /* send the flags field (unused for now) */
811  pq_sendbyte(out, flags);
812 
813  /* send fields */
814  pq_sendint64(out, commit_lsn);
815  pq_sendint64(out, txn->end_lsn);
816  pq_sendint64(out, txn->commit_time);
817 }
TimestampTz commit_time
unsigned char uint8
Definition: c.h:427
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
TransactionId xid
#define Assert(condition)
Definition: c.h:800
XLogRecPtr end_lsn
#define TransactionIdIsValid(xid)
Definition: transam.h:41

◆ logicalrep_write_stream_start()

void logicalrep_write_stream_start ( StringInfo  out,
TransactionId  xid,
bool  first_segment 
)

Definition at line 755 of file proto.c.

References Assert, LOGICAL_REP_MSG_STREAM_START, pq_sendbyte(), pq_sendint32(), and TransactionIdIsValid.

Referenced by pgoutput_stream_start().

757 {
759 
761 
762  /* transaction ID (we're starting to stream, so must be valid) */
763  pq_sendint32(out, xid);
764 
765  /* 1 if this is the first streaming segment for this xid */
766  pq_sendbyte(out, first_segment ? 1 : 0);
767 }
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:800
#define TransactionIdIsValid(xid)
Definition: transam.h:41

◆ logicalrep_write_stream_stop()

void logicalrep_write_stream_stop ( StringInfo  out)

Definition at line 789 of file proto.c.

References LOGICAL_REP_MSG_STREAM_END, and pq_sendbyte().

Referenced by pgoutput_stream_stop().

790 {
792 }
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161

◆ logicalrep_write_truncate()

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

Definition at line 311 of file proto.c.

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

Referenced by pgoutput_truncate().

316 {
317  int i;
318  uint8 flags = 0;
319 
321 
322  /* transaction ID (if not valid, we're not streaming) */
323  if (TransactionIdIsValid(xid))
324  pq_sendint32(out, xid);
325 
326  pq_sendint32(out, nrelids);
327 
328  /* encode and send truncate flags */
329  if (cascade)
330  flags |= TRUNCATE_CASCADE;
331  if (restart_seqs)
332  flags |= TRUNCATE_RESTART_SEQS;
333  pq_sendint8(out, flags);
334 
335  for (i = 0; i < nrelids; i++)
336  pq_sendint32(out, relids[i]);
337 }
unsigned char uint8
Definition: c.h:427
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
#define TransactionIdIsValid(xid)
Definition: transam.h:41
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 466 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().

467 {
468  TupleDesc desc;
470  bool isnull[MaxTupleAttributeNumber];
471  int i;
472  uint16 nliveatts = 0;
473 
474  desc = RelationGetDescr(rel);
475 
476  for (i = 0; i < desc->natts; i++)
477  {
478  if (TupleDescAttr(desc, i)->attisdropped || TupleDescAttr(desc, i)->attgenerated)
479  continue;
480  nliveatts++;
481  }
482  pq_sendint16(out, nliveatts);
483 
484  /* try to allocate enough memory from the get-go */
485  enlargeStringInfo(out, tuple->t_len +
486  nliveatts * (1 + 4));
487 
488  heap_deform_tuple(tuple, desc, values, isnull);
489 
490  /* Write the values */
491  for (i = 0; i < desc->natts; i++)
492  {
493  HeapTuple typtup;
494  Form_pg_type typclass;
495  Form_pg_attribute att = TupleDescAttr(desc, i);
496  char *outputstr;
497 
498  if (att->attisdropped || att->attgenerated)
499  continue;
500 
501  if (isnull[i])
502  {
504  continue;
505  }
506 
507  if (att->attlen == -1 && VARATT_IS_EXTERNAL_ONDISK(values[i]))
508  {
509  /*
510  * Unchanged toasted datum. (Note that we don't promise to detect
511  * unchanged data in general; this is just a cheap check to avoid
512  * sending large values unnecessarily.)
513  */
515  continue;
516  }
517 
518  typtup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(att->atttypid));
519  if (!HeapTupleIsValid(typtup))
520  elog(ERROR, "cache lookup failed for type %u", att->atttypid);
521  typclass = (Form_pg_type) GETSTRUCT(typtup);
522 
523  /*
524  * Send in binary if requested and type has suitable send function.
525  */
526  if (binary && OidIsValid(typclass->typsend))
527  {
528  bytea *outputbytes;
529  int len;
530 
532  outputbytes = OidSendFunctionCall(typclass->typsend, values[i]);
533  len = VARSIZE(outputbytes) - VARHDRSZ;
534  pq_sendint(out, len, 4); /* length */
535  pq_sendbytes(out, VARDATA(outputbytes), len); /* data */
536  pfree(outputbytes);
537  }
538  else
539  {
541  outputstr = OidOutputFunctionCall(typclass->typoutput, values[i]);
542  pq_sendcountedtext(out, outputstr, strlen(outputstr), false);
543  pfree(outputstr);
544  }
545 
546  ReleaseSysCache(typtup);
547  }
548 }
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:483
#define VARSIZE(PTR)
Definition: postgres.h:303
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define VARHDRSZ
Definition: c.h:623
#define LOGICALREP_COLUMN_NULL
Definition: logicalproto.h:79
#define LOGICALREP_COLUMN_TEXT
Definition: logicalproto.h:81
#define OidIsValid(objectId)
Definition: c.h:706
#define LOGICALREP_COLUMN_BINARY
Definition: logicalproto.h:82
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161
#define LOGICALREP_COLUMN_UNCHANGED
Definition: logicalproto.h:80
unsigned short uint16
Definition: c.h:428
void pfree(void *pointer)
Definition: mcxt.c:1057
#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:1115
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1163
#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:165
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:228
int i
Definition: c.h:617

◆ logicalrep_write_typ()

void logicalrep_write_typ ( StringInfo  out,
TransactionId  xid,
Oid  typoid 
)

Definition at line 422 of file proto.c.

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

Referenced by send_relation_and_attrs().

423 {
424  Oid basetypoid = getBaseType(typoid);
425  HeapTuple tup;
426  Form_pg_type typtup;
427 
429 
430  /* transaction ID (if not valid, we're not streaming) */
431  if (TransactionIdIsValid(xid))
432  pq_sendint32(out, xid);
433 
434  tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(basetypoid));
435  if (!HeapTupleIsValid(tup))
436  elog(ERROR, "cache lookup failed for type %u", basetypoid);
437  typtup = (Form_pg_type) GETSTRUCT(tup);
438 
439  /* use Oid as relation identifier */
440  pq_sendint32(out, typoid);
441 
442  /* send qualified type name */
443  logicalrep_write_namespace(out, typtup->typnamespace);
444  pq_sendstring(out, NameStr(typtup->typname));
445 
446  ReleaseSysCache(tup);
447 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
static void logicalrep_write_namespace(StringInfo out, Oid nspid)
Definition: proto.c:721
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:1115
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1163
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_type * Form_pg_type
Definition: pg_type.h:255
#define elog(elevel,...)
Definition: elog.h:228
#define NameStr(name)
Definition: c.h:677
#define TransactionIdIsValid(xid)
Definition: transam.h:41
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2414

◆ logicalrep_write_update()

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

Definition at line 185 of file proto.c.

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

Referenced by pgoutput_change().

187 {
189 
190  Assert(rel->rd_rel->relreplident == REPLICA_IDENTITY_DEFAULT ||
191  rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL ||
192  rel->rd_rel->relreplident == REPLICA_IDENTITY_INDEX);
193 
194  /* transaction ID (if not valid, we're not streaming) */
195  if (TransactionIdIsValid(xid))
196  pq_sendint32(out, xid);
197 
198  /* use Oid as relation identifier */
199  pq_sendint32(out, RelationGetRelid(rel));
200 
201  if (oldtuple != NULL)
202  {
203  if (rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL)
204  pq_sendbyte(out, 'O'); /* old tuple follows */
205  else
206  pq_sendbyte(out, 'K'); /* old key follows */
207  logicalrep_write_tuple(out, rel, oldtuple, binary);
208  }
209 
210  pq_sendbyte(out, 'N'); /* new tuple follows */
211  logicalrep_write_tuple(out, rel, newtuple, binary);
212 }
static void logicalrep_write_tuple(StringInfo out, Relation rel, HeapTuple tuple, bool binary)
Definition: proto.c:466
Form_pg_class rd_rel
Definition: rel.h:110
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:800
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define RelationGetRelid(relation)
Definition: rel.h:457