Commit Graph

301 Commits

Author SHA1 Message Date
Bruce Momjian 6560407c7d Pgindent run before 9.1 beta2. 2011-06-09 14:32:50 -04:00
Andrew Dunstan ccd69b8886 Allow building with perl 5.14.
Patch from Alex Hunsaker.
2011-06-04 19:32:10 -04:00
Alvaro Herrera 7de38741c0 Remove usage of &PL_sv_undef in hashes and arrays
According to perlguts, &PL_sv_undef is not the right thing to use in
those cases because it doesn't behave the same way as an undef value via
Perl code.  Seems the intuitive way to deal with undef values is subtly
enough broken that it's hard to notice when misused.

The broken uses got inadvertently introduced in commit
87bb2ade2c by Alexey Klyukin, Alex
Hunsaker and myself on 2011-02-17; no backpatch is necessary.

Per testing report from Greg Mullane.

Author: Alex Hunsaker
2011-05-30 12:42:48 -04:00
Alvaro Herrera 6b449d9051 Fix declaration of $_TD in "strict" trigger functions
This was broken in commit ef19dc6d39 by
the Bunce/Hunsaker/Dunstan team, which moved the declaration from
plperl_create_sub to plperl_call_perl_trigger_func.  This doesn't
actually work because the validator code would not find the variable
declared; and even if you manage to get past the validator, it still
doesn't work because get_sv("_TD", GV_ADD) doesn't have the expected
effect.  The only reason this got beyond testing is that it only fails
in strict mode.

We need to declare it as a global just like %_SHARED; it is simpler than
trying to actually do what the patch initially intended, and is said to
have the same performance benefit.

As a more serious issue, fix $_TD not being properly local()ized,
meaning nested trigger functions would clobber $_TD.

Alex Hunsaker, per test report from Greg Mullane
2011-05-18 23:56:18 -04:00
Andrew Dunstan 860be17ec3 Assorted minor changes to silence Windows compiler warnings.
Mostly to do with macro redefinitions or object signedness.
2011-04-25 12:56:53 -04:00
Bruce Momjian bf50caf105 pgindent run before PG 9.1 beta 1. 2011-04-10 11:42:00 -04:00
Tom Lane 2594cf0e8c Revise the API for GUC variable assign hooks.
The previous functions of assign hooks are now split between check hooks
and assign hooks, where the former can fail but the latter shouldn't.
Aside from being conceptually clearer, this approach exposes the
"canonicalized" form of the variable value to guc.c without having to do
an actual assignment.  And that lets us fix the problem recently noted by
Bernd Helmle that the auto-tune patch for wal_buffers resulted in bogus
log messages about "parameter "wal_buffers" cannot be changed without
restarting the server".  There may be some speed advantage too, because
this design lets hook functions avoid re-parsing variable values when
restoring a previous state after a rollback (they can store a pre-parsed
representation of the value instead).  This patch also resolves a
longstanding annoyance about custom error messages from variable assign
hooks: they should modify, not appear separately from, guc.c's own message
about "invalid parameter value".
2011-04-07 00:12:02 -04:00
Tom Lane 63b656b7bf Create extension infrastructure for the core procedural languages.
This mostly just involves creating control, install, and
update-from-unpackaged scripts for them.  However, I had to adjust plperl
and plpython to not share the same support functions between variants,
because we can't put the same function into multiple extensions.

catversion bump forced due to new contents of pg_pltemplate, and because
initdb now installs plpgsql as an extension not a bare language.

Add support for regression testing these as extensions not bare
languages.

Fix a couple of other issues that popped up while testing this: my initial
hack at pg_dump binary-upgrade support didn't work right, and we don't want
an extra schema permissions test after all.

Documentation changes still to come, but I'm committing now to see
whether the MSVC build scripts need work (likely they do).
2011-03-04 21:51:14 -05:00
Bruce Momjian 964b46d00e Initialize variable to quiet compiler. 2011-02-19 08:14:32 -05:00
Alvaro Herrera 87bb2ade2c Convert Postgres arrays to Perl arrays on PL/perl input arguments
More generally, arrays are turned in Perl array references, and row and
composite types are turned into Perl hash references.  This is done
recursively, in a way that's natural to every Perl programmer.

To avoid a backwards compatibility hit, the string representation of
each structure is also available if the function requests it.

Authors: Alexey Klyukin and Alex Hunsaker.
Some code cleanups by me.
2011-02-17 22:20:40 -03:00
Alvaro Herrera f7b51d175a pgindent run on plperl.c 2011-02-17 22:20:39 -03:00
Andrew Dunstan 50d89d422f Force strings passed to and from plperl to be in UTF8 encoding.
String are converted to UTF8 on the way into perl and to the
database encoding on the way back. This avoids a number of
observed anomalies, and ensures Perl a consistent view of the
world.

Some minor code cleanups are also accomplished.

Alex Hunsaker, reviewed by Andy Colson.
2011-02-06 17:29:26 -05:00
Andrew Dunstan c73fe72e27 Add comment on why we're passing a useless 'false' to the plperl function compiler.
It's for compatibility with modules like PostgreSQL::PLPerl::NYTProf.
2011-02-02 12:45:42 -05:00
Andrew Dunstan ef19dc6d39 Set up PLPerl trigger data using C code instead of Perl code.
This is an efficiency change, and means we now no longer have to run
"out $_TD; local $_TD = shift;", which was especially pointless in the case of
non-trigger functions where the passed value was always undef anyway.

A tiny open issue is whether we should get rid of the $prolog argument of
mkfunc, and the corresponding pushed value, which is now just a constant "false".

Tim Bunce, reviewed by Alex Hunsaker.
2011-02-01 09:43:25 -05:00
Tom Lane 518b1e96c0 Suppress "control reaches end of non-void function" warning from gcc 4.5.
Not sure why I'm seeing this on Fedora 14 and not earlier versions.
Seems like a regression that gcc no longer knows that DIE() doesn't return.
Still, adding a dummy return is harmless enough.
2011-01-22 18:01:31 -05:00
Peter Eisentraut fc946c39ae Remove useless whitespace at end of lines 2010-11-23 22:34:55 +02:00
Tom Lane 76b12e0af7 Revert removal of trigger flag from plperl function hash key.
As noted by Jan Urbanski, this flag is in fact needed to ensure that the
function's input/result conversion functions are set up as expected.

