PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
test_decoding.c File Reference
#include "postgres.h"
#include "access/sysattr.h"
#include "catalog/pg_class.h"
#include "catalog/pg_type.h"
#include "nodes/parsenodes.h"
#include "replication/output_plugin.h"
#include "replication/logical.h"
#include "replication/message.h"
#include "replication/origin.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/relcache.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
Include dependency graph for test_decoding.c:

Go to the source code of this file.

Data Structures

struct  TestDecodingData
 

Functions

void _PG_init (void)
 
void _PG_output_plugin_init (OutputPluginCallbacks *cb)
 
static void pg_decode_startup (LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
 
static void pg_decode_shutdown (LogicalDecodingContext *ctx)
 
static void pg_decode_begin_txn (LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
 
static void pg_output_begin (LogicalDecodingContext *ctx, TestDecodingData *data, ReorderBufferTXN *txn, bool last_write)
 
static void pg_decode_commit_txn (LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void pg_decode_change (LogicalDecodingContext *ctx, ReorderBufferTXN *txn, Relation rel, ReorderBufferChange *change)
 
static bool pg_decode_filter (LogicalDecodingContext *ctx, RepOriginId origin_id)
 
static void pg_decode_message (LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size sz, const char *message)
 
static void print_literal (StringInfo s, Oid typid, char *outputstr)
 
static void tuple_to_stringinfo (StringInfo s, TupleDesc tupdesc, HeapTuple tuple, bool skip_nulls)
 

Variables

 PG_MODULE_MAGIC
 

Function Documentation

void _PG_init ( void  )

Definition at line 73 of file test_decoding.c.

74 {
75  /* other plugins can perform things here */
76 }
void _PG_output_plugin_init ( OutputPluginCallbacks cb)

Definition at line 80 of file test_decoding.c.

References AssertVariableIsOfType, OutputPluginCallbacks::begin_cb, OutputPluginCallbacks::change_cb, OutputPluginCallbacks::commit_cb, OutputPluginCallbacks::filter_by_origin_cb, OutputPluginCallbacks::message_cb, pg_decode_begin_txn(), pg_decode_change(), pg_decode_commit_txn(), pg_decode_filter(), pg_decode_message(), pg_decode_shutdown(), pg_decode_startup(), OutputPluginCallbacks::shutdown_cb, and OutputPluginCallbacks::startup_cb.

81 {
83 
91 }
static void pg_decode_shutdown(LogicalDecodingContext *ctx)
void(* LogicalOutputPluginInit)(struct OutputPluginCallbacks *cb)
Definition: output_plugin.h:35
static bool pg_decode_filter(LogicalDecodingContext *ctx, RepOriginId origin_id)
static void pg_decode_begin_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
LogicalDecodeMessageCB message_cb
LogicalDecodeCommitCB commit_cb
static void pg_decode_message(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size sz, const char *message)
static void pg_decode_commit_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
LogicalDecodeChangeCB change_cb
Definition: output_plugin.h:99
static void pg_decode_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: test_decoding.c:96
static void pg_decode_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, Relation rel, ReorderBufferChange *change)
LogicalDecodeShutdownCB shutdown_cb
LogicalDecodeStartupCB startup_cb
Definition: output_plugin.h:97
LogicalDecodeBeginCB begin_cb
Definition: output_plugin.h:98
LogicalDecodeFilterByOriginCB filter_by_origin_cb
void _PG_output_plugin_init(OutputPluginCallbacks *cb)
Definition: test_decoding.c:80
#define AssertVariableIsOfType(varname, typename)
Definition: c.h:783
static void pg_decode_begin_txn ( LogicalDecodingContext ctx,
ReorderBufferTXN txn 
)
static

Definition at line 202 of file test_decoding.c.

References LogicalDecodingContext::output_plugin_private, pg_output_begin(), TestDecodingData::skip_empty_xacts, and TestDecodingData::xact_wrote_changes.

Referenced by _PG_output_plugin_init().

203 {
205 
206  data->xact_wrote_changes = false;
207  if (data->skip_empty_xacts)
208  return;
209 
210  pg_output_begin(ctx, data, txn, true);
211 }
void * output_plugin_private
Definition: logical.h:62
static void pg_output_begin(LogicalDecodingContext *ctx, TestDecodingData *data, ReorderBufferTXN *txn, bool last_write)
static void pg_decode_change ( LogicalDecodingContext ctx,
ReorderBufferTXN txn,
Relation  rel,
ReorderBufferChange change 
)
static

Definition at line 395 of file test_decoding.c.

References ReorderBufferChange::action, appendStringInfoChar(), appendStringInfoString(), Assert, TestDecodingData::context, ReorderBufferChange::data, get_namespace_name(), get_rel_namespace(), MemoryContextReset(), MemoryContextSwitchTo(), NameStr, NULL, LogicalDecodingContext::out, LogicalDecodingContext::output_plugin_private, OutputPluginPrepareWrite(), OutputPluginWrite(), pg_output_begin(), quote_qualified_identifier(), RelationGetDescr, RelationGetForm, RelationGetRelid, REORDER_BUFFER_CHANGE_DELETE, REORDER_BUFFER_CHANGE_INSERT, REORDER_BUFFER_CHANGE_UPDATE, TestDecodingData::skip_empty_xacts, ReorderBufferChange::tp, tuple_to_stringinfo(), and TestDecodingData::xact_wrote_changes.

Referenced by _PG_output_plugin_init().

397 {
398  TestDecodingData *data;
399  Form_pg_class class_form;
400  TupleDesc tupdesc;
401  MemoryContext old;
402 
403  data = ctx->output_plugin_private;
404 
405  /* output BEGIN if we haven't yet */
406  if (data->skip_empty_xacts && !data->xact_wrote_changes)
407  {
408  pg_output_begin(ctx, data, txn, false);
409  }
410  data->xact_wrote_changes = true;
411 
412  class_form = RelationGetForm(relation);
413  tupdesc = RelationGetDescr(relation);
414 
415  /* Avoid leaking memory by using and resetting our own context */
416  old = MemoryContextSwitchTo(data->context);
417 
418  OutputPluginPrepareWrite(ctx, true);
419 
420  appendStringInfoString(ctx->out, "table ");
425  NameStr(class_form->relname)));
426  appendStringInfoChar(ctx->out, ':');
427 
428  switch (change->action)
429  {
431  appendStringInfoString(ctx->out, " INSERT:");
432  if (change->data.tp.newtuple == NULL)
433  appendStringInfoString(ctx->out, " (no-tuple-data)");
434  else
435  tuple_to_stringinfo(ctx->out, tupdesc,
436  &change->data.tp.newtuple->tuple,
437  false);
438  break;
440  appendStringInfoString(ctx->out, " UPDATE:");
441  if (change->data.tp.oldtuple != NULL)
442  {
443  appendStringInfoString(ctx->out, " old-key:");
444  tuple_to_stringinfo(ctx->out, tupdesc,
445  &change->data.tp.oldtuple->tuple,
446  true);
447  appendStringInfoString(ctx->out, " new-tuple:");
448  }
449 
450  if (change->data.tp.newtuple == NULL)
451  appendStringInfoString(ctx->out, " (no-tuple-data)");
452  else
453  tuple_to_stringinfo(ctx->out, tupdesc,
454  &change->data.tp.newtuple->tuple,
455  false);
456  break;
458  appendStringInfoString(ctx->out, " DELETE:");
459 
460  /* if there was no PK, we only know that a delete happened */
461  if (change->data.tp.oldtuple == NULL)
462  appendStringInfoString(ctx->out, " (no-tuple-data)");
463  /* In DELETE, only the replica identity is present; display that */
464  else
465  tuple_to_stringinfo(ctx->out, tupdesc,
466  &change->data.tp.oldtuple->tuple,
467  true);
468  break;
469  default:
470  Assert(false);
471  }
472 
475 
476  OutputPluginWrite(ctx, true);
477 }
static void tuple_to_stringinfo(StringInfo s, TupleDesc tupdesc, HeapTuple tuple, bool skip_nulls)
#define RelationGetDescr(relation)
Definition: rel.h:425
#define RelationGetForm(relation)
Definition: rel.h:407
Oid get_rel_namespace(Oid relid)
Definition: lsyscache.c:1718
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
union ReorderBufferChange::@49 data
void * output_plugin_private
Definition: logical.h:62
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:135
enum ReorderBufferChangeType action
Definition: reorderbuffer.h:77
struct ReorderBufferChange::@49::@50 tp
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
char * quote_qualified_identifier(const char *qualifier, const char *ident)
Definition: ruleutils.c:10200
MemoryContext context
Definition: test_decoding.c:43
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:472
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static void pg_output_begin(LogicalDecodingContext *ctx, TestDecodingData *data, ReorderBufferTXN *txn, bool last_write)
FormData_pg_class * Form_pg_class
Definition: pg_class.h:95
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:485
StringInfo out
Definition: logical.h:57
#define NameStr(name)
Definition: c.h:499
#define RelationGetRelid(relation)
Definition: rel.h:413
static void pg_decode_commit_txn ( LogicalDecodingContext ctx,
ReorderBufferTXN txn,
XLogRecPtr  commit_lsn 
)
static

