304304 </para>
305305
306306 <para>
307- The advantage of using an extension, rather than just running the
307+ Themain advantage of using an extension, rather than just running the
308308 <acronym>SQL</> script to load a bunch of <quote>loose</> objects
309309 into your database, is that <productname>PostgreSQL</> will then
310310 understand that the objects of the extension go together. You can
331331 data; see below.)
332332 </para>
333333
334+ <para>
335+ The extension mechanism also has provisions for packaging modification
336+ scripts that adjust the definitions of the SQL objects contained in an
337+ extension. For example, if version 1.1 of an extension adds one function
338+ and changes the body of another function compared to 1.0, the extension
339+ author can provide an <firstterm>update script</> that makes just those
340+ two changes. The <command>ALTER EXTENSION UPDATE</> command can then
341+ be used to apply these changes and track which version of the extension
342+ is actually installed in a given database.
343+ </para>
344+
334345 <para>
335346 The kinds of SQL objects that can be members of an extension are shown in
336347 the description of <xref linkend="sql-alterextension">. Notably, objects
355366 file for each extension, which must be named the same as the extension
356367 with a suffix of <literal>.control</>, and must be placed in the
357368 installation's <literal>SHAREDIR/contrib</literal> directory. There
358- must also be a <acronym>SQL</> script file, which typically is
359- named after the extension with a suffix of <literal>.sql</>, and is also
360- placed in the <literal>SHAREDIR/contrib</literal> directory; but these
361- defaults can be overridden by the control file.
369+ must also be at least one <acronym>SQL</> script file, which follows the
370+ naming pattern
371+ <literal><replaceable>extension</>-<replaceable>version</>.sql</literal>
372+ (for example, <literal>foo-1.0.sql</> for version <literal>1.0</> of
373+ extension <literal>foo</>). By default, the script file(s) are also
374+ placed in the <literal>SHAREDIR/contrib</literal> directory; but the
375+ control file can specify a different directory for the script file(s).
362376 </para>
363377
364378 <para>
376390
377391 <variablelist>
378392 <varlistentry>
379- <term><varname>script </varname> (<type>string</type>)</term>
393+ <term><varname>directory </varname> (<type>string</type>)</term>
380394 <listitem>
381395 <para>
382- The filename of the extension's <acronym>SQL</> script.
383- Defaults to the same name as the control file, but with the
384- <literal>.sql</literal> extension. Unless an absolute path is
385- given, the name is relative to the <literal>SHAREDIR/contrib</literal>
386- directory.
396+ The directory containing the extension's <acronym>SQL</> script
397+ file(s). Unless an absolute path is given, the name is relative to
398+ the <literal>SHAREDIR/contrib</literal> directory.
387399 </para>
388400 </listitem>
389401 </varlistentry>
390402
391403 <varlistentry>
392- <term><varname>version </varname> (<type>string</type>)</term>
404+ <term><varname>default_version </varname> (<type>string</type>)</term>
393405 <listitem>
394406 <para>
395- The version of the extension. Any string can be given.
407+ The default version of the extension (the one that will be installed
408+ if no version is specified in <command>CREATE EXTENSION</>). Although
409+ this can be omitted, that will result in <command>CREATE EXTENSION</>
410+ failing if no <literal>VERSION</> option appears, so you generally
411+ don't want to do that.
396412 </para>
397413 </listitem>
398414 </varlistentry>
403419 <para>
404420 A comment (any string) about the extension. Alternatively,
405421 the comment can be set by means of the <xref linkend="sql-comment">
406- command.
422+ command in the script file .
407423 </para>
408424 </listitem>
409425 </varlistentry>
423439 <term><varname>encoding</varname> (<type>string</type>)</term>
424440 <listitem>
425441 <para>
426- The character set encoding used by the script file. This should
427- be specified if the script file contains any non-ASCII characters.
428- Otherwise the script will be assumed to be in the encoding of the
429- database it is loaded into.
442+ The character set encoding used by the script file(s). This should
443+ be specified if the script files contain any non-ASCII characters.
444+ Otherwise the files will be assumed to be in the database encoding.
430445 </para>
431446 </listitem>
432447 </varlistentry>
457472 </variablelist>
458473
459474 <para>
460- An extension's <acronym>SQL</> script file can contain any SQL commands,
475+ In addition to the primary control file
476+ <literal><replaceable>extension</>.control</literal>,
477+ an extension can have secondary control files named in the style
478+ <literal><replaceable>extension</>-<replaceable>version</>.control</literal>.
479+ If supplied, these must be located in the script file directory.
480+ Secondary control files follow the same format as the primary control
481+ file. Any parameters set in a secondary control file override the
482+ primary control file when installing or updating to that version of
483+ the extension. However, the parameters <varname>directory</>,
484+ <varname>default_version</>, and <varname>encoding</> cannot be set in
485+ a secondary control file; in particular, the same encoding must be used
486+ in all script files associated with the extension.
487+ </para>
488+
489+ <para>
490+ An extension's <acronym>SQL</> script files can contain any SQL commands,
461491 except for transaction control commands (<command>BEGIN</>,
462492 <command>COMMIT</>, etc) and commands that cannot be executed inside a
463493 transaction block (such as <command>VACUUM</>). This is because the
464- scriptfile is implicitly executed within a transaction block.
494+ scriptfiles are implicitly executed within a transaction block.
465495 </para>
466496
467497 <para>
468- While the scriptfile can contain any characters allowed by the specified
469- encoding,the controlfile should contain only plain ASCII, because there
470- is no way for <productname>PostgreSQL</> to know what encodingthe
498+ While the scriptfiles can contain any characters allowed by the specified
499+ encoding, controlfiles should contain only plain ASCII, because there
500+ is no way for <productname>PostgreSQL</> to know what encodinga
471501 control file is in. In practice this is only an issue if you want to
472502 use non-ASCII characters in the extension's comment. Recommended
473- practice in that case is to not use the <varname>comment</> parameter
474- in the control file , but instead use <command>COMMENT ON EXTENSION</>
475- withinthe script file to set the comment.
503+ practice in that case is to not use thecontrol file <varname>comment</>
504+ parameter , but instead use <command>COMMENT ON EXTENSION</>
505+ withina script file to set the comment.
476506 </para>
477507
478508 </sect2>
@@ -629,6 +659,91 @@ SELECT pg_catalog.pg_extension_config_dump('my_config', 'WHERE NOT standard_entr
629659 </para>
630660 </sect2>
631661
662+ <sect2>
663+ <title>Extension Updates</title>
664+
665+ <para>
666+ One advantage of the extension mechanism is that it provides convenient
667+ ways to manage updates to the SQL commands that define an extension's
668+ objects. This is done by associating a version name or number with
669+ each released version of the extension's installation script.
670+ In addition, if you want users to be able to update their databases
671+ dynamically from one version to the next, you should provide
672+ <firstterm>update scripts</> that make the necessary changes to go from
673+ one version to the next. Update scripts have names following the pattern
674+ <literal><replaceable>extension</>-<replaceable>oldversion</>-<replaceable>newversion</>.sql</literal>
675+ (for example, <literal>foo-1.0-1.1.sql</> contains the commands to modify
676+ version <literal>1.0</> of extension <literal>foo</> into version
677+ <literal>1.1</>).
678+ </para>
679+
680+ <para>
681+ Given that a suitable update script is available, the command
682+ <command>ALTER EXTENSION ... UPDATE</> will update an installed extension
683+ to the specified new version. The update script is run in the same
684+ environment that <command>CREATE EXTENSION</> provides for installation
685+ scripts: in particular, <varname>search_path</> is set up in the same
686+ way, and any new objects created by the script are automatically added
687+ to the extension.
688+ </para>
689+
690+ <para>
691+ The update mechanism can be used to solve an important special case:
692+ converting a <quote>loose</> collection of objects into an extension.
693+ Before the extension mechanism was added to
694+ <productname>PostgreSQL</productname> (in 9.1), many people wrote
695+ extension modules that simply created assorted unpackaged objects.
696+ Given an existing database containing such objects, how can we convert
697+ the objects into a properly packaged extension? Dropping them and then
698+ doing a plain <command>CREATE EXTENSION</> is one way, but it's not
699+ desirable if the objects have dependencies (for example, if there are
700+ table columns of a data type created by the extension). The way to fix
701+ this situation is to create an empty extension, then use <command>ALTER
702+ EXTENSION ADD</> to attach each pre-existing object to the extension,
703+ then finally create any new objects that are in the current extension
704+ version but were not in the unpackaged release. <command>CREATE
705+ EXTENSION</> supports this case with its <literal>FROM</> <replaceable
706+ class="parameter">old_version</> option, which causes it to not run the
707+ normal installation script for the target version, but instead the update
708+ script named
709+ <literal><replaceable>extension</>-<replaceable>old_version</>-<replaceable>target_version</>.sql</literal>.
710+ The choice of the dummy version name to use as <replaceable
711+ class="parameter">old_version</> is up to the extension author, though
712+ <literal>unpackaged</> is a common convention. If you have multiple
713+ prior versions you need to be able to update into extension style, use
714+ multiple dummy version names to identify them.
715+ </para>
716+
717+ <para>
718+ <command>ALTER EXTENSION</> is able to execute sequences of update
719+ script files to achieve a requested update. For example, if only
720+ <literal>foo-1.0-1.1.sql</> and <literal>foo-1.1-2.0.sql</> are
721+ available, <command>ALTER EXTENSION</> will apply them in sequence if an
722+ update to version <literal>2.0</> is requested when <literal>1.0</> is
723+ currently installed.
724+ </para>
725+
726+ <para>
727+ <productname>PostgreSQL</> doesn't assume anything about the properties
728+ of version names: for example, it does not know whether <literal>1.1</>
729+ follows <literal>1.0</>. It just matches up the available version names
730+ and follows the path that requires applying the fewest update scripts.
731+ </para>
732+
733+ <para>
734+ Sometimes it is useful to provide <quote>downgrade</> scripts, for
735+ example <literal>foo-1.1-1.0.sql</> to allow reverting the changes
736+ associated with version <literal>1.1</>. If you do that, be careful
737+ of the possibility that a downgrade script might unexpectedly
738+ get applied because it yields a shorter path. The risky case is where
739+ there is a <quote>fast path</> update script that jumps ahead several
740+ versions as well as a downgrade script to the fast path's start point.
741+ It might take fewer steps to apply the downgrade and then the fast
742+ path than to move ahead one version at a time. If the downgrade script
743+ drops any irreplaceable objects, this will yield undesirable results.
744+ </para>
745+ </sect2>
746+
632747 <sect2>
633748 <title>Extension Example</title>
634749
@@ -640,7 +755,7 @@ SELECT pg_catalog.pg_extension_config_dump('my_config', 'WHERE NOT standard_entr
640755 </para>
641756
642757 <para>
643- The script file <filename>pair.sql</> looks like this:
758+ The script file <filename>pair-1.0 .sql</> looks like this:
644759
645760<programlisting><![CDATA[
646761CREATE TYPE pair AS ( k text, v text );
@@ -671,7 +786,7 @@ CREATE OPERATOR ~> (LEFTARG = text, RIGHTARG = text, PROCEDURE = pair);
671786<programlisting>
672787# pair extension
673788comment = 'A key/value pair data type'
674- version = '0.1.2 '
789+ default_version = '1.0 '
675790relocatable = true
676791</programlisting>
677792 </para>
@@ -682,7 +797,7 @@ relocatable = true
682797
683798<programlisting>
684799EXTENSION = pair
685- DATA = pair.sql
800+ DATA = pair-1.0 .sql
686801
687802PG_CONFIG = pg_config
688803PGXS := $(shell $(PG_CONFIG) --pgxs)
@@ -739,7 +854,7 @@ include $(PGXS)
739854<programlisting>
740855MODULES = isbn_issn
741856EXTENSION = isbn_issn
742- DATA_built = isbn_issn.sql
857+ DATA_built = isbn_issn-1.0 .sql
743858DOCS = README.isbn_issn
744859
745860PG_CONFIG = pg_config