Add a regression test to discourage anyone from making same mistake
in future.
2010-10-31 11:42:51 -04:00
Andrew Dunstan 6c3c7b533e Allow generic record arguments to plperl functions 2010-10-28 20:48:12 -04:00
Andrew Dunstan 2d01ec0708 Remove unnecessary use of trigger flag to hash plperl functions 2010-10-24 23:53:44 -04:00
Tom Lane 2ec993a7cb Support triggers on views.
This patch adds the SQL-standard concept of an INSTEAD OF trigger, which
is fired instead of performing a physical insert/update/delete.  The
trigger function is passed the entire old and/or new rows of the view,
and must figure out what to do to the underlying tables to implement
the update.  So this feature can be used to implement updatable views
using trigger programming style rather than rule hacking.

In passing, this patch corrects the names of some columns in the
information_schema.triggers view.  It seems the SQL committee renamed
them somewhere between SQL:99 and SQL:2003.

Dean Rasheed, reviewed by Bernd Helmle; some additional hacking by me.
2010-10-10 13:45:07 -04:00
Tom Lane 50595b5fce Use a separate interpreter for each calling SQL userid in plperl and pltcl.
There are numerous methods by which a Perl or Tcl function can subvert
the behavior of another such function executed later; for example, by
redefining standard functions or operators called by the target function.
If the target function is SECURITY DEFINER, or is called by such a
function, this means that any ordinary SQL user with Perl or Tcl language
usage rights can do essentially anything with the privileges of the target
function's owner.

To close this security hole, create a separate Perl or Tcl interpreter for
each SQL userid under which plperl or pltcl functions are executed within
a session.  However, all plperlu or pltclu functions run within a session
still share a single interpreter, since they all execute at the trust
level of a database superuser anyway.

Note: this change results in a functionality loss when libperl has been
built without the "multiplicity" option: it's no longer possible to call
plperl functions under different userids in one session, since such a
libperl can't support multiple interpreters in one process.  However, such
a libperl already failed to support concurrent use of plperl and plperlu,
so it's likely that few people use such versions with Postgres.

Security: CVE-2010-3433
2010-09-30 17:18:51 -04:00
Magnus Hagander 9f2e211386 Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00
Bruce Momjian 239d769e7e pgindent run for 9.0, second run 2010-07-06 19:19:02 +00:00
Peter Eisentraut 89474cc321 Message tuning 2010-06-29 04:12:47 +00:00
Andrew Dunstan 3659c62350 Remove perl symbol table additions for plperl functions, and mention of it
in the release notes, as it is not apparently providing anything useful.
2010-06-16 14:50:34 +00:00
Andrew Dunstan a6eeb8c10d Follow up a visit from the style police. 2010-05-17 19:43:04 +00:00
Andrew Dunstan 1f474d299d Abandon the use of Perl's Safe.pm to enforce restrictions in plperl, as it is
fundamentally insecure. Instead apply an opmask to the whole interpreter that
imposes restrictions on unsafe operations. These restrictions are much harder
to subvert than is Safe.pm, since there is no container to be broken out of.
Backported to release 7.4.

In releases 7.4, 8.0 and 8.1 this also includes the necessary backporting of
the two interpreters model for plperl and plperlu adopted in release 8.2.

In versions 8.0 and up, the use of Perl's POSIX module to undo its locale
mangling on Windows has become insecure with these changes, so it is
replaced by our own routine, which is also faster.

Nice side effects of the changes include that it is now possible to use perl's
"strict" pragma in a natural way in plperl, and that perl's $a and
$b variables now work as expected in sort routines, and that function
compilation is significantly faster.

Tim Bunce and Andrew Dunstan, with reviews from Alex Hunsaker and
Alexey Klyukin.

Security: CVE-2010-1169
2010-05-13 16:39:43 +00:00
Tom Lane 9287567eff Fix bogus order of cleanup steps in plperl_inline_handler.
Per Alex Hunsaker
2010-04-18 19:16:06 +00:00
Peter Eisentraut ab5694e80d Message style tuning 2010-03-18 19:02:46 +00:00
Peter Eisentraut c92e2702f1 Message style tuning 2010-03-17 21:31:17 +00:00
Tom Lane dac7a37dd3 Use SvROK(sv) rather than directly checking SvTYPE(sv) == SVt_RV in plperl.
The latter is considered unwarranted chumminess with the implementation,
and can lead to crashes with recent Perl versions.

Report and fix by Tim Bunce.  Back-patch to all versions containing the
questionable coding pattern.
2010-03-09 22:34:38 +00:00
Andrew Dunstan 6fcd5010e2 Add checks for valid encoding of query strings passed to SPI by plperl. 2010-03-09 02:48:33 +00:00
Bruce Momjian 65e806cba1 pgindent run for 9.0 2010-02-26 02:01:40 +00:00
Andrew Dunstan 56adf3703c Clean up package namespace use and use of Safe in plperl.
Prevent use of another buggy version of Safe.pm.
Only register the exit handler if we have  successfully created an interpreter.
Change log level of perl warnings from NOTICE to WARNING.

The infrastructure is there if in future we decide to allow
DBAs to specify extra modules that will be allowed in trusted code.
However, for now the relevant variables are declared as lexicals
rather than as package variables, so that they are not (or should not be)
accessible.

Mostly code from Tim Bunce, reviewed by Alex Hunsaker, with some
tweaks by me.
2010-02-16 21:39:52 +00:00
Alvaro Herrera 4a5ee2a419 Move main error message text in plperl into errmsg from errdetail,
and move the context information into errcontext instead of errmsg.
This makes them better conform to our guidelines.

Also remove a few errcode declarations that were providing the default
value ERRCODE_INTERNAL_ERROR.
2010-02-15 22:23:25 +00:00
Robert Haas e26c539e9f Wrap calls to SearchSysCache and related functions using macros.
The purpose of this change is to eliminate the need for every caller
of SearchSysCache, SearchSysCacheCopy, SearchSysCacheExists,
GetSysCacheOid, and SearchSysCacheList to know the maximum number
of allowable keys for a syscache entry (currently 4).  This will
make it far easier to increase the maximum number of keys in a
future release should we choose to do so, and it makes the code
shorter, too.

