PostgreSQL Source Code  git master
streamutil.c File Reference
#include "postgres_fe.h"
#include <sys/time.h>
#include <unistd.h>
#include "access/xlog_internal.h"
#include "common/connect.h"
#include "common/fe_memutils.h"
#include "common/file_perm.h"
#include "common/logging.h"
#include "common/string.h"
#include "datatype/timestamp.h"
#include "port/pg_bswap.h"
#include "pqexpbuffer.h"
#include "receivelog.h"
#include "streamutil.h"
Include dependency graph for streamutil.c:

Go to the source code of this file.

Macros

#define ERRCODE_DUPLICATE_OBJECT   "42710"
 
#define MINIMUM_VERSION_FOR_SHOW_CMD   100000
 
#define MINIMUM_VERSION_FOR_GROUP_ACCESS   110000
 

Functions

static bool RetrieveDataDirCreatePerm (PGconn *conn)
 
PGconnGetConnection (void)
 
bool RetrieveWalSegSize (PGconn *conn)
 
bool RunIdentifySystem (PGconn *conn, char **sysid, TimeLineID *starttli, XLogRecPtr *startpos, char **db_name)
 
bool GetSlotInformation (PGconn *conn, const char *slot_name, XLogRecPtr *restart_lsn, TimeLineID *restart_tli)
 
bool CreateReplicationSlot (PGconn *conn, const char *slot_name, const char *plugin, bool is_temporary, bool is_physical, bool reserve_wal, bool slot_exists_ok, bool two_phase)
 
bool DropReplicationSlot (PGconn *conn, const char *slot_name)
 
void AppendPlainCommandOption (PQExpBuffer buf, bool use_new_option_syntax, char *option_name)
 
void AppendStringCommandOption (PQExpBuffer buf, bool use_new_option_syntax, char *option_name, char *option_value)
 
void AppendIntegerCommandOption (PQExpBuffer buf, bool use_new_option_syntax, char *option_name, int32 option_value)
 
TimestampTz feGetCurrentTimestamp (void)
 
void feTimestampDifference (TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
 
bool feTimestampDifferenceExceeds (TimestampTz start_time, TimestampTz stop_time, int msec)
 
void fe_sendint64 (int64 i, char *buf)
 
int64 fe_recvint64 (char *buf)
 

Variables

int WalSegSz
 
const char * progname
 
char * connection_string = NULL
 
char * dbhost = NULL
 
char * dbuser = NULL
 
char * dbport = NULL
 
char * dbname = NULL
 
int dbgetpassword = 0
 
static char * password = NULL
 
PGconnconn = NULL
 

Macro Definition Documentation

◆ ERRCODE_DUPLICATE_OBJECT

#define ERRCODE_DUPLICATE_OBJECT   "42710"

Definition at line 32 of file streamutil.c.

◆ MINIMUM_VERSION_FOR_GROUP_ACCESS

#define MINIMUM_VERSION_FOR_GROUP_ACCESS   110000

Definition at line 44 of file streamutil.c.

◆ MINIMUM_VERSION_FOR_SHOW_CMD

#define MINIMUM_VERSION_FOR_SHOW_CMD   100000

Definition at line 39 of file streamutil.c.

Function Documentation

◆ AppendIntegerCommandOption()

void AppendIntegerCommandOption ( PQExpBuffer  buf,
bool  use_new_option_syntax,
char *  option_name,
int32  option_value 
)

Definition at line 784 of file streamutil.c.

786 {
787  AppendPlainCommandOption(buf, use_new_option_syntax, option_name);
788 
789  appendPQExpBuffer(buf, " %d", option_value);
790 }
static char * buf
Definition: pg_test_fsync.c:73
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
void AppendPlainCommandOption(PQExpBuffer buf, bool use_new_option_syntax, char *option_name)
Definition: streamutil.c:740

References AppendPlainCommandOption(), appendPQExpBuffer(), and buf.

Referenced by BaseBackup().

◆ AppendPlainCommandOption()

void AppendPlainCommandOption ( PQExpBuffer  buf,
bool  use_new_option_syntax,
char *  option_name 
)

Definition at line 740 of file streamutil.c.

742 {
743  if (buf->len > 0 && buf->data[buf->len - 1] != '(')
744  {
745  if (use_new_option_syntax)
746  appendPQExpBufferStr(buf, ", ");
747  else
749  }
750 
751  appendPQExpBuffer(buf, " %s", option_name);
752 }
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367

References appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), and buf.

Referenced by AppendIntegerCommandOption(), AppendStringCommandOption(), BaseBackup(), and CreateReplicationSlot().

◆ AppendStringCommandOption()

void AppendStringCommandOption ( PQExpBuffer  buf,
bool  use_new_option_syntax,
char *  option_name,
char *  option_value 
)

Definition at line 761 of file streamutil.c.

763 {
764  AppendPlainCommandOption(buf, use_new_option_syntax, option_name);
765 
766  if (option_value != NULL)
767  {
768  size_t length = strlen(option_value);
769  char *escaped_value = palloc(1 + 2 * length);
770 
771  PQescapeStringConn(conn, escaped_value, option_value, length, NULL);
772  appendPQExpBuffer(buf, " '%s'", escaped_value);
773  pfree(escaped_value);
774  }
775 }
size_t PQescapeStringConn(PGconn *conn, char *to, const char *from, size_t length, int *error)
Definition: fe-exec.c:4105
void pfree(void *pointer)
Definition: mcxt.c:1508
void * palloc(Size size)
Definition: mcxt.c:1304
PGconn * conn
Definition: streamutil.c:54

References AppendPlainCommandOption(), appendPQExpBuffer(), buf, conn, palloc(), pfree(), and PQescapeStringConn().

Referenced by BaseBackup(), and CreateReplicationSlot().

◆ CreateReplicationSlot()