Definition at line 226 of file test_decoding.c.

References appendStringInfo(), appendStringInfoString(), ReorderBufferTXN::commit_time, TestDecodingData::include_timestamp, TestDecodingData::include_xids, LogicalDecodingContext::out, LogicalDecodingContext::output_plugin_private, OutputPluginPrepareWrite(), OutputPluginWrite(), TestDecodingData::skip_empty_xacts, timestamptz_to_str(), TestDecodingData::xact_wrote_changes, and ReorderBufferTXN::xid.

Referenced by _PG_output_plugin_init().

228 {
230 
231  if (data->skip_empty_xacts && !data->xact_wrote_changes)
232  return;
233 
234  OutputPluginPrepareWrite(ctx, true);
235  if (data->include_xids)
236  appendStringInfo(ctx->out, "COMMIT %u", txn->xid);
237  else
238  appendStringInfoString(ctx->out, "COMMIT");
239 
240  if (data->include_timestamp)
241  appendStringInfo(ctx->out, " (at %s)",
243 
244  OutputPluginWrite(ctx, true);
245 }
TimestampTz commit_time
void * output_plugin_private
Definition: logical.h:62
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
TransactionId xid
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:472
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:485
StringInfo out
Definition: logical.h:57
const char * timestamptz_to_str(TimestampTz t)
Definition: timestamp.c:1709
static bool pg_decode_filter ( LogicalDecodingContext ctx,
RepOriginId  origin_id 
)
static

Definition at line 248 of file test_decoding.c.

References InvalidRepOriginId, TestDecodingData::only_local, and LogicalDecodingContext::output_plugin_private.

Referenced by _PG_output_plugin_init().

250 {
252 
253  if (data->only_local && origin_id != InvalidRepOriginId)
254  return true;
255  return false;
256 }
void * output_plugin_private
Definition: logical.h:62
#define InvalidRepOriginId
Definition: origin.h:34
static void pg_decode_message ( LogicalDecodingContext ctx,
ReorderBufferTXN txn,
XLogRecPtr  message_lsn,
bool  transactional,
const char *  prefix,
Size  sz,
const char *  message 
)
static

Definition at line 480 of file test_decoding.c.

References appendBinaryStringInfo(), appendStringInfo(), LogicalDecodingContext::out, OutputPluginPrepareWrite(), and OutputPluginWrite().

Referenced by _PG_output_plugin_init().

483 {
484  OutputPluginPrepareWrite(ctx, true);
485  appendStringInfo(ctx->out, "message: transactional: %d prefix: %s, sz: %zu content:",
486  transactional, prefix, sz);
487  appendBinaryStringInfo(ctx->out, message, sz);
488  OutputPluginWrite(ctx, true);
489 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:472
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:485
StringInfo out
Definition: logical.h:57
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:240
static void pg_decode_shutdown ( LogicalDecodingContext ctx)
static

Definition at line 192 of file test_decoding.c.

References TestDecodingData::context, MemoryContextDelete(), and LogicalDecodingContext::output_plugin_private.

Referenced by _PG_output_plugin_init().

193 {
195 
196  /* cleanup our own resources via memory context reset */
198 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
void * output_plugin_private
Definition: logical.h:62
MemoryContext context
Definition: test_decoding.c:43
static void pg_decode_startup ( LogicalDecodingContext ctx,
OutputPluginOptions opt,
bool  is_init 
)
static

Definition at line 96 of file test_decoding.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), DefElem::arg, Assert, LogicalDecodingContext::context, TestDecodingData::context, DefElem::defname, ereport, errcode(), errmsg(), ERROR, TestDecodingData::include_timestamp, TestDecodingData::include_xids, IsA, lfirst, NULL, TestDecodingData::only_local, OUTPUT_PLUGIN_BINARY_OUTPUT, LogicalDecodingContext::output_plugin_options, LogicalDecodingContext::output_plugin_private, OUTPUT_PLUGIN_TEXTUAL_OUTPUT, OutputPluginOptions::output_type, palloc0(), parse_bool(), TestDecodingData::skip_empty_xacts, and strVal.

Referenced by _PG_output_plugin_init().

98 {
100  TestDecodingData *data;
101 
102  data = palloc0(sizeof(TestDecodingData));
104  "text conversion context",
106  data->include_xids = true;
107  data->include_timestamp = false;
108  data->skip_empty_xacts = false;
109  data->only_local = false;
110 
111  ctx->output_plugin_private = data;
112 
114 
115  foreach(option, ctx->output_plugin_options)
116  {
117  DefElem *elem = lfirst(option);
118 
119  Assert(elem->arg == NULL || IsA(elem->arg, String));
120 
121  if (strcmp(elem->defname, "include-xids") == 0)
122  {
123  /* if option does not provide a value, it means its value is true */
124  if (elem->arg == NULL)
125  data->include_xids = true;
126  else if (!parse_bool(strVal(elem->arg), &data->include_xids))
127  ereport(ERROR,
128  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
129  errmsg("could not parse value \"%s\" for parameter \"%s\"",
130  strVal(elem->arg), elem->defname)));
131  }
132  else if (strcmp(elem->defname, "include-timestamp") == 0)
133  {
134  if (elem->arg == NULL)
135  data->include_timestamp = true;
136  else if (!parse_bool(strVal(elem->arg), &data->include_timestamp))
137  ereport(ERROR,
138  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
139  errmsg("could not parse value \"%s\" for parameter \"%s\"",
140  strVal(elem->arg), elem->defname)));
141  }
142  else if (strcmp(elem->defname, "force-binary") == 0)
143  {
144  bool force_binary;
145 
146  if (elem->arg == NULL)
147  continue;
148  else if (!parse_bool(strVal(elem->arg), &force_binary))
149  ereport(ERROR,
150  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
151  errmsg("could not parse value \"%s\" for parameter \"%s\"",
152  strVal(elem->arg), elem->defname)));
153 
154  if (force_binary)
156  }
157  else if (strcmp(elem->defname, "skip-empty-xacts") == 0)
158  {
159 
160  if (elem->arg == NULL)
161  data->skip_empty_xacts = true;
162  else if (!parse_bool(strVal(elem->arg), &data->skip_empty_xacts))
163  ereport(ERROR,
164  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
165  errmsg("could not parse value \"%s\" for parameter \"%s\"",
166  strVal(elem->arg), elem->defname)));
167  }
168  else if (strcmp(elem->defname, "only-local") == 0)
169  {
170 
171  if (elem->arg == NULL)
172  data->only_local = true;
173  else if (!parse_bool(strVal(elem->arg), &data->only_local))
174  ereport(ERROR,
175  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
176  errmsg("could not parse value \"%s\" for parameter \"%s\"",
177  strVal(elem->arg), elem->defname)));
178  }
179  else
180  {
181  ereport(ERROR,
182  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
183  errmsg("option \"%s\" = \"%s\" is unknown",
184  elem->defname,
185  elem->arg ? strVal(elem->arg) : "(null)")));
186  }
187  }
188 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:569
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
void * output_plugin_private
Definition: logical.h:62
MemoryContext context
Definition: logical.h:32
List * output_plugin_options
Definition: logical.h:46
bool parse_bool(const char *value, bool *result)
Definition: bool.c:30
OutputPluginOutputType output_type
Definition: output_plugin.h:28
#define ERROR
Definition: elog.h:43
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
#define ereport(elevel, rest)
Definition: elog.h:122
Node * arg
Definition: parsenodes.h:709
MemoryContext context
Definition: test_decoding.c:43
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
void * palloc0(Size size)
Definition: mcxt.c:878
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:708
static void pg_output_begin ( LogicalDecodingContext ctx,
TestDecodingData data,
ReorderBufferTXN txn,
bool  last_write 
)
static