Design and review by Tom Lane.
2010-02-14 18:42:19 +00:00
Andrew Dunstan 1b04b8f1bc Add plperl.on_plperl_init and plperl.on_plperlu_init settings for language-specific startup. Rename recently added plperl.on_perl_init to plperl.on_init. Also, code cleanup for utf8 hack. Patch from Tim Bunce, reviewed by Alex Hunsaker. 2010-02-12 19:35:25 +00:00
Andrew Dunstan 7e30c0067c Free reference in correct Perl context. Backpatch to release 8.2. Patch from Tim Bunce. 2010-02-12 04:31:14 +00:00
Andrew Dunstan 85d67ccd75 Add plperl.on_perl_init setting to provide for initializing the perl library on load. Also, handle END blocks in plperl.
Database access is disallowed during both these operations, although it might be allowed in END blocks in future.

Patch from Tim Bunce.
2010-01-30 01:46:57 +00:00
Andrew Dunstan 1f98cccb94 Fix bug found by warning from recent gcc. patch from Tim Bunce. 2010-01-28 23:06:09 +00:00
Andrew Dunstan 1a7c2f9dea Various small improvements and cleanups for PL/Perl.
- Allow (ineffective) use of 'require' in plperl
    If the required module is not already loaded then it dies.
    So "use strict;" now works in plperl.

- Pre-load the feature module if perl >= 5.10.
    So "use feature :5.10;" now works in plperl.

- Stored procedure subs are now given names.
    The names are not visible in ordinary use, but they make
    tools like Devel::NYTProf and Devel::Cover much more useful.

- Simplified and generalized the subroutine creation code.
    Now one code path for generating sub source code, not four.
    Can generate multiple 'use' statements with specific imports
    (which handles plperl.use_strict currently and can easily
    be extended to handle a plperl.use_feature=':5.12' in future).

- Disallows use of Safe version 2.20 which is broken for PL/Perl.
    http://rt.perl.org/rt3/Ticket/Display.html?id=72068

- Assorted minor optimizations by pre-growing data structures.

Patch from Tim Bunce, reviewed by Alex Hunsaker.
2010-01-26 23:11:56 +00:00
Andrew Dunstan 05672e5045 Add utility functions to PLPerl:
quote_literal, quote_nullable, quote_ident,
    encode_bytea, decode_bytea, looks_like_number,
    encode_array_literal, encode_array_constructor.
Split SPI.xs into two - SPI.xs now contains only SPI functions. Remainder
are in new Util.xs.
Some more code and documentation cleanup along the way, as well as
adding some CVS markers to files missing them.

Original patch from Tim Bunce, with a little editing from me.
2010-01-20 01:08:21 +00:00
Andrew Dunstan a2b34b16be Tidy up and refactor plperl.c.
- Changed MULTIPLICITY check from runtime to compiletime.
    No loads the large Config module.
- Changed plperl_init_interp() to return new interp
    and not alter the global interp_state
- Moved plperl_safe_init() call into check_interp().
- Removed plperl_safe_init_done state variable
    as interp_state now covers that role.
- Changed plperl_create_sub() to take a plperl_proc_desc argument.
- Simplified return value handling in plperl_create_sub.
- Changed perl.com link in the docs to perl.org and tweaked
    wording to clarify that require, not use, is what's blocked.
- Moved perl code in large multi-line C string literal macros
    out to plc_*.pl files.
- Added a test2macro.pl utility to convert the plc_*.pl files to
    macros in a perlchunks.h file which is #included
- Simplifed plperl_safe_init() slightly
- Optimized pg_verifymbstr calls to avoid unneeded strlen()s.

Patch from Tim Bunce, with minor editing from me.
2010-01-09 02:40:50 +00:00
Andrew Dunstan 1c4c741ea9 Check values passed back from PLPerl to the database, via function return,
trigger tuple modification or SPI call, to ensure they are valid in the
server encoding. Along the way, replace uses of SvPV(foo, PL_na)
with SvPV_nolen(foo) as recommended in the perl docs. Bug report from
Hannu Krosing.
2010-01-04 20:29:59 +00:00
Tom Lane 85d02a6586 Redefine Datum as uintptr_t, instead of unsigned long.
This is more in keeping with modern practice, and is a first step towards
porting to Win64 (which has sizeof(pointer) > sizeof(long)).

Tsutomu Yamada, Magnus Hagander, Tom Lane
2009-12-31 19:41:37 +00:00
Heikki Linnakangas 84d723b6ce Previous fix for temporary file management broke returning a set from
PL/pgSQL function within an exception handler. Make sure we use the right
resource owner when we create the tuplestore to hold returned tuples.

Simplify tuplestore API so that the caller doesn't need to be in the right
memory context when calling tuplestore_put* functions. tuplestore.c
automatically switches to the memory context used when the tuplestore was
created. Tuplesort was already modified like this earlier. This patch also
removes the now useless MemoryContextSwitch calls from callers.

Report by Aleksei on pgsql-bugs on Dec 22 2009. Backpatch to 8.1, like
the previous patch that broke this.
2009-12-29 17:40:59 +00:00
Tom Lane 67ec9adaf1 Fix session-lifespan memory leak when a plperl function is redefined:
we have to tell Perl it can release its compiled copy of the function
text.  Noted by Alexey Klyukin.