bool CreateReplicationSlot ( PGconn conn,
const char *  slot_name,
const char *  plugin,
bool  is_temporary,
bool  is_physical,
bool  reserve_wal,
bool  slot_exists_ok,
bool  two_phase 
)

Definition at line 583 of file streamutil.c.

586 {
587  PQExpBuffer query;
588  PGresult *res;
589  bool use_new_option_syntax = (PQserverVersion(conn) >= 150000);
590 
591  query = createPQExpBuffer();
592 
593  Assert((is_physical && plugin == NULL) ||
594  (!is_physical && plugin != NULL));
595  Assert(!(two_phase && is_physical));
596  Assert(slot_name != NULL);
597 
598  /* Build base portion of query */
599  appendPQExpBuffer(query, "CREATE_REPLICATION_SLOT \"%s\"", slot_name);
600  if (is_temporary)
601  appendPQExpBufferStr(query, " TEMPORARY");
602  if (is_physical)
603  appendPQExpBufferStr(query, " PHYSICAL");
604  else
605  appendPQExpBuffer(query, " LOGICAL \"%s\"", plugin);
606 
607  /* Add any requested options */
608  if (use_new_option_syntax)
609  appendPQExpBufferStr(query, " (");
610  if (is_physical)
611  {
612  if (reserve_wal)
613  AppendPlainCommandOption(query, use_new_option_syntax,
614  "RESERVE_WAL");
615  }
616  else
617  {
618  if (two_phase && PQserverVersion(conn) >= 150000)
619  AppendPlainCommandOption(query, use_new_option_syntax,
620  "TWO_PHASE");
621 
622  if (PQserverVersion(conn) >= 100000)
623  {
624  /* pg_recvlogical doesn't use an exported snapshot, so suppress */
625  if (use_new_option_syntax)
626  AppendStringCommandOption(query, use_new_option_syntax,
627  "SNAPSHOT", "nothing");
628  else
629  AppendPlainCommandOption(query, use_new_option_syntax,
630  "NOEXPORT_SNAPSHOT");
631  }
632  }
633  if (use_new_option_syntax)
634  {
635  /* Suppress option list if it would be empty, otherwise terminate */
636  if (query->data[query->len - 1] == '(')
637  {
638  query->len -= 2;
639  query->data[query->len] = '\0';
640  }
641  else
642  appendPQExpBufferChar(query, ')');
643  }
644 
645  /* Now run the query */
646  res = PQexec(conn, query->data);
648  {
649  const char *sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
650 
651  if (slot_exists_ok &&
652  sqlstate &&
653  strcmp(sqlstate, ERRCODE_DUPLICATE_OBJECT) == 0)
654  {
655  destroyPQExpBuffer(query);
656  PQclear(res);
657  return true;
658  }
659  else
660  {
661  pg_log_error("could not send replication command \"%s\": %s",
662  query->data, PQerrorMessage(conn));
663 
664  destroyPQExpBuffer(query);
665  PQclear(res);
666  return false;
667  }
668  }
669 
670  if (PQntuples(res) != 1 || PQnfields(res) != 4)
671  {
672  pg_log_error("could not create replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields",
673  slot_name,
674  PQntuples(res), PQnfields(res), 1, 4);
675 
676  destroyPQExpBuffer(query);
677  PQclear(res);
678  return false;
679  }
680 
681  destroyPQExpBuffer(query);
682  PQclear(res);
683  return true;
684 }
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6938
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6948
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3371
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3441
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2224
char * PQresultErrorField(const PGresult *res, int fieldcode)
Definition: fe-exec.c:3426
int PQnfields(const PGresult *res)
Definition: fe-exec.c:3449
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:103
Assert(fmt[strlen(fmt) - 1] !='\n')
#define pg_log_error(...)
Definition: logging.h:106
static bool slot_exists_ok
Definition: pg_receivewal.c:50
static bool two_phase
static const char * plugin
#define PG_DIAG_SQLSTATE
Definition: postgres_ext.h:56
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:72
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:114
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
void AppendStringCommandOption(PQExpBuffer buf, bool use_new_option_syntax, char *option_name, char *option_value)
Definition: streamutil.c:761

References AppendPlainCommandOption(), appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), AppendStringCommandOption(), Assert(), conn, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), ERRCODE_DUPLICATE_OBJECT, PQExpBufferData::len, PG_DIAG_SQLSTATE, pg_log_error, PGRES_TUPLES_OK, plugin, PQclear(), PQerrorMessage(), PQexec(), PQnfields(), PQntuples(), PQresultErrorField(), PQresultStatus(), PQserverVersion(), res, slot_exists_ok, and two_phase.

◆ DropReplicationSlot()

bool DropReplicationSlot ( PGconn conn,
const char *  slot_name 
)

Definition at line 691 of file streamutil.c.

692 {
693  PQExpBuffer query;
694  PGresult *res;
695 
696  Assert(slot_name != NULL);
697 
698  query = createPQExpBuffer();
699 
700  /* Build query */
701  appendPQExpBuffer(query, "DROP_REPLICATION_SLOT \"%s\"",
702  slot_name);
703  res = PQexec(conn, query->data);
705  {
706  pg_log_error("could not send replication command \"%s\": %s",
707  query->data, PQerrorMessage(conn));
708 
709  destroyPQExpBuffer(query);
710  PQclear(res);
711  return false;
712  }
713 
714  if (PQntuples(res) != 0 || PQnfields(res) != 0)
715  {
716  pg_log_error("could not drop replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields",
717  slot_name,
718  PQntuples(res), PQnfields(res), 0, 0);
719 
720  destroyPQExpBuffer(query);
721  PQclear(res);
722  return false;
723  }
724 
725  destroyPQExpBuffer(query);
726  PQclear(res);
727  return true;
728 }
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:100

References appendPQExpBuffer(), Assert(), conn, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), pg_log_error, PGRES_COMMAND_OK, PQclear(), PQerrorMessage(), PQexec(), PQnfields(), PQntuples(), PQresultStatus(), and res.

