{"id":366,"date":"2021-12-12T13:31:16","date_gmt":"2021-12-12T21:31:16","guid":{"rendered":"http:\/\/aklaver.org\/wordpress\/?p=366"},"modified":"2021-12-14T08:27:44","modified_gmt":"2021-12-14T16:27:44","slug":"compiling-plpython3u","status":"publish","type":"post","link":"https:\/\/aklaver.org\/wordpress\/2021\/12\/12\/compiling-plpython3u\/","title":{"rendered":"Compiling plpython(3)u"},"content":{"rendered":"\n<p>Time marches and Python 2 is now past EOL, with the last release 2.7 no longer<br>supported as of 2020-01-01. In the Postgres world Python 2 lives on though as the<br>default version when referring to the procedural language plpythonu per<br>documentation <a href=\"https:\/\/www.postgresql.org\/docs\/current\/plpython-python23.html\">plpython 2\/3<\/a>. In order to use a Python 3 version of the procedural language the plpython3u variant needs to be present. For users getting their Postgres through a packaging system; DEB, RPM, etc this is taken care of with packages available for both versions of Python. If you are compiling Postgres from source, either from necessity or habit, the process is a bit more involved.<\/p>\n\n\n\n<p>At this point plpythonu will only be built for Python 2(&gt;=2.6). When you run<br>configure it will probe for Python versions in the order of python, python3,<br>python2. If python -V resolves to a Python 2 version then you will get<br>plpythonu if it resolves to Python 3 you will get plpython3u. The question is how to build for the other version of Python? The answer is you can put your thumb on the scale by setting the environment variable PYTHON to the Python version you want built:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nexport PYTHON=python3\n<\/pre><\/div>\n\n\n<p>What follows is the procedure I use where the system python is Python 2.<\/p>\n\n\n\n<p>Initial configure. For this example I am using a minimal setup to just show the Python building. Normally there would be additional arguments say &#8211;with-openssl &#8211;with-libxml, etc.<\/p>\n\n\n\n<p>Verify what python is pointing to.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\npython -V\n2.7.x\n<\/pre><\/div>\n\n\n<p>The configure finds the Python 2 libraries.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n\/configure --with-python --prefix=\/usr\/local\/pgsql14\nchecking for python\u2026 \/usr\/bin\/python\nconfigure: using python 2.7.18 (default, Apr 23 2020, 09:27:04) &#x5B;GCC]\nchecking for Python distutils module\u2026 yes\nchecking Python configuration directory\u2026 \/usr\/lib64\/python2.7\/config\nchecking Python include directories\u2026 -I\/usr\/include\/python2.7\nchecking how to link an embedded Python application\u2026 -L\/usr\/lib64 -lpython2.7\n-lpthread -ldl -lutil -lm\n<\/pre><\/div>\n\n\n<p>Build the Python 2 version of the procedural language and install it. At this point the make is done at the top level of the build to have the entire source tree be built.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmake\nsudo make install\n\/usr\/bin\/mkdir -p '\/usr\/local\/pgsql14\/lib64'\n\/usr\/bin\/install -c -m 755 plpython2.so '\/usr\/local\/pgsql14\/lib64\/plpython2.so'\n\/usr\/bin\/mkdir -p '\/usr\/local\/pgsql14\/share\/extension' '\/usr\/local\/pgsql14\/include\/server' '\/usr\/local\/pgsql14\/lib64\/pgxs\/src\/pl\/plpython'\n\/usr\/bin\/install -c -m 644 .\/plpython2u.control .\/plpython2u--1.0.sql .\/plpythonu.control .\/plpythonu--1.0.sql '\/usr\/local\/pgsql14\/share\/extension\/'\n\/usr\/bin\/install -c -m 644 .\/plpython.h .\/plpy_cursorobject.h .\/plpy_elog.h .\/plpy_exec.h .\/plpy_main.h .\/plpy_planobject.h .\/plpy_plpymodule.h .\/plpy_procedure.h .\/plpy_resultobject.h .\/plpy_spi.h .\/plpy_subxactobject.h .\/plpy_typeio.h .\/plpy_util.h '\/usr\/local\/pgsql14\/include\/server'\n\/usr\/bin\/install -c -m 644 .\/regress-python3-mangle.mk '\/usr\/local\/pgsql14\/lib64\/pgxs\/src\/pl\/plpython'\/\\\n<\/pre><\/div>\n\n\n<p><\/p>\n\n\n\n<p>The procedural language plpython2u is essentially symlinked to plpythonu in the above.<\/p>\n\n\n\n<p>Use the PYTHON environment variable to point at python3. Verify that variable was set.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nexport PYTHON=python3\nenv | grep -i PYTHON\nPYTHON=python3\n<\/pre><\/div>\n\n\n<p>Run configure again to pick up the new version(3) of Python, which it does.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n.\/configure --with-python --prefix=\/usr\/local\/pgsql14\nchecking for PYTHON\u2026 python3\nconfigure: using python 3.6.12 (default, Dec 02 2020, 09:44:23) &#x5B;GCC]\nchecking for Python distutils module\u2026 yes\nchecking Python configuration directory\u2026 \/usr\/lib64\/python3.6\/config-3.6m-x86_64-linux-gnu\nchecking Python include directories\u2026 -I\/usr\/include\/python3.6m\nchecking how to link an embedded Python application\u2026 -L\/usr\/lib64 -lpython3.6m -lpthread -ldl -lutil -lm\n<\/pre><\/div>\n\n\n<p>Change directories to get to plpython subdirectory. Run make clean to get rid of previous Python 2 build. Then do the make\/make install to build install plpython3u.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncd src\/pl\/plpython\/\nmake clean\nmake\nsudo make install\n\n\/usr\/bin\/mkdir -p '\/usr\/local\/pgsql14\/lib64'\n\/usr\/bin\/install -c -m 755 plpython3.so '\/usr\/local\/pgsql14\/lib64\/plpython3.so'\n\/usr\/bin\/mkdir -p '\/usr\/local\/pgsql14\/share\/extension' '\/usr\/local\/pgsql14\/include\/server' '\/usr\/local\/pgsql14\/lib64\/pgxs\/src\/pl\/plpython'\n\/usr\/bin\/install -c -m 644 .\/plpython3u.control .\/plpython3u--1.0.sql '\/usr\/local\/pgsql14\/share\/extension\/'\n\/usr\/bin\/install -c -m 644 .\/plpython.h .\/plpy_cursorobject.h .\/plpy_elog.h .\/plpy_exec.h .\/plpy_main.h .\/plpy_planobject.h .\/plpy_plpymodule.h .\/plpy_procedure.h .\/plpy_resultobject.h .\/plpy_spi.h .\/plpy_subxactobject.h .\/plpy_typeio.h .\/plpy_util.h '\/usr\/local\/pgsql14\/include\/server'\n\/usr\/bin\/install -c -m 644 .\/regress-python3-mangle.mk '\/usr\/local\/pgsql14\/lib64\/pgxs\/src\/pl\/plpython'\n<\/pre><\/div>\n\n\n<p>Now that they have been built it is just a matter of installing them, as extensions, into the database or databases of you choice.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncreate extension plpythonu;\ncreate extension plpython3u;\n<\/pre><\/div>\n\n\n<p>One heads up about having both versions installed in the same database.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nDO $$\n    plpy.notice('Python 2')\n$$ LANGUAGE plpythonu;\nNOTICE:  Python 2\nDO\nDO $$\n    plpy.notice('Python 3')\n$$ LANGUAGE plpython3u;\nFATAL:  multiple Python libraries are present in session\nDETAIL:  Only one Python major version can be used in one session.\nserver closed the connection unexpectedly\n        This probably means the server terminated abnormally\n        before or while processing the request.\nThe connection to the server was lost. Attempting reset: Succeeded.\n\n<\/pre><\/div>\n\n\n<p>As the message says you can only use one plpythonu version at a time in a given session.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Time marches and Python 2 is now past EOL, with the last release 2.7 no longersupported as of 2020-01-01. In the Postgres world Python 2 lives on though as thedefault version when referring to the procedural language plpythonu perdocumentation plpython &hellip; <a href=\"https:\/\/aklaver.org\/wordpress\/2021\/12\/12\/compiling-plpython3u\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-366","post","type-post","status-publish","format-standard","hentry","category-postgres"],"_links":{"self":[{"href":"https:\/\/aklaver.org\/wordpress\/wp-json\/wp\/v2\/posts\/366","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aklaver.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aklaver.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aklaver.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aklaver.org\/wordpress\/wp-json\/wp\/v2\/comments?post=366"}],"version-history":[{"count":15,"href":"https:\/\/aklaver.org\/wordpress\/wp-json\/wp\/v2\/posts\/366\/revisions"}],"predecessor-version":[{"id":381,"href":"https:\/\/aklaver.org\/wordpress\/wp-json\/wp\/v2\/posts\/366\/revisions\/381"}],"wp:attachment":[{"href":"https:\/\/aklaver.org\/wordpress\/wp-json\/wp\/v2\/media?parent=366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aklaver.org\/wordpress\/wp-json\/wp\/v2\/categories?post=366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aklaver.org\/wordpress\/wp-json\/wp\/v2\/tags?post=366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}