Back-patch to 8.2 --- the problem exists further back, but this patch
won't work without modification, and it's probably not worth the trouble.
2009-11-29 21:02:16 +00:00
Tom Lane 42b2907d12 Add support for anonymous code blocks (DO blocks) to PL/Perl.
Joshua Tolley, reviewed by Brendan Jurd and Tim Bunce
2009-11-29 03:02:27 +00:00
Tom Lane ef59fa0453 Ensure the previous Perl interpreter selection is restored upon exit from
plperl_call_handler, in both the normal and error-exit paths.  Per report
from Alexey Klyukin.
2009-10-31 18:11:59 +00:00
Andrew Dunstan 176c3c8db9 Convert a perl array to a postgres array when returned by Set Returning Functions as well as non SRFs. Backpatch to 8.1 where these facilities were introduced. with a little help from Abhijit Menon-Sen. 2009-09-28 17:31:12 +00:00
Peter Eisentraut e3f027115a errcontext support in PL/Perl
Author: Alexey Klyukin <alexk@commandprompt.com>
2009-09-16 06:06:12 +00:00
Bruce Momjian d747140279 8.4 pgindent run, with new combined Linux/FreeBSD/MinGW typedef list
provided by Andrew.
2009-06-11 14:49:15 +00:00
Tom Lane af98bb2ad7 Move variable declaration to avoid 'unused variable' warning when the
ifdef doesn't trigger.  Not worth back-patching.  Per buildfarm reports.
2009-06-06 03:45:36 +00:00
Andrew Dunstan 1978d7f13f Adjust recent PERL_SYS_INIT3 call to avoid platforms where it might fail, and to remove compilation warning. Backpatch the release 7.4 2009-06-05 20:32:00 +00:00
Andrew Dunstan fd416db406 Initialise perl library as documented in perl API. Backpatch to release 7.4. 2009-06-04 15:59:55 +00:00
Peter Eisentraut 1491b4c47d another small message tweak 2009-02-20 10:39:19 +00:00
Peter Eisentraut 80e26caa02 Wordsmithing for PL/Perl messages 2009-02-19 10:33:17 +00:00
Tom Lane 1cfd9e8834 Fix executor/spi.h to follow our usual conventions for include files, ie,
not include postgres.h nor anything else it doesn't directly need.  Add
#includes to calling files as needed to compensate.  Per my proposal of
yesterday.

This should be noted as a source code change in the 8.4 release notes,
since it's likely to require changes in add-on modules.
2009-01-07 13:44:37 +00:00
Peter Eisentraut 218b4e8dd8 Append major version number and for libraries soname major version number
to the gettext domain name, to simplify parallel installations.

Also, rename set_text_domain() to pg_bindtextdomain(), because that is what
it does.
2008-12-11 07:34:09 +00:00
Tom Lane cd35e9d746 Some infrastructure changes for the upcoming auto-explain contrib module:
* Refactor explain.c slightly to export a convenient-to-use subroutine
for printing EXPLAIN results.

* Provide hooks for plugins to get control at ExecutorStart and ExecutorEnd
as well as ExecutorRun.

* Add some minimal support for tracking the total runtime of ExecutorRun.
This code won't actually do anything unless a plugin prods it to.

* Change the API of the DefineCustomXXXVariable functions to allow nonzero
"flags" to be specified for a custom GUC variable.  While at it, also make
the "bootstrap" default value for custom GUCs be explicitly specified as a
parameter to these functions.  This is to eliminate confusion over where the
default comes from, as has been expressed in the past by some users of the
custom-variable facility.

* Refactor GUC code a bit to ensure that a custom variable gets initialized to
something valid (like its default value) even if the placeholder value was
invalid.
2008-11-19 01:10:24 +00:00
Tom Lane 05bba3d176 Be more tense about not creating tuplestores with randomAccess = true unless
backwards scan could actually happen.  In particular, pass a flag to
materialize-mode SRFs that tells them whether they need to require random
access.  In passing, also suppress unneeded backward-scan overhead for a
Portal's holdStore tuplestore.  Per my proposal about reducing I/O costs for
tuplestores.
2008-10-29 00:00:39 +00:00
Alvaro Herrera 2532c54d82 Improve translatability of error messages for external modules by tweaking
the ereport macro.  Included in this commit are enough files for starting
plpgsql, plpython, plperl and pltcl translations.
2008-10-09 17:24:05 +00:00
Tom Lane 7692d8d5b7 Support statement-level ON TRUNCATE triggers. Simon Riggs 2008-03-28 00:21:56 +00:00
Tom Lane 220db7ccd8 Simplify and standardize conversions between TEXT datums and ordinary C
strings.  This patch introduces four support functions cstring_to_text,
cstring_to_text_with_len, text_to_cstring, and text_to_cstring_buffer, and
two macros CStringGetTextDatum and TextDatumGetCString.  A number of
existing macros that provided variants on these themes were removed.

Most of the places that need to make such conversions now require just one
function or macro call, in place of the multiple notational layers that used
to be needed.  There are no longer any direct calls of textout or textin,
and we got most of the places that were using handmade conversions via
memcpy (there may be a few still lurking, though).

This commit doesn't make any serious effort to eliminate transient memory
leaks caused by detoasting toasted text objects before they reach
text_to_cstring.  We changed PG_GETARG_TEXT_P to PG_GETARG_TEXT_PP in a few
places where it was easy, but much more could be done.

Brendan Jurd and Tom Lane
2008-03-25 22:42:46 +00:00
Neil Conway 1d812a98b4 Add a new tuplestore API function, tuplestore_putvalues(). This is
identical to tuplestore_puttuple(), except it operates on arrays of
Datums + nulls rather than a fully-formed HeapTuple. In several places
that use the tuplestore API, this means we can avoid creating a
HeapTuple altogether, saving a copy.
2008-03-25 19:26:54 +00:00
Andrew Dunstan cd76ad6a2e Document the fix for perl 5.10 with this comment:
* The temporary enabling of the caller opcode here is to work around a
 * bug in perl 5.10, which unkindly changed the way its Safe.pm works, without
 * notice. It is quite safe, as caller is informational only, and in any case
 * we only enable it while we load the 'strict' module.