◆ fe_recvint64()

int64 fe_recvint64 ( char *  buf)

Definition at line 862 of file streamutil.c.

863 {
864  uint64 n64;
865 
866  memcpy(&n64, buf, sizeof(n64));
867 
868  return pg_ntoh64(n64);
869 }
#define pg_ntoh64(x)
Definition: pg_bswap.h:126

References buf, and pg_ntoh64.

Referenced by ProcessXLogDataMsg(), and StreamLogicalLog().

◆ fe_sendint64()

void fe_sendint64 ( int64  i,
char *  buf 
)

Definition at line 851 of file streamutil.c.

852 {
853  uint64 n64 = pg_hton64(i);
854 
855  memcpy(buf, &n64, sizeof(n64));
856 }
int i
Definition: isn.c:73
#define pg_hton64(x)
Definition: pg_bswap.h:122

References buf, i, and pg_hton64.

Referenced by sendFeedback().

◆ feGetCurrentTimestamp()

TimestampTz feGetCurrentTimestamp ( void  )

Definition at line 797 of file streamutil.c.

798 {
799  TimestampTz result;
800  struct timeval tp;
801 
802  gettimeofday(&tp, NULL);
803 
804  result = (TimestampTz) tp.tv_sec -
806  result = (result * USECS_PER_SEC) + tp.tv_usec;
807 
808  return result;
809 }
int64 TimestampTz
Definition: timestamp.h:39
#define USECS_PER_SEC
Definition: timestamp.h:134
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:234
#define SECS_PER_DAY
Definition: timestamp.h:126
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:235
int gettimeofday(struct timeval *tp, void *tzp)

References gettimeofday(), POSTGRES_EPOCH_JDATE, SECS_PER_DAY, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

Referenced by flushAndSendFeedback(), HandleCopyStream(), ProcessKeepaliveMsg(), and StreamLogicalLog().

◆ feTimestampDifference()

void feTimestampDifference ( TimestampTz  start_time,
TimestampTz  stop_time,
long *  secs,
int *  microsecs 
)

Definition at line 816 of file streamutil.c.

818 {
819  TimestampTz diff = stop_time - start_time;
820 
821  if (diff <= 0)
822  {
823  *secs = 0;
824  *microsecs = 0;
825  }
826  else
827  {
828  *secs = (long) (diff / USECS_PER_SEC);
829  *microsecs = (int) (diff % USECS_PER_SEC);
830  }
831 }
static time_t start_time
Definition: pg_ctl.c:94

References start_time, and USECS_PER_SEC.

Referenced by CalculateCopyStreamSleeptime(), and StreamLogicalLog().

◆ feTimestampDifferenceExceeds()

bool feTimestampDifferenceExceeds ( TimestampTz  start_time,
TimestampTz  stop_time,
int  msec 
)

Definition at line 838 of file streamutil.c.

841 {
842  TimestampTz diff = stop_time - start_time;
843 
844  return (diff >= msec * INT64CONST(1000));
845 }

References start_time.

Referenced by HandleCopyStream(), and StreamLogicalLog().

◆ GetConnection()

PGconn* GetConnection ( void  )

Definition at line 62 of file streamutil.c.