Definition at line 214 of file test_decoding.c.

References appendStringInfo(), appendStringInfoString(), TestDecodingData::include_xids, LogicalDecodingContext::out, OutputPluginPrepareWrite(), OutputPluginWrite(), and ReorderBufferTXN::xid.

Referenced by pg_decode_begin_txn(), and pg_decode_change().

215 {
216  OutputPluginPrepareWrite(ctx, last_write);
217  if (data->include_xids)
218  appendStringInfo(ctx->out, "BEGIN %u", txn->xid);
219  else
220  appendStringInfoString(ctx->out, "BEGIN");
221  OutputPluginWrite(ctx, last_write);
222 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
TransactionId xid
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:472
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:485
StringInfo out
Definition: logical.h:57
static void print_literal ( StringInfo  s,
Oid  typid,
char *  outputstr 
)
static

Definition at line 266 of file test_decoding.c.

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), BITOID, BOOLOID, FLOAT4OID, FLOAT8OID, INT2OID, INT4OID, INT8OID, NUMERICOID, OIDOID, SQL_STR_DOUBLE, and VARBITOID.

Referenced by tuple_to_stringinfo().

267 {
268  const char *valptr;
269 
270  switch (typid)
271  {
272  case INT2OID:
273  case INT4OID:
274  case INT8OID:
275  case OIDOID:
276  case FLOAT4OID:
277  case FLOAT8OID:
278  case NUMERICOID:
279  /* NB: We don't care about Inf, NaN et al. */
280  appendStringInfoString(s, outputstr);
281  break;
282 
283  case BITOID:
284  case VARBITOID:
285  appendStringInfo(s, "B'%s'", outputstr);
286  break;
287 
288  case BOOLOID:
289  if (strcmp(outputstr, "t") == 0)
290  appendStringInfoString(s, "true");
291  else
292  appendStringInfoString(s, "false");
293  break;
294 
295  default:
296  appendStringInfoChar(s, '\'');
297  for (valptr = outputstr; *valptr; valptr++)
298  {
299  char ch = *valptr;
300 
301  if (SQL_STR_DOUBLE(ch, false))
302  appendStringInfoChar(s, ch);
303  appendStringInfoChar(s, ch);
304  }
305  appendStringInfoChar(s, '\'');
306  break;
307  }
308 }
#define OIDOID
Definition: pg_type.h:328
#define NUMERICOID
Definition: pg_type.h:546
#define INT4OID
Definition: pg_type.h:316
#define VARBITOID
Definition: pg_type.h:538
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define INT2OID
Definition: pg_type.h:308
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
#define FLOAT4OID
Definition: pg_type.h:408
#define INT8OID
Definition: pg_type.h:304
#define BITOID
Definition: pg_type.h:534
#define FLOAT8OID
Definition: pg_type.h:411
#define BOOLOID
Definition: pg_type.h:288
#define SQL_STR_DOUBLE(ch, escape_backslash)
Definition: c.h:507
static void tuple_to_stringinfo ( StringInfo  s,
TupleDesc  tupdesc,
HeapTuple  tuple,
bool  skip_nulls 
)
static