2008-01-23 00:55:47 +00:00
Andrew Dunstan ac11d9dc4b Work around for perl 5.10 bug - fix due to perl hacker Simon Cozens. 2008-01-22 20:17:37 +00:00
Tom Lane ba9da684b5 Suppress compiler warnings in recent plperl patch. Avoid uselessly expensive
lookup of the well-known OID of textout().
2007-12-01 17:58:42 +00:00
Andrew Dunstan 6e73b50449 Workaround for perl problem where evaluating UTF8 regexes can cause
implicit loading of modules, thereby breaking Safe rules.
We compile and call a tiny perl function on trusted interpreter init, after which
the problem does not occur.
2007-12-01 15:20:34 +00:00
Bruce Momjian f6e8730d11 Re-run pgindent with updated list of typedefs. (Updated README should
avoid this problem in the future.)
2007-11-15 22:25:18 +00:00
Bruce Momjian fdf5a5efb7 pgindent run for 8.3. 2007-11-15 21:14:46 +00:00
Tom Lane 9403598059 Fix plperl and pltcl to include the name of the current function when
passing on errors from the language interpreter.  (plpython seems
fairly OK about this already.)  Per gripe from Robert Kleemann.
2007-10-05 17:06:11 +00:00
Tom Lane 3cabc675ab Fix incorrect tests for undef Perl values in some places in plperl.c.
The correct test for defined-ness is SvOK(sv), not anything involving
SvTYPE.  Per bug #3415 from Matt Taylor.
Back-patch as far as 8.0; no apparent problem in 7.x.
2007-06-28 17:49:59 +00:00
Tom Lane 57690c6803 Support enum data types. Along the way, use macros for the values of
pg_type.typtype whereever practical.  Tom Dunstan, with some kibitzing
from Tom Lane.
2007-04-02 03:49:42 +00:00
Tom Lane c398300330 Combine cmin and cmax fields of HeapTupleHeaders into a single field, by
keeping private state in each backend that has inserted and deleted the same
tuple during its current top-level transaction.  This is sufficient since
there is no need to be able to determine the cmin/cmax from any other
transaction.  This gets us back down to 23-byte headers, removing a penalty
paid in 8.0 to support subtransactions.  Patch by Heikki Linnakangas, with
minor revisions by moi, following a design hashed out awhile back on the
pghackers list.
2007-02-09 03:35:35 +00:00
Bruce Momjian 8b4ff8b6a1 Wording cleanup for error messages. Also change can't -> cannot.
Standard English uses "may", "can", and "might" in different ways:

        may - permission, "You may borrow my rake."

        can - ability, "I can lift that log."

        might - possibility, "It might rain today."

Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice.  Similarly, "It may crash" is better stated, "It might crash".
2007-02-01 19:10:30 +00:00
Andrew Dunstan ee57938c0b remove unnecessary and now inaccurate cast which I should have removed with other old code. 2007-01-27 16:46:21 +00:00
Andrew Dunstan 175a242187 Allow args to spi_prepare to be standard type aliaes as well as those known in pg_type. Fixes bug #2917. Add some regression tests for these cases. 2007-01-27 01:55:57 +00:00
Andrew Dunstan f87fcd43c6 remove duplicate declaration, per report from Magnus Hagander. 2006-11-21 16:59:02 +00:00
Andrew Dunstan 751e3e6bd8 Force plperl and plperlu to run in separate interpreters. Create an error
on an attempt to create the second interpreter if this is not supported by
the perl installation. Per recent -hackers discussion.
2006-11-13 17:13:57 +00:00
Tom Lane def651f48f Clean up local redeclarations of variables with DLLIMPORT, per report
from Magnus that MSVC complains about this.
2006-10-19 18:32:48 +00:00
Tom Lane a3dff39c53 Adjust plperl to ensure that all strings and hash keys passed to Perl
are marked as UTF8 when the database encoding is UTF8.  This should
avoid inconsistencies like that exhibited in bug #2683 from Vitali Stupin.
2006-10-15 18:56:39 +00:00
Bruce Momjian f99a569a2e pgindent run for 8.2. 2006-10-04 00:30:14 +00:00
Tom Lane ea2e263539 Add new return codes SPI_OK_INSERT_RETURNING etc to the SPI API.
Fix all the standard PLs to be able to return tuples from FOO_RETURNING
statements as well as utility statements that return tuples.  Also,
fix oversight that SPI_processed wasn't set for a utility statement
returning tuples.  Per recent discussion.
2006-08-27 23:47:58 +00:00
Bruce Momjian 12cf0fdf19 Back out plperl OUT hash/array parameter patch, again. 2006-08-13 17:31:10 +00:00
Bruce Momjian b5633943cb Re-apply plperl patch that allows OUT parameters to be placed into Perl
hash and array variables.  (regression output updated)
2006-08-13 02:37:11 +00:00
Bruce Momjian 4178de3d43 Back out patch for plperl to handle OUT paramaters into arrays and
hashes.  Was causing regression failures.
2006-08-12 04:16:45 +00:00
Bruce Momjian c07fbcf577 plperl:
Allow conversion from perl to postgresql array in OUT parameters. Second,
allow hash form output from procedures with one OUT argument.

Pavel Stehule
2006-08-11 19:42:35 +00:00
Tom Lane b09bfcaa57 Add a feature for automatic initialization and finalization of dynamically
loaded libraries: call functions _PG_init() and _PG_fini() if the library
defines such symbols.  Hence we no longer need to specify an initialization
function in preload_libraries: we can assume that the library used the
_PG_init() convention, instead.  This removes one source of pilot error
in use of preloaded libraries.  Original patch by Ralf Engelschall,
preload_libraries changes by me.
2006-08-08 19:15:09 +00:00
Tom Lane 06e10abc0b Fix problems with cached tuple descriptors disappearing while still in use
by creating a reference-count mechanism, similar to what we did a long time
ago for catcache entries.  The back branches have an ugly solution involving
lots of extra copies, but this way is more efficient.  Reference counting is
only applied to tupdescs that are actually in caches --- there seems no need
to use it for tupdescs that are generated in the executor, since they'll go
away during plan shutdown by virtue of being in the per-query memory context.
Neil Conway and Tom Lane
2006-06-16 18:42:24 +00:00
Tom Lane a0ffab351e Magic blocks don't do us any good unless we use 'em ... so install one
in every shared library.
2006-05-30 22:12:16 +00:00
Andrew Dunstan 21e343da35 Make plperl's $_TD trigger data a global rather than a lexical variable,
with a fresh local value for each invocation, to avoid unexpected sharing
violations. Per recent -hackers discussion.
2006-05-29 13:51:23 +00:00
Andrew Dunstan 777f72cd37 Add table_name and table_schema to plperl trigger data. relname is
kept but now deprecated. Patch from Adam Sjøgren. Add regression test to
show plperl trigger data (Andrew).
TBD: apply similar changes to plpgsql, plpython and pltcl.
2006-05-26 17:34:16 +00:00
Tom Lane 147d4bf3e5 Modify all callers of datatype input and receive functions so that if these
functions are not strict, they will be called (passing a NULL first parameter)
during any attempt to input a NULL value of their datatype.  Currently, all
our input functions are strict and so this commit does not change any
behavior.  However, this will make it possible to build domain input functions
that centralize checking of domain constraints, thereby closing numerous holes
in our domain support, as per previous discussion.

