PostgreSQL Source Code  git master
version.c File Reference
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
#include "fe_utils/string_utils.h"
#include "pg_upgrade.h"
Include dependency graph for version.c:

Go to the source code of this file.

Functions

bool check_for_data_types_usage (ClusterInfo *cluster, const char *base_query, const char *output_path)
 
bool check_for_data_type_usage (ClusterInfo *cluster, const char *type_name, const char *output_path)
 
void old_9_3_check_for_line_data_type_usage (ClusterInfo *cluster)
 
void old_9_6_check_for_unknown_data_type_usage (ClusterInfo *cluster)
 
void old_9_6_invalidate_hash_indexes (ClusterInfo *cluster, bool check_mode)
 
void old_11_check_for_sql_identifier_data_type_usage (ClusterInfo *cluster)
 
void report_extension_updates (ClusterInfo *cluster)
 

Function Documentation

◆ check_for_data_type_usage()

bool check_for_data_type_usage ( ClusterInfo cluster,
const char *  type_name,
const char *  output_path 
)

Definition at line 153 of file version.c.

156 {
157  bool found;
158  char *base_query;
159 
160  base_query = psprintf("SELECT '%s'::pg_catalog.regtype AS oid",
161  type_name);
162 
163  found = check_for_data_types_usage(cluster, base_query, output_path);
164 
165  free(base_query);
166 
167  return found;
168 }
bool check_for_data_types_usage(ClusterInfo *cluster, const char *base_query, const char *output_path)
Definition: version.c:31
void cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
Definition: cluster.c:110
#define free(a)
Definition: header.h:65
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46

References check_for_data_types_usage(), cluster(), free, and psprintf().

Referenced by check_for_aclitem_data_type_usage(), check_for_jsonb_9_4_usage(), check_for_removed_data_type_usage(), old_11_check_for_sql_identifier_data_type_usage(), old_9_3_check_for_line_data_type_usage(), and old_9_6_check_for_unknown_data_type_usage().

◆ check_for_data_types_usage()

bool check_for_data_types_usage ( ClusterInfo cluster,
const char *  base_query,
const char *  output_path 
)

Definition at line 31 of file version.c.

34 {
35  bool found = false;
36  FILE *script = NULL;
37  int dbnum;
38 
39  for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
40  {
41  DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
42  PGconn *conn = connectToServer(cluster, active_db->db_name);
43  PQExpBufferData querybuf;
44  PGresult *res;
45  bool db_used = false;
46  int ntups;
47  int rowno;
48  int i_nspname,
49  i_relname,
50  i_attname;
51 
52  /*
53  * The type(s) of interest might be wrapped in a domain, array,
54  * composite, or range, and these container types can be nested (to
55  * varying extents depending on server version, but that's not of
56  * concern here). To handle all these cases we need a recursive CTE.
57  */
58  initPQExpBuffer(&querybuf);
59  appendPQExpBuffer(&querybuf,
60  "WITH RECURSIVE oids AS ( "
61  /* start with the type(s) returned by base_query */
62  " %s "
63  " UNION ALL "
64  " SELECT * FROM ( "
65  /* inner WITH because we can only reference the CTE once */
66  " WITH x AS (SELECT oid FROM oids) "
67  /* domains on any type selected so far */
68  " SELECT t.oid FROM pg_catalog.pg_type t, x WHERE typbasetype = x.oid AND typtype = 'd' "
69  " UNION ALL "
70  /* arrays over any type selected so far */
71  " SELECT t.oid FROM pg_catalog.pg_type t, x WHERE typelem = x.oid AND typtype = 'b' "
72  " UNION ALL "
73  /* composite types containing any type selected so far */
74  " SELECT t.oid FROM pg_catalog.pg_type t, pg_catalog.pg_class c, pg_catalog.pg_attribute a, x "
75  " WHERE t.typtype = 'c' AND "
76  " t.oid = c.reltype AND "
77  " c.oid = a.attrelid AND "
78  " NOT a.attisdropped AND "
79  " a.atttypid = x.oid "
80  " UNION ALL "
81  /* ranges containing any type selected so far */
82  " SELECT t.oid FROM pg_catalog.pg_type t, pg_catalog.pg_range r, x "
83  " WHERE t.typtype = 'r' AND r.rngtypid = t.oid AND r.rngsubtype = x.oid"
84  " ) foo "
85  ") "
86  /* now look for stored columns of any such type */
87  "SELECT n.nspname, c.relname, a.attname "
88  "FROM pg_catalog.pg_class c, "
89  " pg_catalog.pg_namespace n, "
90  " pg_catalog.pg_attribute a "
91  "WHERE c.oid = a.attrelid AND "
92  " NOT a.attisdropped AND "
93  " a.atttypid IN (SELECT oid FROM oids) AND "
94  " c.relkind IN ("
95  CppAsString2(RELKIND_RELATION) ", "
96  CppAsString2(RELKIND_MATVIEW) ", "
97  CppAsString2(RELKIND_INDEX) ") AND "
98  " c.relnamespace = n.oid AND "
99  /* exclude possible orphaned temp tables */
100  " n.nspname !~ '^pg_temp_' AND "
101  " n.nspname !~ '^pg_toast_temp_' AND "
102  /* exclude system catalogs, too */
103  " n.nspname NOT IN ('pg_catalog', 'information_schema')",
104  base_query);
105 
106  res = executeQueryOrDie(conn, "%s", querybuf.data);
107 
108  ntups = PQntuples(res);
109  i_nspname = PQfnumber(res, "nspname");
110  i_relname = PQfnumber(res, "relname");
111  i_attname = PQfnumber(res, "attname");
112  for (rowno = 0; rowno < ntups; rowno++)
113  {
114  found = true;
115  if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
116  pg_fatal("could not open file \"%s\": %s", output_path,
117  strerror(errno));
118  if (!db_used)
119  {
120  fprintf(script, "In database: %s\n", active_db->db_name);
121  db_used = true;
122  }
123  fprintf(script, " %s.%s.%s\n",
124  PQgetvalue(res, rowno, i_nspname),
125  PQgetvalue(res, rowno, i_relname),
126  PQgetvalue(res, rowno, i_attname));
127  }
128 
129  PQclear(res);
130 
131  termPQExpBuffer(&querybuf);
132 
133  PQfinish(conn);
134  }
135 
136  if (script)
137  fclose(script);
138 
139  return found;
140 }
#define CppAsString2(x)
Definition: c.h:316
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4602
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3395
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3790
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3503
#define pg_fatal(...)
#define fopen_priv(path, mode)
Definition: pg_upgrade.h:391
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
Definition: server.c:28
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2
#define strerror
Definition: port.h:251
#define fprintf
Definition: port.h:242
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:129
PGconn * conn
Definition: streamutil.c:54
char * db_name
Definition: pg_upgrade.h:175