Definition at line 312 of file test_decoding.c.

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), tupleDesc::attrs, format_type_be(), getTypeOutputInfo(), heap_getattr, HeapTupleHeaderGetOid, InvalidOid, NameStr, tupleDesc::natts, OidOutputFunctionCall(), PG_DETOAST_DATUM, PointerGetDatum, print_literal(), quote_identifier(), HeapTupleData::t_data, val, and VARATT_IS_EXTERNAL_ONDISK.

Referenced by pg_decode_change().

313 {
314  int natt;
315  Oid oid;
316 
317  /* print oid of tuple, it's not included in the TupleDesc */
318  if ((oid = HeapTupleHeaderGetOid(tuple->t_data)) != InvalidOid)
319  {
320  appendStringInfo(s, " oid[oid]:%u", oid);
321  }
322 
323  /* print all columns individually */
324  for (natt = 0; natt < tupdesc->natts; natt++)
325  {
326  Form_pg_attribute attr; /* the attribute itself */
327  Oid typid; /* type of current attribute */
328  Oid typoutput; /* output function */
329  bool typisvarlena;
330  Datum origval; /* possibly toasted Datum */
331  bool isnull; /* column is null? */
332 
333  attr = tupdesc->attrs[natt];
334 
335  /*
336  * don't print dropped columns, we can't be sure everything is
337  * available for them
338  */
339  if (attr->attisdropped)
340  continue;
341 
342  /*
343  * Don't print system columns, oid will already have been printed if
344  * present.
345  */
346  if (attr->attnum < 0)
347  continue;
348 
349  typid = attr->atttypid;
350 
351  /* get Datum from tuple */
352  origval = heap_getattr(tuple, natt + 1, tupdesc, &isnull);
353 
354  if (isnull && skip_nulls)
355  continue;
356 
357  /* print attribute name */
358  appendStringInfoChar(s, ' ');
359  appendStringInfoString(s, quote_identifier(NameStr(attr->attname)));
360 
361  /* print attribute type */
362  appendStringInfoChar(s, '[');
364  appendStringInfoChar(s, ']');
365 
366  /* query output function */
367  getTypeOutputInfo(typid,
368  &typoutput, &typisvarlena);
369 
370  /* print separator */
371  appendStringInfoChar(s, ':');
372 
373  /* print data */
374  if (isnull)
375  appendStringInfoString(s, "null");
376  else if (typisvarlena && VARATT_IS_EXTERNAL_ONDISK(origval))
377  appendStringInfoString(s, "unchanged-toast-datum");
378  else if (!typisvarlena)
379  print_literal(s, typid,
380  OidOutputFunctionCall(typoutput, origval));
381  else
382  {
383  Datum val; /* definitely detoasted Datum */
384 
385  val = PointerGetDatum(PG_DETOAST_DATUM(origval));
386  print_literal(s, typid, OidOutputFunctionCall(typoutput, val));
387  }
388  }
389 }
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: postgres.h:315
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2600
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10114
#define PointerGetDatum(X)
Definition: postgres.h:562
Form_pg_attribute * attrs
Definition: tupdesc.h:74
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
unsigned int Oid
Definition: postgres_ext.h:31
int natts
Definition: tupdesc.h:73
HeapTupleHeader t_data
Definition: htup.h:67
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
static void print_literal(StringInfo s, Oid typid, char *outputstr)
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:189
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:769
uintptr_t Datum
Definition: postgres.h:372
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleHeaderGetOid(tup)
Definition: htup_details.h:465
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:2056
#define NameStr(name)
Definition: c.h:499
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:197
long val
Definition: informix.c:689

Variable Documentation

PG_MODULE_MAGIC

Definition at line 35 of file test_decoding.c.