While at it, I took the opportunity to introduce convenience functions
InputFunctionCall, OutputFunctionCall, etc to use in code that calls I/O
functions.  This eliminates a lot of grotty-looking casts, but the main
motivation is to make it easier to grep for these places if we ever need
to touch them again.
2006-04-04 19:35:37 +00:00
Neil Conway a323ede280 Fix a few places that were checking for the return value of palloc() to be
non-NULL: palloc() ereports on OOM, so we can safely assume it returns a
valid pointer.
2006-03-19 22:22:56 +00:00
Tom Lane 20ab467d76 Improve parser so that we can show an error cursor position for errors
during parse analysis, not only errors detected in the flex/bison stages.
This is per my earlier proposal.  This commit includes all the basic
infrastructure, but locations are only tracked and reported for errors
involving column references, function calls, and operators.  More could
be done later but this seems like a good set to start with.  I've also
moved the ReportSyntaxErrorPosition logic out of psql and into libpq,
which should make it available to more people --- even within psql this
is an improvement because warnings weren't handled by ReportSyntaxErrorPosition.
2006-03-14 22:48:25 +00:00
Bruce Momjian 98d42c2450 Remove copyright notices from Jan (per author approval), and those files
derived from Jan's.
2006-03-11 16:43:22 +00:00
Andrew Dunstan 5d723d05c0 Prepared queries for PLPerl, plus fixing a small plperl memory leak. Patch
and docs from Dmitry Karasik, slightly editorialised.
2006-03-05 16:40:51 +00:00
Neil Conway e24cea8be5 Fix typo in comment. 2006-02-28 23:38:13 +00:00
Neil Conway e1f06d8057 Fix a few minor typos in comments in PL/Perl. 2006-02-26 22:26:39 +00:00
Andrew Dunstan 4400ca2bad Undo perl's nasty locale setting on Windows. Since we can't do that as
elsewhere by setting the environment appropriately, we make perl do it
right after interpreter startup by calling its POSIX::setlocale().
2006-01-28 16:20:31 +00:00
Neil Conway ebdefb93b2 Per a bug report from Theo Schlossnagle, plperl_return_next() leaks
memory in the executor's per-query memory context. It also inefficient:
it invokes get_call_result_type() and TupleDescGetAttInMetadata() for
every call to return_next, rather than invoking them once (per PL/Perl
function call) and memoizing the result.

This patch makes the following changes:

- refactor the code to include all the "per PL/Perl function call" data
inside a single struct, "current_call_data". This means we don't need to
save and restore N pointers for every recursive call into PL/Perl, we
can just save and restore one.

- lookup the return type metadata needed by plperl_return_next() once,
and then stash it in "current_call_data", so as to avoid doing the
lookup for every call to return_next.

- create a temporary memory context in which to evaluate the return
type's input functions. This memory context is reset for each call to
return_next.

The patch appears to fix the memory leak, and substantially reduces
the overhead imposed by return_next.
2006-01-28 03:28:15 +00:00
Andrew Dunstan 9bb76d0b3f Rationalise perl header inclusions via a common include file, which also
declares routines in plperl.c and spi_internal.c used in other files.

Along the way, also stop perl from hijacking stdio and other stuff on Windows.
2006-01-08 22:27:52 +00:00
Andrew Dunstan eb29d89ffd Move declaration of check_function_bodies to where the perl headers
haven't had a chance to mangle the definition of DLLIMPORT (thanks again, perl guys).
2005-12-29 14:28:31 +00:00
Tom Lane c104cd2038 Fix plperl validator to honor check_function_bodies: when that is OFF,
we want it to check the argument/result data types and no more.  In
particular, libperl shouldn't get initialized in this case.
2005-12-28 18:34:16 +00:00
Bruce Momjian 436a2956d8 Re-run pgindent, fixing a problem where comment lines after a blank
comment line where output as too long, and update typedefs for /lib
directory.  Also fix case where identifiers were used as variable names
in the backend, but as typedefs in ecpg (favor the backend for
indenting).

Backpatch to 8.1.X.
2005-11-22 18:17:34 +00:00
Andrew Dunstan b410475672 translate undef to NULL for result arrayref, now that we allow NULLs in arrays. Update plperl regression test accordingly. 2005-11-18 17:00:28 +00:00
Tom Lane 800af89004 Code review for spi_query/spi_fetchrow patch: handle errors sanely,
avoid leaking memory.  I would add a regression test for error handling
except it seems eval{} can't be used in unprivileged plperl :-(
2005-10-18 17:13:14 +00:00
Bruce Momjian 1dc3498251 Standard pgindent run for 8.1. 2005-10-15 02:49:52 +00:00
Tom Lane 6f64cf592b Get rid of inappropriate use of croak(). Per report from Michael Fuhr. 2005-08-24 19:06:28 +00:00
Tom Lane a62604508f Fix up plperl 'use_strict' so that it can be enabled or disabled on the
fly.  Fix problem with incompletely duplicated setup code.  Andrew Dunstan,
from an idea of Michael Fuhr's.
2005-08-24 18:16:58 +00:00
Tom Lane 2299ceab1c Invoke mksafefunc and mkunsafefunc with :: decoration. This seems a good
idea on consistency grounds, whether or not it really fixes bug #1831.
Michael Fuhr
2005-08-20 19:19:21 +00:00
Tom Lane 65790b9e01 Un-break plperl for non-set case. 2005-08-12 21:26:32 +00:00
Bruce Momjian abc8a0a0fe More rsi assignment line too. 2005-08-12 21:09:34 +00:00
Bruce Momjian 60672b59b8 > The attached patch moves a plperl sanity check into the correct
> position. Performing the check in the existing position allows the call
> to go through to perl first, possibly resulting in a SEGV.

Andrew Dunstan
2005-08-12 20:48:03 +00:00
Tom Lane 2d481b31af Fix plperl crash with list value return for an array result type.
Reported by Michael Fuhr, fixed by Andrew Dunstan.
2005-07-12 20:35:42 +00:00
Tom Lane 220e6bfc53 Fix plperl to do recursion safely, and fix a problem with array results.
Add suitable regression tests.  Andrew Dunstan
2005-07-12 01:16:22 +00:00
Bruce Momjian e8a3e6b8a0 Rename xmalloc to pg_malloc for consistency with psql usage.
Add missing plperl include.
2005-07-10 16:13:13 +00:00
Bruce Momjian b135508c98 Following up a previous thought I had, yesterday I realised how to
return arays nicely without having to make the plperl programmer aware
of anything. The attached patch allows plperl to return an arrayref
where the function returns an array type. It silently calls a perl
function to stringify the array before passing it to the pg array
parser. Non-array returns are handled as before (i.e. passed through
this process) so it is backwards compatible. I will presently submit
regression tests and docs.