63 {
64  PGconn *tmpconn;
65  int argcount = 7; /* dbname, replication, fallback_app_name,
66  * host, user, port, password */
67  int i;
68  const char **keywords;
69  const char **values;
70  const char *tmpparam;
71  bool need_password;
72  PQconninfoOption *conn_opts = NULL;
73  PQconninfoOption *conn_opt;
74  char *err_msg = NULL;
75 
76  /* pg_recvlogical uses dbname only; others use connection_string only. */
77  Assert(dbname == NULL || connection_string == NULL);
78 
79  /*
80  * Merge the connection info inputs given in form of connection string,
81  * options and default values (dbname=replication, replication=true, etc.)
82  */
83  i = 0;
85  {
86  conn_opts = PQconninfoParse(connection_string, &err_msg);
87  if (conn_opts == NULL)
88  pg_fatal("%s", err_msg);
89 
90  for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
91  {
92  if (conn_opt->val != NULL && conn_opt->val[0] != '\0')
93  argcount++;
94  }
95 
96  keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
97  values = pg_malloc0((argcount + 1) * sizeof(*values));
98 
99  /*
100  * Set dbname here already, so it can be overridden by a dbname in the
101  * connection string.
102  */
103  keywords[i] = "dbname";
104  values[i] = "replication";
105  i++;
106 
107  for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
108  {
109  if (conn_opt->val != NULL && conn_opt->val[0] != '\0')
110  {
111  keywords[i] = conn_opt->keyword;
112  values[i] = conn_opt->val;
113  i++;
114  }
115  }
116  }
117  else
118  {
119  keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
120  values = pg_malloc0((argcount + 1) * sizeof(*values));
121  keywords[i] = "dbname";
122  values[i] = dbname;
123  i++;
124  }
125 
126  keywords[i] = "replication";
127  values[i] = dbname == NULL ? "true" : "database";
128  i++;
129  keywords[i] = "fallback_application_name";
130  values[i] = progname;
131  i++;
132 
133  if (dbhost)
134  {
135  keywords[i] = "host";
136  values[i] = dbhost;
137  i++;
138  }
139  if (dbuser)
140  {
141  keywords[i] = "user";
142  values[i] = dbuser;
143  i++;
144  }
145  if (dbport)
146  {
147  keywords[i] = "port";
148  values[i] = dbport;
149  i++;
150  }
151 
152  /* If -W was given, force prompt for password, but only the first time */
153  need_password = (dbgetpassword == 1 && !password);
154 
155  do
156  {
157  /* Get a new password if appropriate */
158  if (need_password)
159  {
160  free(password);
161  password = simple_prompt("Password: ", false);
162  need_password = false;
163  }
164 
165  /* Use (or reuse, on a subsequent connection) password if we have it */
166  if (password)
167  {
168  keywords[i] = "password";
169  values[i] = password;
170  }
171  else
172  {
173  keywords[i] = NULL;
174  values[i] = NULL;
175  }
176 
177  /*
178  * Only expand dbname when we did not already parse the argument as a
179  * connection string ourselves.
180  */
181  tmpconn = PQconnectdbParams(keywords, values, !connection_string);
182 
183  /*
184  * If there is too little memory even to allocate the PGconn object
185  * and PQconnectdbParams returns NULL, we call exit(1) directly.
186  */
187  if (!tmpconn)
188  pg_fatal("could not connect to server");
189 
190  /* If we need a password and -w wasn't given, loop back and get one */
191  if (PQstatus(tmpconn) == CONNECTION_BAD &&
192  PQconnectionNeedsPassword(tmpconn) &&
193  dbgetpassword != -1)
194  {
195  PQfinish(tmpconn);
196  need_password = true;
197  }
198  }
199  while (need_password);
200 
201  if (PQstatus(tmpconn) != CONNECTION_OK)
202  {
203  pg_log_error("%s", PQerrorMessage(tmpconn));
204  PQfinish(tmpconn);
205  free(values);
206  free(keywords);
207  PQconninfoFree(conn_opts);
208  return NULL;
209  }
210 
211  /* Connection ok! */
212  free(values);
213  free(keywords);
214  PQconninfoFree(conn_opts);
215 
216  /*
217  * Set always-secure search path, so malicious users can't get control.
218  * The capacity to run normal SQL queries was added in PostgreSQL 10, so
219  * the search path cannot be changed (by us or attackers) on earlier
220  * versions.
221  */
222  if (dbname != NULL && PQserverVersion(tmpconn) >= 100000)
223  {
224  PGresult *res;
225 
228  {
229  pg_log_error("could not clear search_path: %s",
230  PQerrorMessage(tmpconn));
231  PQclear(res);
232  PQfinish(tmpconn);
233  exit(1);
234  }
235  PQclear(res);
236  }
237 
238  /*
239  * Ensure we have the same value of integer_datetimes (now always "on") as
240  * the server we are connecting to.
241  */
242  tmpparam = PQparameterStatus(tmpconn, "integer_datetimes");
243  if (!tmpparam)
244  {
245  pg_log_error("could not determine server setting for integer_datetimes");
246  PQfinish(tmpconn);
247  exit(1);
248  }
249 
250  if (strcmp(tmpparam, "on") != 0)
251  {
252  pg_log_error("integer_datetimes compile flag does not match server");
253  PQfinish(tmpconn);
254  exit(1);
255  }
256 
257  /*
258  * Retrieve the source data directory mode and use it to construct a umask
259  * for creating directories and files.
260  */
261  if (!RetrieveDataDirCreatePerm(tmpconn))
262  {
263  PQfinish(tmpconn);
264  exit(1);
265  }
266 
267  return tmpconn;
268 }
static Datum values[MAXATTR]
Definition: bootstrap.c:152
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6913
PQconninfoOption * PQconninfoParse(const char *conninfo, char **errmsg)
Definition: fe-connect.c:5529
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:678
void PQconninfoFree(PQconninfoOption *connOptions)
Definition: fe-connect.c:6781
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6999
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6895
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4669
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
#define free(a)
Definition: header.h:65
@ CONNECTION_BAD
Definition: libpq-fe.h:61
@ CONNECTION_OK
Definition: libpq-fe.h:60
exit(1)
#define pg_fatal(...)
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
int dbgetpassword
Definition: streamutil.c:52
char * dbhost
Definition: streamutil.c:48
static char * password
Definition: streamutil.c:53
char * dbport
Definition: streamutil.c:50
char * connection_string
Definition: streamutil.c:47
const char * progname
Definition: streamutil.c:46
static bool RetrieveDataDirCreatePerm(PGconn *conn)
Definition: streamutil.c:354
char * dbname
Definition: streamutil.c:51
char * dbuser
Definition: streamutil.c:49

References ALWAYS_SECURE_SEARCH_PATH_SQL, Assert(), CONNECTION_BAD, CONNECTION_OK, connection_string, dbgetpassword, dbhost, dbname, dbport, dbuser, exit(), free, i, _PQconninfoOption::keyword, password, pg_fatal, pg_log_error, pg_malloc0(), PGRES_TUPLES_OK, PQclear(), PQconnectdbParams(), PQconnectionNeedsPassword(), PQconninfoFree(), PQconninfoParse(), PQerrorMessage(), PQexec(), PQfinish(), PQparameterStatus(), PQresultStatus(), PQserverVersion(), PQstatus(), progname, res, RetrieveDataDirCreatePerm(), simple_prompt(), _PQconninfoOption::val, and values.

◆ GetSlotInformation()

bool GetSlotInformation ( PGconn conn,
const char *  slot_name,
XLogRecPtr restart_lsn,
TimeLineID restart_tli 
)

Definition at line 489 of file streamutil.c.

