@@ -1853,6 +1853,104 @@ find_install_path(List *evi_list, ExtensionVersionInfo *evi_target,
18531853 return evi_start ;
18541854}
18551855
1856+ /*
1857+ * Figures out which script(s) we need to run to install the desired
1858+ * version of the extension. If we do not have a script that directly
1859+ * does what is needed, we try to find a sequence of update scripts that
1860+ * will get us there.
1861+ */
1862+ static List *
1863+ find_versions_to_apply (ExtensionControlFile * pcontrol , const char * * versionName )
1864+ {
1865+ char * filename ;
1866+ struct stat fst ;
1867+ List * updateVersions ;
1868+ List * evi_list ;
1869+ ExtensionVersionInfo * evi_start ;
1870+ ExtensionVersionInfo * evi_target ;
1871+
1872+ filename = get_extension_script_filename (pcontrol , NULL , * versionName );
1873+ if (!tleext && stat (filename , & fst ) == 0 )
1874+ updateVersions = NIL ; /* Easy, no extra scripts */
1875+ else if (tleext && funcstat (filename ))
1876+ updateVersions = NIL ; /* Also easy, no extra scripts */
1877+ else
1878+ {
1879+ /*
1880+ * Look for best way to install this version
1881+ *
1882+ * Extract the version update graph from the script directory
1883+ */
1884+ evi_list = get_ext_ver_list (pcontrol );
1885+
1886+ /* Identify the target version */
1887+ evi_target = get_ext_ver_info (* versionName , & evi_list );
1888+
1889+ /* Identify best path to reach target */
1890+ evi_start = find_install_path (evi_list , evi_target ,
1891+ & updateVersions );
1892+
1893+ /* Fail if no path ... */
1894+ if (evi_start == NULL )
1895+ ereport (ERROR ,
1896+ (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
1897+ errmsg ("extension \"%s\" has no installation script nor update path for version \"%s\"" ,
1898+ pcontrol -> name , * versionName )));
1899+
1900+ /* Otherwise, install best starting point and then upgrade */
1901+ * versionName = evi_start -> name ;
1902+ }
1903+
1904+ return updateVersions ;
1905+ }
1906+
1907+ static void
1908+ record_sql_function_dependencies (char * extensionName ,
1909+ const char * versionName ,
1910+ List * updateVersions ,
1911+ ObjectAddress address )
1912+ {
1913+ char * sqlname ;
1914+ Oid sqlfuncid ;
1915+ ObjectAddress sqlfunc ;
1916+
1917+ sqlname = psprintf ("%s--%s.sql" , extensionName , versionName );
1918+ sqlfuncid = get_tlefunc_oid_if_exists (sqlname );
1919+
1920+ /* If it exists, record dependencies on sql function for base version */
1921+ if (sqlfuncid != InvalidOid )
1922+ {
1923+ sqlfunc .classId = ProcedureRelationId ;
1924+ sqlfunc .objectId = sqlfuncid ;
1925+ sqlfunc .objectSubId = 0 ;
1926+ recordDependencyOn (& address , & sqlfunc , DEPENDENCY_NORMAL );
1927+ }
1928+
1929+ /*
1930+ * If necessary update scripts are found, record dependency on each script
1931+ */
1932+ if (updateVersions != NULL )
1933+ {
1934+ const char * oldVersionName = versionName ;
1935+ ListCell * lcv ;
1936+
1937+ foreach (lcv , updateVersions )
1938+ {
1939+ ObjectAddress upgradesqlfunc ;
1940+
1941+ versionName = (char * ) lfirst (lcv );
1942+ sqlname = psprintf ("%s--%s--%s.sql" , extensionName , oldVersionName , versionName );
1943+ sqlfuncid = get_tlefunc_oid_if_exists (sqlname );
1944+
1945+ upgradesqlfunc .classId = ProcedureRelationId ;
1946+ upgradesqlfunc .objectId = sqlfuncid ;
1947+ upgradesqlfunc .objectSubId = 0 ;
1948+
1949+ recordDependencyOn (& address , & upgradesqlfunc , DEPENDENCY_NORMAL );
1950+ }
1951+ }
1952+ }
1953+
18561954/*
18571955 * CREATE EXTENSION worker
18581956 *
@@ -1875,7 +1973,6 @@ CreateExtensionInternal(char *extensionName,
18751973 ExtensionControlFile * pcontrol ;
18761974 ExtensionControlFile * control ;
18771975 char * filename ;
1878- struct stat fst ;
18791976 List * updateVersions ;
18801977 List * requiredExtensions ;
18811978 List * requiredSchemas ;
@@ -1885,13 +1982,7 @@ CreateExtensionInternal(char *extensionName,
18851982 bool prevTLEState ;
18861983 char * ctlname ;
18871984 Oid ctlfuncid ;
1888- char * sqlname ;
1889- Oid sqlfuncid ;
1890- List * evi_list ;
1891- ExtensionVersionInfo * evi_start ;
1892- ExtensionVersionInfo * evi_target ;
18931985 ObjectAddress ctlfunc ;
1894- ObjectAddress sqlfunc ;
18951986
18961987 /*
18971988 * We have to do some state checking here if we are cascading through a
@@ -1926,43 +2017,7 @@ CreateExtensionInternal(char *extensionName,
19262017 }
19272018 check_valid_version_name (versionName );
19282019
1929- /*
1930- * Figure out which script(s) we need to run to install the desired
1931- * version of the extension. If we do not have a script that directly
1932- * does what is needed, we try to find a sequence of update scripts that
1933- * will get us there.
1934- */
1935- filename = get_extension_script_filename (pcontrol , NULL , versionName );
1936- if (!tleext && stat (filename , & fst ) == 0 )
1937- updateVersions = NIL ; /* Easy, no extra scripts */
1938- else if (tleext && funcstat (filename ))
1939- updateVersions = NIL ; /* Also easy, no extra scripts */
1940- else
1941- {
1942- /*
1943- * Look for best way to install this version
1944- *
1945- * Extract the version update graph from the script directory
1946- */
1947- evi_list = get_ext_ver_list (pcontrol );
1948-
1949- /* Identify the target version */
1950- evi_target = get_ext_ver_info (versionName , & evi_list );
1951-
1952- /* Identify best path to reach target */
1953- evi_start = find_install_path (evi_list , evi_target ,
1954- & updateVersions );
1955-
1956- /* Fail if no path ... */
1957- if (evi_start == NULL )
1958- ereport (ERROR ,
1959- (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
1960- errmsg ("extension \"%s\" has no installation script nor update path for version \"%s\"" ,
1961- pcontrol -> name , versionName )));
1962-
1963- /* Otherwise, install best starting point and then upgrade */
1964- versionName = evi_start -> name ;
1965- }
2020+ updateVersions = find_versions_to_apply (pcontrol , & versionName );
19662021
19672022 /*
19682023 * Fetch control parameters for installation target version
@@ -2122,47 +2177,24 @@ CreateExtensionInternal(char *extensionName,
21222177 if (ctlfuncid == InvalidOid )
21232178 elog (ERROR , "could not find control function %s for extension %s in schema %s" , quote_identifier (ctlname ), quote_identifier (extensionName ), quote_identifier (PG_TLE_NSPNAME ));
21242179
2125- sqlname = psprintf ("%s--%s.sql" , extensionName , versionName );
2126- sqlfuncid = get_tlefunc_oid_if_exists (sqlname );
2127-
21282180 /* Record dependencies on control function */
21292181 ctlfunc .classId = ProcedureRelationId ;
21302182 ctlfunc .objectId = ctlfuncid ;
21312183 ctlfunc .objectSubId = 0 ;
21322184 recordDependencyOn (& address , & ctlfunc , DEPENDENCY_NORMAL );
21332185
2134- /* If it exists, record dependencies on sql function for base version */
2135- if (sqlfuncid != InvalidOid )
2136- {
2137- sqlfunc .classId = ProcedureRelationId ;
2138- sqlfunc .objectId = sqlfuncid ;
2139- sqlfunc .objectSubId = 0 ;
2140- recordDependencyOn (& address , & sqlfunc , DEPENDENCY_NORMAL );
2141- }
2186+ record_sql_function_dependencies (extensionName , versionName , updateVersions , address );
21422187
21432188 /*
2144- * If necessary update scripts are found, record dependency on each
2145- * script
2189+ * Record dependencies such that default version can be installed
2190+ * after a pg_dump
21462191 */
2147- if (updateVersions != NULL )
2192+ if (pcontrol -> default_version )
21482193 {
2149- const char * oldVersionName = versionName ;
2150- ListCell * lcv ;
2151-
2152- foreach (lcv , updateVersions )
2153- {
2154- ObjectAddress upgradesqlfunc ;
2155-
2156- versionName = (char * ) lfirst (lcv );
2157- sqlname = psprintf ("%s--%s--%s.sql" , extensionName , oldVersionName , versionName );
2158- sqlfuncid = get_tlefunc_oid_if_exists (sqlname );
2194+ const char * defaultVersion = pcontrol -> default_version ;
21592195
2160- upgradesqlfunc .classId = ProcedureRelationId ;
2161- upgradesqlfunc .objectId = sqlfuncid ;
2162- upgradesqlfunc .objectSubId = 0 ;
2163-
2164- recordDependencyOn (& address , & upgradesqlfunc , DEPENDENCY_NORMAL );
2165- }
2196+ updateVersions = find_versions_to_apply (pcontrol , & defaultVersion );
2197+ record_sql_function_dependencies (extensionName , defaultVersion , updateVersions , address );
21662198 }
21672199 }
21682200
@@ -4876,6 +4908,9 @@ pg_tle_set_default_version(PG_FUNCTION_ARGS)
48764908 char * ctlsql ;
48774909 ExtensionControlFile * control ;
48784910 char * filename ;
4911+ List * updateVersions ;
4912+ Oid extensionOid ;
4913+ ObjectAddress extAddress ;
48794914
48804915 if (PG_ARGISNULL (0 ))
48814916 ereport (ERROR ,
@@ -4974,6 +5009,25 @@ pg_tle_set_default_version(PG_FUNCTION_ARGS)
49745009 if (SPI_finish () != SPI_OK_FINISH )
49755010 elog (ERROR , "SPI_finish failed" );
49765011
5012+ /*
5013+ * When default version is updated we update the dependencies so that
5014+ * pg_dump can maintain the correct order.
5015+ */
5016+ extensionOid = get_extension_oid (extname , true);
5017+ if (extensionOid != InvalidOid )
5018+ {
5019+ const char * defaultVersion = control -> default_version ;
5020+
5021+ extAddress .classId = ExtensionRelationId ;
5022+ extAddress .objectId = extensionOid ;
5023+ extAddress .objectSubId = 0 ;
5024+
5025+ SET_TLEEXT ;
5026+ updateVersions = find_versions_to_apply (control , & defaultVersion );
5027+ UNSET_TLEEXT ;
5028+ record_sql_function_dependencies (extname , defaultVersion , updateVersions , extAddress );
5029+ }
5030+
49775031 /* flag that we are done manipulating pg_tle artifacts */
49785032 UNSET_TLEART ;
49795033
0 commit comments