example:

andrew=# create or replace function blah() returns text[][] language
plperl as $$ return [['a"b','c,d'],['e\\f','g']]; $$;
CREATE FUNCTION
andrew=# select blah();
            blah
-----------------------------
 {{"a\"b","c,d"},{"e\\f",g}}


This would complete half of the TODO item:

  . Pass arrays natively instead of as text between plperl and postgres

(The other half is translating pg array arguments to perl arrays - that
will have to wait for 8.1).

Some of this patch is adapted from a previously submitted patch from
Sergej Sergeev. Both he and Abhijit Menon-Sen have looked it over
briefly and tentatively said it looks ok.

Andrew Dunstan
2005-07-10 15:32:47 +00:00
Bruce Momjian 6d92f2106f The attached patch implements spi_query() and spi_fetchrow() functions
for PL/Perl, to avoid loading the entire result set into memory as the
existing spi_exec_query() function does.

Here's how one might use the new functions:

    $x = spi_query("select ...");
    while (defined ($y = spi_fetchrow($x))) {
        ...
        return_next(...);
    }

The changes do not affect the spi_exec_query() interface in any way.

Abhijit Menon-Sen
2005-07-10 15:19:43 +00:00
Bruce Momjian eefdbba062 Currently, nonfatal warnings are not trapped (as they should be) by
plperl - the attached small patch remedies that omission, and adds a
small regression test for error and warning output - the new regression
input and expected output are in separate attached files.

Andrew Dunstan
2005-07-06 22:44:49 +00:00
Bruce Momjian 77838f7380 Currently, nonfatal warnings are not trapped (as they should be) by
plperl - the attached small patch remedies that omission.

Andrew Dunstan
2005-07-06 22:33:39 +00:00
Tom Lane bee9aef43b Fix memory leak in plperl_hash_from_tuple(), per report from Jean-Max Reymond. 2005-07-03 21:56:16 +00:00
Tom Lane 84d73a6dbc Add a validator function for plperl. Andrew Dunstan 2005-06-22 16:45:51 +00:00
Bruce Momjian d092524418 > Here's a patch I added against plperl, originally against beta5, now
> against rc1. It simply checks with GetDatabaseEncoding() if the current
> database is in UTF-8, and if so, sets the UTF-8 flag on the arguments
> that are passed to perl. This means that it isn't necessary to
> utf8::upgrade() every string, as perl has no way of knowing offhand
> that a string is UTF-8 -- but postgres does, because the database
> encoding is specified, so it makes sense to turn the flag on. You
> should also be able to properly manipulate UTF-8 strings now from
> plperl as opposed to plperlu, because otherwise you'd have to use
> encoding 'utf8' which was not allowed. It could also eliminate some
> unexpected bugs if you assume that perl knows the string is unicode.
It
> is enabled only for perl 5.6 and higher, so earlier versions will not
> be affected.
>
> I have been assured by crab that the patch is quite harmless and will
> not break anything. It would be great to see it in 8 final! :-)

David Kamholz
2005-06-15 00:35:16 +00:00
Bruce Momjian bebe7c5600 Here's a patch to do the following:
1. Rename spi_return_next to return_next.
2. Add a new test for return_next.
3. Update the expected output.
4. Update the documentation.

Abhijit Menon-Sen
2005-06-05 03:16:42 +00:00
Bruce Momjian d995014fac At 2005-05-21 20:18:50 +0530, ams@oryx.com wrote:
>
> > The second issue is where plperl returns a large result set.

I have attached the following seven patches to address this problem:

1. Trivial. Replaces some errant spaces with tabs.

2. Trivial. Fixes the spelling of Jan's name, and gets rid of many
   inane, useless, annoying, and often misleading comments. Here's
   a sample: "plperl_init_all() - Initialize all".

   (I have tried to add some useful comments here and there, and will
   continue to do so now and again.)

3. Trivial. Splits up some long lines.