491 {
492  PGresult *res;
493  PQExpBuffer query;
494  XLogRecPtr lsn_loc = InvalidXLogRecPtr;
495  TimeLineID tli_loc = 0;
496 
497  if (restart_lsn)
498  *restart_lsn = lsn_loc;
499  if (restart_tli)
500  *restart_tli = tli_loc;
501 
502  query = createPQExpBuffer();
503  appendPQExpBuffer(query, "READ_REPLICATION_SLOT %s", slot_name);
504  res = PQexec(conn, query->data);
505  destroyPQExpBuffer(query);
506 
508  {
509  pg_log_error("could not send replication command \"%s\": %s",
510  "READ_REPLICATION_SLOT", PQerrorMessage(conn));
511  PQclear(res);
512  return false;
513  }
514 
515  /* The command should always return precisely one tuple and three fields */
516  if (PQntuples(res) != 1 || PQnfields(res) != 3)
517  {
518  pg_log_error("could not read replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields",
519  slot_name, PQntuples(res), PQnfields(res), 1, 3);
520  PQclear(res);
521  return false;
522  }
523 
524  /*
525  * When the slot doesn't exist, the command returns a tuple with NULL
526  * values. This checks only the slot type field.
527  */
528  if (PQgetisnull(res, 0, 0))
529  {
530  pg_log_error("replication slot \"%s\" does not exist", slot_name);
531  PQclear(res);
532  return false;
533  }
534 
535  /*
536  * Note that this cannot happen as READ_REPLICATION_SLOT supports only
537  * physical slots, but play it safe.
538  */
539  if (strcmp(PQgetvalue(res, 0, 0), "physical") != 0)
540  {
541  pg_log_error("expected a physical replication slot, got type \"%s\" instead",
542  PQgetvalue(res, 0, 0));
543  PQclear(res);
544  return false;
545  }
546 
547  /* restart LSN */
548  if (!PQgetisnull(res, 0, 1))
549  {
550  uint32 hi,
551  lo;
552 
553  if (sscanf(PQgetvalue(res, 0, 1), "%X/%X", &hi, &lo) != 2)
554  {
555  pg_log_error("could not parse restart_lsn \"%s\" for replication slot \"%s\"",
556  PQgetvalue(res, 0, 1), slot_name);
557  PQclear(res);
558  return false;
559  }
560  lsn_loc = ((uint64) hi) << 32 | lo;
561  }
562 
563  /* current TLI */
564  if (!PQgetisnull(res, 0, 2))
565  tli_loc = (TimeLineID) atol(PQgetvalue(res, 0, 2));
566 
567  PQclear(res);
568 
569  /* Assign results if requested */
570  if (restart_lsn)
571  *restart_lsn = lsn_loc;
572  if (restart_tli)
573  *restart_tli = tli_loc;
574 
575  return true;
576 }
unsigned int uint32
Definition: c.h:493
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3836
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3861
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
uint32 TimeLineID
Definition: xlogdefs.h:59

References appendPQExpBuffer(), conn, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), InvalidXLogRecPtr, pg_log_error, PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQexec(), PQgetisnull(), PQgetvalue(), PQnfields(), PQntuples(), PQresultStatus(), and res.

Referenced by StreamLog().

◆ RetrieveDataDirCreatePerm()

static bool RetrieveDataDirCreatePerm ( PGconn conn)
static

Definition at line 354 of file streamutil.c.

355 {
356  PGresult *res;
358 
359  /* check connection existence */
360  Assert(conn != NULL);
361 
362  /* for previous versions leave the default group access */
364  return true;
365 
366  res = PQexec(conn, "SHOW data_directory_mode");
368  {
369  pg_log_error("could not send replication command \"%s\": %s",
370  "SHOW data_directory_mode", PQerrorMessage(conn));
371 
372  PQclear(res);
373  return false;
374  }
375  if (PQntuples(res) != 1 || PQnfields(res) < 1)
376  {
377  pg_log_error("could not fetch group access flag: got %d rows and %d fields, expected %d rows and %d or more fields",
378  PQntuples(res), PQnfields(res), 1, 1);
379 
380  PQclear(res);
381  return false;
382  }
383 
384  if (sscanf(PQgetvalue(res, 0, 0), "%o", &data_directory_mode) != 1)
385  {
386  pg_log_error("group access flag could not be parsed: %s",
387  PQgetvalue(res, 0, 0));
388 
389  PQclear(res);
390  return false;
391  }
392 
394 
395  PQclear(res);
396  return true;
397 }
void SetDataDirectoryCreatePerm(int dataDirMode)
Definition: file_perm.c:34
int data_directory_mode
Definition: globals.c:74
#define MINIMUM_VERSION_FOR_GROUP_ACCESS
Definition: streamutil.c:44

References Assert(), conn, data_directory_mode, MINIMUM_VERSION_FOR_GROUP_ACCESS, pg_log_error, PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQexec(), PQgetvalue(), PQnfields(), PQntuples(), PQresultStatus(), PQserverVersion(), res, and SetDataDirectoryCreatePerm().

Referenced by GetConnection().

◆ RetrieveWalSegSize()

bool RetrieveWalSegSize ( PGconn conn)

Definition at line 275 of file streamutil.c.

276 {
277  PGresult *res;
278  char xlog_unit[3];
279  int xlog_val,
280  multiplier = 1;
281 
282  /* check connection existence */
283  Assert(conn != NULL);
284 
285  /* for previous versions set the default xlog seg size */
287  {
289  return true;
290  }
291 
292  res = PQexec(conn, "SHOW wal_segment_size");
294  {
295  pg_log_error("could not send replication command \"%s\": %s",
296  "SHOW wal_segment_size", PQerrorMessage(conn));
297 
298  PQclear(res);
299  return false;
300  }
301  if (PQntuples(res) != 1 || PQnfields(res) < 1)
302  {
303  pg_log_error("could not fetch WAL segment size: got %d rows and %d fields, expected %d rows and %d or more fields",
304  PQntuples(res), PQnfields(res), 1, 1);
305 
306  PQclear(res);
307  return false;
308  }
309 
310  /* fetch xlog value and unit from the result */
311  if (sscanf(PQgetvalue(res, 0, 0), "%d%2s", &xlog_val, xlog_unit) != 2)
312  {
313  pg_log_error("WAL segment size could not be parsed");
314  PQclear(res);
315  return false;
316  }
317 
318  PQclear(res);
319 
320  /* set the multiplier based on unit to convert xlog_val to bytes */
321  if (strcmp(xlog_unit, "MB") == 0)
322  multiplier = 1024 * 1024;
323  else if (strcmp(xlog_unit, "GB") == 0)
324  multiplier = 1024 * 1024 * 1024;
325 
326  /* convert and set WalSegSz */
327  WalSegSz = xlog_val * multiplier;
328 
330  {
331  pg_log_error(ngettext("remote server reported invalid WAL segment size (%d byte)",
332  "remote server reported invalid WAL segment size (%d bytes)",
333  WalSegSz),
334  WalSegSz);
335  pg_log_error_detail("The WAL segment size must be a power of two between 1 MB and 1 GB.");
336  return false;
337  }
338 
339  return true;
340 }
#define ngettext(s, p, n)
Definition: c.h:1168
#define pg_log_error_detail(...)
Definition: logging.h:109
#define DEFAULT_XLOG_SEG_SIZE
int WalSegSz
Definition: streamutil.c:34
#define MINIMUM_VERSION_FOR_SHOW_CMD
Definition: streamutil.c:39
#define IsValidWalSegSize(size)
Definition: xlog_internal.h:96

