Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * extension.c
4 : * Commands to manipulate extensions
5 : *
6 : * Extensions in PostgreSQL allow management of collections of SQL objects.
7 : *
8 : * All we need internally to manage an extension is an OID so that the
9 : * dependent objects can be associated with it. An extension is created by
10 : * populating the pg_extension catalog from a "control" file.
11 : * The extension control file is parsed with the same parser we use for
12 : * postgresql.conf. An extension also has an installation script file,
13 : * containing SQL commands to create the extension's objects.
14 : *
15 : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
16 : * Portions Copyright (c) 1994, Regents of the University of California
17 : *
18 : *
19 : * IDENTIFICATION
20 : * src/backend/commands/extension.c
21 : *
22 : *-------------------------------------------------------------------------
23 : */
24 : #include "postgres.h"
25 :
26 : #include <dirent.h>
27 : #include <limits.h>
28 : #include <sys/file.h>
29 : #include <sys/stat.h>
30 : #include <unistd.h>
31 :
32 : #include "access/genam.h"
33 : #include "access/htup_details.h"
34 : #include "access/relation.h"
35 : #include "access/table.h"
36 : #include "access/xact.h"
37 : #include "catalog/catalog.h"
38 : #include "catalog/dependency.h"
39 : #include "catalog/indexing.h"
40 : #include "catalog/namespace.h"
41 : #include "catalog/objectaccess.h"
42 : #include "catalog/pg_authid.h"
43 : #include "catalog/pg_collation.h"
44 : #include "catalog/pg_database.h"
45 : #include "catalog/pg_depend.h"
46 : #include "catalog/pg_extension.h"
47 : #include "catalog/pg_namespace.h"
48 : #include "catalog/pg_type.h"
49 : #include "commands/alter.h"
50 : #include "commands/comment.h"
51 : #include "commands/defrem.h"
52 : #include "commands/extension.h"
53 : #include "commands/schemacmds.h"
54 : #include "funcapi.h"
55 : #include "mb/pg_wchar.h"
56 : #include "miscadmin.h"
57 : #include "nodes/pg_list.h"
58 : #include "nodes/queryjumble.h"
59 : #include "storage/fd.h"
60 : #include "tcop/utility.h"
61 : #include "utils/acl.h"
62 : #include "utils/builtins.h"
63 : #include "utils/conffiles.h"
64 : #include "utils/fmgroids.h"
65 : #include "utils/lsyscache.h"
66 : #include "utils/memutils.h"
67 : #include "utils/rel.h"
68 : #include "utils/snapmgr.h"
69 : #include "utils/syscache.h"
70 : #include "utils/varlena.h"
71 :
72 :
73 : /* GUC */
74 : char *Extension_control_path;
75 :
76 : /* Globally visible state variables */
77 : bool creating_extension = false;
78 : Oid CurrentExtensionObject = InvalidOid;
79 :
80 : /*
81 : * Internal data structure to hold the results of parsing a control file
82 : */
83 : typedef struct ExtensionControlFile
84 : {
85 : char *name; /* name of the extension */
86 : char *basedir; /* base directory where control and script
87 : * files are located */
88 : char *control_dir; /* directory where control file was found */
89 : char *directory; /* directory for script files */
90 : char *default_version; /* default install target version, if any */
91 : char *module_pathname; /* string to substitute for
92 : * MODULE_PATHNAME */
93 : char *comment; /* comment, if any */
94 : char *schema; /* target schema (allowed if !relocatable) */
95 : bool relocatable; /* is ALTER EXTENSION SET SCHEMA supported? */
96 : bool superuser; /* must be superuser to install? */
97 : bool trusted; /* allow becoming superuser on the fly? */
98 : int encoding; /* encoding of the script file, or -1 */
99 : List *requires; /* names of prerequisite extensions */
100 : List *no_relocate; /* names of prerequisite extensions that
101 : * should not be relocated */
102 : } ExtensionControlFile;
103 :
104 : /*
105 : * Internal data structure for update path information
106 : */
107 : typedef struct ExtensionVersionInfo
108 : {
109 : char *name; /* name of the starting version */
110 : List *reachable; /* List of ExtensionVersionInfo's */
111 : bool installable; /* does this version have an install script? */
112 : /* working state for Dijkstra's algorithm: */
113 : bool distance_known; /* is distance from start known yet? */
114 : int distance; /* current worst-case distance estimate */
115 : struct ExtensionVersionInfo *previous; /* current best predecessor */
116 : } ExtensionVersionInfo;
117 :
118 : /*
119 : * Information for script_error_callback()
120 : */
121 : typedef struct
122 : {
123 : const char *sql; /* entire script file contents */
124 : const char *filename; /* script file pathname */
125 : ParseLoc stmt_location; /* current stmt start loc, or -1 if unknown */
126 : ParseLoc stmt_len; /* length in bytes; 0 means "rest of string" */
127 : } script_error_callback_arg;
128 :
129 : /*
130 : * A location based on the extension_control_path GUC.
131 : *
132 : * The macro field stores the name of a macro (for example “$system”) that
133 : * the extension_control_path processing supports, and which can be replaced
134 : * by a system value stored in loc.
135 : *
136 : * For non-system paths the macro field is NULL.
137 : */
138 : typedef struct
139 : {
140 : char *macro;
141 : char *loc;
142 : } ExtensionLocation;
143 :
144 : /* Local functions */
145 : static List *find_update_path(List *evi_list,
146 : ExtensionVersionInfo *evi_start,
147 : ExtensionVersionInfo *evi_target,
148 : bool reject_indirect,
149 : bool reinitialize);
150 : static Oid get_required_extension(char *reqExtensionName,
151 : char *extensionName,
152 : char *origSchemaName,
153 : bool cascade,
154 : List *parents,
155 : bool is_create);
156 : static void get_available_versions_for_extension(ExtensionControlFile *pcontrol,
157 : Tuplestorestate *tupstore,
158 : TupleDesc tupdesc,
159 : ExtensionLocation *location);
160 : static Datum convert_requires_to_datum(List *requires);
161 : static void ApplyExtensionUpdates(Oid extensionOid,
162 : ExtensionControlFile *pcontrol,
163 : const char *initialVersion,
164 : List *updateVersions,
165 : char *origSchemaName,
166 : bool cascade,
167 : bool is_create);
168 : static void ExecAlterExtensionContentsRecurse(AlterExtensionContentsStmt *stmt,
169 : ObjectAddress extension,
170 : ObjectAddress object);
171 : static char *read_whole_file(const char *filename, int *length);
172 : static ExtensionControlFile *new_ExtensionControlFile(const char *extname);
173 :
174 : char *find_in_paths(const char *basename, List *paths);
175 :
176 : /*
177 : * Return the extension location. If the current user doesn't have sufficient
178 : * privilege, don't show the location.
179 : */
180 : static char *
181 15124 : get_extension_location(ExtensionLocation *loc)
182 : {
183 : /* We only want to show extension paths for superusers. */
184 15124 : if (superuser())
185 : {
186 : /* Return the macro value if present to avoid showing system paths. */
187 14672 : if (loc->macro != NULL)
188 14652 : return loc->macro;
189 : else
190 20 : return loc->loc;
191 : }
192 : else
193 : {
194 : /* Similar to pg_stat_activity for unprivileged users */
195 452 : return "<insufficient privilege>";
196 : }
197 : }
198 :
199 : /*
200 : * get_extension_oid - given an extension name, look up the OID
201 : *
202 : * If missing_ok is false, throw an error if extension name not found. If
203 : * true, just return InvalidOid.
204 : */
205 : Oid
206 3070 : get_extension_oid(const char *extname, bool missing_ok)
207 : {
208 : Oid result;
209 :
210 3070 : result = GetSysCacheOid1(EXTENSIONNAME, Anum_pg_extension_oid,
211 : CStringGetDatum(extname));
212 :
213 3070 : if (!OidIsValid(result) && !missing_ok)
214 12 : ereport(ERROR,
215 : (errcode(ERRCODE_UNDEFINED_OBJECT),
216 : errmsg("extension \"%s\" does not exist",
217 : extname)));
218 :
219 3058 : return result;
220 : }
221 :
222 : /*
223 : * get_extension_name - given an extension OID, look up the name
224 : *
225 : * Returns a palloc'd string, or NULL if no such extension.
226 : */
227 : char *
228 124 : get_extension_name(Oid ext_oid)
229 : {
230 : char *result;
231 : HeapTuple tuple;
232 :
233 124 : tuple = SearchSysCache1(EXTENSIONOID, ObjectIdGetDatum(ext_oid));
234 :
235 124 : if (!HeapTupleIsValid(tuple))
236 18 : return NULL;
237 :
238 106 : result = pstrdup(NameStr(((Form_pg_extension) GETSTRUCT(tuple))->extname));
239 106 : ReleaseSysCache(tuple);
240 :
241 106 : return result;
242 : }
243 :
244 : /*
245 : * get_extension_schema - given an extension OID, fetch its extnamespace
246 : *
247 : * Returns InvalidOid if no such extension.
248 : */
249 : Oid
250 58 : get_extension_schema(Oid ext_oid)
251 : {
252 : Oid result;
253 : HeapTuple tuple;
254 :
255 58 : tuple = SearchSysCache1(EXTENSIONOID, ObjectIdGetDatum(ext_oid));
256 :
257 58 : if (!HeapTupleIsValid(tuple))
258 0 : return InvalidOid;
259 :
260 58 : result = ((Form_pg_extension) GETSTRUCT(tuple))->extnamespace;
261 58 : ReleaseSysCache(tuple);
262 :
263 58 : return result;
264 : }
265 :
266 : /*
267 : * Utility functions to check validity of extension and version names
268 : */
269 : static void
270 622 : check_valid_extension_name(const char *extensionname)
271 : {
272 622 : int namelen = strlen(extensionname);
273 :
274 : /*
275 : * Disallow empty names (the parser rejects empty identifiers anyway, but
276 : * let's check).
277 : */
278 622 : if (namelen == 0)
279 0 : ereport(ERROR,
280 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
281 : errmsg("invalid extension name: \"%s\"", extensionname),
282 : errdetail("Extension names must not be empty.")));
283 :
284 : /*
285 : * No double dashes, since that would make script filenames ambiguous.
286 : */
287 622 : if (strstr(extensionname, "--"))
288 0 : ereport(ERROR,
289 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
290 : errmsg("invalid extension name: \"%s\"", extensionname),
291 : errdetail("Extension names must not contain \"--\".")));
292 :
293 : /*
294 : * No leading or trailing dash either. (We could probably allow this, but
295 : * it would require much care in filename parsing and would make filenames
296 : * visually if not formally ambiguous. Since there's no real-world use
297 : * case, let's just forbid it.)
298 : */
299 622 : if (extensionname[0] == '-' || extensionname[namelen - 1] == '-')
300 0 : ereport(ERROR,
301 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
302 : errmsg("invalid extension name: \"%s\"", extensionname),
303 : errdetail("Extension names must not begin or end with \"-\".")));
304 :
305 : /*
306 : * No directory separators either (this is sufficient to prevent ".."
307 : * style attacks).
308 : */
309 622 : if (first_dir_separator(extensionname) != NULL)
310 0 : ereport(ERROR,
311 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
312 : errmsg("invalid extension name: \"%s\"", extensionname),
313 : errdetail("Extension names must not contain directory separator characters.")));
314 622 : }
315 :
316 : static void
317 656 : check_valid_version_name(const char *versionname)
318 : {
319 656 : int namelen = strlen(versionname);
320 :
321 : /*
322 : * Disallow empty names (we could possibly allow this, but there seems
323 : * little point).
324 : */
325 656 : if (namelen == 0)
326 0 : ereport(ERROR,
327 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
328 : errmsg("invalid extension version name: \"%s\"", versionname),
329 : errdetail("Version names must not be empty.")));
330 :
331 : /*
332 : * No double dashes, since that would make script filenames ambiguous.
333 : */
334 656 : if (strstr(versionname, "--"))
335 0 : ereport(ERROR,
336 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
337 : errmsg("invalid extension version name: \"%s\"", versionname),
338 : errdetail("Version names must not contain \"--\".")));
339 :
340 : /*
341 : * No leading or trailing dash either.
342 : */
343 656 : if (versionname[0] == '-' || versionname[namelen - 1] == '-')
344 0 : ereport(ERROR,
345 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
346 : errmsg("invalid extension version name: \"%s\"", versionname),
347 : errdetail("Version names must not begin or end with \"-\".")));
348 :
349 : /*
350 : * No directory separators either (this is sufficient to prevent ".."
351 : * style attacks).
352 : */
353 656 : if (first_dir_separator(versionname) != NULL)
354 0 : ereport(ERROR,
355 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
356 : errmsg("invalid extension version name: \"%s\"", versionname),
357 : errdetail("Version names must not contain directory separator characters.")));
358 656 : }
359 :
360 : /*
361 : * Utility functions to handle extension-related path names
362 : */
363 : static bool
364 48542 : is_extension_control_filename(const char *filename)
365 : {
366 48542 : const char *extension = strrchr(filename, '.');
367 :
368 48542 : return (extension != NULL) && (strcmp(extension, ".control") == 0);
369 : }
370 :
371 : static bool
372 536184 : is_extension_script_filename(const char *filename)
373 : {
374 536184 : const char *extension = strrchr(filename, '.');
375 :
376 536184 : return (extension != NULL) && (strcmp(extension, ".sql") == 0);
377 : }
378 :
379 : /*
380 : * Return a list of directories declared in the extension_control_path GUC.
381 : */
382 : static List *
383 800 : get_extension_control_directories(void)
384 : {
385 : char sharepath[MAXPGPATH];
386 : char *system_dir;
387 : char *ecp;
388 800 : List *paths = NIL;
389 :
390 800 : get_share_path(my_exec_path, sharepath);
391 :
392 800 : system_dir = psprintf("%s/extension", sharepath);
393 :
394 800 : if (strlen(Extension_control_path) == 0)
395 : {
396 0 : ExtensionLocation *location = palloc_object(ExtensionLocation);
397 :
398 0 : location->macro = NULL;
399 0 : location->loc = system_dir;
400 0 : paths = lappend(paths, location);
401 : }
402 : else
403 : {
404 : /* Duplicate the string so we can modify it */
405 800 : ecp = pstrdup(Extension_control_path);
406 :
407 : for (;;)
408 36 : {
409 : int len;
410 : char *mangled;
411 836 : char *piece = first_path_var_separator(ecp);
412 836 : ExtensionLocation *location = palloc_object(ExtensionLocation);
413 :
414 : /* Get the length of the next path on ecp */
415 836 : if (piece == NULL)
416 800 : len = strlen(ecp);
417 : else
418 36 : len = piece - ecp;
419 :
420 : /* Copy the next path found on ecp */
421 836 : piece = palloc(len + 1);
422 836 : strlcpy(piece, ecp, len + 1);
423 :
424 : /*
425 : * Substitute the path macro if needed or append "extension"
426 : * suffix if it is a custom extension control path.
427 : */
428 836 : if (strcmp(piece, "$system") == 0)
429 : {
430 800 : location->macro = pstrdup(piece);
431 800 : mangled = substitute_path_macro(piece, "$system", system_dir);
432 : }
433 : else
434 : {
435 36 : location->macro = NULL;
436 36 : mangled = psprintf("%s/extension", piece);
437 : }
438 836 : pfree(piece);
439 :
440 : /* Canonicalize the path based on the OS and add to the list */
441 836 : canonicalize_path(mangled);
442 836 : location->loc = mangled;
443 836 : paths = lappend(paths, location);
444 :
445 : /* Break if ecp is empty or move to the next path on ecp */
446 836 : if (ecp[len] == '\0')
447 800 : break;
448 : else
449 36 : ecp += len + 1;
450 : }
451 : }
452 :
453 800 : return paths;
454 : }
455 :
456 : /*
457 : * Find control file for extension with name in control->name, looking in
458 : * available paths. Return the full file name, or NULL if not found.
459 : * If found, the directory is recorded in control->control_dir.
460 : */
461 : static char *
462 664 : find_extension_control_filename(ExtensionControlFile *control)
463 : {
464 : char *basename;
465 : char *result;
466 : List *paths;
467 :
468 : Assert(control->name);
469 :
470 664 : basename = psprintf("%s.control", control->name);
471 :
472 664 : paths = get_extension_control_directories();
473 664 : result = find_in_paths(basename, paths);
474 :
475 664 : if (result)
476 : {
477 : const char *p;
478 :
479 664 : p = strrchr(result, '/');
480 : Assert(p);
481 664 : control->control_dir = pnstrdup(result, p - result);
482 : }
483 :
484 664 : return result;
485 : }
486 :
487 : static char *
488 6662 : get_extension_script_directory(ExtensionControlFile *control)
489 : {
490 : /*
491 : * The directory parameter can be omitted, absolute, or relative to the
492 : * installation's base directory, which can be the sharedir or a custom
493 : * path that was set via extension_control_path. It depends on where the
494 : * .control file was found.
495 : */
496 6662 : if (!control->directory)
497 6644 : return pstrdup(control->control_dir);
498 :
499 18 : if (is_absolute_path(control->directory))
500 0 : return pstrdup(control->directory);
501 :
502 : Assert(control->basedir != NULL);
503 18 : return psprintf("%s/%s", control->basedir, control->directory);
504 : }
505 :
506 : static char *
507 3384 : get_extension_aux_control_filename(ExtensionControlFile *control,
508 : const char *version)
509 : {
510 : char *result;
511 : char *scriptdir;
512 :
513 3384 : scriptdir = get_extension_script_directory(control);
514 :
515 3384 : result = (char *) palloc(MAXPGPATH);
516 3384 : snprintf(result, MAXPGPATH, "%s/%s--%s.control",
517 : scriptdir, control->name, version);
518 :
519 3384 : pfree(scriptdir);
520 :
521 3384 : return result;
522 : }
523 :
524 : static char *
525 1760 : get_extension_script_filename(ExtensionControlFile *control,
526 : const char *from_version, const char *version)
527 : {
528 : char *result;
529 : char *scriptdir;
530 :
531 1760 : scriptdir = get_extension_script_directory(control);
532 :
533 1760 : result = (char *) palloc(MAXPGPATH);
534 1760 : if (from_version)
535 546 : snprintf(result, MAXPGPATH, "%s/%s--%s--%s.sql",
536 : scriptdir, control->name, from_version, version);
537 : else
538 1214 : snprintf(result, MAXPGPATH, "%s/%s--%s.sql",
539 : scriptdir, control->name, version);
540 :
541 1760 : pfree(scriptdir);
542 :
543 1760 : return result;
544 : }
545 :
546 :
547 : /*
548 : * Parse contents of primary or auxiliary control file, and fill in
549 : * fields of *control. We parse primary file if version == NULL,
550 : * else the optional auxiliary file for that version.
551 : *
552 : * If control->control_dir is not NULL, use that to read and parse the
553 : * control file, otherwise search for the file in extension_control_path.
554 : *
555 : * Control files are supposed to be very short, half a dozen lines,
556 : * so we don't worry about memory allocation risks here. Also we don't
557 : * worry about what encoding it's in; all values are expected to be ASCII.
558 : */
559 : static void
560 19172 : parse_extension_control_file(ExtensionControlFile *control,
561 : const char *version)
562 : {
563 : char *filename;
564 : FILE *file;
565 : ConfigVariable *item,
566 19172 : *head = NULL,
567 19172 : *tail = NULL;
568 :
569 : /*
570 : * Locate the file to read. Auxiliary files are optional.
571 : */
572 19172 : if (version)
573 3384 : filename = get_extension_aux_control_filename(control, version);
574 : else
575 : {
576 : /*
577 : * If control_dir is already set, use it, else do a path search.
578 : */
579 15788 : if (control->control_dir)
580 : {
581 15124 : filename = psprintf("%s/%s.control", control->control_dir, control->name);
582 : }
583 : else
584 664 : filename = find_extension_control_filename(control);
585 : }
586 :
587 19172 : if (!filename)
588 : {
589 0 : ereport(ERROR,
590 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
591 : errmsg("extension \"%s\" is not available", control->name),
592 : errhint("The extension must first be installed on the system where PostgreSQL is running.")));
593 : }
594 :
595 : /* Assert that the control_dir ends with /extension */
596 : Assert(control->control_dir != NULL);
597 : Assert(strcmp(control->control_dir + strlen(control->control_dir) - strlen("/extension"), "/extension") == 0);
598 :
599 38344 : control->basedir = pnstrdup(
600 19172 : control->control_dir,
601 19172 : strlen(control->control_dir) - strlen("/extension"));
602 :
603 19172 : if ((file = AllocateFile(filename, "r")) == NULL)
604 : {
605 : /* no complaint for missing auxiliary file */
606 3384 : if (errno == ENOENT && version)
607 : {
608 3384 : pfree(filename);
609 3384 : return;
610 : }
611 :
612 0 : ereport(ERROR,
613 : (errcode_for_file_access(),
614 : errmsg("could not open extension control file \"%s\": %m",
615 : filename)));
616 : }
617 :
618 : /*
619 : * Parse the file content, using GUC's file parsing code. We need not
620 : * check the return value since any errors will be thrown at ERROR level.
621 : */
622 15788 : (void) ParseConfigFp(file, filename, CONF_FILE_START_DEPTH, ERROR,
623 : &head, &tail);
624 :
625 15788 : FreeFile(file);
626 :
627 : /*
628 : * Convert the ConfigVariable list into ExtensionControlFile entries.
629 : */
630 84796 : for (item = head; item != NULL; item = item->next)
631 : {
632 69008 : if (strcmp(item->name, "directory") == 0)
633 : {
634 16 : if (version)
635 0 : ereport(ERROR,
636 : (errcode(ERRCODE_SYNTAX_ERROR),
637 : errmsg("parameter \"%s\" cannot be set in a secondary extension control file",
638 : item->name)));
639 :
640 16 : control->directory = pstrdup(item->value);
641 : }
642 68992 : else if (strcmp(item->name, "default_version") == 0)
643 : {
644 15788 : if (version)
645 0 : ereport(ERROR,
646 : (errcode(ERRCODE_SYNTAX_ERROR),
647 : errmsg("parameter \"%s\" cannot be set in a secondary extension control file",
648 : item->name)));
649 :
650 15788 : control->default_version = pstrdup(item->value);
651 : }
652 53204 : else if (strcmp(item->name, "module_pathname") == 0)
653 : {
654 12782 : control->module_pathname = pstrdup(item->value);
655 : }
656 40422 : else if (strcmp(item->name, "comment") == 0)
657 : {
658 15788 : control->comment = pstrdup(item->value);
659 : }
660 24634 : else if (strcmp(item->name, "schema") == 0)
661 : {
662 1658 : control->schema = pstrdup(item->value);
663 : }
664 22976 : else if (strcmp(item->name, "relocatable") == 0)
665 : {
666 15644 : if (!parse_bool(item->value, &control->relocatable))
667 0 : ereport(ERROR,
668 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
669 : errmsg("parameter \"%s\" requires a Boolean value",
670 : item->name)));
671 : }
672 7332 : else if (strcmp(item->name, "superuser") == 0)
673 : {
674 1228 : if (!parse_bool(item->value, &control->superuser))
675 0 : ereport(ERROR,
676 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
677 : errmsg("parameter \"%s\" requires a Boolean value",
678 : item->name)));
679 : }
680 6104 : else if (strcmp(item->name, "trusted") == 0)
681 : {
682 3588 : if (!parse_bool(item->value, &control->trusted))
683 0 : ereport(ERROR,
684 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
685 : errmsg("parameter \"%s\" requires a Boolean value",
686 : item->name)));
687 : }
688 2516 : else if (strcmp(item->name, "encoding") == 0)
689 : {
690 0 : control->encoding = pg_valid_server_encoding(item->value);
691 0 : if (control->encoding < 0)
692 0 : ereport(ERROR,
693 : (errcode(ERRCODE_UNDEFINED_OBJECT),
694 : errmsg("\"%s\" is not a valid encoding name",
695 : item->value)));
696 : }
697 2516 : else if (strcmp(item->name, "requires") == 0)
698 : {
699 : /* Need a modifiable copy of string */
700 2372 : char *rawnames = pstrdup(item->value);
701 :
702 : /* Parse string into list of identifiers */
703 2372 : if (!SplitIdentifierString(rawnames, ',', &control->requires))
704 : {
705 : /* syntax error in name list */
706 0 : ereport(ERROR,
707 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
708 : errmsg("parameter \"%s\" must be a list of extension names",
709 : item->name)));
710 : }
711 : }
712 144 : else if (strcmp(item->name, "no_relocate") == 0)
713 : {
714 : /* Need a modifiable copy of string */
715 144 : char *rawnames = pstrdup(item->value);
716 :
717 : /* Parse string into list of identifiers */
718 144 : if (!SplitIdentifierString(rawnames, ',', &control->no_relocate))
719 : {
720 : /* syntax error in name list */
721 0 : ereport(ERROR,
722 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
723 : errmsg("parameter \"%s\" must be a list of extension names",
724 : item->name)));
725 : }
726 : }
727 : else
728 0 : ereport(ERROR,
729 : (errcode(ERRCODE_SYNTAX_ERROR),
730 : errmsg("unrecognized parameter \"%s\" in file \"%s\"",
731 : item->name, filename)));
732 : }
733 :
734 15788 : FreeConfigVariables(head);
735 :
736 15788 : if (control->relocatable && control->schema != NULL)
737 0 : ereport(ERROR,
738 : (errcode(ERRCODE_SYNTAX_ERROR),
739 : errmsg("parameter \"schema\" cannot be specified when \"relocatable\" is true")));
740 :
741 15788 : pfree(filename);
742 : }
743 :
744 : /*
745 : * Read the primary control file for the specified extension.
746 : */
747 : static ExtensionControlFile *
748 664 : read_extension_control_file(const char *extname)
749 : {
750 664 : ExtensionControlFile *control = new_ExtensionControlFile(extname);
751 :
752 : /*
753 : * Parse the primary control file.
754 : */
755 664 : parse_extension_control_file(control, NULL);
756 :
757 664 : return control;
758 : }
759 :
760 : /*
761 : * Read the auxiliary control file for the specified extension and version.
762 : *
763 : * Returns a new modified ExtensionControlFile struct; the original struct
764 : * (reflecting just the primary control file) is not modified.
765 : */
766 : static ExtensionControlFile *
767 3384 : read_extension_aux_control_file(const ExtensionControlFile *pcontrol,
768 : const char *version)
769 : {
770 : ExtensionControlFile *acontrol;
771 :
772 : /*
773 : * Flat-copy the struct. Pointer fields share values with original.
774 : */
775 3384 : acontrol = palloc_object(ExtensionControlFile);
776 3384 : memcpy(acontrol, pcontrol, sizeof(ExtensionControlFile));
777 :
778 : /*
779 : * Parse the auxiliary control file, overwriting struct fields
780 : */
781 3384 : parse_extension_control_file(acontrol, version);
782 :
783 3384 : return acontrol;
784 : }
785 :
786 : /*
787 : * Read an SQL script file into a string, and convert to database encoding
788 : */
789 : static char *
790 1142 : read_extension_script_file(const ExtensionControlFile *control,
791 : const char *filename)
792 : {
793 : int src_encoding;
794 : char *src_str;
795 : char *dest_str;
796 : int len;
797 :
798 1142 : src_str = read_whole_file(filename, &len);
799 :
800 : /* use database encoding if not given */
801 1142 : if (control->encoding < 0)
802 1142 : src_encoding = GetDatabaseEncoding();
803 : else
804 0 : src_encoding = control->encoding;
805 :
806 : /* make sure that source string is valid in the expected encoding */
807 1142 : (void) pg_verify_mbstr(src_encoding, src_str, len, false);
808 :
809 : /*
810 : * Convert the encoding to the database encoding. read_whole_file
811 : * null-terminated the string, so if no conversion happens the string is
812 : * valid as is.
813 : */
814 1142 : dest_str = pg_any_to_server(src_str, len, src_encoding);
815 :
816 1142 : return dest_str;
817 : }
818 :
819 : /*
820 : * error context callback for failures in script-file execution
821 : */
822 : static void
823 178 : script_error_callback(void *arg)
824 : {
825 178 : script_error_callback_arg *callback_arg = (script_error_callback_arg *) arg;
826 178 : const char *query = callback_arg->sql;
827 178 : int location = callback_arg->stmt_location;
828 178 : int len = callback_arg->stmt_len;
829 : int syntaxerrposition;
830 : const char *lastslash;
831 :
832 : /*
833 : * If there is a syntax error position, convert to internal syntax error;
834 : * otherwise report the current query as an item of context stack.
835 : *
836 : * Note: we'll provide no context except the filename if there's neither
837 : * an error position nor any known current query. That shouldn't happen
838 : * though: all errors reported during raw parsing should come with an
839 : * error position.
840 : */
841 178 : syntaxerrposition = geterrposition();
842 178 : if (syntaxerrposition > 0)
843 : {
844 : /*
845 : * If we do not know the bounds of the current statement (as would
846 : * happen for an error occurring during initial raw parsing), we have
847 : * to use a heuristic to decide how much of the script to show. We'll
848 : * also use the heuristic in the unlikely case that syntaxerrposition
849 : * is outside what we think the statement bounds are.
850 : */
851 2 : if (location < 0 || syntaxerrposition < location ||
852 0 : (len > 0 && syntaxerrposition > location + len))
853 : {
854 : /*
855 : * Our heuristic is pretty simple: look for semicolon-newline
856 : * sequences, and break at the last one strictly before
857 : * syntaxerrposition and the first one strictly after. It's
858 : * certainly possible to fool this with semicolon-newline embedded
859 : * in a string literal, but it seems better to do this than to
860 : * show the entire extension script.
861 : *
862 : * Notice we cope with Windows-style newlines (\r\n) regardless of
863 : * platform. This is because there might be such newlines in
864 : * script files on other platforms.
865 : */
866 2 : int slen = strlen(query);
867 :
868 2 : location = len = 0;
869 758 : for (int loc = 0; loc < slen; loc++)
870 : {
871 758 : if (query[loc] != ';')
872 754 : continue;
873 4 : if (query[loc + 1] == '\r')
874 0 : loc++;
875 4 : if (query[loc + 1] == '\n')
876 : {
877 4 : int bkpt = loc + 2;
878 :
879 4 : if (bkpt < syntaxerrposition)
880 2 : location = bkpt;
881 2 : else if (bkpt > syntaxerrposition)
882 : {
883 2 : len = bkpt - location;
884 2 : break; /* no need to keep searching */
885 : }
886 : }
887 : }
888 : }
889 :
890 : /* Trim leading/trailing whitespace, for consistency */
891 2 : query = CleanQuerytext(query, &location, &len);
892 :
893 : /*
894 : * Adjust syntaxerrposition. It shouldn't be pointing into the
895 : * whitespace we just trimmed, but cope if it is.
896 : */
897 2 : syntaxerrposition -= location;
898 2 : if (syntaxerrposition < 0)
899 0 : syntaxerrposition = 0;
900 2 : else if (syntaxerrposition > len)
901 0 : syntaxerrposition = len;
902 :
903 : /* And report. */
904 2 : errposition(0);
905 2 : internalerrposition(syntaxerrposition);
906 2 : internalerrquery(pnstrdup(query, len));
907 : }
908 176 : else if (location >= 0)
909 : {
910 : /*
911 : * Since no syntax cursor will be shown, it's okay and helpful to trim
912 : * the reported query string to just the current statement.
913 : */
914 176 : query = CleanQuerytext(query, &location, &len);
915 176 : errcontext("SQL statement \"%.*s\"", len, query);
916 : }
917 :
918 : /*
919 : * Trim the reported file name to remove the path. We know that
920 : * get_extension_script_filename() inserted a '/', regardless of whether
921 : * we're on Windows.
922 : */
923 178 : lastslash = strrchr(callback_arg->filename, '/');
924 178 : if (lastslash)
925 178 : lastslash++;
926 : else
927 0 : lastslash = callback_arg->filename; /* shouldn't happen, but cope */
928 :
929 : /*
930 : * If we have a location (which, as said above, we really always should)
931 : * then report a line number to aid in localizing problems in big scripts.
932 : */
933 178 : if (location >= 0)
934 : {
935 178 : int linenumber = 1;
936 :
937 87112 : for (query = callback_arg->sql; *query; query++)
938 : {
939 87112 : if (--location < 0)
940 178 : break;
941 86934 : if (*query == '\n')
942 3338 : linenumber++;
943 : }
944 178 : errcontext("extension script file \"%s\", near line %d",
945 : lastslash, linenumber);
946 : }
947 : else
948 0 : errcontext("extension script file \"%s\"", lastslash);
949 178 : }
950 :
951 : /*
952 : * Execute given SQL string.
953 : *
954 : * The filename the string came from is also provided, for error reporting.
955 : *
956 : * Note: it's tempting to just use SPI to execute the string, but that does
957 : * not work very well. The really serious problem is that SPI will parse,
958 : * analyze, and plan the whole string before executing any of it; of course
959 : * this fails if there are any plannable statements referring to objects
960 : * created earlier in the script. A lesser annoyance is that SPI insists
961 : * on printing the whole string as errcontext in case of any error, and that
962 : * could be very long.
963 : */
964 : static void
965 1138 : execute_sql_string(const char *sql, const char *filename)
966 : {
967 : script_error_callback_arg callback_arg;
968 : ErrorContextCallback scripterrcontext;
969 : List *raw_parsetree_list;
970 : DestReceiver *dest;
971 : ListCell *lc1;
972 :
973 : /*
974 : * Setup error traceback support for ereport().
975 : */
976 1138 : callback_arg.sql = sql;
977 1138 : callback_arg.filename = filename;
978 1138 : callback_arg.stmt_location = -1;
979 1138 : callback_arg.stmt_len = -1;
980 :
981 1138 : scripterrcontext.callback = script_error_callback;
982 1138 : scripterrcontext.arg = &callback_arg;
983 1138 : scripterrcontext.previous = error_context_stack;
984 1138 : error_context_stack = &scripterrcontext;
985 :
986 : /*
987 : * Parse the SQL string into a list of raw parse trees.
988 : */
989 1138 : raw_parsetree_list = pg_parse_query(sql);
990 :
991 : /* All output from SELECTs goes to the bit bucket */
992 1136 : dest = CreateDestReceiver(DestNone);
993 :
994 : /*
995 : * Do parse analysis, rule rewrite, planning, and execution for each raw
996 : * parsetree. We must fully execute each query before beginning parse
997 : * analysis on the next one, since there may be interdependencies.
998 : */
999 13632 : foreach(lc1, raw_parsetree_list)
1000 : {
1001 12520 : RawStmt *parsetree = lfirst_node(RawStmt, lc1);
1002 : MemoryContext per_parsetree_context,
1003 : oldcontext;
1004 : List *stmt_list;
1005 : ListCell *lc2;
1006 :
1007 : /* Report location of this query for error context callback */
1008 12520 : callback_arg.stmt_location = parsetree->stmt_location;
1009 12520 : callback_arg.stmt_len = parsetree->stmt_len;
1010 :
1011 : /*
1012 : * We do the work for each parsetree in a short-lived context, to
1013 : * limit the memory used when there are many commands in the string.
1014 : */
1015 : per_parsetree_context =
1016 12520 : AllocSetContextCreate(CurrentMemoryContext,
1017 : "execute_sql_string per-statement context",
1018 : ALLOCSET_DEFAULT_SIZES);
1019 12520 : oldcontext = MemoryContextSwitchTo(per_parsetree_context);
1020 :
1021 : /* Be sure parser can see any DDL done so far */
1022 12520 : CommandCounterIncrement();
1023 :
1024 12520 : stmt_list = pg_analyze_and_rewrite_fixedparams(parsetree,
1025 : sql,
1026 : NULL,
1027 : 0,
1028 : NULL);
1029 12520 : stmt_list = pg_plan_queries(stmt_list, sql, CURSOR_OPT_PARALLEL_OK, NULL);
1030 :
1031 25016 : foreach(lc2, stmt_list)
1032 : {
1033 12520 : PlannedStmt *stmt = lfirst_node(PlannedStmt, lc2);
1034 :
1035 12520 : CommandCounterIncrement();
1036 :
1037 12520 : PushActiveSnapshot(GetTransactionSnapshot());
1038 :
1039 12520 : if (stmt->utilityStmt == NULL)
1040 : {
1041 : QueryDesc *qdesc;
1042 :
1043 16 : qdesc = CreateQueryDesc(stmt,
1044 : sql,
1045 : GetActiveSnapshot(), NULL,
1046 : dest, NULL, NULL, 0);
1047 :
1048 16 : ExecutorStart(qdesc, 0);
1049 16 : ExecutorRun(qdesc, ForwardScanDirection, 0);
1050 16 : ExecutorFinish(qdesc);
1051 16 : ExecutorEnd(qdesc);
1052 :
1053 16 : FreeQueryDesc(qdesc);
1054 : }
1055 : else
1056 : {
1057 12504 : if (IsA(stmt->utilityStmt, TransactionStmt))
1058 0 : ereport(ERROR,
1059 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1060 : errmsg("transaction control statements are not allowed within an extension script")));
1061 :
1062 12504 : ProcessUtility(stmt,
1063 : sql,
1064 : false,
1065 : PROCESS_UTILITY_QUERY,
1066 : NULL,
1067 : NULL,
1068 : dest,
1069 : NULL);
1070 : }
1071 :
1072 12496 : PopActiveSnapshot();
1073 : }
1074 :
1075 : /* Clean up per-parsetree context. */
1076 12496 : MemoryContextSwitchTo(oldcontext);
1077 12496 : MemoryContextDelete(per_parsetree_context);
1078 : }
1079 :
1080 1112 : error_context_stack = scripterrcontext.previous;
1081 :
1082 : /* Be sure to advance the command counter after the last script command */
1083 1112 : CommandCounterIncrement();
1084 1112 : }
1085 :
1086 : /*
1087 : * Policy function: is the given extension trusted for installation by a
1088 : * non-superuser?
1089 : *
1090 : * (Update the errhint logic below if you change this.)
1091 : */
1092 : static bool
1093 14 : extension_is_trusted(ExtensionControlFile *control)
1094 : {
1095 : AclResult aclresult;
1096 :
1097 : /* Never trust unless extension's control file says it's okay */
1098 14 : if (!control->trusted)
1099 4 : return false;
1100 : /* Allow if user has CREATE privilege on current database */
1101 10 : aclresult = object_aclcheck(DatabaseRelationId, MyDatabaseId, GetUserId(), ACL_CREATE);
1102 10 : if (aclresult == ACLCHECK_OK)
1103 8 : return true;
1104 2 : return false;
1105 : }
1106 :
1107 : /*
1108 : * Execute the appropriate script file for installing or updating the extension
1109 : *
1110 : * If from_version isn't NULL, it's an update
1111 : *
1112 : * Note: requiredSchemas must be one-for-one with the control->requires list
1113 : */
1114 : static void
1115 1148 : execute_extension_script(Oid extensionOid, ExtensionControlFile *control,
1116 : const char *from_version,
1117 : const char *version,
1118 : List *requiredSchemas,
1119 : const char *schemaName)
1120 : {
1121 1148 : bool switch_to_superuser = false;
1122 : char *filename;
1123 1148 : Oid save_userid = 0;
1124 1148 : int save_sec_context = 0;
1125 : int save_nestlevel;
1126 : StringInfoData pathbuf;
1127 : ListCell *lc;
1128 : ListCell *lc2;
1129 :
1130 : /*
1131 : * Enforce superuser-ness if appropriate. We postpone these checks until
1132 : * here so that the control flags are correctly associated with the right
1133 : * script(s) if they happen to be set in secondary control files.
1134 : */
1135 1148 : if (control->superuser && !superuser())
1136 : {
1137 14 : if (extension_is_trusted(control))
1138 8 : switch_to_superuser = true;
1139 6 : else if (from_version == NULL)
1140 6 : ereport(ERROR,
1141 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1142 : errmsg("permission denied to create extension \"%s\"",
1143 : control->name),
1144 : control->trusted
1145 : ? errhint("Must have CREATE privilege on current database to create this extension.")
1146 : : errhint("Must be superuser to create this extension.")));
1147 : else
1148 0 : ereport(ERROR,
1149 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1150 : errmsg("permission denied to update extension \"%s\"",
1151 : control->name),
1152 : control->trusted
1153 : ? errhint("Must have CREATE privilege on current database to update this extension.")
1154 : : errhint("Must be superuser to update this extension.")));
1155 : }
1156 :
1157 1142 : filename = get_extension_script_filename(control, from_version, version);
1158 :
1159 1142 : if (from_version == NULL)
1160 596 : elog(DEBUG1, "executing extension script for \"%s\" version '%s'", control->name, version);
1161 : else
1162 546 : elog(DEBUG1, "executing extension script for \"%s\" update from version '%s' to '%s'", control->name, from_version, version);
1163 :
1164 : /*
1165 : * If installing a trusted extension on behalf of a non-superuser, become
1166 : * the bootstrap superuser. (This switch will be cleaned up automatically
1167 : * if the transaction aborts, as will the GUC changes below.)
1168 : */
1169 1142 : if (switch_to_superuser)
1170 : {
1171 8 : GetUserIdAndSecContext(&save_userid, &save_sec_context);
1172 8 : SetUserIdAndSecContext(BOOTSTRAP_SUPERUSERID,
1173 : save_sec_context | SECURITY_LOCAL_USERID_CHANGE);
1174 : }
1175 :
1176 : /*
1177 : * Force client_min_messages and log_min_messages to be at least WARNING,
1178 : * so that we won't spam the user with useless NOTICE messages from common
1179 : * script actions like creating shell types.
1180 : *
1181 : * We use the equivalent of a function SET option to allow the setting to
1182 : * persist for exactly the duration of the script execution. guc.c also
1183 : * takes care of undoing the setting on error.
1184 : *
1185 : * log_min_messages can't be set by ordinary users, so for that one we
1186 : * pretend to be superuser.
1187 : */
1188 1142 : save_nestlevel = NewGUCNestLevel();
1189 :
1190 1142 : if (client_min_messages < WARNING)
1191 1138 : (void) set_config_option("client_min_messages", "warning",
1192 : PGC_USERSET, PGC_S_SESSION,
1193 : GUC_ACTION_SAVE, true, 0, false);
1194 1142 : if (log_min_messages < WARNING)
1195 14 : (void) set_config_option_ext("log_min_messages", "warning",
1196 : PGC_SUSET, PGC_S_SESSION,
1197 : BOOTSTRAP_SUPERUSERID,
1198 : GUC_ACTION_SAVE, true, 0, false);
1199 :
1200 : /*
1201 : * Similarly disable check_function_bodies, to ensure that SQL functions
1202 : * won't be parsed during creation.
1203 : */
1204 1142 : if (check_function_bodies)
1205 1142 : (void) set_config_option("check_function_bodies", "off",
1206 : PGC_USERSET, PGC_S_SESSION,
1207 : GUC_ACTION_SAVE, true, 0, false);
1208 :
1209 : /*
1210 : * Set up the search path to have the target schema first, making it be
1211 : * the default creation target namespace. Then add the schemas of any
1212 : * prerequisite extensions, unless they are in pg_catalog which would be
1213 : * searched anyway. (Listing pg_catalog explicitly in a non-first
1214 : * position would be bad for security.) Finally add pg_temp to ensure
1215 : * that temp objects can't take precedence over others.
1216 : */
1217 1142 : initStringInfo(&pathbuf);
1218 1142 : appendStringInfoString(&pathbuf, quote_identifier(schemaName));
1219 1196 : foreach(lc, requiredSchemas)
1220 : {
1221 54 : Oid reqschema = lfirst_oid(lc);
1222 54 : char *reqname = get_namespace_name(reqschema);
1223 :
1224 54 : if (reqname && strcmp(reqname, "pg_catalog") != 0)
1225 34 : appendStringInfo(&pathbuf, ", %s", quote_identifier(reqname));
1226 : }
1227 1142 : appendStringInfoString(&pathbuf, ", pg_temp");
1228 :
1229 1142 : (void) set_config_option("search_path", pathbuf.data,
1230 : PGC_USERSET, PGC_S_SESSION,
1231 : GUC_ACTION_SAVE, true, 0, false);
1232 :
1233 : /*
1234 : * Set creating_extension and related variables so that
1235 : * recordDependencyOnCurrentExtension and other functions do the right
1236 : * things. On failure, ensure we reset these variables.
1237 : */
1238 1142 : creating_extension = true;
1239 1142 : CurrentExtensionObject = extensionOid;
1240 1142 : PG_TRY();
1241 : {
1242 1142 : char *c_sql = read_extension_script_file(control, filename);
1243 : Datum t_sql;
1244 :
1245 : /*
1246 : * We filter each substitution through quote_identifier(). When the
1247 : * arg contains one of the following characters, no one collection of
1248 : * quoting can work inside $$dollar-quoted string literals$$,
1249 : * 'single-quoted string literals', and outside of any literal. To
1250 : * avoid a security snare for extension authors, error on substitution
1251 : * for arguments containing these.
1252 : */
1253 1142 : const char *quoting_relevant_chars = "\"$'\\";
1254 :
1255 : /* We use various functions that want to operate on text datums */
1256 1142 : t_sql = CStringGetTextDatum(c_sql);
1257 :
1258 : /*
1259 : * Reduce any lines beginning with "\echo" to empty. This allows
1260 : * scripts to contain messages telling people not to run them via
1261 : * psql, which has been found to be necessary due to old habits.
1262 : */
1263 1142 : t_sql = DirectFunctionCall4Coll(textregexreplace,
1264 : C_COLLATION_OID,
1265 : t_sql,
1266 1142 : CStringGetTextDatum("^\\\\echo.*$"),
1267 1142 : CStringGetTextDatum(""),
1268 1142 : CStringGetTextDatum("ng"));
1269 :
1270 : /*
1271 : * If the script uses @extowner@, substitute the calling username.
1272 : */
1273 1142 : if (strstr(c_sql, "@extowner@"))
1274 : {
1275 114 : Oid uid = switch_to_superuser ? save_userid : GetUserId();
1276 114 : const char *userName = GetUserNameFromId(uid, false);
1277 114 : const char *qUserName = quote_identifier(userName);
1278 :
1279 114 : t_sql = DirectFunctionCall3Coll(replace_text,
1280 : C_COLLATION_OID,
1281 : t_sql,
1282 114 : CStringGetTextDatum("@extowner@"),
1283 114 : CStringGetTextDatum(qUserName));
1284 114 : if (strpbrk(userName, quoting_relevant_chars))
1285 0 : ereport(ERROR,
1286 : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1287 : errmsg("invalid character in extension owner: must not contain any of \"%s\"",
1288 : quoting_relevant_chars)));
1289 : }
1290 :
1291 : /*
1292 : * If it's not relocatable, substitute the target schema name for
1293 : * occurrences of @extschema@.
1294 : *
1295 : * For a relocatable extension, we needn't do this. There cannot be
1296 : * any need for @extschema@, else it wouldn't be relocatable.
1297 : */
1298 1142 : if (!control->relocatable)
1299 : {
1300 170 : Datum old = t_sql;
1301 170 : const char *qSchemaName = quote_identifier(schemaName);
1302 :
1303 170 : t_sql = DirectFunctionCall3Coll(replace_text,
1304 : C_COLLATION_OID,
1305 : t_sql,
1306 170 : CStringGetTextDatum("@extschema@"),
1307 170 : CStringGetTextDatum(qSchemaName));
1308 170 : if (t_sql != old && strpbrk(schemaName, quoting_relevant_chars))
1309 2 : ereport(ERROR,
1310 : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1311 : errmsg("invalid character in extension \"%s\" schema: must not contain any of \"%s\"",
1312 : control->name, quoting_relevant_chars)));
1313 : }
1314 :
1315 : /*
1316 : * Likewise, substitute required extensions' schema names for
1317 : * occurrences of @extschema:extension_name@.
1318 : */
1319 : Assert(list_length(control->requires) == list_length(requiredSchemas));
1320 1192 : forboth(lc, control->requires, lc2, requiredSchemas)
1321 : {
1322 54 : Datum old = t_sql;
1323 54 : char *reqextname = (char *) lfirst(lc);
1324 54 : Oid reqschema = lfirst_oid(lc2);
1325 54 : char *schemaName = get_namespace_name(reqschema);
1326 54 : const char *qSchemaName = quote_identifier(schemaName);
1327 : char *repltoken;
1328 :
1329 54 : repltoken = psprintf("@extschema:%s@", reqextname);
1330 54 : t_sql = DirectFunctionCall3Coll(replace_text,
1331 : C_COLLATION_OID,
1332 : t_sql,
1333 54 : CStringGetTextDatum(repltoken),
1334 54 : CStringGetTextDatum(qSchemaName));
1335 54 : if (t_sql != old && strpbrk(schemaName, quoting_relevant_chars))
1336 2 : ereport(ERROR,
1337 : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1338 : errmsg("invalid character in extension \"%s\" schema: must not contain any of \"%s\"",
1339 : reqextname, quoting_relevant_chars)));
1340 : }
1341 :
1342 : /*
1343 : * If module_pathname was set in the control file, substitute its
1344 : * value for occurrences of MODULE_PATHNAME.
1345 : */
1346 1138 : if (control->module_pathname)
1347 : {
1348 1042 : t_sql = DirectFunctionCall3Coll(replace_text,
1349 : C_COLLATION_OID,
1350 : t_sql,
1351 1042 : CStringGetTextDatum("MODULE_PATHNAME"),
1352 1042 : CStringGetTextDatum(control->module_pathname));
1353 : }
1354 :
1355 : /* And now back to C string */
1356 1138 : c_sql = text_to_cstring(DatumGetTextPP(t_sql));
1357 :
1358 1138 : execute_sql_string(c_sql, filename);
1359 : }
1360 30 : PG_FINALLY();
1361 : {
1362 1142 : creating_extension = false;
1363 1142 : CurrentExtensionObject = InvalidOid;
1364 : }
1365 1142 : PG_END_TRY();
1366 :
1367 : /*
1368 : * Restore the GUC variables we set above.
1369 : */
1370 1112 : AtEOXact_GUC(true, save_nestlevel);
1371 :
1372 : /*
1373 : * Restore authentication state if needed.
1374 : */
1375 1112 : if (switch_to_superuser)
1376 8 : SetUserIdAndSecContext(save_userid, save_sec_context);
1377 1112 : }
1378 :
1379 : /*
1380 : * Find or create an ExtensionVersionInfo for the specified version name
1381 : *
1382 : * Currently, we just use a List of the ExtensionVersionInfo's. Searching
1383 : * for them therefore uses about O(N^2) time when there are N versions of
1384 : * the extension. We could change the data structure to a hash table if
1385 : * this ever becomes a bottleneck.
1386 : */
1387 : static ExtensionVersionInfo *
1388 7106 : get_ext_ver_info(const char *versionname, List **evi_list)
1389 : {
1390 : ExtensionVersionInfo *evi;
1391 : ListCell *lc;
1392 :
1393 28892 : foreach(lc, *evi_list)
1394 : {
1395 24686 : evi = (ExtensionVersionInfo *) lfirst(lc);
1396 24686 : if (strcmp(evi->name, versionname) == 0)
1397 2900 : return evi;
1398 : }
1399 :
1400 4206 : evi = palloc_object(ExtensionVersionInfo);
1401 4206 : evi->name = pstrdup(versionname);
1402 4206 : evi->reachable = NIL;
1403 4206 : evi->installable = false;
1404 : /* initialize for later application of Dijkstra's algorithm */
1405 4206 : evi->distance_known = false;
1406 4206 : evi->distance = INT_MAX;
1407 4206 : evi->previous = NULL;
1408 :
1409 4206 : *evi_list = lappend(*evi_list, evi);
1410 :
1411 4206 : return evi;
1412 : }
1413 :
1414 : /*
1415 : * Locate the nearest unprocessed ExtensionVersionInfo
1416 : *
1417 : * This part of the algorithm is also about O(N^2). A priority queue would
1418 : * make it much faster, but for now there's no need.
1419 : */
1420 : static ExtensionVersionInfo *
1421 7228 : get_nearest_unprocessed_vertex(List *evi_list)
1422 : {
1423 7228 : ExtensionVersionInfo *evi = NULL;
1424 : ListCell *lc;
1425 :
1426 71414 : foreach(lc, evi_list)
1427 : {
1428 64186 : ExtensionVersionInfo *evi2 = (ExtensionVersionInfo *) lfirst(lc);
1429 :
1430 : /* only vertices whose distance is still uncertain are candidates */
1431 64186 : if (evi2->distance_known)
1432 16508 : continue;
1433 : /* remember the closest such vertex */
1434 47678 : if (evi == NULL ||
1435 40450 : evi->distance > evi2->distance)
1436 11834 : evi = evi2;
1437 : }
1438 :
1439 7228 : return evi;
1440 : }
1441 :
1442 : /*
1443 : * Obtain information about the set of update scripts available for the
1444 : * specified extension. The result is a List of ExtensionVersionInfo
1445 : * structs, each with a subsidiary list of the ExtensionVersionInfos for
1446 : * the versions that can be reached in one step from that version.
1447 : */
1448 : static List *
1449 1518 : get_ext_ver_list(ExtensionControlFile *control)
1450 : {
1451 1518 : List *evi_list = NIL;
1452 1518 : int extnamelen = strlen(control->name);
1453 : char *location;
1454 : DIR *dir;
1455 : struct dirent *de;
1456 :
1457 1518 : location = get_extension_script_directory(control);
1458 1518 : dir = AllocateDir(location);
1459 537702 : while ((de = ReadDir(dir, location)) != NULL)
1460 : {
1461 : char *vername;
1462 : char *vername2;
1463 : ExtensionVersionInfo *evi;
1464 : ExtensionVersionInfo *evi2;
1465 :
1466 : /* must be a .sql file ... */
1467 536184 : if (!is_extension_script_filename(de->d_name))
1468 170214 : continue;
1469 :
1470 : /* ... matching extension name followed by separator */
1471 365970 : if (strncmp(de->d_name, control->name, extnamelen) != 0 ||
1472 4360 : de->d_name[extnamelen] != '-' ||
1473 4206 : de->d_name[extnamelen + 1] != '-')
1474 361764 : continue;
1475 :
1476 : /* extract version name(s) from 'extname--something.sql' filename */
1477 4206 : vername = pstrdup(de->d_name + extnamelen + 2);
1478 4206 : *strrchr(vername, '.') = '\0';
1479 4206 : vername2 = strstr(vername, "--");
1480 4206 : if (!vername2)
1481 : {
1482 : /* It's an install, not update, script; record its version name */
1483 1518 : evi = get_ext_ver_info(vername, &evi_list);
1484 1518 : evi->installable = true;
1485 1518 : continue;
1486 : }
1487 2688 : *vername2 = '\0'; /* terminate first version */
1488 2688 : vername2 += 2; /* and point to second */
1489 :
1490 : /* if there's a third --, it's bogus, ignore it */
1491 2688 : if (strstr(vername2, "--"))
1492 0 : continue;
1493 :
1494 : /* Create ExtensionVersionInfos and link them together */
1495 2688 : evi = get_ext_ver_info(vername, &evi_list);
1496 2688 : evi2 = get_ext_ver_info(vername2, &evi_list);
1497 2688 : evi->reachable = lappend(evi->reachable, evi2);
1498 : }
1499 1518 : FreeDir(dir);
1500 :
1501 1518 : return evi_list;
1502 : }
1503 :
1504 : /*
1505 : * Given an initial and final version name, identify the sequence of update
1506 : * scripts that have to be applied to perform that update.
1507 : *
1508 : * Result is a List of names of versions to transition through (the initial
1509 : * version is *not* included).
1510 : */
1511 : static List *
1512 38 : identify_update_path(ExtensionControlFile *control,
1513 : const char *oldVersion, const char *newVersion)
1514 : {
1515 : List *result;
1516 : List *evi_list;
1517 : ExtensionVersionInfo *evi_start;
1518 : ExtensionVersionInfo *evi_target;
1519 :
1520 : /* Extract the version update graph from the script directory */
1521 38 : evi_list = get_ext_ver_list(control);
1522 :
1523 : /* Initialize start and end vertices */
1524 38 : evi_start = get_ext_ver_info(oldVersion, &evi_list);
1525 38 : evi_target = get_ext_ver_info(newVersion, &evi_list);
1526 :
1527 : /* Find shortest path */
1528 38 : result = find_update_path(evi_list, evi_start, evi_target, false, false);
1529 :
1530 38 : if (result == NIL)
1531 0 : ereport(ERROR,
1532 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1533 : errmsg("extension \"%s\" has no update path from version \"%s\" to version \"%s\"",
1534 : control->name, oldVersion, newVersion)));
1535 :
1536 38 : return result;
1537 : }
1538 :
1539 : /*
1540 : * Apply Dijkstra's algorithm to find the shortest path from evi_start to
1541 : * evi_target.
1542 : *
1543 : * If reject_indirect is true, ignore paths that go through installable
1544 : * versions. This saves work when the caller will consider starting from
1545 : * all installable versions anyway.
1546 : *
1547 : * If reinitialize is false, assume the ExtensionVersionInfo list has not
1548 : * been used for this before, and the initialization done by get_ext_ver_info
1549 : * is still good. Otherwise, reinitialize all transient fields used here.
1550 : *
1551 : * Result is a List of names of versions to transition through (the initial
1552 : * version is *not* included). Returns NIL if no such path.
1553 : */
1554 : static List *
1555 1758 : find_update_path(List *evi_list,
1556 : ExtensionVersionInfo *evi_start,
1557 : ExtensionVersionInfo *evi_target,
1558 : bool reject_indirect,
1559 : bool reinitialize)
1560 : {
1561 : List *result;
1562 : ExtensionVersionInfo *evi;
1563 : ListCell *lc;
1564 :
1565 : /* Caller error if start == target */
1566 : Assert(evi_start != evi_target);
1567 : /* Caller error if reject_indirect and target is installable */
1568 : Assert(!(reject_indirect && evi_target->installable));
1569 :
1570 1758 : if (reinitialize)
1571 : {
1572 14352 : foreach(lc, evi_list)
1573 : {
1574 12632 : evi = (ExtensionVersionInfo *) lfirst(lc);
1575 12632 : evi->distance_known = false;
1576 12632 : evi->distance = INT_MAX;
1577 12632 : evi->previous = NULL;
1578 : }
1579 : }
1580 :
1581 1758 : evi_start->distance = 0;
1582 :
1583 7228 : while ((evi = get_nearest_unprocessed_vertex(evi_list)) != NULL)
1584 : {
1585 7228 : if (evi->distance == INT_MAX)
1586 708 : break; /* all remaining vertices are unreachable */
1587 6520 : evi->distance_known = true;
1588 6520 : if (evi == evi_target)
1589 1050 : break; /* found shortest path to target */
1590 10246 : foreach(lc, evi->reachable)
1591 : {
1592 4776 : ExtensionVersionInfo *evi2 = (ExtensionVersionInfo *) lfirst(lc);
1593 : int newdist;
1594 :
1595 : /* if reject_indirect, treat installable versions as unreachable */
1596 4776 : if (reject_indirect && evi2->installable)
1597 0 : continue;
1598 4776 : newdist = evi->distance + 1;
1599 4776 : if (newdist < evi2->distance)
1600 : {
1601 4776 : evi2->distance = newdist;
1602 4776 : evi2->previous = evi;
1603 : }
1604 0 : else if (newdist == evi2->distance &&
1605 0 : evi2->previous != NULL &&
1606 0 : strcmp(evi->name, evi2->previous->name) < 0)
1607 : {
1608 : /*
1609 : * Break ties in favor of the version name that comes first
1610 : * according to strcmp(). This behavior is undocumented and
1611 : * users shouldn't rely on it. We do it just to ensure that
1612 : * if there is a tie, the update path that is chosen does not
1613 : * depend on random factors like the order in which directory
1614 : * entries get visited.
1615 : */
1616 0 : evi2->previous = evi;
1617 : }
1618 : }
1619 : }
1620 :
1621 : /* Return NIL if target is not reachable from start */
1622 1758 : if (!evi_target->distance_known)
1623 708 : return NIL;
1624 :
1625 : /* Build and return list of version names representing the update path */
1626 1050 : result = NIL;
1627 3914 : for (evi = evi_target; evi != evi_start; evi = evi->previous)
1628 2864 : result = lcons(evi->name, result);
1629 :
1630 1050 : return result;
1631 : }
1632 :
1633 : /*
1634 : * Given a target version that is not directly installable, find the
1635 : * best installation sequence starting from a directly-installable version.
1636 : *
1637 : * evi_list: previously-collected version update graph
1638 : * evi_target: member of that list that we want to reach
1639 : *
1640 : * Returns the best starting-point version, or NULL if there is none.
1641 : * On success, *best_path is set to the path from the start point.
1642 : *
1643 : * If there's more than one possible start point, prefer shorter update paths,
1644 : * and break any ties arbitrarily on the basis of strcmp'ing the starting
1645 : * versions' names.
1646 : */
1647 : static ExtensionVersionInfo *
1648 1720 : find_install_path(List *evi_list, ExtensionVersionInfo *evi_target,
1649 : List **best_path)
1650 : {
1651 1720 : ExtensionVersionInfo *evi_start = NULL;
1652 : ListCell *lc;
1653 :
1654 1720 : *best_path = NIL;
1655 :
1656 : /*
1657 : * We don't expect to be called for an installable target, but if we are,
1658 : * the answer is easy: just start from there, with an empty update path.
1659 : */
1660 1720 : if (evi_target->installable)
1661 0 : return evi_target;
1662 :
1663 : /* Consider all installable versions as start points */
1664 14352 : foreach(lc, evi_list)
1665 : {
1666 12632 : ExtensionVersionInfo *evi1 = (ExtensionVersionInfo *) lfirst(lc);
1667 : List *path;
1668 :
1669 12632 : if (!evi1->installable)
1670 10912 : continue;
1671 :
1672 : /*
1673 : * Find shortest path from evi1 to evi_target; but no need to consider
1674 : * paths going through other installable versions.
1675 : */
1676 1720 : path = find_update_path(evi_list, evi1, evi_target, true, true);
1677 1720 : if (path == NIL)
1678 708 : continue;
1679 :
1680 : /* Remember best path */
1681 1012 : if (evi_start == NULL ||
1682 0 : list_length(path) < list_length(*best_path) ||
1683 0 : (list_length(path) == list_length(*best_path) &&
1684 0 : strcmp(evi_start->name, evi1->name) < 0))
1685 : {
1686 1012 : evi_start = evi1;
1687 1012 : *best_path = path;
1688 : }
1689 : }
1690 :
1691 1720 : return evi_start;
1692 : }
1693 :
1694 : /*
1695 : * CREATE EXTENSION worker
1696 : *
1697 : * When CASCADE is specified, CreateExtensionInternal() recurses if required
1698 : * extensions need to be installed. To sanely handle cyclic dependencies,
1699 : * the "parents" list contains a list of names of extensions already being
1700 : * installed, allowing us to error out if we recurse to one of those.
1701 : */
1702 : static ObjectAddress
1703 618 : CreateExtensionInternal(char *extensionName,
1704 : char *schemaName,
1705 : const char *versionName,
1706 : bool cascade,
1707 : List *parents,
1708 : bool is_create)
1709 : {
1710 618 : char *origSchemaName = schemaName;
1711 618 : Oid schemaOid = InvalidOid;
1712 618 : Oid extowner = GetUserId();
1713 : ExtensionControlFile *pcontrol;
1714 : ExtensionControlFile *control;
1715 : char *filename;
1716 : struct stat fst;
1717 : List *updateVersions;
1718 : List *requiredExtensions;
1719 : List *requiredSchemas;
1720 : Oid extensionOid;
1721 : ObjectAddress address;
1722 : ListCell *lc;
1723 :
1724 : /*
1725 : * Read the primary control file. Note we assume that it does not contain
1726 : * any non-ASCII data, so there is no need to worry about encoding at this
1727 : * point.
1728 : */
1729 618 : pcontrol = read_extension_control_file(extensionName);
1730 :
1731 : /*
1732 : * Determine the version to install
1733 : */
1734 618 : if (versionName == NULL)
1735 : {
1736 608 : if (pcontrol->default_version)
1737 608 : versionName = pcontrol->default_version;
1738 : else
1739 0 : ereport(ERROR,
1740 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1741 : errmsg("version to install must be specified")));
1742 : }
1743 618 : check_valid_version_name(versionName);
1744 :
1745 : /*
1746 : * Figure out which script(s) we need to run to install the desired
1747 : * version of the extension. If we do not have a script that directly
1748 : * does what is needed, we try to find a sequence of update scripts that
1749 : * will get us there.
1750 : */
1751 618 : filename = get_extension_script_filename(pcontrol, NULL, versionName);
1752 618 : if (stat(filename, &fst) == 0)
1753 : {
1754 : /* Easy, no extra scripts */
1755 482 : updateVersions = NIL;
1756 : }
1757 : else
1758 : {
1759 : /* Look for best way to install this version */
1760 : List *evi_list;
1761 : ExtensionVersionInfo *evi_start;
1762 : ExtensionVersionInfo *evi_target;
1763 :
1764 : /* Extract the version update graph from the script directory */
1765 136 : evi_list = get_ext_ver_list(pcontrol);
1766 :
1767 : /* Identify the target version */
1768 136 : evi_target = get_ext_ver_info(versionName, &evi_list);
1769 :
1770 : /* Identify best path to reach target */
1771 136 : evi_start = find_install_path(evi_list, evi_target,
1772 : &updateVersions);
1773 :
1774 : /* Fail if no path ... */
1775 136 : if (evi_start == NULL)
1776 0 : ereport(ERROR,
1777 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1778 : errmsg("extension \"%s\" has no installation script nor update path for version \"%s\"",
1779 : pcontrol->name, versionName)));
1780 :
1781 : /* Otherwise, install best starting point and then upgrade */
1782 136 : versionName = evi_start->name;
1783 : }
1784 :
1785 : /*
1786 : * Fetch control parameters for installation target version
1787 : */
1788 618 : control = read_extension_aux_control_file(pcontrol, versionName);
1789 :
1790 : /*
1791 : * Determine the target schema to install the extension into
1792 : */
1793 618 : if (schemaName)
1794 : {
1795 : /* If the user is giving us the schema name, it must exist already. */
1796 56 : schemaOid = get_namespace_oid(schemaName, false);
1797 : }
1798 :
1799 614 : if (control->schema != NULL)
1800 : {
1801 : /*
1802 : * The extension is not relocatable and the author gave us a schema
1803 : * for it.
1804 : *
1805 : * Unless CASCADE parameter was given, it's an error to give a schema
1806 : * different from control->schema if control->schema is specified.
1807 : */
1808 152 : if (schemaName && strcmp(control->schema, schemaName) != 0 &&
1809 4 : !cascade)
1810 2 : ereport(ERROR,
1811 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1812 : errmsg("extension \"%s\" must be installed in schema \"%s\"",
1813 : control->name,
1814 : control->schema)));
1815 :
1816 : /* Always use the schema from control file for current extension. */
1817 150 : schemaName = control->schema;
1818 :
1819 : /* Find or create the schema in case it does not exist. */
1820 150 : schemaOid = get_namespace_oid(schemaName, true);
1821 :
1822 150 : if (!OidIsValid(schemaOid))
1823 : {
1824 6 : CreateSchemaStmt *csstmt = makeNode(CreateSchemaStmt);
1825 :
1826 6 : csstmt->schemaname = schemaName;
1827 6 : csstmt->authrole = NULL; /* will be created by current user */
1828 6 : csstmt->schemaElts = NIL;
1829 6 : csstmt->if_not_exists = false;
1830 6 : CreateSchemaCommand(csstmt, "(generated CREATE SCHEMA command)",
1831 : -1, -1);
1832 :
1833 : /*
1834 : * CreateSchemaCommand includes CommandCounterIncrement, so new
1835 : * schema is now visible.
1836 : */
1837 6 : schemaOid = get_namespace_oid(schemaName, false);
1838 : }
1839 : }
1840 462 : else if (!OidIsValid(schemaOid))
1841 : {
1842 : /*
1843 : * Neither user nor author of the extension specified schema; use the
1844 : * current default creation namespace, which is the first explicit
1845 : * entry in the search_path.
1846 : */
1847 414 : List *search_path = fetch_search_path(false);
1848 :
1849 414 : if (search_path == NIL) /* nothing valid in search_path? */
1850 0 : ereport(ERROR,
1851 : (errcode(ERRCODE_UNDEFINED_SCHEMA),
1852 : errmsg("no schema has been selected to create in")));
1853 414 : schemaOid = linitial_oid(search_path);
1854 414 : schemaName = get_namespace_name(schemaOid);
1855 414 : if (schemaName == NULL) /* recently-deleted namespace? */
1856 0 : ereport(ERROR,
1857 : (errcode(ERRCODE_UNDEFINED_SCHEMA),
1858 : errmsg("no schema has been selected to create in")));
1859 :
1860 414 : list_free(search_path);
1861 : }
1862 :
1863 : /*
1864 : * Make note if a temporary namespace has been accessed in this
1865 : * transaction.
1866 : */
1867 612 : if (isTempNamespace(schemaOid))
1868 4 : MyXactFlags |= XACT_FLAGS_ACCESSEDTEMPNAMESPACE;
1869 :
1870 : /*
1871 : * We don't check creation rights on the target namespace here. If the
1872 : * extension script actually creates any objects there, it will fail if
1873 : * the user doesn't have such permissions. But there are cases such as
1874 : * procedural languages where it's convenient to set schema = pg_catalog
1875 : * yet we don't want to restrict the command to users with ACL_CREATE for
1876 : * pg_catalog.
1877 : */
1878 :
1879 : /*
1880 : * Look up the prerequisite extensions, install them if necessary, and
1881 : * build lists of their OIDs and the OIDs of their target schemas.
1882 : */
1883 612 : requiredExtensions = NIL;
1884 612 : requiredSchemas = NIL;
1885 666 : foreach(lc, control->requires)
1886 : {
1887 64 : char *curreq = (char *) lfirst(lc);
1888 : Oid reqext;
1889 : Oid reqschema;
1890 :
1891 64 : reqext = get_required_extension(curreq,
1892 : extensionName,
1893 : origSchemaName,
1894 : cascade,
1895 : parents,
1896 : is_create);
1897 54 : reqschema = get_extension_schema(reqext);
1898 54 : requiredExtensions = lappend_oid(requiredExtensions, reqext);
1899 54 : requiredSchemas = lappend_oid(requiredSchemas, reqschema);
1900 : }
1901 :
1902 : /*
1903 : * Insert new tuple into pg_extension, and create dependency entries.
1904 : */
1905 602 : address = InsertExtensionTuple(control->name, extowner,
1906 602 : schemaOid, control->relocatable,
1907 : versionName,
1908 : PointerGetDatum(NULL),
1909 : PointerGetDatum(NULL),
1910 : requiredExtensions);
1911 602 : extensionOid = address.objectId;
1912 :
1913 : /*
1914 : * Apply any control-file comment on extension
1915 : */
1916 602 : if (control->comment != NULL)
1917 602 : CreateComments(extensionOid, ExtensionRelationId, 0, control->comment);
1918 :
1919 : /*
1920 : * Execute the installation script file
1921 : */
1922 602 : execute_extension_script(extensionOid, control,
1923 : NULL, versionName,
1924 : requiredSchemas,
1925 : schemaName);
1926 :
1927 : /*
1928 : * If additional update scripts have to be executed, apply the updates as
1929 : * though a series of ALTER EXTENSION UPDATE commands were given
1930 : */
1931 570 : ApplyExtensionUpdates(extensionOid, pcontrol,
1932 : versionName, updateVersions,
1933 : origSchemaName, cascade, is_create);
1934 :
1935 570 : return address;
1936 : }
1937 :
1938 : /*
1939 : * Get the OID of an extension listed in "requires", possibly creating it.
1940 : */
1941 : static Oid
1942 66 : get_required_extension(char *reqExtensionName,
1943 : char *extensionName,
1944 : char *origSchemaName,
1945 : bool cascade,
1946 : List *parents,
1947 : bool is_create)
1948 : {
1949 : Oid reqExtensionOid;
1950 :
1951 66 : reqExtensionOid = get_extension_oid(reqExtensionName, true);
1952 66 : if (!OidIsValid(reqExtensionOid))
1953 : {
1954 44 : if (cascade)
1955 : {
1956 : /* Must install it. */
1957 : ObjectAddress addr;
1958 : List *cascade_parents;
1959 : ListCell *lc;
1960 :
1961 : /* Check extension name validity before trying to cascade. */
1962 40 : check_valid_extension_name(reqExtensionName);
1963 :
1964 : /* Check for cyclic dependency between extensions. */
1965 44 : foreach(lc, parents)
1966 : {
1967 6 : char *pname = (char *) lfirst(lc);
1968 :
1969 6 : if (strcmp(pname, reqExtensionName) == 0)
1970 2 : ereport(ERROR,
1971 : (errcode(ERRCODE_INVALID_RECURSION),
1972 : errmsg("cyclic dependency detected between extensions \"%s\" and \"%s\"",
1973 : reqExtensionName, extensionName)));
1974 : }
1975 :
1976 38 : ereport(NOTICE,
1977 : (errmsg("installing required extension \"%s\"",
1978 : reqExtensionName)));
1979 :
1980 : /* Add current extension to list of parents to pass down. */
1981 38 : cascade_parents = lappend(list_copy(parents), extensionName);
1982 :
1983 : /*
1984 : * Create the required extension. We propagate the SCHEMA option
1985 : * if any, and CASCADE, but no other options.
1986 : */
1987 38 : addr = CreateExtensionInternal(reqExtensionName,
1988 : origSchemaName,
1989 : NULL,
1990 : cascade,
1991 : cascade_parents,
1992 : is_create);
1993 :
1994 : /* Get its newly-assigned OID. */
1995 34 : reqExtensionOid = addr.objectId;
1996 : }
1997 : else
1998 4 : ereport(ERROR,
1999 : (errcode(ERRCODE_UNDEFINED_OBJECT),
2000 : errmsg("required extension \"%s\" is not installed",
2001 : reqExtensionName),
2002 : is_create ?
2003 : errhint("Use CREATE EXTENSION ... CASCADE to install required extensions too.") : 0));
2004 : }
2005 :
2006 56 : return reqExtensionOid;
2007 : }
2008 :
2009 : /*
2010 : * CREATE EXTENSION
2011 : */
2012 : ObjectAddress
2013 582 : CreateExtension(ParseState *pstate, CreateExtensionStmt *stmt)
2014 : {
2015 582 : DefElem *d_schema = NULL;
2016 582 : DefElem *d_new_version = NULL;
2017 582 : DefElem *d_cascade = NULL;
2018 582 : char *schemaName = NULL;
2019 582 : char *versionName = NULL;
2020 582 : bool cascade = false;
2021 : ListCell *lc;
2022 :
2023 : /* Check extension name validity before any filesystem access */
2024 582 : check_valid_extension_name(stmt->extname);
2025 :
2026 : /*
2027 : * Check for duplicate extension name. The unique index on
2028 : * pg_extension.extname would catch this anyway, and serves as a backstop
2029 : * in case of race conditions; but this is a friendlier error message, and
2030 : * besides we need a check to support IF NOT EXISTS.
2031 : */
2032 582 : if (get_extension_oid(stmt->extname, true) != InvalidOid)
2033 : {
2034 2 : if (stmt->if_not_exists)
2035 : {
2036 2 : ereport(NOTICE,
2037 : (errcode(ERRCODE_DUPLICATE_OBJECT),
2038 : errmsg("extension \"%s\" already exists, skipping",
2039 : stmt->extname)));
2040 2 : return InvalidObjectAddress;
2041 : }
2042 : else
2043 0 : ereport(ERROR,
2044 : (errcode(ERRCODE_DUPLICATE_OBJECT),
2045 : errmsg("extension \"%s\" already exists",
2046 : stmt->extname)));
2047 : }
2048 :
2049 : /*
2050 : * We use global variables to track the extension being created, so we can
2051 : * create only one extension at the same time.
2052 : */
2053 580 : if (creating_extension)
2054 0 : ereport(ERROR,
2055 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2056 : errmsg("nested CREATE EXTENSION is not supported")));
2057 :
2058 : /* Deconstruct the statement option list */
2059 676 : foreach(lc, stmt->options)
2060 : {
2061 96 : DefElem *defel = (DefElem *) lfirst(lc);
2062 :
2063 96 : if (strcmp(defel->defname, "schema") == 0)
2064 : {
2065 46 : if (d_schema)
2066 0 : errorConflictingDefElem(defel, pstate);
2067 46 : d_schema = defel;
2068 46 : schemaName = defGetString(d_schema);
2069 : }
2070 50 : else if (strcmp(defel->defname, "new_version") == 0)
2071 : {
2072 10 : if (d_new_version)
2073 0 : errorConflictingDefElem(defel, pstate);
2074 10 : d_new_version = defel;
2075 10 : versionName = defGetString(d_new_version);
2076 : }
2077 40 : else if (strcmp(defel->defname, "cascade") == 0)
2078 : {
2079 40 : if (d_cascade)
2080 0 : errorConflictingDefElem(defel, pstate);
2081 40 : d_cascade = defel;
2082 40 : cascade = defGetBoolean(d_cascade);
2083 : }
2084 : else
2085 0 : elog(ERROR, "unrecognized option: %s", defel->defname);
2086 : }
2087 :
2088 : /* Call CreateExtensionInternal to do the real work. */
2089 580 : return CreateExtensionInternal(stmt->extname,
2090 : schemaName,
2091 : versionName,
2092 : cascade,
2093 : NIL,
2094 : true);
2095 : }
2096 :
2097 : /*
2098 : * InsertExtensionTuple
2099 : *
2100 : * Insert the new pg_extension row, and create extension's dependency entries.
2101 : * Return the OID assigned to the new row.
2102 : *
2103 : * This is exported for the benefit of pg_upgrade, which has to create a
2104 : * pg_extension entry (and the extension-level dependencies) without
2105 : * actually running the extension's script.
2106 : *
2107 : * extConfig and extCondition should be arrays or PointerGetDatum(NULL).
2108 : * We declare them as plain Datum to avoid needing array.h in extension.h.
2109 : */
2110 : ObjectAddress
2111 610 : InsertExtensionTuple(const char *extName, Oid extOwner,
2112 : Oid schemaOid, bool relocatable, const char *extVersion,
2113 : Datum extConfig, Datum extCondition,
2114 : List *requiredExtensions)
2115 : {
2116 : Oid extensionOid;
2117 : Relation rel;
2118 : Datum values[Natts_pg_extension];
2119 : bool nulls[Natts_pg_extension];
2120 : HeapTuple tuple;
2121 : ObjectAddress myself;
2122 : ObjectAddress nsp;
2123 : ObjectAddresses *refobjs;
2124 : ListCell *lc;
2125 :
2126 : /*
2127 : * Build and insert the pg_extension tuple
2128 : */
2129 610 : rel = table_open(ExtensionRelationId, RowExclusiveLock);
2130 :
2131 610 : memset(values, 0, sizeof(values));
2132 610 : memset(nulls, 0, sizeof(nulls));
2133 :
2134 610 : extensionOid = GetNewOidWithIndex(rel, ExtensionOidIndexId,
2135 : Anum_pg_extension_oid);
2136 610 : values[Anum_pg_extension_oid - 1] = ObjectIdGetDatum(extensionOid);
2137 610 : values[Anum_pg_extension_extname - 1] =
2138 610 : DirectFunctionCall1(namein, CStringGetDatum(extName));
2139 610 : values[Anum_pg_extension_extowner - 1] = ObjectIdGetDatum(extOwner);
2140 610 : values[Anum_pg_extension_extnamespace - 1] = ObjectIdGetDatum(schemaOid);
2141 610 : values[Anum_pg_extension_extrelocatable - 1] = BoolGetDatum(relocatable);
2142 610 : values[Anum_pg_extension_extversion - 1] = CStringGetTextDatum(extVersion);
2143 :
2144 610 : if (extConfig == PointerGetDatum(NULL))
2145 610 : nulls[Anum_pg_extension_extconfig - 1] = true;
2146 : else
2147 0 : values[Anum_pg_extension_extconfig - 1] = extConfig;
2148 :
2149 610 : if (extCondition == PointerGetDatum(NULL))
2150 610 : nulls[Anum_pg_extension_extcondition - 1] = true;
2151 : else
2152 0 : values[Anum_pg_extension_extcondition - 1] = extCondition;
2153 :
2154 610 : tuple = heap_form_tuple(rel->rd_att, values, nulls);
2155 :
2156 610 : CatalogTupleInsert(rel, tuple);
2157 :
2158 610 : heap_freetuple(tuple);
2159 610 : table_close(rel, RowExclusiveLock);
2160 :
2161 : /*
2162 : * Record dependencies on owner, schema, and prerequisite extensions
2163 : */
2164 610 : recordDependencyOnOwner(ExtensionRelationId, extensionOid, extOwner);
2165 :
2166 610 : refobjs = new_object_addresses();
2167 :
2168 610 : ObjectAddressSet(myself, ExtensionRelationId, extensionOid);
2169 :
2170 610 : ObjectAddressSet(nsp, NamespaceRelationId, schemaOid);
2171 610 : add_exact_object_address(&nsp, refobjs);
2172 :
2173 662 : foreach(lc, requiredExtensions)
2174 : {
2175 52 : Oid reqext = lfirst_oid(lc);
2176 : ObjectAddress otherext;
2177 :
2178 52 : ObjectAddressSet(otherext, ExtensionRelationId, reqext);
2179 52 : add_exact_object_address(&otherext, refobjs);
2180 : }
2181 :
2182 : /* Record all of them (this includes duplicate elimination) */
2183 610 : record_object_address_dependencies(&myself, refobjs, DEPENDENCY_NORMAL);
2184 610 : free_object_addresses(refobjs);
2185 :
2186 : /* Post creation hook for new extension */
2187 610 : InvokeObjectPostCreateHook(ExtensionRelationId, extensionOid, 0);
2188 :
2189 610 : return myself;
2190 : }
2191 :
2192 : /*
2193 : * Guts of extension deletion.
2194 : *
2195 : * All we need do here is remove the pg_extension tuple itself. Everything
2196 : * else is taken care of by the dependency infrastructure.
2197 : */
2198 : void
2199 164 : RemoveExtensionById(Oid extId)
2200 : {
2201 : Relation rel;
2202 : SysScanDesc scandesc;
2203 : HeapTuple tuple;
2204 : ScanKeyData entry[1];
2205 :
2206 : /*
2207 : * Disallow deletion of any extension that's currently open for insertion;
2208 : * else subsequent executions of recordDependencyOnCurrentExtension()
2209 : * could create dangling pg_depend records that refer to a no-longer-valid
2210 : * pg_extension OID. This is needed not so much because we think people
2211 : * might write "DROP EXTENSION foo" in foo's own script files, as because
2212 : * errors in dependency management in extension script files could give
2213 : * rise to cases where an extension is dropped as a result of recursing
2214 : * from some contained object. Because of that, we must test for the case
2215 : * here, not at some higher level of the DROP EXTENSION command.
2216 : */
2217 164 : if (extId == CurrentExtensionObject)
2218 0 : ereport(ERROR,
2219 : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2220 : errmsg("cannot drop extension \"%s\" because it is being modified",
2221 : get_extension_name(extId))));
2222 :
2223 164 : rel = table_open(ExtensionRelationId, RowExclusiveLock);
2224 :
2225 164 : ScanKeyInit(&entry[0],
2226 : Anum_pg_extension_oid,
2227 : BTEqualStrategyNumber, F_OIDEQ,
2228 : ObjectIdGetDatum(extId));
2229 164 : scandesc = systable_beginscan(rel, ExtensionOidIndexId, true,
2230 : NULL, 1, entry);
2231 :
2232 164 : tuple = systable_getnext(scandesc);
2233 :
2234 : /* We assume that there can be at most one matching tuple */
2235 164 : if (HeapTupleIsValid(tuple))
2236 164 : CatalogTupleDelete(rel, &tuple->t_self);
2237 :
2238 164 : systable_endscan(scandesc);
2239 :
2240 164 : table_close(rel, RowExclusiveLock);
2241 164 : }
2242 :
2243 : /*
2244 : * This function lists the available extensions (one row per primary control
2245 : * file in the control directory). We parse each control file and report the
2246 : * interesting fields.
2247 : *
2248 : * The system view pg_available_extensions provides a user interface to this
2249 : * SRF, adding information about whether the extensions are installed in the
2250 : * current DB.
2251 : */
2252 : Datum
2253 124 : pg_available_extensions(PG_FUNCTION_ARGS)
2254 : {
2255 124 : ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
2256 : List *locations;
2257 : DIR *dir;
2258 : struct dirent *de;
2259 124 : List *found_ext = NIL;
2260 :
2261 : /* Build tuplestore to hold the result rows */
2262 124 : InitMaterializedSRF(fcinfo, 0);
2263 :
2264 124 : locations = get_extension_control_directories();
2265 :
2266 388 : foreach_ptr(ExtensionLocation, location, locations)
2267 : {
2268 140 : dir = AllocateDir(location->loc);
2269 :
2270 : /*
2271 : * If the control directory doesn't exist, we want to silently return
2272 : * an empty set. Any other error will be reported by ReadDir.
2273 : */
2274 140 : if (dir == NULL && errno == ENOENT)
2275 : {
2276 : /* do nothing */
2277 : }
2278 : else
2279 : {
2280 44356 : while ((de = ReadDir(dir, location->loc)) != NULL)
2281 : {
2282 : ExtensionControlFile *control;
2283 : char *extname;
2284 : String *extname_str;
2285 : Datum values[4];
2286 : bool nulls[4];
2287 :
2288 44216 : if (!is_extension_control_filename(de->d_name))
2289 30436 : continue;
2290 :
2291 : /* extract extension name from 'name.control' filename */
2292 13788 : extname = pstrdup(de->d_name);
2293 13788 : *strrchr(extname, '.') = '\0';
2294 :
2295 : /* ignore it if it's an auxiliary control file */
2296 13788 : if (strstr(extname, "--"))
2297 0 : continue;
2298 :
2299 : /*
2300 : * Ignore already-found names. They are not reachable by the
2301 : * path search, so don't show them.
2302 : */
2303 13788 : extname_str = makeString(extname);
2304 13788 : if (list_member(found_ext, extname_str))
2305 8 : continue;
2306 : else
2307 13780 : found_ext = lappend(found_ext, extname_str);
2308 :
2309 13780 : control = new_ExtensionControlFile(extname);
2310 13780 : control->control_dir = pstrdup(location->loc);
2311 13780 : parse_extension_control_file(control, NULL);
2312 :
2313 13780 : memset(values, 0, sizeof(values));
2314 13780 : memset(nulls, 0, sizeof(nulls));
2315 :
2316 : /* name */
2317 13780 : values[0] = DirectFunctionCall1(namein,
2318 : CStringGetDatum(control->name));
2319 : /* default_version */
2320 13780 : if (control->default_version == NULL)
2321 0 : nulls[1] = true;
2322 : else
2323 13780 : values[1] = CStringGetTextDatum(control->default_version);
2324 :
2325 : /* location */
2326 13780 : values[2] = CStringGetTextDatum(get_extension_location(location));
2327 :
2328 : /* comment */
2329 13780 : if (control->comment == NULL)
2330 0 : nulls[3] = true;
2331 : else
2332 13780 : values[3] = CStringGetTextDatum(control->comment);
2333 :
2334 13780 : tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
2335 : values, nulls);
2336 : }
2337 :
2338 140 : FreeDir(dir);
2339 : }
2340 : }
2341 :
2342 124 : return (Datum) 0;
2343 : }
2344 :
2345 : /*
2346 : * This function lists the available extension versions (one row per
2347 : * extension installation script). For each version, we parse the related
2348 : * control file(s) and report the interesting fields.
2349 : *
2350 : * The system view pg_available_extension_versions provides a user interface
2351 : * to this SRF, adding information about which versions are installed in the
2352 : * current DB.
2353 : */
2354 : Datum
2355 12 : pg_available_extension_versions(PG_FUNCTION_ARGS)
2356 : {
2357 12 : ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
2358 : List *locations;
2359 : DIR *dir;
2360 : struct dirent *de;
2361 12 : List *found_ext = NIL;
2362 :
2363 : /* Build tuplestore to hold the result rows */
2364 12 : InitMaterializedSRF(fcinfo, 0);
2365 :
2366 12 : locations = get_extension_control_directories();
2367 :
2368 48 : foreach_ptr(ExtensionLocation, location, locations)
2369 : {
2370 24 : dir = AllocateDir(location->loc);
2371 :
2372 : /*
2373 : * If the control directory doesn't exist, we want to silently return
2374 : * an empty set. Any other error will be reported by ReadDir.
2375 : */
2376 24 : if (dir == NULL && errno == ENOENT)
2377 : {
2378 : /* do nothing */
2379 : }
2380 : else
2381 : {
2382 4350 : while ((de = ReadDir(dir, location->loc)) != NULL)
2383 : {
2384 : ExtensionControlFile *control;
2385 : char *extname;
2386 : String *extname_str;
2387 :
2388 4326 : if (!is_extension_control_filename(de->d_name))
2389 2976 : continue;
2390 :
2391 : /* extract extension name from 'name.control' filename */
2392 1350 : extname = pstrdup(de->d_name);
2393 1350 : *strrchr(extname, '.') = '\0';
2394 :
2395 : /* ignore it if it's an auxiliary control file */
2396 1350 : if (strstr(extname, "--"))
2397 0 : continue;
2398 :
2399 : /*
2400 : * Ignore already-found names. They are not reachable by the
2401 : * path search, so don't shown them.
2402 : */
2403 1350 : extname_str = makeString(extname);
2404 1350 : if (list_member(found_ext, extname_str))
2405 6 : continue;
2406 : else
2407 1344 : found_ext = lappend(found_ext, extname_str);
2408 :
2409 : /* read the control file */
2410 1344 : control = new_ExtensionControlFile(extname);
2411 1344 : control->control_dir = pstrdup(location->loc);
2412 1344 : parse_extension_control_file(control, NULL);
2413 :
2414 : /* scan extension's script directory for install scripts */
2415 1344 : get_available_versions_for_extension(control, rsinfo->setResult,
2416 : rsinfo->setDesc,
2417 : location);
2418 : }
2419 :
2420 24 : FreeDir(dir);
2421 : }
2422 : }
2423 :
2424 12 : return (Datum) 0;
2425 : }
2426 :
2427 : /*
2428 : * Inner loop for pg_available_extension_versions:
2429 : * read versions of one extension, add rows to tupstore
2430 : */
2431 : static void
2432 1344 : get_available_versions_for_extension(ExtensionControlFile *pcontrol,
2433 : Tuplestorestate *tupstore,
2434 : TupleDesc tupdesc,
2435 : ExtensionLocation *location)
2436 : {
2437 : List *evi_list;
2438 : ListCell *lc;
2439 :
2440 : /* Extract the version update graph from the script directory */
2441 1344 : evi_list = get_ext_ver_list(pcontrol);
2442 :
2443 : /* For each installable version ... */
2444 4272 : foreach(lc, evi_list)
2445 : {
2446 2928 : ExtensionVersionInfo *evi = (ExtensionVersionInfo *) lfirst(lc);
2447 : ExtensionControlFile *control;
2448 : Datum values[9];
2449 : bool nulls[9];
2450 : ListCell *lc2;
2451 :
2452 2928 : if (!evi->installable)
2453 1584 : continue;
2454 :
2455 : /*
2456 : * Fetch parameters for specific version (pcontrol is not changed)
2457 : */
2458 1344 : control = read_extension_aux_control_file(pcontrol, evi->name);
2459 :
2460 1344 : memset(values, 0, sizeof(values));
2461 1344 : memset(nulls, 0, sizeof(nulls));
2462 :
2463 : /* name */
2464 1344 : values[0] = DirectFunctionCall1(namein,
2465 : CStringGetDatum(control->name));
2466 : /* version */
2467 1344 : values[1] = CStringGetTextDatum(evi->name);
2468 : /* superuser */
2469 1344 : values[2] = BoolGetDatum(control->superuser);
2470 : /* trusted */
2471 1344 : values[3] = BoolGetDatum(control->trusted);
2472 : /* relocatable */
2473 1344 : values[4] = BoolGetDatum(control->relocatable);
2474 : /* schema */
2475 1344 : if (control->schema == NULL)
2476 1212 : nulls[5] = true;
2477 : else
2478 132 : values[5] = DirectFunctionCall1(namein,
2479 : CStringGetDatum(control->schema));
2480 : /* requires */
2481 1344 : if (control->requires == NIL)
2482 1140 : nulls[6] = true;
2483 : else
2484 204 : values[6] = convert_requires_to_datum(control->requires);
2485 :
2486 : /* location */
2487 1344 : values[7] = CStringGetTextDatum(get_extension_location(location));
2488 :
2489 : /* comment */
2490 1344 : if (control->comment == NULL)
2491 0 : nulls[8] = true;
2492 : else
2493 1344 : values[8] = CStringGetTextDatum(control->comment);
2494 :
2495 1344 : tuplestore_putvalues(tupstore, tupdesc, values, nulls);
2496 :
2497 : /*
2498 : * Find all non-directly-installable versions that would be installed
2499 : * starting from this version, and report them, inheriting the
2500 : * parameters that aren't changed in updates from this version.
2501 : */
2502 4272 : foreach(lc2, evi_list)
2503 : {
2504 2928 : ExtensionVersionInfo *evi2 = (ExtensionVersionInfo *) lfirst(lc2);
2505 : List *best_path;
2506 :
2507 2928 : if (evi2->installable)
2508 1344 : continue;
2509 1584 : if (find_install_path(evi_list, evi2, &best_path) == evi)
2510 : {
2511 : /*
2512 : * Fetch parameters for this version (pcontrol is not changed)
2513 : */
2514 876 : control = read_extension_aux_control_file(pcontrol, evi2->name);
2515 :
2516 : /* name stays the same */
2517 : /* version */
2518 876 : values[1] = CStringGetTextDatum(evi2->name);
2519 : /* superuser */
2520 876 : values[2] = BoolGetDatum(control->superuser);
2521 : /* trusted */
2522 876 : values[3] = BoolGetDatum(control->trusted);
2523 : /* relocatable */
2524 876 : values[4] = BoolGetDatum(control->relocatable);
2525 : /* schema stays the same */
2526 : /* requires */
2527 876 : if (control->requires == NIL)
2528 864 : nulls[6] = true;
2529 : else
2530 : {
2531 12 : values[6] = convert_requires_to_datum(control->requires);
2532 12 : nulls[6] = false;
2533 : }
2534 : /* comment and location stay the same */
2535 :
2536 876 : tuplestore_putvalues(tupstore, tupdesc, values, nulls);
2537 : }
2538 : }
2539 : }
2540 1344 : }
2541 :
2542 : /*
2543 : * Test whether the given extension exists (not whether it's installed)
2544 : *
2545 : * This checks for the existence of a matching control file in the extension
2546 : * directory. That's not a bulletproof check, since the file might be
2547 : * invalid, but this is only used for hints so it doesn't have to be 100%
2548 : * right.
2549 : */
2550 : bool
2551 0 : extension_file_exists(const char *extensionName)
2552 : {
2553 0 : bool result = false;
2554 : List *locations;
2555 : DIR *dir;
2556 : struct dirent *de;
2557 :
2558 0 : locations = get_extension_control_directories();
2559 :
2560 0 : foreach_ptr(char, location, locations)
2561 : {
2562 0 : dir = AllocateDir(location);
2563 :
2564 : /*
2565 : * If the control directory doesn't exist, we want to silently return
2566 : * false. Any other error will be reported by ReadDir.
2567 : */
2568 0 : if (dir == NULL && errno == ENOENT)
2569 : {
2570 : /* do nothing */
2571 : }
2572 : else
2573 : {
2574 0 : while ((de = ReadDir(dir, location)) != NULL)
2575 : {
2576 : char *extname;
2577 :
2578 0 : if (!is_extension_control_filename(de->d_name))
2579 0 : continue;
2580 :
2581 : /* extract extension name from 'name.control' filename */
2582 0 : extname = pstrdup(de->d_name);
2583 0 : *strrchr(extname, '.') = '\0';
2584 :
2585 : /* ignore it if it's an auxiliary control file */
2586 0 : if (strstr(extname, "--"))
2587 0 : continue;
2588 :
2589 : /* done if it matches request */
2590 0 : if (strcmp(extname, extensionName) == 0)
2591 : {
2592 0 : result = true;
2593 0 : break;
2594 : }
2595 : }
2596 :
2597 0 : FreeDir(dir);
2598 : }
2599 0 : if (result)
2600 0 : break;
2601 : }
2602 :
2603 0 : return result;
2604 : }
2605 :
2606 : /*
2607 : * Convert a list of extension names to a name[] Datum
2608 : */
2609 : static Datum
2610 216 : convert_requires_to_datum(List *requires)
2611 : {
2612 : Datum *datums;
2613 : int ndatums;
2614 : ArrayType *a;
2615 : ListCell *lc;
2616 :
2617 216 : ndatums = list_length(requires);
2618 216 : datums = (Datum *) palloc(ndatums * sizeof(Datum));
2619 216 : ndatums = 0;
2620 516 : foreach(lc, requires)
2621 : {
2622 300 : char *curreq = (char *) lfirst(lc);
2623 :
2624 300 : datums[ndatums++] =
2625 300 : DirectFunctionCall1(namein, CStringGetDatum(curreq));
2626 : }
2627 216 : a = construct_array_builtin(datums, ndatums, NAMEOID);
2628 216 : return PointerGetDatum(a);
2629 : }
2630 :
2631 : /*
2632 : * This function reports the version update paths that exist for the
2633 : * specified extension.
2634 : */
2635 : Datum
2636 0 : pg_extension_update_paths(PG_FUNCTION_ARGS)
2637 : {
2638 0 : Name extname = PG_GETARG_NAME(0);
2639 0 : ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
2640 : List *evi_list;
2641 : ExtensionControlFile *control;
2642 : ListCell *lc1;
2643 :
2644 : /* Check extension name validity before any filesystem access */
2645 0 : check_valid_extension_name(NameStr(*extname));
2646 :
2647 : /* Build tuplestore to hold the result rows */
2648 0 : InitMaterializedSRF(fcinfo, 0);
2649 :
2650 : /* Read the extension's control file */
2651 0 : control = read_extension_control_file(NameStr(*extname));
2652 :
2653 : /* Extract the version update graph from the script directory */
2654 0 : evi_list = get_ext_ver_list(control);
2655 :
2656 : /* Iterate over all pairs of versions */
2657 0 : foreach(lc1, evi_list)
2658 : {
2659 0 : ExtensionVersionInfo *evi1 = (ExtensionVersionInfo *) lfirst(lc1);
2660 : ListCell *lc2;
2661 :
2662 0 : foreach(lc2, evi_list)
2663 : {
2664 0 : ExtensionVersionInfo *evi2 = (ExtensionVersionInfo *) lfirst(lc2);
2665 : List *path;
2666 : Datum values[3];
2667 : bool nulls[3];
2668 :
2669 0 : if (evi1 == evi2)
2670 0 : continue;
2671 :
2672 : /* Find shortest path from evi1 to evi2 */
2673 0 : path = find_update_path(evi_list, evi1, evi2, false, true);
2674 :
2675 : /* Emit result row */
2676 0 : memset(values, 0, sizeof(values));
2677 0 : memset(nulls, 0, sizeof(nulls));
2678 :
2679 : /* source */
2680 0 : values[0] = CStringGetTextDatum(evi1->name);
2681 : /* target */
2682 0 : values[1] = CStringGetTextDatum(evi2->name);
2683 : /* path */
2684 0 : if (path == NIL)
2685 0 : nulls[2] = true;
2686 : else
2687 : {
2688 : StringInfoData pathbuf;
2689 : ListCell *lcv;
2690 :
2691 0 : initStringInfo(&pathbuf);
2692 : /* The path doesn't include start vertex, but show it */
2693 0 : appendStringInfoString(&pathbuf, evi1->name);
2694 0 : foreach(lcv, path)
2695 : {
2696 0 : char *versionName = (char *) lfirst(lcv);
2697 :
2698 0 : appendStringInfoString(&pathbuf, "--");
2699 0 : appendStringInfoString(&pathbuf, versionName);
2700 : }
2701 0 : values[2] = CStringGetTextDatum(pathbuf.data);
2702 0 : pfree(pathbuf.data);
2703 : }
2704 :
2705 0 : tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
2706 : values, nulls);
2707 : }
2708 : }
2709 :
2710 0 : return (Datum) 0;
2711 : }
2712 :
2713 : /*
2714 : * pg_extension_config_dump
2715 : *
2716 : * Record information about a configuration table that belongs to an
2717 : * extension being created, but whose contents should be dumped in whole
2718 : * or in part during pg_dump.
2719 : */
2720 : Datum
2721 12 : pg_extension_config_dump(PG_FUNCTION_ARGS)
2722 : {
2723 12 : Oid tableoid = PG_GETARG_OID(0);
2724 12 : text *wherecond = PG_GETARG_TEXT_PP(1);
2725 : char *tablename;
2726 : Relation extRel;
2727 : ScanKeyData key[1];
2728 : SysScanDesc extScan;
2729 : HeapTuple extTup;
2730 : Datum arrayDatum;
2731 : Datum elementDatum;
2732 : int arrayLength;
2733 : int arrayIndex;
2734 : bool isnull;
2735 : Datum repl_val[Natts_pg_extension];
2736 : bool repl_null[Natts_pg_extension];
2737 : bool repl_repl[Natts_pg_extension];
2738 : ArrayType *a;
2739 :
2740 : /*
2741 : * We only allow this to be called from an extension's SQL script. We
2742 : * shouldn't need any permissions check beyond that.
2743 : */
2744 12 : if (!creating_extension)
2745 0 : ereport(ERROR,
2746 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2747 : errmsg("%s can only be called from an SQL script executed by CREATE EXTENSION",
2748 : "pg_extension_config_dump()")));
2749 :
2750 : /*
2751 : * Check that the table exists and is a member of the extension being
2752 : * created. This ensures that we don't need to register an additional
2753 : * dependency to protect the extconfig entry.
2754 : */
2755 12 : tablename = get_rel_name(tableoid);
2756 12 : if (tablename == NULL)
2757 0 : ereport(ERROR,
2758 : (errcode(ERRCODE_UNDEFINED_TABLE),
2759 : errmsg("OID %u does not refer to a table", tableoid)));
2760 12 : if (getExtensionOfObject(RelationRelationId, tableoid) !=
2761 : CurrentExtensionObject)
2762 0 : ereport(ERROR,
2763 : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2764 : errmsg("table \"%s\" is not a member of the extension being created",
2765 : tablename)));
2766 :
2767 : /*
2768 : * Add the table OID and WHERE condition to the extension's extconfig and
2769 : * extcondition arrays.
2770 : *
2771 : * If the table is already in extconfig, treat this as an update of the
2772 : * WHERE condition.
2773 : */
2774 :
2775 : /* Find the pg_extension tuple */
2776 12 : extRel = table_open(ExtensionRelationId, RowExclusiveLock);
2777 :
2778 12 : ScanKeyInit(&key[0],
2779 : Anum_pg_extension_oid,
2780 : BTEqualStrategyNumber, F_OIDEQ,
2781 : ObjectIdGetDatum(CurrentExtensionObject));
2782 :
2783 12 : extScan = systable_beginscan(extRel, ExtensionOidIndexId, true,
2784 : NULL, 1, key);
2785 :
2786 12 : extTup = systable_getnext(extScan);
2787 :
2788 12 : if (!HeapTupleIsValid(extTup)) /* should not happen */
2789 0 : elog(ERROR, "could not find tuple for extension %u",
2790 : CurrentExtensionObject);
2791 :
2792 12 : memset(repl_val, 0, sizeof(repl_val));
2793 12 : memset(repl_null, false, sizeof(repl_null));
2794 12 : memset(repl_repl, false, sizeof(repl_repl));
2795 :
2796 : /* Build or modify the extconfig value */
2797 12 : elementDatum = ObjectIdGetDatum(tableoid);
2798 :
2799 12 : arrayDatum = heap_getattr(extTup, Anum_pg_extension_extconfig,
2800 : RelationGetDescr(extRel), &isnull);
2801 12 : if (isnull)
2802 : {
2803 : /* Previously empty extconfig, so build 1-element array */
2804 6 : arrayLength = 0;
2805 6 : arrayIndex = 1;
2806 :
2807 6 : a = construct_array_builtin(&elementDatum, 1, OIDOID);
2808 : }
2809 : else
2810 : {
2811 : /* Modify or extend existing extconfig array */
2812 : Oid *arrayData;
2813 : int i;
2814 :
2815 6 : a = DatumGetArrayTypeP(arrayDatum);
2816 :
2817 6 : arrayLength = ARR_DIMS(a)[0];
2818 6 : if (ARR_NDIM(a) != 1 ||
2819 6 : ARR_LBOUND(a)[0] != 1 ||
2820 6 : arrayLength < 0 ||
2821 6 : ARR_HASNULL(a) ||
2822 6 : ARR_ELEMTYPE(a) != OIDOID)
2823 0 : elog(ERROR, "extconfig is not a 1-D Oid array");
2824 6 : arrayData = (Oid *) ARR_DATA_PTR(a);
2825 :
2826 6 : arrayIndex = arrayLength + 1; /* set up to add after end */
2827 :
2828 12 : for (i = 0; i < arrayLength; i++)
2829 : {
2830 6 : if (arrayData[i] == tableoid)
2831 : {
2832 0 : arrayIndex = i + 1; /* replace this element instead */
2833 0 : break;
2834 : }
2835 : }
2836 :
2837 6 : a = array_set(a, 1, &arrayIndex,
2838 : elementDatum,
2839 : false,
2840 : -1 /* varlena array */ ,
2841 : sizeof(Oid) /* OID's typlen */ ,
2842 : true /* OID's typbyval */ ,
2843 : TYPALIGN_INT /* OID's typalign */ );
2844 : }
2845 12 : repl_val[Anum_pg_extension_extconfig - 1] = PointerGetDatum(a);
2846 12 : repl_repl[Anum_pg_extension_extconfig - 1] = true;
2847 :
2848 : /* Build or modify the extcondition value */
2849 12 : elementDatum = PointerGetDatum(wherecond);
2850 :
2851 12 : arrayDatum = heap_getattr(extTup, Anum_pg_extension_extcondition,
2852 : RelationGetDescr(extRel), &isnull);
2853 12 : if (isnull)
2854 : {
2855 6 : if (arrayLength != 0)
2856 0 : elog(ERROR, "extconfig and extcondition arrays do not match");
2857 :
2858 6 : a = construct_array_builtin(&elementDatum, 1, TEXTOID);
2859 : }
2860 : else
2861 : {
2862 6 : a = DatumGetArrayTypeP(arrayDatum);
2863 :
2864 6 : if (ARR_NDIM(a) != 1 ||
2865 6 : ARR_LBOUND(a)[0] != 1 ||
2866 6 : ARR_HASNULL(a) ||
2867 6 : ARR_ELEMTYPE(a) != TEXTOID)
2868 0 : elog(ERROR, "extcondition is not a 1-D text array");
2869 6 : if (ARR_DIMS(a)[0] != arrayLength)
2870 0 : elog(ERROR, "extconfig and extcondition arrays do not match");
2871 :
2872 : /* Add or replace at same index as in extconfig */
2873 6 : a = array_set(a, 1, &arrayIndex,
2874 : elementDatum,
2875 : false,
2876 : -1 /* varlena array */ ,
2877 : -1 /* TEXT's typlen */ ,
2878 : false /* TEXT's typbyval */ ,
2879 : TYPALIGN_INT /* TEXT's typalign */ );
2880 : }
2881 12 : repl_val[Anum_pg_extension_extcondition - 1] = PointerGetDatum(a);
2882 12 : repl_repl[Anum_pg_extension_extcondition - 1] = true;
2883 :
2884 12 : extTup = heap_modify_tuple(extTup, RelationGetDescr(extRel),
2885 : repl_val, repl_null, repl_repl);
2886 :
2887 12 : CatalogTupleUpdate(extRel, &extTup->t_self, extTup);
2888 :
2889 12 : systable_endscan(extScan);
2890 :
2891 12 : table_close(extRel, RowExclusiveLock);
2892 :
2893 12 : PG_RETURN_VOID();
2894 : }
2895 :
2896 : /*
2897 : * pg_get_loaded_modules
2898 : *
2899 : * SQL-callable function to get per-loaded-module information. Modules
2900 : * (shared libraries) aren't necessarily one-to-one with extensions, but
2901 : * they're sufficiently closely related to make this file a good home.
2902 : */
2903 : Datum
2904 0 : pg_get_loaded_modules(PG_FUNCTION_ARGS)
2905 : {
2906 0 : ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
2907 : DynamicFileList *file_scanner;
2908 :
2909 : /* Build tuplestore to hold the result rows */
2910 0 : InitMaterializedSRF(fcinfo, 0);
2911 :
2912 0 : for (file_scanner = get_first_loaded_module(); file_scanner != NULL;
2913 0 : file_scanner = get_next_loaded_module(file_scanner))
2914 : {
2915 : const char *library_path,
2916 : *module_name,
2917 : *module_version;
2918 : const char *sep;
2919 0 : Datum values[3] = {0};
2920 0 : bool nulls[3] = {0};
2921 :
2922 0 : get_loaded_module_details(file_scanner,
2923 : &library_path,
2924 : &module_name,
2925 : &module_version);
2926 :
2927 0 : if (module_name == NULL)
2928 0 : nulls[0] = true;
2929 : else
2930 0 : values[0] = CStringGetTextDatum(module_name);
2931 0 : if (module_version == NULL)
2932 0 : nulls[1] = true;
2933 : else
2934 0 : values[1] = CStringGetTextDatum(module_version);
2935 :
2936 : /* For security reasons, we don't show the directory path */
2937 0 : sep = last_dir_separator(library_path);
2938 0 : if (sep)
2939 0 : library_path = sep + 1;
2940 0 : values[2] = CStringGetTextDatum(library_path);
2941 :
2942 0 : tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
2943 : values, nulls);
2944 : }
2945 :
2946 0 : return (Datum) 0;
2947 : }
2948 :
2949 : /*
2950 : * extension_config_remove
2951 : *
2952 : * Remove the specified table OID from extension's extconfig, if present.
2953 : * This is not currently exposed as a function, but it could be;
2954 : * for now, we just invoke it from ALTER EXTENSION DROP.
2955 : */
2956 : static void
2957 60 : extension_config_remove(Oid extensionoid, Oid tableoid)
2958 : {
2959 : Relation extRel;
2960 : ScanKeyData key[1];
2961 : SysScanDesc extScan;
2962 : HeapTuple extTup;
2963 : Datum arrayDatum;
2964 : int arrayLength;
2965 : int arrayIndex;
2966 : bool isnull;
2967 : Datum repl_val[Natts_pg_extension];
2968 : bool repl_null[Natts_pg_extension];
2969 : bool repl_repl[Natts_pg_extension];
2970 : ArrayType *a;
2971 :
2972 : /* Find the pg_extension tuple */
2973 60 : extRel = table_open(ExtensionRelationId, RowExclusiveLock);
2974 :
2975 60 : ScanKeyInit(&key[0],
2976 : Anum_pg_extension_oid,
2977 : BTEqualStrategyNumber, F_OIDEQ,
2978 : ObjectIdGetDatum(extensionoid));
2979 :
2980 60 : extScan = systable_beginscan(extRel, ExtensionOidIndexId, true,
2981 : NULL, 1, key);
2982 :
2983 60 : extTup = systable_getnext(extScan);
2984 :
2985 60 : if (!HeapTupleIsValid(extTup)) /* should not happen */
2986 0 : elog(ERROR, "could not find tuple for extension %u",
2987 : extensionoid);
2988 :
2989 : /* Search extconfig for the tableoid */
2990 60 : arrayDatum = heap_getattr(extTup, Anum_pg_extension_extconfig,
2991 : RelationGetDescr(extRel), &isnull);
2992 60 : if (isnull)
2993 : {
2994 : /* nothing to do */
2995 52 : a = NULL;
2996 52 : arrayLength = 0;
2997 52 : arrayIndex = -1;
2998 : }
2999 : else
3000 : {
3001 : Oid *arrayData;
3002 : int i;
3003 :
3004 8 : a = DatumGetArrayTypeP(arrayDatum);
3005 :
3006 8 : arrayLength = ARR_DIMS(a)[0];
3007 8 : if (ARR_NDIM(a) != 1 ||
3008 8 : ARR_LBOUND(a)[0] != 1 ||
3009 8 : arrayLength < 0 ||
3010 8 : ARR_HASNULL(a) ||
3011 8 : ARR_ELEMTYPE(a) != OIDOID)
3012 0 : elog(ERROR, "extconfig is not a 1-D Oid array");
3013 8 : arrayData = (Oid *) ARR_DATA_PTR(a);
3014 :
3015 8 : arrayIndex = -1; /* flag for no deletion needed */
3016 :
3017 24 : for (i = 0; i < arrayLength; i++)
3018 : {
3019 16 : if (arrayData[i] == tableoid)
3020 : {
3021 0 : arrayIndex = i; /* index to remove */
3022 0 : break;
3023 : }
3024 : }
3025 : }
3026 :
3027 : /* If tableoid is not in extconfig, nothing to do */
3028 60 : if (arrayIndex < 0)
3029 : {
3030 60 : systable_endscan(extScan);
3031 60 : table_close(extRel, RowExclusiveLock);
3032 60 : return;
3033 : }
3034 :
3035 : /* Modify or delete the extconfig value */
3036 0 : memset(repl_val, 0, sizeof(repl_val));
3037 0 : memset(repl_null, false, sizeof(repl_null));
3038 0 : memset(repl_repl, false, sizeof(repl_repl));
3039 :
3040 0 : if (arrayLength <= 1)
3041 : {
3042 : /* removing only element, just set array to null */
3043 0 : repl_null[Anum_pg_extension_extconfig - 1] = true;
3044 : }
3045 : else
3046 : {
3047 : /* squeeze out the target element */
3048 : Datum *dvalues;
3049 : int nelems;
3050 : int i;
3051 :
3052 : /* We already checked there are no nulls */
3053 0 : deconstruct_array_builtin(a, OIDOID, &dvalues, NULL, &nelems);
3054 :
3055 0 : for (i = arrayIndex; i < arrayLength - 1; i++)
3056 0 : dvalues[i] = dvalues[i + 1];
3057 :
3058 0 : a = construct_array_builtin(dvalues, arrayLength - 1, OIDOID);
3059 :
3060 0 : repl_val[Anum_pg_extension_extconfig - 1] = PointerGetDatum(a);
3061 : }
3062 0 : repl_repl[Anum_pg_extension_extconfig - 1] = true;
3063 :
3064 : /* Modify or delete the extcondition value */
3065 0 : arrayDatum = heap_getattr(extTup, Anum_pg_extension_extcondition,
3066 : RelationGetDescr(extRel), &isnull);
3067 0 : if (isnull)
3068 : {
3069 0 : elog(ERROR, "extconfig and extcondition arrays do not match");
3070 : }
3071 : else
3072 : {
3073 0 : a = DatumGetArrayTypeP(arrayDatum);
3074 :
3075 0 : if (ARR_NDIM(a) != 1 ||
3076 0 : ARR_LBOUND(a)[0] != 1 ||
3077 0 : ARR_HASNULL(a) ||
3078 0 : ARR_ELEMTYPE(a) != TEXTOID)
3079 0 : elog(ERROR, "extcondition is not a 1-D text array");
3080 0 : if (ARR_DIMS(a)[0] != arrayLength)
3081 0 : elog(ERROR, "extconfig and extcondition arrays do not match");
3082 : }
3083 :
3084 0 : if (arrayLength <= 1)
3085 : {
3086 : /* removing only element, just set array to null */
3087 0 : repl_null[Anum_pg_extension_extcondition - 1] = true;
3088 : }
3089 : else
3090 : {
3091 : /* squeeze out the target element */
3092 : Datum *dvalues;
3093 : int nelems;
3094 : int i;
3095 :
3096 : /* We already checked there are no nulls */
3097 0 : deconstruct_array_builtin(a, TEXTOID, &dvalues, NULL, &nelems);
3098 :
3099 0 : for (i = arrayIndex; i < arrayLength - 1; i++)
3100 0 : dvalues[i] = dvalues[i + 1];
3101 :
3102 0 : a = construct_array_builtin(dvalues, arrayLength - 1, TEXTOID);
3103 :
3104 0 : repl_val[Anum_pg_extension_extcondition - 1] = PointerGetDatum(a);
3105 : }
3106 0 : repl_repl[Anum_pg_extension_extcondition - 1] = true;
3107 :
3108 0 : extTup = heap_modify_tuple(extTup, RelationGetDescr(extRel),
3109 : repl_val, repl_null, repl_repl);
3110 :
3111 0 : CatalogTupleUpdate(extRel, &extTup->t_self, extTup);
3112 :
3113 0 : systable_endscan(extScan);
3114 :
3115 0 : table_close(extRel, RowExclusiveLock);
3116 : }
3117 :
3118 : /*
3119 : * Execute ALTER EXTENSION SET SCHEMA
3120 : */
3121 : ObjectAddress
3122 12 : AlterExtensionNamespace(const char *extensionName, const char *newschema, Oid *oldschema)
3123 : {
3124 : Oid extensionOid;
3125 : Oid nspOid;
3126 : Oid oldNspOid;
3127 : AclResult aclresult;
3128 : Relation extRel;
3129 : ScanKeyData key[2];
3130 : SysScanDesc extScan;
3131 : HeapTuple extTup;
3132 : Form_pg_extension extForm;
3133 : Relation depRel;
3134 : SysScanDesc depScan;
3135 : HeapTuple depTup;
3136 : ObjectAddresses *objsMoved;
3137 : ObjectAddress extAddr;
3138 :
3139 12 : extensionOid = get_extension_oid(extensionName, false);
3140 :
3141 12 : nspOid = LookupCreationNamespace(newschema);
3142 :
3143 : /*
3144 : * Permission check: must own extension. Note that we don't bother to
3145 : * check ownership of the individual member objects ...
3146 : */
3147 12 : if (!object_ownercheck(ExtensionRelationId, extensionOid, GetUserId()))
3148 0 : aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_EXTENSION,
3149 : extensionName);
3150 :
3151 : /* Permission check: must have creation rights in target namespace */
3152 12 : aclresult = object_aclcheck(NamespaceRelationId, nspOid, GetUserId(), ACL_CREATE);
3153 12 : if (aclresult != ACLCHECK_OK)
3154 0 : aclcheck_error(aclresult, OBJECT_SCHEMA, newschema);
3155 :
3156 : /*
3157 : * If the schema is currently a member of the extension, disallow moving
3158 : * the extension into the schema. That would create a dependency loop.
3159 : */
3160 12 : if (getExtensionOfObject(NamespaceRelationId, nspOid) == extensionOid)
3161 0 : ereport(ERROR,
3162 : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3163 : errmsg("cannot move extension \"%s\" into schema \"%s\" "
3164 : "because the extension contains the schema",
3165 : extensionName, newschema)));
3166 :
3167 : /* Locate the pg_extension tuple */
3168 12 : extRel = table_open(ExtensionRelationId, RowExclusiveLock);
3169 :
3170 12 : ScanKeyInit(&key[0],
3171 : Anum_pg_extension_oid,
3172 : BTEqualStrategyNumber, F_OIDEQ,
3173 : ObjectIdGetDatum(extensionOid));
3174 :
3175 12 : extScan = systable_beginscan(extRel, ExtensionOidIndexId, true,
3176 : NULL, 1, key);
3177 :
3178 12 : extTup = systable_getnext(extScan);
3179 :
3180 12 : if (!HeapTupleIsValid(extTup)) /* should not happen */
3181 0 : elog(ERROR, "could not find tuple for extension %u",
3182 : extensionOid);
3183 :
3184 : /* Copy tuple so we can modify it below */
3185 12 : extTup = heap_copytuple(extTup);
3186 12 : extForm = (Form_pg_extension) GETSTRUCT(extTup);
3187 :
3188 12 : systable_endscan(extScan);
3189 :
3190 : /*
3191 : * If the extension is already in the target schema, just silently do
3192 : * nothing.
3193 : */
3194 12 : if (extForm->extnamespace == nspOid)
3195 : {
3196 0 : table_close(extRel, RowExclusiveLock);
3197 0 : return InvalidObjectAddress;
3198 : }
3199 :
3200 : /* Check extension is supposed to be relocatable */
3201 12 : if (!extForm->extrelocatable)
3202 0 : ereport(ERROR,
3203 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3204 : errmsg("extension \"%s\" does not support SET SCHEMA",
3205 : NameStr(extForm->extname))));
3206 :
3207 12 : objsMoved = new_object_addresses();
3208 :
3209 : /* store the OID of the namespace to-be-changed */
3210 12 : oldNspOid = extForm->extnamespace;
3211 :
3212 : /*
3213 : * Scan pg_depend to find objects that depend directly on the extension,
3214 : * and alter each one's schema.
3215 : */
3216 12 : depRel = table_open(DependRelationId, AccessShareLock);
3217 :
3218 12 : ScanKeyInit(&key[0],
3219 : Anum_pg_depend_refclassid,
3220 : BTEqualStrategyNumber, F_OIDEQ,
3221 : ObjectIdGetDatum(ExtensionRelationId));
3222 12 : ScanKeyInit(&key[1],
3223 : Anum_pg_depend_refobjid,
3224 : BTEqualStrategyNumber, F_OIDEQ,
3225 : ObjectIdGetDatum(extensionOid));
3226 :
3227 12 : depScan = systable_beginscan(depRel, DependReferenceIndexId, true,
3228 : NULL, 2, key);
3229 :
3230 58 : while (HeapTupleIsValid(depTup = systable_getnext(depScan)))
3231 : {
3232 50 : Form_pg_depend pg_depend = (Form_pg_depend) GETSTRUCT(depTup);
3233 : ObjectAddress dep;
3234 : Oid dep_oldNspOid;
3235 :
3236 : /*
3237 : * If a dependent extension has a no_relocate request for this
3238 : * extension, disallow SET SCHEMA. (XXX it's a bit ugly to do this in
3239 : * the same loop that's actually executing the renames: we may detect
3240 : * the error condition only after having expended a fair amount of
3241 : * work. However, the alternative is to do two scans of pg_depend,
3242 : * which seems like optimizing for failure cases. The rename work
3243 : * will all roll back cleanly enough if we do fail here.)
3244 : */
3245 50 : if (pg_depend->deptype == DEPENDENCY_NORMAL &&
3246 8 : pg_depend->classid == ExtensionRelationId)
3247 : {
3248 8 : char *depextname = get_extension_name(pg_depend->objid);
3249 : ExtensionControlFile *dcontrol;
3250 : ListCell *lc;
3251 :
3252 8 : dcontrol = read_extension_control_file(depextname);
3253 10 : foreach(lc, dcontrol->no_relocate)
3254 : {
3255 4 : char *nrextname = (char *) lfirst(lc);
3256 :
3257 4 : if (strcmp(nrextname, NameStr(extForm->extname)) == 0)
3258 : {
3259 2 : ereport(ERROR,
3260 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3261 : errmsg("cannot SET SCHEMA of extension \"%s\" because other extensions prevent it",
3262 : NameStr(extForm->extname)),
3263 : errdetail("Extension \"%s\" requests no relocation of extension \"%s\".",
3264 : depextname,
3265 : NameStr(extForm->extname))));
3266 : }
3267 : }
3268 : }
3269 :
3270 : /*
3271 : * Otherwise, ignore non-membership dependencies. (Currently, the
3272 : * only other case we could see here is a normal dependency from
3273 : * another extension.)
3274 : */
3275 48 : if (pg_depend->deptype != DEPENDENCY_EXTENSION)
3276 6 : continue;
3277 :
3278 42 : dep.classId = pg_depend->classid;
3279 42 : dep.objectId = pg_depend->objid;
3280 42 : dep.objectSubId = pg_depend->objsubid;
3281 :
3282 42 : if (dep.objectSubId != 0) /* should not happen */
3283 0 : elog(ERROR, "extension should not have a sub-object dependency");
3284 :
3285 : /* Relocate the object */
3286 42 : dep_oldNspOid = AlterObjectNamespace_oid(dep.classId,
3287 : dep.objectId,
3288 : nspOid,
3289 : objsMoved);
3290 :
3291 : /*
3292 : * If not all the objects had the same old namespace (ignoring any
3293 : * that are not in namespaces or are dependent types), complain.
3294 : */
3295 42 : if (dep_oldNspOid != InvalidOid && dep_oldNspOid != oldNspOid)
3296 2 : ereport(ERROR,
3297 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3298 : errmsg("extension \"%s\" does not support SET SCHEMA",
3299 : NameStr(extForm->extname)),
3300 : errdetail("%s is not in the extension's schema \"%s\"",
3301 : getObjectDescription(&dep, false),
3302 : get_namespace_name(oldNspOid))));
3303 : }
3304 :
3305 : /* report old schema, if caller wants it */
3306 8 : if (oldschema)
3307 8 : *oldschema = oldNspOid;
3308 :
3309 8 : systable_endscan(depScan);
3310 :
3311 8 : relation_close(depRel, AccessShareLock);
3312 :
3313 : /* Now adjust pg_extension.extnamespace */
3314 8 : extForm->extnamespace = nspOid;
3315 :
3316 8 : CatalogTupleUpdate(extRel, &extTup->t_self, extTup);
3317 :
3318 8 : table_close(extRel, RowExclusiveLock);
3319 :
3320 : /* update dependency to point to the new schema */
3321 8 : if (changeDependencyFor(ExtensionRelationId, extensionOid,
3322 : NamespaceRelationId, oldNspOid, nspOid) != 1)
3323 0 : elog(ERROR, "could not change schema dependency for extension %s",
3324 : NameStr(extForm->extname));
3325 :
3326 8 : InvokeObjectPostAlterHook(ExtensionRelationId, extensionOid, 0);
3327 :
3328 8 : ObjectAddressSet(extAddr, ExtensionRelationId, extensionOid);
3329 :
3330 8 : return extAddr;
3331 : }
3332 :
3333 : /*
3334 : * Execute ALTER EXTENSION UPDATE
3335 : */
3336 : ObjectAddress
3337 38 : ExecAlterExtensionStmt(ParseState *pstate, AlterExtensionStmt *stmt)
3338 : {
3339 38 : DefElem *d_new_version = NULL;
3340 : char *versionName;
3341 : char *oldVersionName;
3342 : ExtensionControlFile *control;
3343 : Oid extensionOid;
3344 : Relation extRel;
3345 : ScanKeyData key[1];
3346 : SysScanDesc extScan;
3347 : HeapTuple extTup;
3348 : List *updateVersions;
3349 : Datum datum;
3350 : bool isnull;
3351 : ListCell *lc;
3352 : ObjectAddress address;
3353 :
3354 : /*
3355 : * We use global variables to track the extension being created, so we can
3356 : * create/update only one extension at the same time.
3357 : */
3358 38 : if (creating_extension)
3359 0 : ereport(ERROR,
3360 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3361 : errmsg("nested ALTER EXTENSION is not supported")));
3362 :
3363 : /*
3364 : * Look up the extension --- it must already exist in pg_extension
3365 : */
3366 38 : extRel = table_open(ExtensionRelationId, AccessShareLock);
3367 :
3368 38 : ScanKeyInit(&key[0],
3369 : Anum_pg_extension_extname,
3370 : BTEqualStrategyNumber, F_NAMEEQ,
3371 38 : CStringGetDatum(stmt->extname));
3372 :
3373 38 : extScan = systable_beginscan(extRel, ExtensionNameIndexId, true,
3374 : NULL, 1, key);
3375 :
3376 38 : extTup = systable_getnext(extScan);
3377 :
3378 38 : if (!HeapTupleIsValid(extTup))
3379 0 : ereport(ERROR,
3380 : (errcode(ERRCODE_UNDEFINED_OBJECT),
3381 : errmsg("extension \"%s\" does not exist",
3382 : stmt->extname)));
3383 :
3384 38 : extensionOid = ((Form_pg_extension) GETSTRUCT(extTup))->oid;
3385 :
3386 : /*
3387 : * Determine the existing version we are updating from
3388 : */
3389 38 : datum = heap_getattr(extTup, Anum_pg_extension_extversion,
3390 : RelationGetDescr(extRel), &isnull);
3391 38 : if (isnull)
3392 0 : elog(ERROR, "extversion is null");
3393 38 : oldVersionName = text_to_cstring(DatumGetTextPP(datum));
3394 :
3395 38 : systable_endscan(extScan);
3396 :
3397 38 : table_close(extRel, AccessShareLock);
3398 :
3399 : /* Permission check: must own extension */
3400 38 : if (!object_ownercheck(ExtensionRelationId, extensionOid, GetUserId()))
3401 0 : aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_EXTENSION,
3402 0 : stmt->extname);
3403 :
3404 : /*
3405 : * Read the primary control file. Note we assume that it does not contain
3406 : * any non-ASCII data, so there is no need to worry about encoding at this
3407 : * point.
3408 : */
3409 38 : control = read_extension_control_file(stmt->extname);
3410 :
3411 : /*
3412 : * Read the statement option list
3413 : */
3414 76 : foreach(lc, stmt->options)
3415 : {
3416 38 : DefElem *defel = (DefElem *) lfirst(lc);
3417 :
3418 38 : if (strcmp(defel->defname, "new_version") == 0)
3419 : {
3420 38 : if (d_new_version)
3421 0 : errorConflictingDefElem(defel, pstate);
3422 38 : d_new_version = defel;
3423 : }
3424 : else
3425 0 : elog(ERROR, "unrecognized option: %s", defel->defname);
3426 : }
3427 :
3428 : /*
3429 : * Determine the version to update to
3430 : */
3431 38 : if (d_new_version && d_new_version->arg)
3432 38 : versionName = strVal(d_new_version->arg);
3433 0 : else if (control->default_version)
3434 0 : versionName = control->default_version;
3435 : else
3436 : {
3437 0 : ereport(ERROR,
3438 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3439 : errmsg("version to install must be specified")));
3440 : versionName = NULL; /* keep compiler quiet */
3441 : }
3442 38 : check_valid_version_name(versionName);
3443 :
3444 : /*
3445 : * If we're already at that version, just say so
3446 : */
3447 38 : if (strcmp(oldVersionName, versionName) == 0)
3448 : {
3449 0 : ereport(NOTICE,
3450 : (errmsg("version \"%s\" of extension \"%s\" is already installed",
3451 : versionName, stmt->extname)));
3452 0 : return InvalidObjectAddress;
3453 : }
3454 :
3455 : /*
3456 : * Identify the series of update script files we need to execute
3457 : */
3458 38 : updateVersions = identify_update_path(control,
3459 : oldVersionName,
3460 : versionName);
3461 :
3462 : /*
3463 : * Update the pg_extension row and execute the update scripts, one at a
3464 : * time
3465 : */
3466 38 : ApplyExtensionUpdates(extensionOid, control,
3467 : oldVersionName, updateVersions,
3468 : NULL, false, false);
3469 :
3470 34 : ObjectAddressSet(address, ExtensionRelationId, extensionOid);
3471 :
3472 34 : return address;
3473 : }
3474 :
3475 : /*
3476 : * Apply a series of update scripts as though individual ALTER EXTENSION
3477 : * UPDATE commands had been given, including altering the pg_extension row
3478 : * and dependencies each time.
3479 : *
3480 : * This might be more work than necessary, but it ensures that old update
3481 : * scripts don't break if newer versions have different control parameters.
3482 : */
3483 : static void
3484 608 : ApplyExtensionUpdates(Oid extensionOid,
3485 : ExtensionControlFile *pcontrol,
3486 : const char *initialVersion,
3487 : List *updateVersions,
3488 : char *origSchemaName,
3489 : bool cascade,
3490 : bool is_create)
3491 : {
3492 608 : const char *oldVersionName = initialVersion;
3493 : ListCell *lcv;
3494 :
3495 1150 : foreach(lcv, updateVersions)
3496 : {
3497 546 : char *versionName = (char *) lfirst(lcv);
3498 : ExtensionControlFile *control;
3499 : char *schemaName;
3500 : Oid schemaOid;
3501 : List *requiredExtensions;
3502 : List *requiredSchemas;
3503 : Relation extRel;
3504 : ScanKeyData key[1];
3505 : SysScanDesc extScan;
3506 : HeapTuple extTup;
3507 : Form_pg_extension extForm;
3508 : Datum values[Natts_pg_extension];
3509 : bool nulls[Natts_pg_extension];
3510 : bool repl[Natts_pg_extension];
3511 : ObjectAddress myself;
3512 : ListCell *lc;
3513 :
3514 : /*
3515 : * Fetch parameters for specific version (pcontrol is not changed)
3516 : */
3517 546 : control = read_extension_aux_control_file(pcontrol, versionName);
3518 :
3519 : /* Find the pg_extension tuple */
3520 546 : extRel = table_open(ExtensionRelationId, RowExclusiveLock);
3521 :
3522 546 : ScanKeyInit(&key[0],
3523 : Anum_pg_extension_oid,
3524 : BTEqualStrategyNumber, F_OIDEQ,
3525 : ObjectIdGetDatum(extensionOid));
3526 :
3527 546 : extScan = systable_beginscan(extRel, ExtensionOidIndexId, true,
3528 : NULL, 1, key);
3529 :
3530 546 : extTup = systable_getnext(extScan);
3531 :
3532 546 : if (!HeapTupleIsValid(extTup)) /* should not happen */
3533 0 : elog(ERROR, "could not find tuple for extension %u",
3534 : extensionOid);
3535 :
3536 546 : extForm = (Form_pg_extension) GETSTRUCT(extTup);
3537 :
3538 : /*
3539 : * Determine the target schema (set by original install)
3540 : */
3541 546 : schemaOid = extForm->extnamespace;
3542 546 : schemaName = get_namespace_name(schemaOid);
3543 :
3544 : /*
3545 : * Modify extrelocatable and extversion in the pg_extension tuple
3546 : */
3547 546 : memset(values, 0, sizeof(values));
3548 546 : memset(nulls, 0, sizeof(nulls));
3549 546 : memset(repl, 0, sizeof(repl));
3550 :
3551 546 : values[Anum_pg_extension_extrelocatable - 1] =
3552 546 : BoolGetDatum(control->relocatable);
3553 546 : repl[Anum_pg_extension_extrelocatable - 1] = true;
3554 546 : values[Anum_pg_extension_extversion - 1] =
3555 546 : CStringGetTextDatum(versionName);
3556 546 : repl[Anum_pg_extension_extversion - 1] = true;
3557 :
3558 546 : extTup = heap_modify_tuple(extTup, RelationGetDescr(extRel),
3559 : values, nulls, repl);
3560 :
3561 546 : CatalogTupleUpdate(extRel, &extTup->t_self, extTup);
3562 :
3563 546 : systable_endscan(extScan);
3564 :
3565 546 : table_close(extRel, RowExclusiveLock);
3566 :
3567 : /*
3568 : * Look up the prerequisite extensions for this version, install them
3569 : * if necessary, and build lists of their OIDs and the OIDs of their
3570 : * target schemas.
3571 : */
3572 546 : requiredExtensions = NIL;
3573 546 : requiredSchemas = NIL;
3574 548 : foreach(lc, control->requires)
3575 : {
3576 2 : char *curreq = (char *) lfirst(lc);
3577 : Oid reqext;
3578 : Oid reqschema;
3579 :
3580 2 : reqext = get_required_extension(curreq,
3581 : control->name,
3582 : origSchemaName,
3583 : cascade,
3584 : NIL,
3585 : is_create);
3586 2 : reqschema = get_extension_schema(reqext);
3587 2 : requiredExtensions = lappend_oid(requiredExtensions, reqext);
3588 2 : requiredSchemas = lappend_oid(requiredSchemas, reqschema);
3589 : }
3590 :
3591 : /*
3592 : * Remove and recreate dependencies on prerequisite extensions
3593 : */
3594 546 : deleteDependencyRecordsForClass(ExtensionRelationId, extensionOid,
3595 : ExtensionRelationId,
3596 : DEPENDENCY_NORMAL);
3597 :
3598 546 : myself.classId = ExtensionRelationId;
3599 546 : myself.objectId = extensionOid;
3600 546 : myself.objectSubId = 0;
3601 :
3602 548 : foreach(lc, requiredExtensions)
3603 : {
3604 2 : Oid reqext = lfirst_oid(lc);
3605 : ObjectAddress otherext;
3606 :
3607 2 : otherext.classId = ExtensionRelationId;
3608 2 : otherext.objectId = reqext;
3609 2 : otherext.objectSubId = 0;
3610 :
3611 2 : recordDependencyOn(&myself, &otherext, DEPENDENCY_NORMAL);
3612 : }
3613 :
3614 546 : InvokeObjectPostAlterHook(ExtensionRelationId, extensionOid, 0);
3615 :
3616 : /*
3617 : * Finally, execute the update script file
3618 : */
3619 546 : execute_extension_script(extensionOid, control,
3620 : oldVersionName, versionName,
3621 : requiredSchemas,
3622 : schemaName);
3623 :
3624 : /*
3625 : * Update prior-version name and loop around. Since
3626 : * execute_sql_string did a final CommandCounterIncrement, we can
3627 : * update the pg_extension row again.
3628 : */
3629 542 : oldVersionName = versionName;
3630 : }
3631 604 : }
3632 :
3633 : /*
3634 : * Execute ALTER EXTENSION ADD/DROP
3635 : *
3636 : * Return value is the address of the altered extension.
3637 : *
3638 : * objAddr is an output argument which, if not NULL, is set to the address of
3639 : * the added/dropped object.
3640 : */
3641 : ObjectAddress
3642 254 : ExecAlterExtensionContentsStmt(AlterExtensionContentsStmt *stmt,
3643 : ObjectAddress *objAddr)
3644 : {
3645 : ObjectAddress extension;
3646 : ObjectAddress object;
3647 : Relation relation;
3648 :
3649 254 : switch (stmt->objtype)
3650 : {
3651 2 : case OBJECT_DATABASE:
3652 : case OBJECT_EXTENSION:
3653 : case OBJECT_INDEX:
3654 : case OBJECT_PUBLICATION:
3655 : case OBJECT_ROLE:
3656 : case OBJECT_STATISTIC_EXT:
3657 : case OBJECT_SUBSCRIPTION:
3658 : case OBJECT_TABLESPACE:
3659 2 : ereport(ERROR,
3660 : (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
3661 : errmsg("cannot add an object of this type to an extension")));
3662 : break;
3663 252 : default:
3664 : /* OK */
3665 252 : break;
3666 : }
3667 :
3668 : /*
3669 : * Find the extension and acquire a lock on it, to ensure it doesn't get
3670 : * dropped concurrently. A sharable lock seems sufficient: there's no
3671 : * reason not to allow other sorts of manipulations, such as add/drop of
3672 : * other objects, to occur concurrently. Concurrently adding/dropping the
3673 : * *same* object would be bad, but we prevent that by using a non-sharable
3674 : * lock on the individual object, below.
3675 : */
3676 252 : extension = get_object_address(OBJECT_EXTENSION,
3677 252 : (Node *) makeString(stmt->extname),
3678 : &relation, AccessShareLock, false);
3679 :
3680 : /* Permission check: must own extension */
3681 252 : if (!object_ownercheck(ExtensionRelationId, extension.objectId, GetUserId()))
3682 0 : aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_EXTENSION,
3683 0 : stmt->extname);
3684 :
3685 : /*
3686 : * Translate the parser representation that identifies the object into an
3687 : * ObjectAddress. get_object_address() will throw an error if the object
3688 : * does not exist, and will also acquire a lock on the object to guard
3689 : * against concurrent DROP and ALTER EXTENSION ADD/DROP operations.
3690 : */
3691 252 : object = get_object_address(stmt->objtype, stmt->object,
3692 : &relation, ShareUpdateExclusiveLock, false);
3693 :
3694 : Assert(object.objectSubId == 0);
3695 252 : if (objAddr)
3696 252 : *objAddr = object;
3697 :
3698 : /* Permission check: must own target object, too */
3699 252 : check_object_ownership(GetUserId(), stmt->objtype, object,
3700 : stmt->object, relation);
3701 :
3702 : /* Do the update, recursing to any dependent objects */
3703 252 : ExecAlterExtensionContentsRecurse(stmt, extension, object);
3704 :
3705 : /* Finish up */
3706 252 : InvokeObjectPostAlterHook(ExtensionRelationId, extension.objectId, 0);
3707 :
3708 : /*
3709 : * If get_object_address() opened the relation for us, we close it to keep
3710 : * the reference count correct - but we retain any locks acquired by
3711 : * get_object_address() until commit time, to guard against concurrent
3712 : * activity.
3713 : */
3714 252 : if (relation != NULL)
3715 76 : relation_close(relation, NoLock);
3716 :
3717 252 : return extension;
3718 : }
3719 :
3720 : /*
3721 : * ExecAlterExtensionContentsRecurse
3722 : * Subroutine for ExecAlterExtensionContentsStmt
3723 : *
3724 : * Do the bare alteration of object's membership in extension,
3725 : * without permission checks. Recurse to dependent objects, if any.
3726 : */
3727 : static void
3728 416 : ExecAlterExtensionContentsRecurse(AlterExtensionContentsStmt *stmt,
3729 : ObjectAddress extension,
3730 : ObjectAddress object)
3731 : {
3732 : Oid oldExtension;
3733 :
3734 : /*
3735 : * Check existing extension membership.
3736 : */
3737 416 : oldExtension = getExtensionOfObject(object.classId, object.objectId);
3738 :
3739 416 : if (stmt->action > 0)
3740 : {
3741 : /*
3742 : * ADD, so complain if object is already attached to some extension.
3743 : */
3744 104 : if (OidIsValid(oldExtension))
3745 0 : ereport(ERROR,
3746 : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3747 : errmsg("%s is already a member of extension \"%s\"",
3748 : getObjectDescription(&object, false),
3749 : get_extension_name(oldExtension))));
3750 :
3751 : /*
3752 : * Prevent a schema from being added to an extension if the schema
3753 : * contains the extension. That would create a dependency loop.
3754 : */
3755 106 : if (object.classId == NamespaceRelationId &&
3756 2 : object.objectId == get_extension_schema(extension.objectId))
3757 0 : ereport(ERROR,
3758 : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3759 : errmsg("cannot add schema \"%s\" to extension \"%s\" "
3760 : "because the schema contains the extension",
3761 : get_namespace_name(object.objectId),
3762 : stmt->extname)));
3763 :
3764 : /*
3765 : * OK, add the dependency.
3766 : */
3767 104 : recordDependencyOn(&object, &extension, DEPENDENCY_EXTENSION);
3768 :
3769 : /*
3770 : * Also record the initial ACL on the object, if any.
3771 : *
3772 : * Note that this will handle the object's ACLs, as well as any ACLs
3773 : * on object subIds. (In other words, when the object is a table,
3774 : * this will record the table's ACL and the ACLs for the columns on
3775 : * the table, if any).
3776 : */
3777 104 : recordExtObjInitPriv(object.objectId, object.classId);
3778 : }
3779 : else
3780 : {
3781 : /*
3782 : * DROP, so complain if it's not a member.
3783 : */
3784 312 : if (oldExtension != extension.objectId)
3785 0 : ereport(ERROR,
3786 : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3787 : errmsg("%s is not a member of extension \"%s\"",
3788 : getObjectDescription(&object, false),
3789 : stmt->extname)));
3790 :
3791 : /*
3792 : * OK, drop the dependency.
3793 : */
3794 312 : if (deleteDependencyRecordsForClass(object.classId, object.objectId,
3795 : ExtensionRelationId,
3796 : DEPENDENCY_EXTENSION) != 1)
3797 0 : elog(ERROR, "unexpected number of extension dependency records");
3798 :
3799 : /*
3800 : * If it's a relation, it might have an entry in the extension's
3801 : * extconfig array, which we must remove.
3802 : */
3803 312 : if (object.classId == RelationRelationId)
3804 60 : extension_config_remove(extension.objectId, object.objectId);
3805 :
3806 : /*
3807 : * Remove all the initial ACLs, if any.
3808 : *
3809 : * Note that this will remove the object's ACLs, as well as any ACLs
3810 : * on object subIds. (In other words, when the object is a table,
3811 : * this will remove the table's ACL and the ACLs for the columns on
3812 : * the table, if any).
3813 : */
3814 312 : removeExtObjInitPriv(object.objectId, object.classId);
3815 : }
3816 :
3817 : /*
3818 : * Recurse to any dependent objects; currently, this includes the array
3819 : * type of a base type, the multirange type associated with a range type,
3820 : * and the rowtype of a table.
3821 : */
3822 416 : if (object.classId == TypeRelationId)
3823 : {
3824 : ObjectAddress depobject;
3825 :
3826 172 : depobject.classId = TypeRelationId;
3827 172 : depobject.objectSubId = 0;
3828 :
3829 : /* If it has an array type, update that too */
3830 172 : depobject.objectId = get_array_type(object.objectId);
3831 172 : if (OidIsValid(depobject.objectId))
3832 86 : ExecAlterExtensionContentsRecurse(stmt, extension, depobject);
3833 :
3834 : /* If it is a range type, update the associated multirange too */
3835 172 : if (type_is_range(object.objectId))
3836 : {
3837 4 : depobject.objectId = get_range_multirange(object.objectId);
3838 4 : if (!OidIsValid(depobject.objectId))
3839 0 : ereport(ERROR,
3840 : (errcode(ERRCODE_UNDEFINED_OBJECT),
3841 : errmsg("could not find multirange type for data type %s",
3842 : format_type_be(object.objectId))));
3843 4 : ExecAlterExtensionContentsRecurse(stmt, extension, depobject);
3844 : }
3845 : }
3846 416 : if (object.classId == RelationRelationId)
3847 : {
3848 : ObjectAddress depobject;
3849 :
3850 76 : depobject.classId = TypeRelationId;
3851 76 : depobject.objectSubId = 0;
3852 :
3853 : /* It might not have a rowtype, but if it does, update that */
3854 76 : depobject.objectId = get_rel_type_id(object.objectId);
3855 76 : if (OidIsValid(depobject.objectId))
3856 74 : ExecAlterExtensionContentsRecurse(stmt, extension, depobject);
3857 : }
3858 416 : }
3859 :
3860 : /*
3861 : * Read the whole of file into memory.
3862 : *
3863 : * The file contents are returned as a single palloc'd chunk. For convenience
3864 : * of the callers, an extra \0 byte is added to the end. That is not counted
3865 : * in the length returned into *length.
3866 : */
3867 : static char *
3868 1142 : read_whole_file(const char *filename, int *length)
3869 : {
3870 : char *buf;
3871 : FILE *file;
3872 : size_t bytes_to_read;
3873 : struct stat fst;
3874 :
3875 1142 : if (stat(filename, &fst) < 0)
3876 0 : ereport(ERROR,
3877 : (errcode_for_file_access(),
3878 : errmsg("could not stat file \"%s\": %m", filename)));
3879 :
3880 1142 : if (fst.st_size > (MaxAllocSize - 1))
3881 0 : ereport(ERROR,
3882 : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
3883 : errmsg("file \"%s\" is too large", filename)));
3884 1142 : bytes_to_read = (size_t) fst.st_size;
3885 :
3886 1142 : if ((file = AllocateFile(filename, PG_BINARY_R)) == NULL)
3887 0 : ereport(ERROR,
3888 : (errcode_for_file_access(),
3889 : errmsg("could not open file \"%s\" for reading: %m",
3890 : filename)));
3891 :
3892 1142 : buf = (char *) palloc(bytes_to_read + 1);
3893 :
3894 1142 : bytes_to_read = fread(buf, 1, bytes_to_read, file);
3895 :
3896 1142 : if (ferror(file))
3897 0 : ereport(ERROR,
3898 : (errcode_for_file_access(),
3899 : errmsg("could not read file \"%s\": %m", filename)));
3900 :
3901 1142 : FreeFile(file);
3902 :
3903 1142 : buf[bytes_to_read] = '\0';
3904 :
3905 : /*
3906 : * On Windows, manually convert Windows-style newlines (\r\n) to the Unix
3907 : * convention of \n only. This avoids gotchas due to script files
3908 : * possibly getting converted when being transferred between platforms.
3909 : * Ideally we'd do this by using text mode to read the file, but that also
3910 : * causes control-Z to be treated as end-of-file. Historically we've
3911 : * allowed control-Z in script files, so breaking that seems unwise.
3912 : */
3913 : #ifdef WIN32
3914 : {
3915 : char *s,
3916 : *d;
3917 :
3918 : for (s = d = buf; *s; s++)
3919 : {
3920 : if (!(*s == '\r' && s[1] == '\n'))
3921 : *d++ = *s;
3922 : }
3923 : *d = '\0';
3924 : bytes_to_read = d - buf;
3925 : }
3926 : #endif
3927 :
3928 1142 : *length = bytes_to_read;
3929 1142 : return buf;
3930 : }
3931 :
3932 : static ExtensionControlFile *
3933 15788 : new_ExtensionControlFile(const char *extname)
3934 : {
3935 : /*
3936 : * Set up default values. Pointer fields are initially null.
3937 : */
3938 15788 : ExtensionControlFile *control = palloc0_object(ExtensionControlFile);
3939 :
3940 15788 : control->name = pstrdup(extname);
3941 15788 : control->relocatable = false;
3942 15788 : control->superuser = true;
3943 15788 : control->trusted = false;
3944 15788 : control->encoding = -1;
3945 :
3946 15788 : return control;
3947 : }
3948 :
3949 : /*
3950 : * Search for the basename in the list of paths.
3951 : *
3952 : * Similar to find_in_path but for simplicity does not support custom error
3953 : * messages and expects that paths already have all macros replaced.
3954 : */
3955 : char *
3956 664 : find_in_paths(const char *basename, List *paths)
3957 : {
3958 : ListCell *cell;
3959 :
3960 668 : foreach(cell, paths)
3961 : {
3962 668 : ExtensionLocation *location = lfirst(cell);
3963 668 : char *path = location->loc;
3964 : char *full;
3965 :
3966 : Assert(path != NULL);
3967 :
3968 668 : path = pstrdup(path);
3969 668 : canonicalize_path(path);
3970 :
3971 : /* only absolute paths */
3972 668 : if (!is_absolute_path(path))
3973 0 : ereport(ERROR,
3974 : errcode(ERRCODE_INVALID_NAME),
3975 : errmsg("component in parameter \"%s\" is not an absolute path", "extension_control_path"));
3976 :
3977 668 : full = psprintf("%s/%s", path, basename);
3978 :
3979 668 : if (pg_file_exists(full))
3980 664 : return full;
3981 :
3982 4 : pfree(path);
3983 4 : pfree(full);
3984 : }
3985 :
3986 0 : return NULL;
3987 : }
|