4. Converts SRFs in PL/Perl to use a Tuplestore and SFRM_Materialize
   to return the result set, based on the PL/PgSQL model.

   There are two major consequences: result sets will spill to disk when
   they can no longer fit in work_mem; and "select foo_srf()" no longer
   works. (I didn't lose sleep over the latter, since that form is not
   valid in PL/PgSQL, and it's not documented in PL/Perl.)

5. Trivial, but important. Fixes use of "undef" instead of undef. This
   would cause empty functions to fail in bizarre ways. I suspect that
   there's still another (old) bug here. I'll investigate further.

6. Moves the majority of (4) out into a new plperl_return_next()
   function, to make it possible to expose the functionality to
   Perl; cleans up some of the code besides.

7. Add an spi_return_next function for use in Perl code.

If you want to apply the patches and try them out, 8-composite.diff is
what you should use. (Note: my patches depend upon Andrew's use-strict
and %_SHARED patches being applied.)

Here's something to try:

    create or replace function foo() returns setof record as $$
    $i = 0;
    for ("World", "PostgreSQL", "PL/Perl") {
        spi_return_next({f1=>++$i, f2=>'Hello', f3=>$_});
    }
    return;
    $$ language plperl;
    select * from foo() as (f1 integer, f2 text, f3 text);

(Many thanks to Andrews Dunstan and Supernews for their help.)

Abhijit Menon-Sen
2005-06-04 20:33:06 +00:00
Neil Conway 353f111f98 Fix typo in PL/Perl Safe.pm initialization that prevented the proper
sharing of %_SHARED. From Andrew Dunstan.
2005-05-23 01:57:51 +00:00
Tom Lane 278bd0cc22 For some reason access/tupmacs.h has been #including utils/memutils.h,
which is neither needed by nor related to that header.  Remove the bogus
inclusion and instead include the header in those C files that actually
need it.  Also fix unnecessary inclusions and bad inclusion order in
tsearch2 files.
2005-05-06 17:24:55 +00:00
Tom Lane 6c412f0605 Change CREATE TYPE to require datatype output and send functions to have
only one argument.  (Per recent discussion, the option to accept multiple
arguments is pretty useless for user-defined types, and would be a likely
source of security holes if it was used.)  Simplify call sites of
output/send functions to not bother passing more than one argument.
2005-05-01 18:56:19 +00:00
Tom Lane 06fb6105f5 Make plperl work with OUT parameters. 2005-04-01 19:34:06 +00:00
Tom Lane 70c9763d48 Convert oidvector and int2vector into variable-length arrays. This
change saves a great deal of space in pg_proc and its primary index,
and it eliminates the former requirement that INDEX_MAX_KEYS and
FUNC_MAX_ARGS have the same value.  INDEX_MAX_KEYS is still embedded
in the on-disk representation (because it affects index tuple header
size), but FUNC_MAX_ARGS is not.  I believe it would now be possible
to increase FUNC_MAX_ARGS at little cost, but haven't experimented yet.
There are still a lot of vestigial references to FUNC_MAX_ARGS, which
I will clean up in a separate pass.  However, getting rid of it
altogether would require changing the FunctionCallInfoData struct,
and I'm not sure I want to buy into that.
2005-03-29 00:17:27 +00:00
Bruce Momjian e3267b6517 Properly undef _(x) gettext macro. 2005-02-23 04:34:21 +00:00
Bruce Momjian 0542b1e2fe Use _() macro consistently rather than gettext(). Add translation
macros around strings that were missing them.
2005-02-22 04:43:23 +00:00
Tom Lane ce6e2fada0 plperl trigger handler tried to fetch new/old tuples even when fired
as a statement trigger :-(.  Per report from Sokolov Yura.
2005-01-14 16:25:42 +00:00
Tom Lane e24801654a plperl was not being quite paranoid enough about detecting 'undef' values
returned by Perl.  Per report from Nicolas Addington.
2005-01-11 06:08:45 +00:00
Tom Lane af434fcdf4 Update plperl to use ereport() not elog() for user-facing messages,
so that they will be translatable.  Give messages some semblance of
conformance to the style guide.
2004-11-29 20:11:06 +00:00
Tom Lane 5597fee8d7 Avoid getting bit by roundoff error while checking $Safe::VERSION.
Per report from Mark Kirkwood.
2004-11-24 18:47:38 +00:00
Tom Lane 28e9b26f4d Further plperl cleanup: be more paranoid about checking the type of
data returned from Perl.  Consolidate multiple bits of code to convert
a Perl hash to a tuple, and drive the conversion off the keys present
in the hash rather than the tuple column names, so we detect error if
the hash contains keys it shouldn't.  (This means keys not in the hash
will silently default to NULL, which seems ok to me.)  Fix a bunch of
reference-count leaks too.
2004-11-23 00:21:24 +00:00
Tom Lane f5206262c8 Try to instill some sanity in plperl's function result processing.
Get rid of static variables for SETOF result, don't crash when called
from non-FROM context, eliminate dead code, etc.
2004-11-22 20:31:53 +00:00
Tom Lane 32c97fd58f Suppress remaining compile warnings, and add a comment about why
it's not really broken.  Andrew Dunstan
2004-11-21 22:13:37 +00:00
Tom Lane 35f4994133 Fix plperl and pltcl error handling per my previous proposal. SPI
operations are now run as subtransactions, so that errors in them
can be reported as ordinary Perl or Tcl errors and caught by the
normal error handling convention of those languages.  Also do some
minor code cleanup in pltcl.c: extract a large chunk of duplicated
code in pltcl_SPI_execute and pltcl_SPI_execute_plan into a shared
subroutine.
2004-11-21 21:17:07 +00:00
Tom Lane 193a97c2d3 Fix plperl's elog() function to convert elog(ERROR) into Perl croak(),
rather than longjmp'ing clear out of Perl and thereby leaving Perl in
a broken state.  Also some minor prettification of error messages.
Still need to do something with spi_exec_query() error handling.
2004-11-20 19:07:40 +00:00
Tom Lane b6038484f8 Fix memory clobber problem reported by John Hansen: plperl_safe_init()
may expand the Perl stack, therefore we must SPAGAIN to reload the local
stack pointer after calling it.  Also a couple other marginal readability
improvements.
2004-11-18 21:35:42 +00:00
Tom Lane 1c1f2f5b96 Remove ill-considered suppression of gcc warnings in plperl, and fix
some of the bugs exposed thereby.  The remaining 'might be used uninitialized'
warnings look like live bugs, but I am not familiar enough with Perl/C hacking
to tell how to fix them.
2004-11-17 21:23:36 +00:00
Tom Lane 4a0bee976c Adjust safety restrictions for plperl functions. Andrew Dunstan's
patch, but allow srand and disallow sprintf as per subsequent discussion.
2004-11-16 22:05:22 +00:00
Bruce Momjian ce1c20248d I have attached 5 patches (split up for ease of review) to plperl.c.
1. Two minor cleanups:

    - We don't need to call hv_exists+hv_fetch; we should just check the
      return value of hv_fetch.
    - newSVpv("undef",0) is the string "undef", not a real undef.

2. This should fix the bug Andrew Dunstan described in a recent -hackers
   post. It replaces three bogus "eval_pv(key, 0)" calls with newSVpv,
   and eliminates another redundant hv_exists+hv_fetch pair.

3. plperl_build_tuple_argument builds up a string of Perl code to create
   a hash representing the tuple. This patch creates the hash directly.

4. Another minor cleanup: replace a couple of av_store()s with av_push.

5. Analogous to #3 for plperl_trigger_build_args. This patch removes the
   static sv_add_tuple_value function, which does much the same as two
   other utility functions defined later, and merges the functionality
   into plperl_hash_from_tuple.

I have tested the patches to the best of my limited ability, but I would
appreciate it very much if someone else could review and test them too.

(Thanks to Andrew and David Fetter for their help with some testing.)

Abhijit Menon-Sen
2004-10-15 17:08:26 +00:00
Bruce Momjian 59bf879f12 Move -DPLPERL_HAVE_UID_GID to the plperl Makefile, for Win32 only. 2004-10-07 19:01:09 +00:00
Bruce Momjian a5d7ba773d Adjust comments previously moved to column 1 by pgident. 2004-10-07 15:21:58 +00:00
Bruce Momjian 87ea6f0dbe Use #define instead of typedef for plperl because perl defines
uid_t/gid_t too.
2004-10-06 16:36:02 +00:00