References Assert(), conn, DEFAULT_XLOG_SEG_SIZE, IsValidWalSegSize, MINIMUM_VERSION_FOR_SHOW_CMD, ngettext, pg_log_error, pg_log_error_detail, PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQexec(), PQgetvalue(), PQnfields(), PQntuples(), PQresultStatus(), PQserverVersion(), res, and WalSegSz.

Referenced by main().

◆ RunIdentifySystem()

bool RunIdentifySystem ( PGconn conn,
char **  sysid,
TimeLineID starttli,
XLogRecPtr startpos,
char **  db_name 
)

Definition at line 408 of file streamutil.c.

410 {
411  PGresult *res;
412  uint32 hi,
413  lo;
414 
415  /* Check connection existence */
416  Assert(conn != NULL);
417 
418  res = PQexec(conn, "IDENTIFY_SYSTEM");
420  {
421  pg_log_error("could not send replication command \"%s\": %s",
422  "IDENTIFY_SYSTEM", PQerrorMessage(conn));
423 
424  PQclear(res);
425  return false;
426  }
427  if (PQntuples(res) != 1 || PQnfields(res) < 3)
428  {
429  pg_log_error("could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields",
430  PQntuples(res), PQnfields(res), 1, 3);
431 
432  PQclear(res);
433  return false;
434  }
435 
436  /* Get system identifier */
437  if (sysid != NULL)
438  *sysid = pg_strdup(PQgetvalue(res, 0, 0));
439 
440  /* Get timeline ID to start streaming from */
441  if (starttli != NULL)
442  *starttli = atoi(PQgetvalue(res, 0, 1));
443 
444  /* Get LSN start position if necessary */
445  if (startpos != NULL)
446  {
447  if (sscanf(PQgetvalue(res, 0, 2), "%X/%X", &hi, &lo) != 2)
448  {
449  pg_log_error("could not parse write-ahead log location \"%s\"",
450  PQgetvalue(res, 0, 2));
451 
452  PQclear(res);
453  return false;
454  }
455  *startpos = ((uint64) hi) << 32 | lo;
456  }
457 
458  /* Get database name, only available in 9.4 and newer versions */
459  if (db_name != NULL)
460  {
461  *db_name = NULL;
462  if (PQserverVersion(conn) >= 90400)
463  {
464  if (PQnfields(res) < 4)
465  {
466  pg_log_error("could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields",
467  PQntuples(res), PQnfields(res), 1, 4);
468 
469  PQclear(res);
470  return false;
471  }
472  if (!PQgetisnull(res, 0, 3))
473  *db_name = pg_strdup(PQgetvalue(res, 0, 3));
474  }
475  }
476 
477  PQclear(res);
478  return true;
479 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static XLogRecPtr startpos

References Assert(), conn, pg_log_error, pg_strdup(), PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQexec(), PQgetisnull(), PQgetvalue(), PQnfields(), PQntuples(), PQresultStatus(), PQserverVersion(), res, and startpos.

Referenced by BaseBackup(), main(), ReceiveXlogStream(), and StreamLog().

Variable Documentation

◆ conn

PGconn* conn = NULL

Definition at line 54 of file streamutil.c.

Referenced by append_db_pattern_cte(), append_rel_pattern_filtered_cte(), append_rel_pattern_raw_cte(), appendQualifiedRelation(), AppendStringCommandOption(), appendStringLiteralConn(), applyRemoteGucs(), BaseBackup(), build_client_final_message(), build_client_first_message(), build_startup_packet(), buildShSecLabels(), check_expected_areq(), check_for_data_types_usage(), check_for_incompatible_polymorphics(), check_for_isn_and_int8_passing_mismatch(), check_for_pg_role_prefix(), check_for_prepared_transactions(), check_for_tables_with_oids(), check_for_user_defined_encoding_conversions(), check_for_user_defined_postfix_ops(), check_is_install_user(), check_loadable_libraries(), check_new_cluster_logical_replication_slots(), check_new_cluster_subscription_configuration(), check_old_cluster_subscription_state(), check_prepare_conn(), CheckCopyStreamStop(), CheckServerVersionForStreaming(), close_cursor(), cluster_all_databases(), cluster_one_database(), compile_database_list(), compile_relation_list_one_db(), configure_remote_session(), confirm_query_canceled_impl(), connect_pg_server(), connectDatabase(), connectFailureMessage(), connectMaintenanceDatabase(), connectNoDelay(), connectOptions1(), connectToServer(), copy_connection(), CopyStreamPoll(), CopyStreamReceive(), create_cursor(), create_logical_replication_slots(), CreateReplicationSlot(), dblink_cancel_query(), dblink_close(), dblink_connect(), dblink_disconnect(), dblink_error_message(), dblink_exec(), dblink_fetch(), dblink_get_conn(), dblink_get_notify(), dblink_is_busy(), dblink_open(), dblink_record_internal(), dblink_res_error(), dblink_security_check(), dblink_send_query(), disconnect_atexit(), disconnectDatabase(), do_sql_command(), do_sql_command_begin(), do_sql_command_end(), doConnect(), dropDBs(), DropReplicationSlot(), dropRoles(), dropTablespaces(), dumpDatabase(), dumpDatabaseConfig(), dumpDatabases(), dumpLOs(), dumpRoleGUCPrivs(), dumpRoleMembership(), dumpRoles(), dumpTableData_copy(), dumpTablespaces(), dumpUserConfig(), ecpg_raise_backend(), emitHostIdentityInfo(), emitShSecLabels(), estimate_path_cost_size(), executeCommand(), executeMaintenanceCommand(), executeQuery(), executeQueryOrDie(), ExecuteSqlCommand(), exit_nicely(), expand_dbname_patterns(), exportFile(), fetch_more_data(), fillPGconn(), flushAndSendFeedback(), freePGconn(), get_db_conn(), get_db_infos(), get_db_subscription_count(), get_loadable_libraries(), get_old_cluster_logical_slot_infos(), get_parallel_object_list(), get_rel_infos(), get_remote_estimate(), get_tablespace_paths(), get_template0_info(), getAnotherTuple(), getCopyDataMessage(), getCopyResult(), getCopyStart(), getHostaddr(), getNotify(), getParamDescriptions(), getParameterStatus(), getReadyForQuery(), getRowDescriptions(), GetSlotInformation(), gss_read(), handleCopyIn(), handleCopyOut(), HandleCopyStream(), HandleEndOfCopyStream(), handleSyncLoss(), importFile(), init_libpq_conn(), init_libpq_source(), initialize_SSL(), internal_ping(), libpq_append_conn_error(), libpq_fetch_file(), libpq_get_current_wal_insert_lsn(), libpq_prng_init(), libpq_traverse_files(), libpqrcv_alter_slot(), libpqrcv_connect(), libpqrcv_create_slot(), libpqrcv_disconnect(), libpqrcv_endstreaming(), libpqrcv_exec(), libpqrcv_get_backend_pid(), libpqrcv_get_conninfo(), libpqrcv_get_senderinfo(), libpqrcv_identify_system(), libpqrcv_readtimelinehistoryfile(), libpqrcv_receive(), libpqrcv_send(), libpqrcv_server_version(), libpqrcv_startstreaming(), libpqsrv_connect(), libpqsrv_connect_internal(), libpqsrv_connect_params(), libpqsrv_disconnect(), libpqsrv_exec(), libpqsrv_exec_params(), libpqsrv_get_result(), libpqsrv_get_result_last(), lo_close(), lo_creat(), lo_create(), lo_export(), lo_import(), lo_import_internal(), lo_import_with_oid(), lo_initialize(), lo_lseek(), lo_lseek64(), lo_open(), lo_read(), lo_tell(), lo_tell64(), lo_truncate(), lo_truncate64(), lo_unlink(), lo_write(), main(), makeAlterConfigCommand(), materializeResult(), my_SSL_set_fd(), my_truncate(), old_9_6_invalidate_hash_indexes(), open_client_SSL(), openssl_verify_peer_name_matches_certificate_ip(), openssl_verify_peer_name_matches_certificate_name(), overwrite(), ParallelSlotsAdoptConn(), ParallelSlotsTerminate(), parseInput(), pg_fe_sendauth(), pg_GSS_error(), pg_GSS_load_servicename(), pg_GSS_read(), pg_GSS_write(), pg_password_sendauth(), pg_SASL_continue(), pg_SASL_init(), pgfdw_cancel_query(), pgfdw_cancel_query_begin(), pgfdw_cancel_query_end(), pgfdw_exec_cleanup_query(), pgfdw_exec_cleanup_query_begin(), pgfdw_exec_cleanup_query_end(), pgfdw_exec_query(), pgfdw_get_cleanup_result(), pgfdw_get_result(), pgfdw_report_error(), pgfdw_security_check(), pgpassfileWarning(), pgtls_close(), pgtls_get_peer_certificate_hash(), pgtls_init(), pgtls_open_client(), pgtls_read(), pgtls_read_pending(), pgtls_verify_peer_name_matches_certificate_guts(), pgtls_write(), pickout(), postgresAcquireSampleRowsFunc(), postgresAnalyzeForeignTable(), postgresExecForeignTruncate(), postgresGetAnalyzeInfoForForeignTable(), postgresImportForeignSchema(), pq_verify_peer_name_matches_certificate(), pq_verify_peer_name_matches_certificate_ip(), pq_verify_peer_name_matches_certificate_name(), pqAllocCmdQueueEntry(), pqAppendCmdQueueEntry(), PQbackendPID(), pqBuildStartupPacket3(), PQcancelCreate(), PQcancelPoll(), PQchangePassword(), pqCheckInBufferSpace(), pqCheckOutBufferSpace(), pqClearAsyncResult(), PQclientEncoding(), pqClosePGconn(), PQclosePortal(), PQclosePrepared(), pqCommandQueueAdvance(), PQconnectdb(), pqConnectDBComplete(), PQconnectdbParams(), pqConnectDBStart(), PQconnectionNeedsPassword(), PQconnectionUsedGSSAPI(), PQconnectionUsedPassword(), pqConnectOptions2(), PQconnectPoll(), PQconnectStart(), PQconnectStartParams(), PQconninfo(), PQconsumeInput(), PQdb(), PQdefaultSSLKeyPassHook_OpenSSL(), PQdescribePortal(), PQdescribePrepared(), pqDropConnection(), pqDropServerData(), PQencryptPasswordConn(), PQendcopy(), pqEndcopy3(), PQenterPipelineMode(), PQerrorMessage(), PQescapeByteaConn(), PQescapeByteaInternal(), PQescapeIdentifier(), PQescapeInternal(), PQescapeLiteral(), PQescapeStringConn(), PQescapeStringInternal(), PQexec(), PQexecFinish(), PQexecParams(), PQexecPrepared(), PQexecStart(), PQexitPipelineMode(), PQfinish(), PQfireResultCreateEvents(), PQflush(), pqFlush(), PQfn(), pqFunctionCall3(), pqGetc(), PQgetCancel(), PQgetCopyData(), pqGetCopyData3(), pqGetErrorNotice3(), PQgetgssctx(), pqGetInt(), PQgetline(), pqGetline3(), PQgetlineAsync(), pqGetlineAsync3(), pqGetnchar(), pqGetNegotiateProtocolVersion3(), PQgetResult(), pqGets(), pqGets_append(), pqGets_internal(), PQgetssl(), PQgssEncInUse(), PQhost(), PQhostaddr(), PQinstanceData(), PQisBusy(), PQisnonblocking(), pqMakeEmptyPGconn(), PQmakeEmptyPGresult(), PQnotifies(), PQoptions(), pqPacketSend(), PQparameterStatus(), pqParseInput3(), pqParseIntParam(), PQpass(), PQping(), PQpingParams(), pqPipelineFlush(), pqPipelineProcessQueue(), PQpipelineStatus(), PQpipelineSync(), pqPipelineSyncInternal(), PQport(), PQprepare(), pqPrepareAsyncResult(), PQprotocolVersion(), pqPutc(), PQputCopyData(), PQputCopyEnd(), pqPutInt(), PQputline(), pqPutMsgBytes(), pqPutMsgEnd(), pqPutMsgStart(), PQputnbytes(), pqPutnchar(), pqPuts(), pqReadData(), pqReadReady(), pqRecycleCmdQueueEntry(), PQregisterEventProc(), pqReleaseConnHosts(), PQrequestCancel(), PQreset(), PQresetPoll(), PQresetStart(), pqRowProcessor(), pqSaveErrorResult(), pqSaveParameterStatus(), pqSaveWriteError(), pqsecure_close(), pqsecure_initialize(), pqsecure_open_client(), pqsecure_open_gss(), pqsecure_raw_read(), pqsecure_raw_write(), pqsecure_read(), pqsecure_write(), PQsendClosePortal(), PQsendClosePrepared(), PQsendDescribePortal(), PQsendDescribePrepared(), PQsendFlushRequest(), PQsendPipelineSync(), PQsendPrepare(), PQsendQuery(), PQsendQueryContinue(), PQsendQueryGuts(), PQsendQueryInternal(), PQsendQueryParams(), PQsendQueryPrepared(), PQsendQueryStart(), pqSendSome(), PQsendTypedCommand(), PQserverVersion(), PQsetClientEncoding(), PQsetdbLogin(), PQsetErrorContextVisibility(), PQsetErrorVerbosity(), PQsetInstanceData(), PQsetnonblocking(), PQsetNoticeProcessor(), PQsetNoticeReceiver(), PQsetSingleRowMode(), PQsetTraceFlags(), pqSkipnchar(), PQsocket(), pqSocketCheck(), PQssl_passwd_cb(), PQsslAttribute(), PQsslAttributeNames(), PQsslInUse(), PQsslStruct(), PQstatus(), PQtrace(), pqTraceOutputMessage(), pqTraceOutputNoTypeByteMessage(), PQtransactionStatus(), PQtty(), PQuntrace(), PQuser(), pqWait(), pqWaitTimed(), pqWriteReady(), prepareToTerminate(), process_result(), ProcessKeepaliveMsg(), processSQLNamePattern(), ProcessXLogDataMsg(), prohibit_crossdb_refs(), read_server_final_message(), read_server_first_message(), ReceiveArchiveStream(), ReceiveBackupManifest(), ReceiveBackupManifestInMemory(), ReceiveCopyData(), ReceiveTarFile(), ReceiveXlogStream(), reindex_all_databases(), reindex_one_database(), release_conn_addrinfo(), report_extension_updates(), RetrieveDataDirCreatePerm(), RetrieveWalSegSize(), run_permutation(), run_reindex_command(), run_simple_command(), run_simple_query(), run_vacuum_command(), RunIdentifySystem(), scram_exchange(), scram_init(), send_cancellable_query_impl(), sendFeedback(), sendTerminateConn(), set_archive_cancel_info(), set_frozenxids(), SetCancelConn(), setKeepalivesCount(), setKeepalivesIdle(), setKeepalivesInterval(), setTCPUserTimeout(), setup_connection(), sql_conn(), sql_exec(), sql_exec_dumpalldbs(), sql_exec_dumpalltables(), sql_exec_dumpalltbspc(), sql_exec_searchtables(), start_postmaster(), StartLogStreamer(), store_conn_addrinfo(), storeQueryResult(), StreamLog(), StreamLogicalLog(), stringlist_to_identifierstr(), TableCommandResultHandler(), test_cancel(), test_disallowed_in_pipeline(), test_multi_pipelines(), test_nosync(), test_pipeline_abort(), test_pipeline_idle(), test_pipelined_insert(), test_prepared(), test_simple_pipeline(), test_singlerowmode(), test_transaction(), test_uniqviol(), try_complete_step(), useKeepalives(), vacuum_all_databases(), vacuum_one_database(), vacuumlo(), verify_btree_slot_handler(), and verify_heap_slot_handler().

◆ connection_string

char* connection_string = NULL

Definition at line 47 of file streamutil.c.

Referenced by connectDatabase(), GetConnection(), and main().

◆ dbgetpassword

int dbgetpassword = 0

Definition at line 52 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbhost

char* dbhost = NULL

Definition at line 48 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbname

◆ dbport

char* dbport = NULL

Definition at line 50 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ dbuser

char* dbuser = NULL

Definition at line 49 of file streamutil.c.

Referenced by GetConnection(), and main().

◆ password

◆ progname

const char* progname

Definition at line 46 of file streamutil.c.

Referenced by GetConnection().

◆ WalSegSz