References appendPQExpBuffer(), cluster(), conn, connectToServer(), CppAsString2, PQExpBufferData::data, DbInfo::db_name, executeQueryOrDie(), fopen_priv, fprintf, initPQExpBuffer(), pg_fatal, PQclear(), PQfinish(), PQfnumber(), PQgetvalue(), PQntuples(), res, strerror, and termPQExpBuffer().

Referenced by check_for_composite_data_type_usage(), check_for_data_type_usage(), and check_for_reg_data_type_usage().

◆ old_11_check_for_sql_identifier_data_type_usage()

void old_11_check_for_sql_identifier_data_type_usage ( ClusterInfo cluster)

Definition at line 365 of file version.c.

366 {
367  char output_path[MAXPGPATH];
368 
369  prep_status("Checking for invalid \"sql_identifier\" user columns");
370 
371  snprintf(output_path, sizeof(output_path), "%s/%s",
373  "tables_using_sql_identifier.txt");
374 
375  if (check_for_data_type_usage(cluster, "information_schema.sql_identifier",
376  output_path))
377  {
378  pg_log(PG_REPORT, "fatal");
379  pg_fatal("Your installation contains the \"sql_identifier\" data type in user tables.\n"
380  "The on-disk format for this data type has changed, so this\n"
381  "cluster cannot currently be upgraded. You can\n"
382  "drop the problem columns and restart the upgrade.\n"
383  "A list of the problem columns is in the file:\n"
384  " %s", output_path);
385  }
386  else
387  check_ok();
388 }
bool check_for_data_type_usage(ClusterInfo *cluster, const char *type_name, const char *output_path)
Definition: version.c:153
static void check_ok(void)
Definition: initdb.c:2036
#define MAXPGPATH
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
LogOpts log_opts
Definition: util.c:17
@ PG_REPORT
Definition: pg_upgrade.h:248
void prep_status(const char *fmt,...) pg_attribute_printf(1
#define snprintf
Definition: port.h:238
char * basedir
Definition: pg_upgrade.h:289

References LogOpts::basedir, check_for_data_type_usage(), check_ok(), cluster(), log_opts, MAXPGPATH, pg_fatal, pg_log(), PG_REPORT, prep_status(), and snprintf.

Referenced by check_and_dump_old_cluster().

◆ old_9_3_check_for_line_data_type_usage()

void old_9_3_check_for_line_data_type_usage ( ClusterInfo cluster)

Definition at line 180 of file version.c.

181 {
182  char output_path[MAXPGPATH];
183 
184  prep_status("Checking for incompatible \"line\" data type");
185 
186  snprintf(output_path, sizeof(output_path), "%s/%s",
188  "tables_using_line.txt");
189 
190  if (check_for_data_type_usage(cluster, "pg_catalog.line", output_path))
191  {
192  pg_log(PG_REPORT, "fatal");
193  pg_fatal("Your installation contains the \"line\" data type in user tables.\n"
194  "This data type changed its internal and input/output format\n"
195  "between your old and new versions so this\n"
196  "cluster cannot currently be upgraded. You can\n"
197  "drop the problem columns and restart the upgrade.\n"
198  "A list of the problem columns is in the file:\n"
199  " %s", output_path);
200  }
201  else
202  check_ok();
203 }

References LogOpts::basedir, check_for_data_type_usage(), check_ok(), cluster(), log_opts, MAXPGPATH, pg_fatal, pg_log(), PG_REPORT, prep_status(), and snprintf.

Referenced by check_and_dump_old_cluster().

◆ old_9_6_check_for_unknown_data_type_usage()

void old_9_6_check_for_unknown_data_type_usage ( ClusterInfo cluster)

Definition at line 220 of file version.c.

221 {
222  char output_path[MAXPGPATH];
223 
224  prep_status("Checking for invalid \"unknown\" user columns");
225 
226  snprintf(output_path, sizeof(output_path), "%s/%s",
228  "tables_using_unknown.txt");
229 
230  if (check_for_data_type_usage(cluster, "pg_catalog.unknown", output_path))
231  {
232  pg_log(PG_REPORT, "fatal");
233  pg_fatal("Your installation contains the \"unknown\" data type in user tables.\n"
234  "This data type is no longer allowed in tables, so this\n"
235  "cluster cannot currently be upgraded. You can\n"
236  "drop the problem columns and restart the upgrade.\n"
237  "A list of the problem columns is in the file:\n"
238  " %s", output_path);
239  }
240  else
241  check_ok();
242 }

References LogOpts::basedir, check_for_data_type_usage(), check_ok(), cluster(), log_opts, MAXPGPATH, pg_fatal, pg_log(), PG_REPORT, prep_status(), and snprintf.

Referenced by check_and_dump_old_cluster().

◆ old_9_6_invalidate_hash_indexes()

void old_9_6_invalidate_hash_indexes ( ClusterInfo cluster,
bool  check_mode 
)

Definition at line 250 of file version.c.

251 {
252  int dbnum;
253  FILE *script = NULL;
254  bool found = false;
255  char *output_path = "reindex_hash.sql";
256 
257  prep_status("Checking for hash indexes");
258 
259  for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
260  {
261  PGresult *res;
262  bool db_used = false;
263  int ntups;
264  int rowno;
265  int i_nspname,
266  i_relname;
267  DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
268  PGconn *conn = connectToServer(cluster, active_db->db_name);
269 
270  /* find hash indexes */
272  "SELECT n.nspname, c.relname "
273  "FROM pg_catalog.pg_class c, "
274  " pg_catalog.pg_index i, "
275  " pg_catalog.pg_am a, "
276  " pg_catalog.pg_namespace n "
277  "WHERE i.indexrelid = c.oid AND "
278  " c.relam = a.oid AND "
279  " c.relnamespace = n.oid AND "
280  " a.amname = 'hash'"
281  );
282 
283  ntups = PQntuples(res);
284  i_nspname = PQfnumber(res, "nspname");
285  i_relname = PQfnumber(res, "relname");
286  for (rowno = 0; rowno < ntups; rowno++)
287  {
288  found = true;
289  if (!check_mode)
290  {
291  if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
292  pg_fatal("could not open file \"%s\": %s", output_path,
293  strerror(errno));
294  if (!db_used)
295  {
296  PQExpBufferData connectbuf;
297 
298  initPQExpBuffer(&connectbuf);
299  appendPsqlMetaConnect(&connectbuf, active_db->db_name);
300  fputs(connectbuf.data, script);
301  termPQExpBuffer(&connectbuf);
302  db_used = true;
303  }
304  fprintf(script, "REINDEX INDEX %s.%s;\n",
305  quote_identifier(PQgetvalue(res, rowno, i_nspname)),
306  quote_identifier(PQgetvalue(res, rowno, i_relname)));
307  }
308  }
309 
310  PQclear(res);
311 
312  if (!check_mode && db_used)
313  {
314  /* mark hash indexes as invalid */
316  "UPDATE pg_catalog.pg_index i "
317  "SET indisvalid = false "
318  "FROM pg_catalog.pg_class c, "
319  " pg_catalog.pg_am a, "
320  " pg_catalog.pg_namespace n "
321  "WHERE i.indexrelid = c.oid AND "
322  " c.relam = a.oid AND "
323  " c.relnamespace = n.oid AND "
324  " a.amname = 'hash'"));
325  }
326 
327  PQfinish(conn);
328  }
329 
330  if (script)
331  fclose(script);
332 
333  if (found)
334  {
335  report_status(PG_WARNING, "warning");
336  if (check_mode)
337  pg_log(PG_WARNING, "\n"
338  "Your installation contains hash indexes. These indexes have different\n"
339  "internal formats between your old and new clusters, so they must be\n"
340  "reindexed with the REINDEX command. After upgrading, you will be given\n"
341  "REINDEX instructions.");
342  else
343  pg_log(PG_WARNING, "\n"
344  "Your installation contains hash indexes. These indexes have different\n"
345  "internal formats between your old and new clusters, so they must be\n"
346  "reindexed with the REINDEX command. The file\n"
347  " %s\n"
348  "when executed by psql by the database superuser will recreate all invalid\n"
349  "indexes; until then, none of these indexes will be used.",
350  output_path);
351  }
352  else
353  check_ok();
354 }
@ PG_WARNING
Definition: pg_upgrade.h:249
void report_status(eLogType type, const char *fmt,...) pg_attribute_printf(2
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11965
void appendPsqlMetaConnect(PQExpBuffer buf, const char *dbname)
Definition: string_utils.c:590

References appendPsqlMetaConnect(), check_ok(), cluster(), conn, connectToServer(), PQExpBufferData::data, DbInfo::db_name, executeQueryOrDie(), fopen_priv, fprintf, initPQExpBuffer(), pg_fatal, pg_log(), PG_WARNING, PQclear(), PQfinish(), PQfnumber(), PQgetvalue(), PQntuples(), prep_status(), quote_identifier(), report_status(), res, strerror, and termPQExpBuffer().

Referenced by check_and_dump_old_cluster(), and issue_warnings_and_set_wal_level().

◆ report_extension_updates()

void report_extension_updates ( ClusterInfo cluster)

Definition at line 396 of file version.c.

397 {
398  int dbnum;
399  FILE *script = NULL;
400  char *output_path = "update_extensions.sql";
401 
402  prep_status("Checking for extension updates");
403 
404  for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
405  {
406  PGresult *res;
407  bool db_used = false;
408  int ntups;
409  int rowno;
410  int i_name;
411  DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
412  PGconn *conn = connectToServer(cluster, active_db->db_name);
413 
414  /* find extensions needing updates */
416  "SELECT name "
417  "FROM pg_available_extensions "
418  "WHERE installed_version != default_version"
419  );
420 
421  ntups = PQntuples(res);
422  i_name = PQfnumber(res, "name");
423  for (rowno = 0; rowno < ntups; rowno++)
424  {
425  if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
426  pg_fatal("could not open file \"%s\": %s", output_path,
427  strerror(errno));
428  if (!db_used)
429  {
430  PQExpBufferData connectbuf;
431 
432  initPQExpBuffer(&connectbuf);
433  appendPsqlMetaConnect(&connectbuf, active_db->db_name);
434  fputs(connectbuf.data, script);
435  termPQExpBuffer(&connectbuf);
436  db_used = true;
437  }
438  fprintf(script, "ALTER EXTENSION %s UPDATE;\n",
439  quote_identifier(PQgetvalue(res, rowno, i_name)));
440  }
441 
442  PQclear(res);
443 
444  PQfinish(conn);
445  }
446 
447  if (script)
448  {
449  fclose(script);
450  report_status(PG_REPORT, "notice");
451  pg_log(PG_REPORT, "\n"
452  "Your installation contains extensions that should be updated\n"
453  "with the ALTER EXTENSION command. The file\n"
454  " %s\n"
455  "when executed by psql by the database superuser will update\n"
456  "these extensions.",
457  output_path);
458  }
459  else
460  check_ok();
461 }

References appendPsqlMetaConnect(), check_ok(), cluster(), conn, connectToServer(), PQExpBufferData::data, DbInfo::db_name, executeQueryOrDie(), fopen_priv, fprintf, initPQExpBuffer(), pg_fatal, pg_log(), PG_REPORT, PQclear(), PQfinish(), PQfnumber(), PQgetvalue(), PQntuples(), prep_status(), quote_identifier(), report_status(), res, strerror, and termPQExpBuffer().

Referenced by issue_warnings_and_set_wal_level().