Skip to content

Commit abc3120

Browse files
committed
Add server support for "plugin" libraries that can be used for add-on tasks
such as debugging and performance measurement. This consists of two features: a table of "rendezvous variables" that allows separately-loaded shared libraries to communicate, and a new GUC setting "local_preload_libraries" that allows libraries to be loaded into specific sessions without explicit cooperation from the client application. To make local_preload_libraries as flexible as possible, we do not restrict its use to superusers; instead, it is restricted to load only libraries stored in $libdir/plugins/. The existing LOAD command has also been modified to allow non-superusers to LOAD libraries stored in this directory. This patch also renames the existing GUC variable preload_libraries to shared_preload_libraries (after a suggestion by Simon Riggs) and does some code refactoring in dfmgr.c to improve clarity. Korry Douglas, with a little help from Tom Lane.
1 parent 66541c5 commit abc3120

File tree

11 files changed

+353
-113
lines changed

11 files changed

+353
-113
lines changed

doc/src/sgml/config.sgml

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.73 2006/08/08 19:15:07 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.74 2006/08/15 18:26:58 tgl Exp $ -->
22

33
<chapter Id="runtime-config">
44
<title>Server Configuration</title>
@@ -949,10 +949,10 @@ SET ENABLE_SEQSCAN TO OFF;
949949
</listitem>
950950
</varlistentry>
951951

952-
<varlistentry id="guc-preload-libraries" xreflabel="preload_libraries">
953-
<term><varname>preload_libraries</varname> (<type>string</type>)</term>
952+
<varlistentry id="guc-shared-preload-libraries" xreflabel="shared_preload_libraries">
953+
<term><varname>shared_preload_libraries</varname> (<type>string</type>)</term>
954954
<indexterm>
955-
<primary><varname>preload_libraries</> configuration parameter</primary>
955+
<primary><varname>shared_preload_libraries</> configuration parameter</primary>
956956
</indexterm>
957957
<listitem>
958958
<para>
@@ -963,6 +963,7 @@ SET ENABLE_SEQSCAN TO OFF;
963963
<literal>mylib.so</> (or on some platforms,
964964
<literal>mylib.sl</>) to be preloaded from the installation's
965965
standard library directory.
966+
This parameter can only be set at server start.
966967
</para>
967968

968969
<para>
@@ -3642,6 +3643,60 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir'
36423643
</para>
36433644
</listitem>
36443645
</varlistentry>
3646+
3647+
<varlistentry id="guc-local-preload-libraries" xreflabel="local_preload_libraries">
3648+
<term><varname>local_preload_libraries</varname> (<type>string</type>)</term>
3649+
<indexterm>
3650+
<primary><varname>local_preload_libraries</> configuration parameter</primary>
3651+
</indexterm>
3652+
<indexterm>
3653+
<primary><filename>$libdir/plugins</></primary>
3654+
</indexterm>
3655+
<listitem>
3656+
<para>
3657+
This variable specifies one or more shared libraries that are
3658+
to be preloaded at connection start. If more than one library
3659+
is to be loaded, separate their names with commas.
3660+
This parameter cannot be changed after the start of a particular
3661+
session.
3662+
</para>
3663+
3664+
<para>
3665+
Because this is not a superuser-only option, the libraries
3666+
that can be loaded are restricted to those appearing in the
3667+
<filename>plugins</> subdirectory of the installation's
3668+
standard library directory. (It is the database administrator's
3669+
responsibility to ensure that only <quote>safe</> libraries
3670+
are installed there.) Entries in <varname>local_preload_libraries</>
3671+
can specify this directory explicitly, for example
3672+
<literal>$libdir/plugins/mylib</literal>, or just specify
3673+
the library name &mdash; <literal>mylib</literal> would have
3674+
the same effect as <literal>$libdir/plugins/mylib</literal>.
3675+
</para>
3676+
3677+
<para>
3678+
There is no performance advantage to loading a library at session
3679+
start rather than when it is first used. Rather, the intent of
3680+
this feature is to allow debugging or performance-measurement
3681+
libraries to be loaded into specific sessions without an explicit
3682+
<command>LOAD</> command being given. For example, debugging could
3683+
be enabled for all sessions under a given user name by setting
3684+
this parameter with <command>ALTER USER SET</>.
3685+
</para>
3686+
3687+
<para>
3688+
If a specified library is not found,
3689+
the connection attempt will fail.
3690+
</para>
3691+
3692+
<para>
3693+
Every PostgreSQL-supported library has a <quote>magic
3694+
block</> that is checked to guarantee compatibility.
3695+
For this reason, non-PostgreSQL libraries cannot be
3696+
loaded in this way.
3697+
</para>
3698+
</listitem>
3699+
</varlistentry>
36453700

36463701
</variablelist>
36473702
</sect2>

doc/src/sgml/ref/load.sgml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/load.sgml,v 1.21 2005/01/04 00:39:53 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/load.sgml,v 1.22 2006/08/15 18:26:58 tgl Exp $
33
-->
44

55
<refentry id="SQL-LOAD">
@@ -44,6 +44,19 @@ LOAD '<replaceable class="PARAMETER">filename</replaceable>'
4444
shared library file name extension. See <xref linkend="xfunc-c"> for
4545
more information on this topic.
4646
</para>
47+
48+
<indexterm>
49+
<primary><filename>$libdir/plugins</></primary>
50+
</indexterm>
51+
52+
<para>
53+
Non-superusers may only apply <command>LOAD</> to library files
54+
located in <filename>$libdir/plugins/</> &mdash; the specified
55+
<replaceable class="PARAMETER">filename</replaceable> must begin
56+
with exactly that string. (It is the database administrator's
57+
responsibility to ensure that only <quote>safe</> libraries
58+
are installed there.)
59+
</para>
4760
</refsect1>
4861

4962
<refsect1 id="sql-load-compat">

src/backend/postmaster/postmaster.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.498 2006/08/08 19:15:07 tgl Exp $
40+
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.499 2006/08/15 18:26:58 tgl Exp $
4141
*
4242
* NOTES
4343
*
@@ -709,7 +709,7 @@ PostmasterMain(int argc, char *argv[])
709709
/*
710710
* process any libraries that should be preloaded at postmaster start
711711
*/
712-
process_preload_libraries();
712+
process_shared_preload_libraries();
713713

714714
/*
715715
* Remove old temporary files. At this point there can be no other

src/backend/tcop/postgres.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.498 2006/08/13 22:18:08 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.499 2006/08/15 18:26:58 tgl Exp $
1212
*
1313
* NOTES
1414
* this is the "main" module of the postgres backend and
@@ -3000,6 +3000,12 @@ PostgresMain(int argc, char *argv[], const char *username)
30003000
if (IsUnderPostmaster && Log_disconnections)
30013001
on_proc_exit(log_disconnections, 0);
30023002

3003+
/*
3004+
* process any libraries that should be preloaded at backend start
3005+
* (this likewise can't be done until GUC settings are complete)
3006+
*/
3007+
process_local_preload_libraries();
3008+
30033009
/*
30043010
* Send this backend's cancellation info to the frontend.
30053011
*/

src/backend/tcop/utility.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/tcop/utility.c,v 1.265 2006/08/12 20:05:56 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/tcop/utility.c,v 1.266 2006/08/15 18:26:58 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -886,12 +886,9 @@ ProcessUtility(Node *parsetree,
886886
{
887887
LoadStmt *stmt = (LoadStmt *) parsetree;
888888

889-
if (!superuser())
890-
ereport(ERROR,
891-
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
892-
errmsg("must be superuser to do LOAD")));
893889
closeAllVfds(); /* probably not necessary... */
894-
load_file(stmt->filename);
890+
/* Allowed names are restricted if you're not superuser */
891+
load_file(stmt->filename, !superuser());
895892
}
896893
break;
897894

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy