jconway / plr

PL/R - R Procedural Language for PostgreSQL
http://www.joeconway.com
90 stars 41 forks source link

Work Around Solution Found: pl/r does not run on Windows 10 64 bit and *new* hardware #26

Open AndreMikulec opened 7 years ago

AndreMikulec commented 7 years ago

I tried

Win64 plr.dll for Postgres 9.5.x, R-3.3.0 (plr-8.3.0.16)

on

 PostgreSQL 9.5.2, compiled by Visual C++ build 1800, 64-bit

with

R version 3.3.1 (2016-06-21)  Platform: x86_64-w64-mingw32/x64 (64-bit)

on a new-ish Windows 10 64 bit laptop that is only 18 months old.

I am getting this error again.

postgres=# select r_max(5,3);
ERROR:  could not open file "base/12373/2663": No such file or directory
LINE 1: SELECT NULL FROM pg_catalog.pg_class WHERE relname 'plr_modules' AND relnamespace = 2200

So, my 'old hardware' guess is wrong.

pl/r runs fine on Windows 7 64 bit

What is the easiest way ( or an easy way ) to try to debug this problem?

davecramer commented 7 years ago

Unfortunately I don't know enough about debugging on windows to even point you in the right direction.

I do have an idea as to what is causing this, but I have no way to confirm this. Internally there are cache lookups for relations and if somehow due to a source code mismatch the cache id is wrong then this might possibly happen.

Can you also provide me with output of select version() ;

Dave Cramer

On 3 September 2016 at 19:02, AndreMikulec notifications@github.com wrote:

I tried

Win64 plr.dll for Postgres 9.5.x, R-3.3.0 (plr-8.3.0.16) http://www.joeconway.com/plr/plr-8.3.0.16-pg9.5-R3.3.0-win64.zip

on

PostgreSQL 9.5.2, compiled by Visual C++ build 1800, 64-bit

with

R version 3.3.1 (2016-06-21) Platform: x86_64-w64-mingw32/x64 (64-bit)

on a new-ish Windows 10 64 bit laptop that is only 18 months old.

I am getting this error again.

postgres=# select r_max(5,3);ERROR: could not open file "base/12373/2663": No such file or directoryLINE 1: SELECT NULL FROM pg_catalog.pg_class WHERE relname 'plr_modules' AND relnamespace = 2200

So, my 'old hardware' guess is wrong.

pl/r runs fine on Windows 7 64 bit https://github.com/jconway/plr/issues/18

What is the easiest way ( or an easy way ) to try to debug this problem?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jconway/plr/issues/26, or mute the thread https://github.com/notifications/unsubscribe-auth/AAYz9gNoS246Y3My6kldiEbZ-JSo7MSdks5qmfyAgaJpZM4J0amQ .

AndreMikulec commented 7 years ago

postgres=# select version();
                           version
-------------------------------------------------------------
 PostgreSQL 9.5.2, compiled by Visual C++ build 1800, 64-bit
(1 row)
AndreMikulec commented 7 years ago

Here are some links on 'cache lookups for relations' that I came up with. I do not know if they are useful or not. Maybe yet more people can bring information to the topic?


postgres /src/backend/utils/cache/relcache.c
https://searchcode.com/codesearch/view/46755261/

RelCache in PostgreSQL
2016.07.20
http://bitnine.net/postgresql/relcache-in-postgresql/?ckattempt=1

Introduction to Hacking PostgreSQL
With lots of code review!
Gavin Sherry
gavin@alcove.com.au
Alcove Systems Engineering
January 14, 2007
https://linux.org.au/conf/2007/att_data/Miniconfs(2f)PostgreSQL/attachments/hacking_intro.pdf

Introduction to Hacking PostgreSQL
Neil Conway, Gavin Sherry
neilc@samurai.com, swm@alcove.com.au
http://www.neilconway.org/talks/hacking/hack_slides.pdf

relcache_expire
relcache_size
http://www.pgpool.net/docs/latest/pgpool-en.html

search for "relcache" on this page
... Andres Freund ....
[ANNOUNCE] == PostgreSQL Weekly News - January 11 2015 ==
[Posted January 14, 2015 by n8willis]
https://lwn.net/Articles/629537/

Correctly handle relcache invalidation corner case during logical decoding.
Andres Freund
Tue, 6 Jan 2015 23:19:37 +0000 (00:19 +0100)
https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=3fabed070530271771c2345dd7f60b0b28f0aae3

Improve relcache invalidation handling of currently invisible relations.
Andres Freund
Tue, 6 Jan 2015 23:18:00 +0000 (00:18 +0100)
https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=31912d01d8d860ffaafb386437d149e1749962ab

Latest relcache.c work ( most work by tglsfdc )
https://github.com/postgres/postgres/commits/master/src/backend/utils/cache/relcache.c

Andres Freund is anarazel: looks like he still works on PostgreSQL ( but not lately on relcache.c ) 
https://github.com/postgres/postgres/commits/master/?author=anarazel

recent work on relcache.c 
https://github.com/postgres/postgres/commits/master/src/backend/utils/cache/relcache.c?author=tglsfdc

All work by tglsfdc
https://github.com/postgres/postgres/commits/master/?author=tglsfdc

List all projects
Also, another view for 'recent PostgreSQL work ( with author names )
https://git.postgresql.org/gitweb/?o=age
AndreMikulec commented 7 years ago

Just eye-balling the code, Perhaps the call path is going through here.

static bool
haveModulesTable(Oid nspOid)
https://github.com/jconway/plr/blob/master/plr.c

/*
 * haveModulesTable(Oid) -- Check if table plr_modules exists in the namespace
 * designated by the OID input argument.
 */
static bool
haveModulesTable(Oid nspOid)
{
    StringInfo      sql = makeStringInfo();
    char           *sql_format = "SELECT NULL "
                                 "FROM pg_catalog.pg_class "
                                 "WHERE "
                                 "relname = 'plr_modules' AND "
                                 "relnamespace = %u";
    int  spiRc;

    appendStringInfo(sql, sql_format, nspOid);

    spiRc = SPI_exec(sql->data, 1);
    if (spiRc != SPI_OK_SELECT)
        /* internal error */
        elog(ERROR, "haveModulesTable: select from pg_class failed");

    return SPI_processed == 1;
}

Search on "SPI_exec" yields . . .

http://doxygen.postgresql.org/spi_8c.html#a97e241229d4c3b2829bc6dde69abc7da
http://doxygen.postgresql.org/spi_8h.html#a97e241229d4c3b2829bc6dde69abc7da

Definition at line 393 of file spi.c.
References SPI_execute().
Referenced by funny_dup17(), get_tuple_of_interest(), refresh_by_match_merge(), and xpath_table().

Search on "SPI_execute" yields . . .

http://doxygen.postgresql.org/spi_8c.html#af7dc8e377b122d8c8758426f14d38889
http://doxygen.postgresql.org/spi_8h.html#af7dc8e377b122d8c8758426f14d38889

Definition at line 365 of file spi.c.

References _SPI_begin_call(), _SPI_end_call(), _SPI_execute_plan(), 
_SPI_PLAN_MAGIC, _SPI_prepare_oneshot_plan(), _SPI_plan::cursor_options, 
InvalidSnapshot, _SPI_plan::magic, NULL, and SPI_ERROR_ARGUMENT.

Referenced by build_tuplestore_recursively(), crosstab(), exec_stmt_dynexecute(), 
get_crosstab_tuplestore(), initialize_worker_spi(), load_categories_hash(), 
plperl_spi_exec(), pltcl_init_load_unknown(), pltcl_SPI_execute(), 
PLy_spi_execute_query(), 
query_to_oid_list(), query_to_xml_internal(), refresh_by_match_merge(), and SPI_exec().

Next, _SPI_execute_plan is called and this function is about 1300 lines of code.

http://doxygen.postgresql.org/spi_8c.html#a2c69af54ca7196cd5ad4200f42c0062f

static int _SPI_execute_plan    (   SPIPlanPtr  plan,
ParamListInfo   paramLI,
Snapshot    snapshot,
Snapshot    crosscheck_snapshot,
bool    read_only,
bool    fire_triggers,
uint64  tcount 
)   
davecramer commented 7 years ago

I'm aware what relcache is. The possible mismatch I am referring to may be caused by a mismatch of header files when it was built. This is purely a guess

Ironically in one of the slides it suggests that you may want to use a unix variant, which would be far easier than using windows

Dave Cramer

On 4 September 2016 at 16:29, AndreMikulec notifications@github.com wrote:

Just eye-balling the code, Perhaps the call path is going through here.

static bool haveModulesTable(Oid nspOid)https://github.com/jconway/plr/blob/master/plr.c

/* * haveModulesTable(Oid) -- Check if table plr_modules exists in the namespace * designated by the OID input argument. /static boolhaveModulesTable(Oid nspOid) { StringInfo sql = makeStringInfo(); char sql_format = "SELECT NULL " "FROM pg_catalog.pg_class " "WHERE " "relname = 'plr_modules' AND " "relnamespace = %u"; int spiRc;

appendStringInfo(sql, sql_format, nspOid);

spiRc = SPI_exec(sql->data, 1);
if (spiRc != SPI_OK_SELECT)
    /* internal error */
    elog(ERROR, "haveModulesTable: select from pg_class failed");

return SPI_processed == 1;

}

Search on "SPI_exec" yields . . .

http://doxygen.postgresql.org/spi_8c.html#a97e241229d4c3b2829bc6dde69abc7dahttp://doxygen.postgresql.org/spi_8h.html#a97e241229d4c3b2829bc6dde69abc7da

Definition at line 393 of file spi.c. References SPI_execute(). Referenced by funny_dup17(), get_tuple_of_interest(), refresh_by_match_merge(), and xpath_table().

Search on "SPI_execute" yields . . .

http://doxygen.postgresql.org/spi_8c.html#af7dc8e377b122d8c8758426f14d38889http://doxygen.postgresql.org/spi_8h.html#af7dc8e377b122d8c8758426f14d38889

Definition at line 365 of file spi.c.

References _SPI_begin_call(), _SPI_end_call(), _SPI_execute_plan(), _SPI_PLAN_MAGIC, _SPI_prepare_oneshot_plan(), _SPI_plan::cursor_options, InvalidSnapshot, _SPI_plan::magic, NULL, and SPI_ERROR_ARGUMENT.

Referenced by build_tuplestore_recursively(), crosstab(), exec_stmt_dynexecute(), get_crosstab_tuplestore(), initialize_worker_spi(), load_categories_hash(), plperl_spi_exec(), pltcl_init_load_unknown(), pltcl_SPI_execute(), PLy_spi_execute_query(), query_to_oid_list(), query_to_xml_internal(), refresh_by_match_merge(), and SPI_exec().

Next, _SPI_execute_plan is called and this function is about 1300 lines of code.

http://doxygen.postgresql.org/spi_8c.html#a2c69af54ca7196cd5ad4200f42c0062f

static int _SPI_execute_plan ( SPIPlanPtr plan, ParamListInfo paramLI, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool fire_triggers, uint64 tcount )

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jconway/plr/issues/26#issuecomment-244626828, or mute the thread https://github.com/notifications/unsubscribe-auth/AAYz9rS2X4FAGexaL6joHVIshSBI_Thbks5qmypHgaJpZM4J0amQ .

AndreMikulec commented 7 years ago

Dave Cramer, " want to use a unix variant " Unfortunately, I do not have access to a UNIX machine. Also, I have been thinking about temporary-ish workarounds including other pl* languages and ''other pl* languages to R connectors.''

Moreover, what I really need/want/should do is (1) 'isolate' the problem, (2) report it, and (3) possibly come up with a recommendation. When this happens, sometimes, at least one other person may look at the same problem ( and possibly come up with a 'better' solution ). That is why earlier I asked 'debug this problem?' An example is here: quantile produces decreasing output

In this particular github issue, first 'debugging'drawback is the my C/C+ static/dll debugging skill is almost non-existent. The second drawback is that the PostgreSQL codebase is very large and I do not know where to start. So, that 'very large' problem is why I asked about ''debug this problem?' Do you know anyone or can you point me in any direction, to someone who is very good doing PostgreSQL C/C+ static/dll debugging and/or may have more ideas on where to start looking?

davecramer commented 7 years ago

Andrej,

The problem isn't finding someone who is good at debugging postgres,the problem is finding someone who is good at debugging postgres on Windows.

Debugging it is not a huge task once you

1) figure out how to build Postgres with debug symbols 2) figure out how to build R with debug symbols

Then you need to fire up your debugger to debug postgres. In another window start a session with postgres, and attach the debugger to that session.

Put a breakpoint on plr_call_handler which is the function exposed by plr and then step through it until it crashes

Unfortunately I don't have the skills but if you can figure out the other challenges I'll be more than happy to help

Dave Cramer

On 5 September 2016 at 08:42, AndreMikulec notifications@github.com wrote:

Dave Cramer, " want to use a unix variant " Unfortunately, I do not have access to a UNIX machine. Also, I have been thinking about temporary-ish workarounds including other pl* languages and ''other pl* languages to R connectors.''

Moreover, what I really need/want/should do is (1) 'isolate' the problem, (2) report it, and (3) possibly come up with a recommendation. When this happens, sometimes, at least one other person may look at the same problem ( and possibly come up with a 'better' solution ). That is why earlier I asked 'debug this problem?' An example is here: quantile produces decreasing output https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=16672

In this particular github issue, first 'debugging'drawback is the my C/C+ static/dll debugging skill is almost non-existent. The second drawback is that the PostgreSQL codebase is very large and I do not know where to start. So, that 'very large' problem is why I asked about ''debug this problem?' Do you know anyone or can you point me in any direction, to someone who is very good doing PostgreSQL C/C+ static/dll debugging and/or may have more ideas on where to start looking?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jconway/plr/issues/26#issuecomment-244738031, or mute the thread https://github.com/notifications/unsubscribe-auth/AAYz9iEZ_W63L61PF1gbVV86VYJ24hfDks5qnA4wgaJpZM4J0amQ .

AndreMikulec commented 7 years ago

Dave,

(1) Using gdb, (and Code::Blocks), I am 50 levels deep in debugging the plr.c/postgresql. So, far I have not found the problem

(2) In plr.c, of the function haveModulesTable I changed

the line 
          char *sql_format 
to
    const char *sql_format 

That did not help. I still get the same error.

(3). This is a question. Does

postgres=# SELECT * FROM pg_catalog.pg_class WHERE relname = 'plr_modules';" 

supposed to return zero records?

postgres=# SELECT * FROM pg_catalog.pg_class WHERE relname = 'plr_modules';
 relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode |
---------+--------------+---------+-----------+----------+-------+-------------+
(0 rows)
postgres=# create extension plr;
CREATE EXTENSION
postgres=# SELECT NULL FROM pg_catalog.pg_class WHERE relname = 'plr_modules' AND relnamespace = 2200;
 ?column?
----------
(0 rows)

postgres=# SELECT * FROM pg_catalog.pg_class WHERE relname = 'plr_modules' AND relnamespace = 2200;
 relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | relpa
---------+--------------+---------+-----------+----------+-------+-------------+---------------+------
(0 rows)

postgres=# SELECT * FROM pg_catalog.pg_class WHERE relname = 'plr_modules' AND relnamespace = 2200;
 relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | relpa
---------+--------------+---------+-----------+----------+-------+-------------+---------------+------
(0 rows)

postgres=# SELECT * FROM pg_catalog.pg_class WHERE relname = 'plr_modules';
 relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode |
---------+--------------+---------+-----------+----------+-------+-------------+
(0 rows)

postgres=# SELECT * FROM pg_catalog.pg_class WHERE relnamespace = 2200;
     relname      | relnamespace | reltype | reloftype | relowner | relam | relfilenode |
------------------+--------------+---------+-----------+----------+-------+-------------+
 plr_environ_type |         2200 |   16447 |         0 |       10 |     0 |       16445 |
 r_typename       |         2200 |   16451 |         0 |       10 |     0 |       16449 |
 r_version_type   |         2200 |   16456 |         0 |       10 |     0 |       16454 |
(3 rows)
davecramer commented 7 years ago

Hi Andre,

Dave Cramer

On 16 September 2016 at 12:38, AndreMikulec notifications@github.com wrote:

Dave,

(1) Using gdb, (and Code::Blocks), I am 50 levels deep in debugging the plr.c/postgresql. So, far I have not found the problem

(2) In plr.c, of the function haveModulesTable I changed

the line char sql_format to const char sql_format

That did not help. I still get the same error.

Not surprising

(3). This is a question. Does

postgres=# SELECT * FROM pg_catalog.pg_class WHERE relname = 'plr_modules';"

supposed to return zero records?

off the top of my head, no, it should show something

what does \d show ?

postgres=# SELECT * FROM pg_catalog.pg_class WHERE relname = 'plr_modules'; relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode |---------+--------------+---------+-----------+----------+-------+-------------+ (0 rows)

postgres=# create extension plr; CREATE EXTENSION postgres=# SELECT NULL FROM pg_catalog.pg_class WHERE relname = 'plr_modules' AND relnamespace = 2200; ?column?---------- (0 rows)

postgres=# SELECT * FROM pg_catalog.pg_class WHERE relname = 'plr_modules' AND relnamespace = 2200; relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | relpa---------+--------------+---------+-----------+----------+-------+-------------+---------------+------ (0 rows)

postgres=# SELECT * FROM pg_catalog.pg_class WHERE relname = 'plr_modules' AND relnamespace = 2200; relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | relpa---------+--------------+---------+-----------+----------+-------+-------------+---------------+------ (0 rows)

postgres=# SELECT * FROM pg_catalog.pg_class WHERE relname = 'plr_modules'; relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode |---------+--------------+---------+-----------+----------+-------+-------------+ (0 rows)

postgres=# SELECT * FROM pg_catalog.pg_class WHERE relnamespace = 2200; relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode |------------------+--------------+---------+-----------+----------+-------+-------------+ plr_environ_type | 2200 | 16447 | 0 | 10 | 0 | 16445 | r_typename | 2200 | 16451 | 0 | 10 | 0 | 16449 | r_version_type | 2200 | 16456 | 0 | 10 | 0 | 16454 | (3 rows)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jconway/plr/issues/26#issuecomment-247661470, or mute the thread https://github.com/notifications/unsubscribe-auth/AAYz9ucJSAbn0OjwEu7ZrDAJuSe-aqEwks5qqtQkgaJpZM4J0amQ .

AndreMikulec commented 7 years ago

Seems to just show my user tables

postgres=# \d
          List of relations
 Schema |  Name   | Type  |  Owner
--------+---------+-------+----------
 public | ct      | table | postgres
 public | mytable | table | postgres
(2 rows)
postgres=# select count(*) from pg_catalog.pg_class;
 count
-------
   323
(1 row)
postgres=# select * from  pg_catalog.pg_class where relname like 'r\_%'  order by 1;
    relname     | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace |
----------------+--------------+---------+-----------+----------+-------+-------------+---------------+
 r_typename     |         2200 |   16588 |         0 |       10 |     0 |       16586 |             0 |
 r_version_type |         2200 |   16593 |         0 |       10 |     0 |       16591 |             0 |
(2 rows)

postgres=# select * from  pg_catalog.pg_class where relname like 'plr\_%'  order by 1;
     relname      | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace |
------------------+--------------+---------+-----------+----------+-------+-------------+---------------+
 plr_environ_type |         2200 |   16584 |         0 |       10 |     0 |       16582 |             0 |
(1 row)

Maybe, for some reason, CREATE EXTENSION, is not creating those functions?

From INSTALL.txt plr-8.3.0.16-pg9.5-R3.3.0-win64_MAY_22_2016

In the instructions below:
    PostgreSQL is installed to <pgdir>
    R is installed to <rdir>

For example, these directories might be:
    <pgdir> = C:\PostgreSQL\9.4
    <rdir>  = C:\R\R-3.1.2

I recommend you ensure there are no spaces in the pathname to either <pgdir> or <rdir>.
Correct quoting of pathnames with spaces is left as an exercise for the reader ;-)

The following files are contained in this zip file:
---------------------------------------------------
README.txt:         place in <pgdir>\doc\extension
plr.dll:            place in <pgdir>\lib
plr.sql:            place in <pgdir>\share\extension
plr.control:            place in <pgdir>\share\extension
plr--8.3.0.14.sql       place in <pgdir>\share\extension
plr--unpackaged--8.3.0.16.sql   place in <pgdir>\share\extension

Ensure the following environment variables are set *prior* to starting PostgreSQL:
---------------------------------------------------
PATH=<pgdir>\bin;<pgdir>\lib;<rdir>\bin\x64;$PATH
R_HOME=<rdir>

I have all of those files

C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\share\extension>dir plr*.*
 Volume in drive C has no label.
 Volume Serial Number is 04C6-B781

 Directory of C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\share\extension

09/16/2016  07:17 PM             2,893 plr--8.3.0.16.sql
09/16/2016  07:17 PM             1,035 plr--unpackaged--8.3.0.16.sql
09/16/2016  07:17 PM               171 plr.control
09/16/2016  07:17 PM             2,854 plr.sql
               4 File(s)          6,953 bytes
               0 Dir(s)  116,514,676,736 bytes free

C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\lib>dir plr*
 Volume in drive C has no label.
 Volume Serial Number is 04C6-B781

 Directory of C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\lib

09/16/2016  07:17 PM         6,292,805 plr.dll
09/12/2016  12:29 PM         6,292,805 plr_dll_CONSTANT_CHAR
09/16/2016  12:06 PM         6,292,805 plr_dll_CONSTANT_CHAR_POINTER
09/16/2016  06:23 PM         6,293,123 plr_dll_DEBUGRESULT_MYTABLE_MYINT
09/16/2016  04:56 PM         6,293,123 plr_dll_DEBUGRESULT_SEL_1_EQ_0
09/16/2016  04:25 PM         6,293,123 plr_dll_INITSTRINGINFO
               6 File(s)     37,757,784 bytes
               0 Dir(s)  116,514,676,736 bytes free

C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\doc\extension>dir
 Volume in drive C has no label.
 Volume Serial Number is 04C6-B781

 Directory of C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\doc\extension

09/12/2016  01:24 PM    <DIR>          .
09/12/2016  01:24 PM    <DIR>          ..
09/12/2016  07:45 AM             2,050 README.plr
               1 File(s)          2,050 bytes
               2 Dir(s)  116,430,069,760 bytes free

The pgAdmin III, schema public functions shows this

C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\doc\extension>Notepad2 C:\Users\AnonymousUser\Documents\pgadmin.log
SELECT pr.oid, pr.xmin, pr.*, pg_get_function_result(pr.oid) AS typname, typns.nspname AS typnsp, lanname, proargnames, pg_get_expr(proargdefaults, 'pg_catalog.pg_class'::regclass) AS proargdefaultvals, pronargdefaults, proconfig,        pg_get_userbyid(proowner) as funcowner, description,
(SELECT array_agg(label) FROM pg_seclabels sl1 WHERE sl1.objoid=pr.oid) AS labels,
(SELECT array_agg(provider) FROM pg_seclabels sl2 WHERE sl2.objoid=pr.oid) AS providers
  FROM pg_proc pr
  JOIN pg_type typ ON typ.oid=prorettype
  JOIN pg_namespace typns ON typns.oid=typ.typnamespace
  JOIN pg_language lng ON lng.oid=prolang
  LEFT OUTER JOIN pg_description des ON (des.objoid=pr.oid AND des.classoid='pg_proc'::regclass)
 WHERE proisagg = FALSE AND pronamespace = 2200::oid
   AND typname NOT IN ('trigger', 'event_trigger') 
 ORDER BY proname;

  oid  | xmin |       proname       | pronamespace |
-------+------+---------------------+--------------+
 16578 |  566 | install_rcmd        |         2200 |
 16590 |  566 | load_r_typenames    |         2200 |
       |      |                     |              |
       |      |                     |              |
       |      |                     |              |
       |      |                     |              |
       |      |                     |              |
       |      |                     |              |
       |      |                     |              |
       |      |                     |              |
       |      |                     |              |
       |      |                     |              |
       |      |                     |              |
       |      |                     |              |
 16581 |  566 | plr_array_accum     |         2200 |
 16580 |  566 | plr_array_push      |         2200 |
 16574 |  566 | plr_call_handler    |         2200 |
 16585 |  566 | plr_environ         |         2200 |
 16598 |  566 | plr_get_raw         |         2200 |
 16597 |  566 | plr_set_display     |         2200 |
 16595 |  566 | plr_set_rhome       |         2200 |
 16579 |  566 | plr_singleton_array |         2200 |
 16596 |  566 | plr_unset_rhome     |         2200 |
 16576 |  566 | plr_version         |         2200 |
 16589 |  566 | r_typenames         |         2200 |
       |      |                     |              |
       |      |                     |              |
       |      |                     |              |
       |      |                     |              |
       |      |                     |              |
 16594 |  566 | r_version           |         2200 |
       |      |                     |              |
       |      |                     |              |
 16577 |  566 | reload_plr_modules  |         2200 |
(15 rows)

Checking and looking for 'plr_modules'. I have not found it in here.

plr--8.3.0.16.sql
plr--unpackaged--8.3.0.16.sql
plr.sql

plr.c talks about 'table plr_modules (if it exists)' This does not seem immediately helpful.

/*
 * plr_load_modules() - Load procedures from
 *                      table plr_modules (if it exists)
 *
 * The caller is responsible to ensure SPI has already been connected
 * DO NOT make this static --- it has to be callable by reload_plr_modules()
 */
void
plr_load_modules(void)
{
    int             spi_rc;
    char           *cmd;
    int             i;
    int             fno;
    MemoryContext   oldcontext;
    char           *modulesSql;

    /* switch to SPI memory context */
    SWITCHTO_PLR_SPI_CONTEXT(oldcontext);

    /*
     * Check if table plr_modules exists
     */
    if (!haveModulesTable(plr_nspOid))
    {
        /* clean up if SPI was used, and regardless restore caller's context */
        CLEANUP_PLR_SPI_CONTEXT(oldcontext);
        return;
    }

    /* plr_modules table exists -- get SQL code extract table's contents */
    modulesSql = getModulesSql(plr_nspOid);

    /* Read all the row's from it in the order of modseq */
    spi_rc = SPI_exec(modulesSql, 0);

    /* modulesSql no longer needed -- cleanup */
    pfree(modulesSql);

    if (spi_rc != SPI_OK_SELECT)
        /* internal error */
        elog(ERROR, "plr_init_load_modules: select from plr_modules failed");

    /* If there's nothing, no modules exist */
    if (SPI_processed == 0)
    {
        SPI_freetuptable(SPI_tuptable);
        /* clean up if SPI was used, and regardless restore caller's context */
        CLEANUP_PLR_SPI_CONTEXT(oldcontext);
        return;
    }

    /*
     * There is at least on module to load. Get the
     * source from the modsrc load it in the R interpreter
     */
    fno = SPI_fnumber(SPI_tuptable->tupdesc, "modsrc");

    for (i = 0; i < SPI_processed; i++)
    {
        cmd = SPI_getvalue(SPI_tuptable->vals[i],
                            SPI_tuptable->tupdesc, fno);

        if (cmd != NULL)
        {
            load_r_cmd(cmd);
            pfree(cmd);
        }
    }
    SPI_freetuptable(SPI_tuptable);

    /* clean up if SPI was used, and regardless restore caller's context */
    CLEANUP_PLR_SPI_CONTEXT(oldcontext);
}

In these files, no 'CREATE table' exists ....

plr-8.3.0.16-pg9.5-R3.3.0-win64.zip (MAY_22_2016) 

plr.sql           (AS '$libdir/plr','plr_FUNCTION')  
  ==
plr--8.3.0.16.sql (AS 'MODULE_PATHNAME','plr_FUNCTION')

These files are exactly the same

For the most part (with some exceptions), the text . . .

  AS '$libdir/plr','plr_FUNCTION'

is swapped out with

  AS 'MODULE_PATHNAME','plr_FUNCTION'

This is the same in the 10devel (SEP_11_2016) ( and plr )

C:\postgres-master_0ab9c56_debug\contrib\plr\plr.sql
AND
C:\postgres-master_0ab9c56_debug\contrib\plr\plr--8.3.0.16.sql
BUT NOT IN
C:\postgres-master_0ab9c56_debug\contrib\plr\sql\plr.sql

C:\postgres-master_0ab9c56_debug\contrib\plr\sql\plr.sql does have the CREATE TABLE plr_modules like it 'must?' (and plr\sql\plr.sql ist just a collection of (more) plr functions and a 'test'? of plr )

CREATE TABLE plr_modules (

  modseq int4,

  modsrc text

);

Comparing plr-8.3.0.16-pg9.4-win64/plr

plr.sql           (AS '$libdir/plr','plr_FUNCTION')  
  ==
plr--8.3.0.16.sql (AS 'MODULE_PATHNAME','plr_FUNCTION')

Again,

These files are exactly the same ( AND I did get pl/r in PostgreSQL 9.4 on Windows to work )

For the most part (with some exceptions), the text . . .

  AS '$libdir/plr','plr_FUNCTION'

is swapped out with

  AS 'MODULE_PATHNAME','plr_FUNCTION'

From SQL in sql/plr.sql, I try to create/load/run some functions (create table/insert/reload is done first)

-- check version
SELECT plr_version();

-- make typenames available in the global namespace
select load_r_typenames();

CREATE TABLE plr_modules (
  modseq int4,
  modsrc text
);
INSERT INTO plr_modules VALUES (0, 'pg.test.module.load <-function(msg) {print(msg)}');
select reload_plr_modules();

--
-- plr_modules test
--
create or replace function pg_test_module_load(text) returns text as 'pg.test.module.load(arg1)' language 'plr';
select pg_test_module_load('hello world');

So, now I have data.

postgres=# SELECT NULL FROM pg_catalog.pg_class WHERE relname = 'plr_modules' AND relnamespace = 2200;

 ?column?
----------

(1 row)

postgres=# SELECT * FROM pg_catalog.pg_class WHERE relname = 'plr_modules' AND relnamespace = 2200;

   relname   | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | relpages | reltuples | relallvisible | reltoastrelid | relhasindex | relisshared | relpersistence | relkind | relnatts | relchecks | relhasoids | relhaspk
ey | relhasrules | relhastriggers | relhassubclass | relrowsecurity | relforcerowsecurity | relispopulated | relreplident | relfrozenxid | relminmxid | relacl | reloptions
-------------+--------------+---------+-----------+----------+-------+-------------+---------------+----------+-----------+---------------+---------------+-------------+-------------+----------------+---------+----------+-----------+------------+---------
---+-------------+----------------+----------------+----------------+---------------------+----------------+--------------+--------------+------------+--------+------------
 plr_modules |         2200 |   16602 |         0 |       10 |     0 |       16600 |             0 |        0 |         0 |             0 |         16603 | f           | f           | p              | r       |        2 |         0 | f          | f
   | f           | f              | f              | f              | f                   | t              | d            |          569 |          1 |        |
(1 row)

postgres=# SELECT modseq, modsrc FROM public.plr_modules ORDER BY modseq;
 modseq |                      modsrc
--------+--------------------------------------------------
      0 | pg.test.module.load <-function(msg) {print(msg)}
(1 row)

I get a 'different' error.

postgres=# select load_r_typenames();
ERROR:  could not open file "base/12418/16600": No such file or directory
CONTEXT:  SQL statement "SELECT modseq, modsrc FROM public.plr_modules ORDER BY modseq"
postgres=#

Eventually, this error this returns

postgres=# select load_r_typenames();
ERROR:  could not open file "base/12418/1259": No such file or directory
CONTEXT:  SQL statement "SELECT NULL FROM pg_catalog.pg_class WHERE relname = 'plr_modules' AND relnamespace = 2200"
postgres=#

NOTE: SPI_execute in tablefunct.c works fine.

contrib/tablefunc/tablefunct.c

static HTAB *

load_categories_hash(char *cats_sql, MemoryContext per_query_ctx)

  /* Retrieve the category name rows */

  ret = SPI_execute(cats_sql, true, 0);

TargetUser@TARGETMACH /c/postgres-master_0ab9c56_debug/contrib
$ make -C tablefunc clean
make: Entering directory `/c/postgres-master_0ab9c56_debug/contrib/tablefunc'
rm -f tablefunc.dll tablefunc.o win32ver.o
rm -rf results/ regression.diffs regression.out tmp_check/ log/
make: Leaving directory `/c/postgres-master_0ab9c56_debug/contrib/tablefunc'

TargetUser@TARGETMACH /c/postgres-master_0ab9c56_debug/contrib
$ make -C tablefunc all
make: Entering directory `/c/postgres-master_0ab9c56_debug/contrib/tablefunc'
x86_64-w64-mingw32-gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -Og -fno-omit-frame-pointer  -I. -I. -I
../../src/include -I./src/include/port/win32 -DEXEC_BACKEND  "-I../../src/include/port/win32"  -c -o tablefunc.o tablefunc.c
windres -i win32ver.rc -o win32ver.o --include-dir=../../src/include --include-dir=.
x86_64-w64-mingw32-gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -Og -fno-omit-frame-pointer  -shared -s
tatic-libgcc -o tablefunc.dll tablefunc.o win32ver.o -Wl,--export-all-symbols -L../../src/port -L../../src/common -Wl,--allow-multiple-definition -Wl,--disable-auto-import  -Wl,--as-needed -lm  -L../../src/backend -lpostgres
make: Leaving directory `/c/postgres-master_0ab9c56_debug/contrib/tablefunc'

TargetUser@TARGETMACH /c/postgres-master_0ab9c56_debug/contrib
$ make -C tablefunc install
make: Entering directory `/c/postgres-master_0ab9c56_debug/contrib/tablefunc'
/bin/mkdir -p '/usr/local/pgsql_0ab9c56_debug/share/extension'
/bin/mkdir -p '/usr/local/pgsql_0ab9c56_debug/share/extension'
/bin/mkdir -p '/usr/local/pgsql_0ab9c56_debug/lib'
/bin/install -c -m 644 ./tablefunc.control '/usr/local/pgsql_0ab9c56_debug/share/extension/'
/bin/install -c -m 644 ./tablefunc--1.0.sql ./tablefunc--unpackaged--1.0.sql  '/usr/local/pgsql_0ab9c56_debug/share/extension/'
/bin/install -c -m 755  tablefunc.dll '/usr/local/pgsql_0ab9c56_debug/lib/'
make: Leaving directory `/c/postgres-master_0ab9c56_debug/contrib/tablefunc'

TargetUser@TARGETMACH /c/postgres-master_0ab9c56_debug/contrib
$

Then, from ...

C:\postgres-master_0ab9c56_debug\contrib\tablefunc\sql\tablefunc.sql

C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug>cd "C:\postgres-master_0ab9c56_debug\contrib\tablefunc"

C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug>chcp 1252 > nul && "%PGSQL%\bin\psql.exe"  -p 5434
psql (10devel)
Type "help" for help.

postgres=# select version();
                                                                      version
----------------------------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 10devel on x86_64-w64-mingw32, compiled by x86_64-w64-mingw32-gcc.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 6.2.0, 64-bit
(1 row)

C:\postgres-master_0ab9c56_debug\contrib\tablefunc>chcp 1252 > nul && "%PGSQL%\bin\psql.exe"  -p 5434
psql (10devel)
Type "help" for help.

postgres=# \ir sql/tablefunc.sql

CREATE EXTENSION tablefunc;
CREATE EXTENSION
...
 avg
-----
 250
(1 row)
...
SELECT * FROM crosstab#('SELECT rowid, attribute, val FROM ...')
...

 row_name | category_1 | category_2
----------+------------+------------
 test1    | val2       | val2
 test2    | val6       | val6
          | val10      | val10
(3 rows)
...
 row_name | category_1 | category_2 | category_3
----------+------------+------------+------------
 test1    | val2       | val2       | val3
 test2    | val6       | val6       | val7
          | val10      | val10      | val11
(3 rows)
...

DROP TABLE
CREATE TABLE
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
 keyid | parent_keyid | level |  branch
-------+--------------+-------+----------
    11 |              |     0 | 11
    10 |           11 |     1 | 11-10
   111 |           11 |     1 | 11-111
     1 |          111 |     2 | 11-111-1
(4 rows)

Currently, I am using gdb and Code::Block to follow as you recommended. Again, the code is getting so complicated after 50 stack levels deep, I am having some difficulty following it anymore. I am trying to find some some sort of automated 'stepping' mechanism or script. I need to to know exactly where ( what function ) the the crash/error occurs. Does anyone know how to do that?

O.K. I think I found out how to do that. I have some more ...

postgres=# SELECT pg_backend_pid();
 pg_backend_pid
----------------
           3380
(1 row)

Here ...

Code::Blocks->Debug->Attach to process->3380

breaking on the errfinish function may have potential ... from ...
Developer FAQ
https://wiki.postgresql.org/wiki/Developer_FAQ
br errfinish
postgres=# select r_version();

Continuing...

At c:\postgres-master_0ab9c56_debug\src\backend\utils\error\elog.c:414

> bt
#0  errfinish (dummy=0) at elog.c:414
#1  0x00000000006c06f5 in mdopen (reln=reln@entry=0x2d4b58, forknum=forknum@entry=MAIN_FORKNUM, behavior=behavior@entry=9) at md.c:607
#2  0x00000000006c176f in _mdfd_getseg (reln=0x2d4b58, forknum=MAIN_FORKNUM, blkno=blkno@entry=0, skipFsync=skipFsync@entry=0 '\000', behavior=behavior@entry=9) at md.c:1783
#3  0x00000000006c1b14 in mdread (reln=<optimized out>, forknum=<optimized out>, blocknum=0, buffer=0x6a0a380 "") at md.c:741
#4  0x00000000006c2fa8 in smgrread (reln=0x0, reln@entry=0x2d4b58, forknum=forknum@entry=MAIN_FORKNUM, blocknum=blocknum@entry=0, buffer=0x0, buffer@entry=0x6a0a380 "") at smgr.c:628
#5  0x000000000069656c in ReadBuffer_common (smgr=0x2d4b58, relpersistence=<optimized out>, forkNum=forkNum@entry=MAIN_FORKNUM, blockNum=blockNum@entry=0, mode=mode@entry=RBM_NORMAL, strategy=0x0, strategy@entry=0x6e56a608 <R_MissingArg>, hit=0x388cf8f "", hit@entry=0x388cfaf "") at bufmgr.c:890
#6  0x0000000000696a37 in ReadBufferExtended (reln=0x5f84cc8, forkNum=forkNum@entry=MAIN_FORKNUM, blockNum=0, mode=mode@entry=RBM_NORMAL, strategy=strategy@entry=0x0) at bufmgr.c:664
#7  0x0000000000696b0b in ReadBuffer (reln=<optimized out>, blockNum=<optimized out>) at bufmgr.c:596
#8  0x0000000000461191 in _bt_getbuf (rel=rel@entry=0x5f84cc8, blkno=blkno@entry=0, access=access@entry=1) at nbtpage.c:576
#9  0x000000000046162b in _bt_getroot (rel=rel@entry=0x5f84cc8, access=1, access@entry=59298464) at nbtpage.c:158
#10 0x0000000000469893 in _bt_search (rel=rel@entry=0x5f84cc8, keysz=1, keysz@entry=2, scankey=0x388dae0, scankey@entry=0x388db30, nextkey=nextkey@entry=0 '\000', bufP=0x388e4ec, bufP@entry=0x388e53c, access=<optimized out>, access@entry=1, snapshot=0x5fc1cd8) at nbtsearch.c:99
#11 0x000000000046ae47 in _bt_first (scan=<optimized out>, scan@entry=0x5fd30b8, dir=<optimized out>, dir@entry=ForwardScanDirection) at nbtsearch.c:983
#12 0x00000000004660ea in btgettuple (scan=0x5fd30b8, dir=ForwardScanDirection) at nbtree.c:321
#13 0x000000000045b589 in index_getnext_tid (scan=scan@entry=0x5fd30b8, direction=direction@entry=ForwardScanDirection) at indexam.c:415

LOST SYMBOLS?  MANUAL STEPPED IN/NEXT HERE ( HERE - 2ND ROUND - I FELL ASLEEP ON MY PILLOW )
#14 0x000000000045ba04 in index_getnext (scan=0x5fd30b8, direction=direction@entry=ForwardScanDirection) at indexam.c:553
#15 0x000000000045a69f in systable_getnext (sysscan=sysscan@entry=0x5fd3060) at genam.c:416
#16 0x00000000007b7949 in SearchCatCache (cache=0x299a58, v1=<optimized out>, v2=<optimized out>, v3=v3@entry=0, v4=0, v4@entry=1) at catcache.c:1248

(I MANUALLY STEPPED IN/NEXT THROUG HERE, AFTER LOOP 2, IT CONTINUES)
#17 0x00000000007c5600 in SearchSysCache (cacheId=cacheId@entry=44, key1=<optimized out>, key2=<optimized out>, key3=key3@entry=0, key4=0, key4@entry=59305392) at syscache.c:982
#18 0x00000000007c56e1 in GetSysCacheOid (cacheId=cacheId@entry=44, key1=<optimized out>, key2=<optimized out>, key3=key3@entry=0, key4=key4@entry=0) at syscache.c:1060
#19 0x00000000007c78eb in get_relname_relid (relname=<optimized out>, relnamespace=<optimized out>) at lsyscache.c:1653
#20 0x00000000004bf7bb in RangeVarGetRelidExtended (relation=relation@entry=0x5fd2490, lockmode=lockmode@entry=1, missing_ok=missing_ok@entry=1 '\001', nowait=nowait@entry=0 '\000', callback=callback@entry=0x0, callback_arg=callback_arg@entry=0x0) at namespace.c:321
#21 0x0000000000449030 in relation_openrv_extended (relation=relation@entry=0x5fd2490, lockmode=lockmode@entry=1, missing_ok=missing_ok@entry=1 '\001') at heapam.c:1242
#22 0x0000000000449284 in heap_openrv_extended (relation=relation@entry=0x5fd2490, lockmode=lockmode@entry=1, missing_ok=missing_ok@entry=1 '\001') at heapam.c:1348
#23 0x00000000005144b8 in parserOpenTable (pstate=pstate@entry=0x5fd2d18, relation=relation@entry=0x5fd2490, lockmode=1) at parse_relation.c:1131
#24 0x000000000051592e in addRangeTableEntry (pstate=pstate@entry=0x5fd2d18, relation=relation@entry=0x5fd2490, alias=0x0, inh=<optimized out>, inFromCl=inFromCl@entry=1 '\001') at parse_relation.c:1196
#25 0x00000000005005b3 in transformTableEntry (pstate=0x5fd2d18, pstate@entry=0x388ed90, r=r@entry=0x5fd2490) at parse_clause.c:439
#26 0x00000000005009e9 in transformFromClauseItem (pstate=0x388ed90, pstate@entry=0x5fd2d18, n=0x5fd2490, top_rte=0x388ef70, top_rte@entry=0x388ecc0, top_rti=0x40, top_rti@entry=0x388ecbc, namespace=0x388ecd8, namespace@entry=0x388ecc8) at parse_clause.c:872
#27 0x0000000000501433 in transformFromClause (pstate=pstate@entry=0x5fd2d18, frmList=<optimized out>) at parse_clause.c:130
#28 0x00000000004e13e0 in transformSelectStmt (pstate=0x5fd2d18, stmt=stmt@entry=0x5fd2a28) at analyze.c:1156
#29 0x00000000004e1dbb in transformStmt (pstate=pstate@entry=0x5fd2d18, parseTree=parseTree@entry=0x5fd2a28) at analyze.c:270
#30 0x00000000004e282f in transformTopLevelStmt (pstate=pstate@entry=0x5fd2d18, parseTree=parseTree@entry=0x5fd2a28) at analyze.c:215
#31 0x00000000004e28ac in parse_analyze (parseTree=parseTree@entry=0x5fd2a28, sourceText=sourceText@entry=0x5fcfcb0 "SELECT NULL FROM pg_catalog.pg_class WHERE relname = 'plr_modules' AND relnamespace = 2200", paramTypes=paramTypes@entry=0x0, numParams=numParams@entry=0) at analyze.c:108
#32 0x00000000006c709c in pg_analyze_and_rewrite (parsetree=0x5fd2a28, query_string=0x5fcfcb0 "SELECT NULL FROM pg_catalog.pg_class WHERE relname = 'plr_modules' AND relnamespace = 2200", paramTypes=0x0, numParams=0) at postgres.c:658
#33 0x00000000005c34c9 in _SPI_execute_plan (plan=0x388f0f0, plan@entry=0x388f050, paramLI=0x5c3a55 <SPI_exec+21>, paramLI@entry=0x0, snapshot=0x5fc5e58, snapshot@entry=0x0, crosscheck_snapshot=0x388f110, crosscheck_snapshot@entry=0x0, read_only=read_only@entry=0 '\000', fire_triggers=fire_triggers@entry=1 '\001', tcount=tcount@entry=1) at spi.c:2077
#34 0x00000000005c3a1b in SPI_execute (src=0x5fcfcb0 "SELECT NULL FROM pg_catalog.pg_class WHERE relname = 'plr_modules' AND relnamespace = 2200", read_only=read_only@entry=0 '\000', tcount=tcount@entry=1) at spi.c:383
#35 0x00000000005c3a55 in SPI_exec (src=<optimized out>, tcount=tcount@entry=1) at spi.c:395
#36 0x000000006538310b in haveModulesTable (nspOid=2200) at plr.c:1916
#37 0x000000006538387d in plr_load_modules () at plr.c:500
#38 0x0000000065383a0e in plr_init_all (funcid=16755) at plr.c:577

(I MANUALLY STARTED HERE AT LINE 198)
#39 0x00000000653842f0 in plr_call_handler (fcinfo=0x5fc60f0) at plr.c:211

#40 0x000000000059d012 in ExecMakeFunctionResult (fcache=fcache@entry=0x5fc6080, econtext=econtext@entry=0x5fc5e58, isNull=isNull@entry=0x5fc6a30 "", isDone=isDone@entry=0x5fc6b70) at execQual.c:1840
#41 0x000000000059d389 in ExecEvalFunc (fcache=0x5fc6080, econtext=0x5fc5e58, isNull=0x5fc6a30 "", isDone=0x5fc6b70) at execQual.c:2437
#42 0x000000000059e1a6 in ExecTargetList (targetlist=0x5fc5e58, tupdesc=<optimized out>, econtext=0x5fc6a30, econtext@entry=0x5fc5e58, values=0x5fc6a10, isnull=<optimized out>, itemIsDone=0x5fc6b70, isDone=isDone@entry=0x388f4cc) at execQual.c:5486
#43 0x00000000005a052a in ExecProject (projInfo=0x5fc6a50, isDone=0x388f4cc, isDone@entry=0x388f4dc) at execQual.c:5710
#44 0x00000000005b65f8 in ExecResult (node=node@entry=0x5fc5d40) at nodeResult.c:155
#45 0x0000000000599165 in ExecProcNode (node=node@entry=0x5fc5d40) at execProcnode.c:392
#46 0x0000000000594ef9 in ExecutePlan (estate=estate@entry=0x5fc5c28, planstate=0x5fc5d40, use_parallel_mode=0 '\000', operation=operation@entry=CMD_SELECT, sendTuples=1 '\001', sendTuples@entry=0 '\000', numberTuples=0, numberTuples@entry=59307696, direction=direction@entry=ForwardScanDirection, dest=dest@entry=0x5fc3f48) at execMain.c:1567
#47 0x0000000000595aba in standard_ExecutorRun (queryDesc=0x30c6f8, direction=ForwardScanDirection, count=59307696) at execMain.c:338
#48 0x0000000000595b0b in ExecutorRun (queryDesc=queryDesc@entry=0x30c6f8, direction=direction@entry=ForwardScanDirection, count=count@entry=0) at execMain.c:286
#49 0x00000000006c9b80 in PortalRunSelect (portal=0x31a1d8, portal@entry=0x5fc3f48, forward=forward@entry=1 '\001', count=0, count@entry=100417352, dest=dest@entry=0x127ef0) at pquery.c:948
#50 0x00000000006cb247 in PortalRun (portal=0x5fc3f48, portal@entry=0x31a1d8, count=100417352, count@entry=2147483647, isTopLevel=isTopLevel@entry=1 '\001', dest=0x127ef0, dest@entry=0x5fc3f48, altdest=<optimized out>, altdest@entry=0x5fc3f48, completionTag=0x388f900 "", completionTag@entry=0x388f990 "") at pquery.c:789
#51 0x00000000006c75d9 in exec_simple_query (query_string=query_string@entry=0x2c7c28 "select r_version();") at postgres.c:1094
#52 0x00000000006c9475 in PostgresMain (argc=<optimized out>, argv=argv@entry=0x2741f8, dbname=<optimized out>, username=<optimized out>) at postgres.c:4070
#53 0x00000000006576b6 in BackendRun (port=port@entry=0x388fc10) at postmaster.c:4260
#54 0x000000000065ba44 in SubPostmasterMain (argc=argc@entry=3, argv=argv@entry=0x24ffa0) at postmaster.c:4750
#55 0x00000000005d4c42 in main (argc=3, argv=0x24ffa0) at main.c:205

Of

#1 0x6c06f5 mdopen(reln=reln@entry=0x184b58, forknum=forknum@entry=MAIN_FORKNUM, behavior=behavior@entry=9) (md.c:607)
``

At ( path = )
C:\postgres-master_0ab9c56_debug\src\backend\storage\smgr\md.c:585
the Code::Block watch shows  "fd = -1"
If fd means 'file descriptor?" ....

```C
    path = relpath(reln->smgr_rnode, forknum);

    fd = PathNameOpenFile(path, O_RDWR | PG_BINARY, 0600);

    if (fd < 0) ## (Code::Blocks watches 'fd == -1' SO IA M GOING IN HERE) ##   
    {
        ## (DON'T WANT TO GO IN HERE) ##

If 'fd means file descriptor' and my fd == -1, then that is not good.

C:\postgres-master_0ab9c56_debug\src\backend\storage\smgr\md.c:69
#include <fcntl.h>

An explanation is here ...

A file descriptor is a non-negative integer, generally represented in the C programming language 
as the type int (negative values being reserved to indicate "no value" or an error condition).

Some of these functions are declared in the <fcntl.h> header.

https://en.wikipedia.org/wiki/File_descriptor
At C:\postgres-master_0ab9c56_debug\src\backend\storage\file\fd.c:1228

/*
 * open a file in an arbitrary directory
 *
 * NB: if the passed pathname is relative (which it usually is),
 * it will be interpreted relative to the process' working directory
 * (which should always be $PGDATA when this code is running).
 */
File
PathNameOpenFile(FileName fileName, int fileFlags, int fileMode)

My environment variable is correct

C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug>set
PGDATA=C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\data

C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug>dir C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\data\base
 Volume in drive C has no label.
 Volume Serial Number is 04C6-B781

 Directory of C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\data\base

09/11/2016  05:48 PM    <DIR>          .
09/11/2016  05:48 PM    <DIR>          ..
09/11/2016  05:48 PM    <DIR>          1
09/11/2016  05:48 PM    <DIR>          12417
09/27/2016  06:55 PM    <DIR>          12418
               0 File(s)              0 bytes
               5 Dir(s)  116,551,147,520 bytes free

I want to see my permissions. Microsoft can help

Xcacls.vbs ( use 7-zip to open(means Edit) and extract the file: XCACLS.vbs )
http://download.microsoft.com/download/f/7/8/f786aaf3-a37b-45ab-b0a2-8c8c18bbf483/xcacls_installer.exe
https://support.microsoft.com/en-us/kb/825751#bookmark-4

I run
C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\data>cscript XCACLS.vbs base
Then, I follow the instructions, I do add my version of windows: ", 6.1" ( Windows 7) in this case, to the file XCACLS.vbs

Then I rerun.

C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\data>cscript XCACLS.vbs base
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Starting XCACLS.VBS (Version: 5.2) Script at 9/27/2016 7:48:25 PM

Startup directory:
"C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\data"

Arguments Used:
        Filename = "base"

Directory: C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\data\base

Permissions:
Type     Username                Permissions           Inheritance

Allowed  BUILTIN\Administrators  Full Control          This Folder Only
Allowed  BUILTIN\Administrators  Special (Unknown)     Subfolders and Files
Allowed  NT AUTHORITY\SYSTEM     Full Control          This Folder Only
Allowed  NT AUTHORITY\SYSTEM     Special (Unknown)     Subfolders and Files
Allowed  BUILTIN\Users           Read and Execute      This Folder, Subfolde
Allowed  NT AUTHORITY\Authentica Modify                This Folder Only
Allowed  NT AUTHORITY\Authentica Advanced (Delete)     Subfolders and Files

No Auditing set

Owner: TARGETMACH\TargetUser

Operation Complete
Elapsed Time: 1.234375 seconds.

Ending Script at 9/27/2016 7:48:26 PM

The exact same answers(Type, Username, Permissions,Inheritance) was obtained for subfolders

C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\data>cscript XCACLS.vbs  base/12417
C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\data>cscript XCACLS.vbs  base/12418
C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\data>cscript XCACLS.vbs  base/1

Here is the point of view from msys, if it matters . . .

TargetUser@TARGETMACH /usr/local/pgsql_0ab9c56_debug/data/base
$ ls -alrt
total 176
drwxr-xr-x  2 TargetUser Administrators 40960 Sep 11 17:48 1
drwxr-xr-x  2 TargetUser Administrators 65536 Sep 11 17:48 12417
drwxr-xr-x  5 TargetUser Administrators     0 Sep 11 17:48 .
drwxr-xr-x 19 TargetUser Administrators  8192 Sep 27 18:55 ..
drwxr-xr-x  2 TargetUser Administrators 65536 Sep 27 18:55 12418

I want to see the values of the fileFlags of O_RDWR and PG_BINARY.

PathNameOpenFile(FileName fileName, int fileFlags, int fileMode)
fd = PathNameOpenFile(path, O_RDWR | PG_BINARY, 0600);

Code::Blocks thread search finds PG_BINARY here.

C:\postgres-master_0ab9c56_debug\src\include\c.h:118

#define PG_BINARY 0

Code::Blocks thread search can not find the O_RDWR definition. So, I look for it in another way.

TargetUser@TARGETMACH /c/postgres-master_0ab9c56_debug
$ find . -name '*.h' -type f | xargs grep -l "O_RDWR" | sort

TargetUser@TARGETMACH /c/postgres-master_0ab9c56_debug
$ find . -name '*.c' -type f | xargs grep -l "O_RDWR" | sort
./contrib/pg_standby/pg_standby.c
./contrib/pg_stat_statements/pg_stat_statements.c
./src/backend/access/transam/slru.c
./src/backend/access/transam/timeline.c
./src/backend/access/transam/xlog.c
./src/backend/replication/slot.c
./src/backend/storage/file/copydir.c
./src/backend/storage/file/fd.c
./src/backend/storage/ipc/dsm_impl.c
./src/backend/storage/smgr/md.c
./src/backend/utils/init/miscinit.c
./src/backend/utils/misc/guc.c
./src/bin/initdb/initdb.c
./src/bin/pg_dump/pg_backup_tar.c
./src/bin/pg_resetxlog/pg_resetxlog.c
./src/bin/pg_test_fsync/pg_test_fsync.c
./src/bin/pg_upgrade/exec.c
./src/bin/pg_upgrade/file.c
./src/interfaces/libpq/open.c
./src/port/mkdtemp.c
./src/port/open.c
./src/test/thread/thread_test.c

TargetUser@TARGETMACH /c/postgres-master_0ab9c56_debug
$

So, I still can not find the defintion of O_RDWR.

I guess: it seems like it should be in fd.c(or a header) C:\postgres-master_0ab9c56_debug\src\backend\storage\file\fd.c:64

#ifndef WIN32
#include <sys/mman.h>
#endif

Here is something about it.

sys/mman.h: No such file or directory

  https://code.google.com/archive/p/mman-win32/source/default/source

http://stackoverflow.com/questions/1810568/sys-mman-h-no-such-file-or-directory

I can not find O_RDWR anywhere.

TargetUser@TARGETMACH /c/postgres-master_0ab9c56_debug
$ find . -name '*mman.h' -print 2>/dev/null

TargetUser@TARGETMACH /c/postgres-master_0ab9c56_debug

I am tired. I am going to sleep.

Two questions.

  1. Maybe I should debug_more ( -g ? ), so some of my symbols are not optimized_out ( e.g. path in PathNameOpenFile ). Or, does this not make a difference, right now?
  2. Should I ask some more PostgreSQL people, Or, does that not matter right now?
davecramer commented 7 years ago

On 27 September 2016 at 18:09, AndreMikulec notifications@github.com wrote:

Seems to just show my user tables

postgres=# \d List of relations Schema | Name | Type | Owner--------+---------+-------+---------- public | ct | table | postgres public | mytable | table | postgres (2 rows)

postgres=# select count(*) from pg_catalog.pg_class; count------- 323 (1 row)

postgres=# select * from pg_catalog.pgclass where relname like 'r%' order by 1; relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace |----------------+--------------+---------+-----------+----------+-------+-------------+---------------+ r_typename | 2200 | 16588 | 0 | 10 | 0 | 16586 | 0 | r_version_type | 2200 | 16593 | 0 | 10 | 0 | 16591 | 0 | (2 rows)

postgres=# select * from pg_catalog.pgclass where relname like 'plr%' order by 1; relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace |------------------+--------------+---------+-----------+----------+-------+-------------+---------------+ plr_environ_type | 2200 | 16584 | 0 | 10 | 0 | 16582 | 0 | (1 row)

Maybe, for some reason, CREATE EXTENSION, is not creating those functions?

From INSTALL.txt plr-8.3.0.16-pg9.5-R3.3.0-win64_MAY_22_2016

In the instructions below: PostgreSQL is installed to R is installed to

For example, these directories might be:

= C:\PostgreSQL\9.4 = C:\R\R-3.1.2 I recommend you ensure there are no spaces in the pathname to either or . Correct quoting of pathnames with spaces is left as an exercise for the reader ;-) ## The following files are contained in this zip file: README.txt: place in \doc\extension plr.dll: place in \lib plr.sql: place in \share\extension plr.control: place in \share\extension plr--8.3.0.14.sql place in \share\extension plr--unpackaged--8.3.0.16.sql place in \share\extension ## Ensure the following environment variables are set _prior_ to starting PostgreSQL: PATH=\bin;\lib;\bin\x64;$PATH R_HOME= I have all of those files C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\share\extension>dir plr_._ Volume in drive C has no label. Volume Serial Number is 04C6-B781 Directory of C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\share\extension 09/16/2016 07:17 PM 2,893 plr--8.3.0.16.sql 09/16/2016 07:17 PM 1,035 plr--unpackaged--8.3.0.16.sql 09/16/2016 07:17 PM 171 plr.control 09/16/2016 07:17 PM 2,854 plr.sql 4 File(s) 6,953 bytes 0 Dir(s) 116,514,676,736 bytes free C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\lib>dir plr* Volume in drive C has no label. Volume Serial Number is 04C6-B781 Directory of C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\lib 09/16/2016 07:17 PM 6,292,805 plr.dll 09/12/2016 12:29 PM 6,292,805 plr_dll_CONSTANT_CHAR 09/16/2016 12:06 PM 6,292,805 plr_dll_CONSTANT_CHAR_POINTER 09/16/2016 06:23 PM 6,293,123 plr_dll_DEBUGRESULT_MYTABLE_MYINT 09/16/2016 04:56 PM 6,293,123 plr_dll_DEBUGRESULT_SEL_1_EQ_0 09/16/2016 04:25 PM 6,293,123 plr_dll_INITSTRINGINFO 6 File(s) 37,757,784 bytes 0 Dir(s) 116,514,676,736 bytes free C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\doc\extension>dir Volume in drive C has no label. Volume Serial Number is 04C6-B781 Directory of C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\doc\extension 09/12/2016 01:24 PM . 09/12/2016 01:24 PM .. 09/12/2016 07:45 AM 2,050 README.plr 1 File(s) 2,050 bytes 2 Dir(s) 116,430,069,760 bytes free The pgAdmin III, schema public functions shows this C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug\doc\extension>Notepad2 C:\Users\AnonymousUser\Documents\pgadmin.log SELECT pr.oid, pr.xmin, pr.*, pg_get_function_result(pr.oid) AS typname, typns.nspname AS typnsp, lanname, proargnames, pg_get_expr(proargdefaults, 'pg_catalog.pg_class'::regclass) AS proargdefaultvals, pronargdefaults, proconfig, pg_get_userbyid(proowner) as funcowner, description, (SELECT array_agg(label) FROM pg_seclabels sl1 WHERE sl1.objoid=pr.oid) AS labels, (SELECT array_agg(provider) FROM pg_seclabels sl2 WHERE sl2.objoid=pr.oid) AS providers FROM pg_proc pr JOIN pg_type typ ON typ.oid=prorettype JOIN pg_namespace typns ON typns.oid=typ.typnamespace JOIN pg_language lng ON lng.oid=prolang LEFT OUTER JOIN pg_description des ON (des.objoid=pr.oid AND des.classoid='pg_proc'::regclass) WHERE proisagg = FALSE AND pronamespace = 2200::oid AND typname NOT IN ('trigger', 'event_trigger') ORDER BY proname; oid | xmin | proname | pronamespace |-------+------+---------------------+--------------+ 16578 | 566 | install_rcmd | 2200 | 16590 | 566 | load_r_typenames | 2200 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 16581 | 566 | plr_array_accum | 2200 | 16580 | 566 | plr_array_push | 2200 | 16574 | 566 | plr_call_handler | 2200 | 16585 | 566 | plr_environ | 2200 | 16598 | 566 | plr_get_raw | 2200 | 16597 | 566 | plr_set_display | 2200 | 16595 | 566 | plr_set_rhome | 2200 | 16579 | 566 | plr_singleton_array | 2200 | 16596 | 566 | plr_unset_rhome | 2200 | 16576 | 566 | plr_version | 2200 | 16589 | 566 | r_typenames | 2200 | | | | | | | | | | | | | | | | | | | | | 16594 | 566 | r_version | 2200 | | | | | | | | | 16577 | 566 | reload_plr_modules | 2200 | (15 rows) Checking and looking for 'plr_modules'. I have _not_ found it in here. plr--8.3.0.16.sql plr--unpackaged--8.3.0.16.sql plr.sql plr.c talks about 'table plr_modules (if it exists)' This does not seem immediately helpful. /\* \* plr_load_modules() - Load procedures from \* table plr_modules (if it exists) \* \* The caller is responsible to ensure SPI has already been connected \* DO NOT make this static --- it has to be callable by reload_plr_modules() */voidplr_load_modules(void) { int spi_rc; char *cmd; int i; int fno; MemoryContext oldcontext; char *modulesSql; ``` /* switch to SPI memory context */ SWITCHTO_PLR_SPI_CONTEXT(oldcontext); /* * Check if table plr_modules exists */ if (!haveModulesTable(plr_nspOid)) { /* clean up if SPI was used, and regardless restore caller's context */ CLEANUP_PLR_SPI_CONTEXT(oldcontext); return; } /* plr_modules table exists -- get SQL code extract table's contents */ modulesSql = getModulesSql(plr_nspOid); /* Read all the row's from it in the order of modseq */ spi_rc = SPI_exec(modulesSql, 0); /* modulesSql no longer needed -- cleanup */ pfree(modulesSql); if (spi_rc != SPI_OK_SELECT) /* internal error */ elog(ERROR, "plr_init_load_modules: select from plr_modules failed"); /* If there's nothing, no modules exist */ if (SPI_processed == 0) { SPI_freetuptable(SPI_tuptable); /* clean up if SPI was used, and regardless restore caller's context */ CLEANUP_PLR_SPI_CONTEXT(oldcontext); return; } /* * There is at least on module to load. Get the * source from the modsrc load it in the R interpreter */ fno = SPI_fnumber(SPI_tuptable->tupdesc, "modsrc"); for (i = 0; i < SPI_processed; i++) { cmd = SPI_getvalue(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, fno); if (cmd != NULL) { load_r_cmd(cmd); pfree(cmd); } } SPI_freetuptable(SPI_tuptable); /* clean up if SPI was used, and regardless restore caller's context */ CLEANUP_PLR_SPI_CONTEXT(oldcontext); ``` } In these files, no 'CREATE table' exists .... plr-8.3.0.16-pg9.5-R3.3.0-win64.zip (MAY_22_2016) plr.sql (AS '$libdir/plr','plr_FUNCTION') == plr--8.3.0.16.sql (AS 'MODULE_PATHNAME','plr_FUNCTION') These files are exactly the same For the most part (with some exceptions), the text . . . AS '$libdir/plr','plr_FUNCTION' is swapped out with AS 'MODULE_PATHNAME','plr_FUNCTION' This is the same in the 10devel (SEP_11_2016) ( and plr ) C:\postgres-master_0ab9c56_debug\contrib\plr\plr.sql AND C:\postgres-master_0ab9c56_debug\contrib\plr\plr--8.3.0.16.sql BUT NOT IN C:\postgres-master_0ab9c56_debug\contrib\plr\sql\plr.sql C:\postgres-master_0ab9c56_debug\contrib\plr\sql\plr.sql does have the CREATE TABLE plr_modules like it 'must?' (and plr\sql\plr.sql ist just a collection of (more) plr functions and a 'test'? of plr ) CREATE TABLE plr_modules ( modseq int4, modsrc text ); Comparing plr-8.3.0.16-pg9.4-win64/plr plr.sql (AS '$libdir/plr','plr_FUNCTION') == plr--8.3.0.16.sql (AS 'MODULE_PATHNAME','plr_FUNCTION') Again, These files are exactly the same ( AND I did get pl/r in PostgreSQL 9.4 on Windows to work ) For the most part (with some exceptions), the text . . . AS '$libdir/plr','plr_FUNCTION' is swapped out with AS 'MODULE_PATHNAME','plr_FUNCTION' From SQL in sql/plr.sql, I try to create/load/run some functions (create table/insert/reload is done first) -- check versionSELECT plr_version(); -- make typenames available in the global namespaceselect load_r_typenames(); CREATE TABLE plr_modules ( modseq int4, modsrc text );INSERT INTO plr_modules VALUES (0, 'pg.test.module.load <-function(msg) {print(msg)}');select reload_plr_modules(); ---- plr_modules test--create or replace function pg_test_module_load(text) returns text as 'pg.test.module.load(arg1)' language 'plr';select pg_test_module_load('hello world'); So, now I have data. postgres=# SELECT NULL FROM pg_catalog.pg_class WHERE relname = 'plr_modules' AND relnamespace = 2200; ?column?---------- (1 row) postgres=# SELECT \* FROM pg_catalog.pg_class WHERE relname = 'plr_modules' AND relnamespace = 2200; relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | relpages | reltuples | relallvisible | reltoastrelid | relhasindex | relisshared | relpersistence | relkind | relnatts | relchecks | relhasoids | relhaspk ey | relhasrules | relhastriggers | relhassubclass | relrowsecurity | relforcerowsecurity | relispopulated | relreplident | relfrozenxid | relminmxid | relacl | reloptions-------------+--------------+---------+-----------+----------+-------+-------------+---------------+----------+-----------+---------------+---------------+-------------+-------------+----------------+---------+----------+-----------+------------+------------+-------------+----------------+----------------+----------------+---------------------+----------------+--------------+--------------+------------+--------+------------ plr_modules | 2200 | 16602 | 0 | 10 | 0 | 16600 | 0 | 0 | 0 | 0 | 16603 | f | f | p | r | 2 | 0 | f | f | f | f | f | f | f | t | d | 569 | 1 | | (1 row) postgres=# SELECT modseq, modsrc FROM public.plr_modules ORDER BY modseq; modseq | modsrc--------+-------------------------------------------------- 0 | pg.test.module.load <-function(msg) {print(msg)} (1 row) I get a 'different' error. postgres=# select load_r_typenames(); ERROR: could not open file "base/12418/16600": No such file or directory CONTEXT: SQL statement "SELECT modseq, modsrc FROM public.plr_modules ORDER BY modseq" postgres=# Eventually, this error this returns postgres=# select load_r_typenames(); ERROR: could not open file "base/12418/1259": No such file or directory CONTEXT: SQL statement "SELECT NULL FROM pg_catalog.pg_class WHERE relname = 'plr_modules' AND relnamespace = 2200" postgres=# NOTE: SPI_execute in tablefunct.c works fine. contrib/tablefunc/tablefunct.c static HTAB * load_categories_hash(char *cats_sql, MemoryContext per_query_ctx) /\* Retrieve the category name rows */ ret = SPI_execute(cats_sql, true, 0); TargetUser@TARGETMACH /c/postgres-master_0ab9c56_debug/contrib $ make -C tablefunc clean make: Entering directory `/c/postgres-master_0ab9c56_debug/contrib/tablefunc' rm -f tablefunc.dll tablefunc.o win32ver.o rm -rf results/ regression.diffs regression.out tmp_check/ log/ make: Leaving directory`/c/postgres-master_0ab9c56_debug/contrib/tablefunc' TargetUser@TARGETMACH /c/postgres-master_0ab9c56_debug/contrib $ make -C tablefunc all make: Entering directory `/c/postgres-master_0ab9c56_debug/contrib/tablefunc' x86_64-w64-mingw32-gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -Og -fno-omit-frame-pointer -I. -I. -I ../../src/include -I./src/include/port/win32 -DEXEC_BACKEND "-I../../src/include/port/win32" -c -o tablefunc.o tablefunc.c windres -i win32ver.rc -o win32ver.o --include-dir=../../src/include --include-dir=. x86_64-w64-mingw32-gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -Og -fno-omit-frame-pointer -shared -s tatic-libgcc -o tablefunc.dll tablefunc.o win32ver.o -Wl,--export-all-symbols -L../../src/port -L../../src/common -Wl,--allow-multiple-definition -Wl,--disable-auto-import -Wl,--as-needed -lm -L../../src/backend -lpostgres make: Leaving directory`/c/postgres-master_0ab9c56_debug/contrib/tablefunc' TargetUser@TARGETMACH /c/postgres-master_0ab9c56_debug/contrib $ make -C tablefunc install make: Entering directory `/c/postgres-master_0ab9c56_debug/contrib/tablefunc' /bin/mkdir -p '/usr/local/pgsql_0ab9c56_debug/share/extension' /bin/mkdir -p '/usr/local/pgsql_0ab9c56_debug/share/extension' /bin/mkdir -p '/usr/local/pgsql_0ab9c56_debug/lib' /bin/install -c -m 644 ./tablefunc.control '/usr/local/pgsql_0ab9c56_debug/share/extension/' /bin/install -c -m 644 ./tablefunc--1.0.sql ./tablefunc--unpackaged--1.0.sql '/usr/local/pgsql_0ab9c56_debug/share/extension/' /bin/install -c -m 755 tablefunc.dll '/usr/local/pgsql_0ab9c56_debug/lib/' make: Leaving directory`/c/postgres-master_0ab9c56_debug/contrib/tablefunc' TargetUser@TARGETMACH /c/postgres-master_0ab9c56_debug/contrib $ Then, from ... C:\postgres-master_0ab9c56_debug\contrib\tablefunc\sql\tablefunc.sql C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug>cd "C:\postgres-master_0ab9c56_debug\contrib\tablefunc" C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug>chcp 1252 > nul && "%PGSQL%\bin\psql.exe" -p 5434 psql (10devel) Type "help" for help. postgres=# select version(); version---------------------------------------------------------------------------------------------------------------------------------------------------- PostgreSQL 10devel on x86_64-w64-mingw32, compiled by x86_64-w64-mingw32-gcc.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 6.2.0, 64-bit (1 row) C:\postgres-master_0ab9c56_debug\contrib\tablefunc>chcp 1252 > nul && "%PGSQL%\bin\psql.exe" -p 5434 psql (10devel) Type "help" for help. postgres=# \ir sql/tablefunc.sql CREATE EXTENSION tablefunc; CREATE EXTENSION ... avg----- 250 (1 row) ...SELECT \* FROM crosstab#('SELECT rowid, attribute, val FROM ...') ... row_name | category_1 | category_2----------+------------+------------ test1 | val2 | val2 test2 | val6 | val6 | val10 | val10 (3 rows) ... row_name | category_1 | category_2 | category_3----------+------------+------------+------------ test1 | val2 | val2 | val3 test2 | val6 | val6 | val7 | val10 | val10 | val11 (3 rows) ... DROP TABLE CREATE TABLE INSERT 0 1 INSERT 0 1 INSERT 0 1 INSERT 0 1 keyid | parent_keyid | level | branch-------+--------------+-------+---------- 11 | | 0 | 11 10 | 11 | 1 | 11-10 111 | 11 | 1 | 11-111 1 | 111 | 2 | 11-111-1 (4 rows) Currently, I am using gdb and Code::Block to follow as you recommended. Again, the code is getting so complicated after 50 stack levels deep, I am having some difficulty following it anymore. I am trying to find some some sort of automated 'stepping' mechanism or script. I need to to know exactly where ( what function ) the the crash/error occurs. Does anyone know how to do that? — You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jconway/plr/issues/26#issuecomment-250014066, or mute the thread https://github.com/notifications/unsubscribe-auth/AAYz9sltKzQ0-KxK-CLZElDq8emdC6_6ks5quZQggaJpZM4J0amQ .

Hi Andre

I applaud your tenacity. What I would do is put a breakpoint in ereport and wait until it gets called. This is where it outputs the error message.

Thanks,

Dave Cramer

AndreMikulec commented 7 years ago

O.K.

ereport ends up being a macro ( so I can not break on it) that is redefined many times in many places.

Here is what I have. ( There does exist an ereport further below. )

Breakpoints are at

plr_call_handler
PathNameOpenFile

and I run

select r_version();

Here is what happens.

* 1    Thread 3644.0x10d8 PathNameOpenFile (fileName=fileName@entry=0xf147d68 "base/12418/2682", fileFlags=fileFlags@entry=32770, fileMode=fileMode@entry=384) at fd.c:1229
>>>>>>cb_gdb:
SUCCEED ... CONTINUE ...

* 1    Thread 3644.0x10d8 PathNameOpenFile (fileName=fileName@entry=0xf147d68 "base/12418/2612", fileFlags=fileFlags@entry=32770, fileMode=fileMode@entry=384) at fd.c:1229
>>>>>>cb_gdb:
SUCCEED ... CONTINUE ...

PLR
* 1    Thread 3644.0x10d8 plr_call_handler (fcinfo=0xf145f80) at plr.c:199
>>>>>>cb_gdb:
PLR... CONTINUE ...

> bt
#0  plr_call_handler (fcinfo=0xf145f80) at plr.c:199
#1  0x0000000000598027 in ExecMakeFunctionResult (fcache=fcache@entry=0xf145f10, econtext=econtext@entry=0xf145ce8, isNull=isNull@entry=0xf1468c0 "", isDone=isDone@entry=0xf146a00) at execQual.c:1840
#2  0x0000000000598264 in ExecEvalFunc (fcache=0xf145f10, econtext=0xf145ce8, isNull=0xf1468c0 "", isDone=0xf146a00) at execQual.c:2437
#3  0x000000000059b4ad in ExecTargetList (isDone=0xf145bd0, itemIsDone=0xf146a00, isnull=0xf1468c0 "", values=0x1, econtext=0x0, tupdesc=<optimized out>, targetlist=0xf145ce8) at execQual.c:5486
#4  ExecProject (projInfo=<optimized out>, isDone=0xf145bd0, isDone@entry=0x387ec1c) at execQual.c:5710
#5  0x00000000005b18c1 in ExecResult (node=node@entry=0xf145bd0) at nodeResult.c:155
#6  0x0000000000593ff1 in ExecProcNode (node=node@entry=0xf145bd0) at execProcnode.c:392
#7  0x000000000058ffde in ExecutePlan (dest=0xf143dd8, direction=<optimized out>, numberTuples=0, sendTuples=1 '\001', operation=CMD_SELECT, use_parallel_mode=<optimized out>, planstate=0xf145bd0, estate=0xf145ab8) at execMain.c:1567
#8  standard_ExecutorRun (queryDesc=0x1cd948, direction=<optimized out>, count=0) at execMain.c:338
#9  0x000000000059009a in ExecutorRun (queryDesc=queryDesc@entry=0x1cd948, direction=direction@entry=ForwardScanDirection, count=count@entry=0) at execMain.c:286
#10 0x00000000006c0a03 in PortalRunSelect (portal=0x1dc348, portal@entry=0xf143dd8, forward=forward@entry=1 '\001', count=0, count@entry=252984792, dest=dest@entry=0xe2ba8) at pquery.c:948
#11 0x00000000006c20f9 in PortalRun (portal=0xf143dd8, portal@entry=0x1dc348, count=252984792, count@entry=2147483647, isTopLevel=isTopLevel@entry=0 '\000', dest=0xe2ba8, dest@entry=0xf143dd8, altdest=<optimized out>, altdest@entry=0xf143dd8, completionTag=0x387f180 "", completionTag@entry=0x387f1b8 "¸ãi") at pquery.c:789
#12 0x00000000006be813 in exec_simple_query (query_string=0x7cd03f <string_compare+17> "HƒÄ ]ÃUH‰åHƒì H‰ÊH‹\réT*") at postgres.c:1094
#13 PostgresMain (argc=<optimized out>, argv=argv@entry=0x1344d8, dbname=<optimized out>, username=<optimized out>) at postgres.c:4070
#14 0x0000000000653d3a in BackendRun (port=0x387fc30) at postmaster.c:4260
#15 SubPostmasterMain (argc=argc@entry=3, argv=argv@entry=0x10ffa0) at postmaster.c:4750
#16 0x00000000005cf6b8 in main (argc=3, argv=0x10ffa0) at main.c:205

> bt
#0  PathNameOpenFile (fileName=fileName@entry=0xf14f040 "base/12418/2663", fileFlags=fileFlags@entry=32770, fileMode=fileMode@entry=384) at fd.c:1229
#1  0x00000000006b75fb in mdopen (reln=reln@entry=0x18fdc8, forknum=forknum@entry=MAIN_FORKNUM, behavior=behavior@entry=9) at md.c:587
#2  0x00000000006b87e3 in _mdfd_getseg (reln=0x18fdc8, forknum=MAIN_FORKNUM, blkno=blkno@entry=0, skipFsync=skipFsync@entry=0 '\000', behavior=behavior@entry=9) at md.c:1783
#3  0x00000000006b8bc8 in mdread (reln=<optimized out>, forknum=<optimized out>, blocknum=0, buffer=0x6a42380 "") at md.c:741
#4  0x00000000006ba05a in smgrread (reln=0xf14f040, reln@entry=0x0, forknum=32770, forknum@entry=FSM_FORKNUM, blocknum=384, blocknum@entry=252787568, buffer=0x387c170 "Oð\024\017", buffer@entry=0x6a42380 "") at smgr.c:628
#5  0x000000000068d22e in ReadBuffer_common (smgr=0x0, relpersistence=<optimized out>, forkNum=FSM_FORKNUM, forkNum@entry=MAIN_FORKNUM, blockNum=252787568, blockNum@entry=0, mode=mode@entry=RBM_NORMAL, strategy=strategy@entry=0x0, hit=0x387c5bf "", hit@entry=0x387c5df "") at bufmgr.c:890
#6  0x000000000068dccb in ReadBufferExtended (reln=0xf113b70, forkNum=forkNum@entry=MAIN_FORKNUM, blockNum=0, mode=mode@entry=RBM_NORMAL, strategy=strategy@entry=0x0) at bufmgr.c:664
#7  0x000000000068dd98 in ReadBuffer (reln=<optimized out>, blockNum=<optimized out>) at bufmgr.c:596
#8  0x000000000045e5cf in _bt_getbuf (rel=rel@entry=0xf113b70, blkno=blkno@entry=0, access=access@entry=1) at nbtpage.c:576
#9  0x000000000045eac5 in _bt_getroot (rel=rel@entry=0xf113b70, access=1, access@entry=254581064) at nbtpage.c:158
#10 0x00000000004666e4 in _bt_search (rel=rel@entry=0xf113b70, keysz=1, keysz@entry=2, scankey=0xf141b68, scankey@entry=0x387d1e0, nextkey=nextkey@entry=0 '\000', bufP=0x387db8c, bufP@entry=0x387dbec, access=<optimized out>, access@entry=1, snapshot=<optimized out>) at nbtsearch.c:99
#11 0x0000000000467bf4 in _bt_first (scan=<optimized out>, scan@entry=0xf14ef28, dir=<optimized out>, dir@entry=ForwardScanDirection) at nbtsearch.c:983
#12 0x0000000000462f5d in btgettuple (scan=0xf14ef28, dir=ForwardScanDirection) at nbtree.c:321
#13 0x0000000000458d3e in index_getnext_tid (scan=scan@entry=0xf14ef28, direction=direction@entry=ForwardScanDirection) at indexam.c:415
#14 0x000000000045906d in index_getnext (scan=0xf14ef28, direction=direction@entry=ForwardScanDirection) at indexam.c:553
#15 0x0000000000457e42 in systable_getnext (sysscan=sysscan@entry=0xf14eed0) at genam.c:416
#16 0x00000000007ac61f in SearchCatCache (cache=0x159d38, v1=<optimized out>, v2=<optimized out>, v3=v3@entry=0, v4=0, v4@entry=1) at catcache.c:1248
#17 0x00000000007b9ec8 in SearchSysCache (cacheId=cacheId@entry=44, key1=<optimized out>, key2=<optimized out>, key3=key3@entry=0, key4=0, key4@entry=92863616) at syscache.c:982
#18 0x00000000007b9fa5 in GetSysCacheOid (cacheId=cacheId@entry=44, key1=<optimized out>, key2=<optimized out>, key3=key3@entry=0, key4=key4@entry=0) at syscache.c:1060
#19 0x00000000007bc2cb in get_relname_relid (relname=<optimized out>, relnamespace=<optimized out>) at lsyscache.c:1653
#20 0x00000000004bb000 in RangeVarGetRelidExtended (relation=relation@entry=0xf14e300, lockmode=lockmode@entry=1, missing_ok=missing_ok@entry=1 '\001', nowait=nowait@entry=0 '\000', callback=callback@entry=0x0, callback_arg=callback_arg@entry=0x0) at namespace.c:321
#21 0x00000000004432da in relation_openrv_extended (relation=relation@entry=0xf14e300, lockmode=lockmode@entry=1, missing_ok=missing_ok@entry=1 '\001') at heapam.c:1242
#22 0x000000000044352c in heap_openrv_extended (relation=relation@entry=0xf14e300, lockmode=lockmode@entry=1, missing_ok=missing_ok@entry=1 '\001') at heapam.c:1348
#23 0x000000000050eef4 in parserOpenTable (pstate=pstate@entry=0xf14eb88, relation=relation@entry=0xf14e300, lockmode=1) at parse_relation.c:1131
#24 0x00000000005104f2 in addRangeTableEntry (pstate=pstate@entry=0xf14eb88, relation=relation@entry=0xf14e300, alias=0x0, inh=<optimized out>, inFromCl=inFromCl@entry=1 '\001') at parse_relation.c:1196
#25 0x00000000004fa617 in transformTableEntry (r=0xf14e300, pstate=0xf14eb88) at parse_clause.c:439
#26 transformFromClauseItem (pstate=pstate@entry=0xf14eb88, n=0xf14e300, top_rte=0x8, top_rte@entry=0x387e370, top_rti=0xf14dca8, top_rti@entry=0x387e36c, namespace=namespace@entry=0x387e378) at parse_clause.c:872
#27 0x00000000004fbe93 in transformFromClause (pstate=pstate@entry=0xf14eb88, frmList=<optimized out>) at parse_clause.c:130
#28 0x00000000004dd2a9 in transformSelectStmt (stmt=0x0, pstate=0xf14eb88) at analyze.c:1156
#29 transformStmt (pstate=pstate@entry=0xf14eb88, parseTree=0x0, parseTree@entry=0xf14e898) at analyze.c:270
#30 0x00000000004def9a in transformTopLevelStmt (pstate=pstate@entry=0xf14eb88, parseTree=parseTree@entry=0xf14e898) at analyze.c:215
#31 0x00000000004df01c in parse_analyze (parseTree=parseTree@entry=0xf14e898, sourceText=sourceText@entry=0xf14bb20 "SELECT NULL FROM pg_catalog.pg_class WHERE relname = 'plr_modules' AND relnamespace = 2200", paramTypes=paramTypes@entry=0x0, numParams=numParams@entry=0) at analyze.c:108
#32 0x00000000006bd49f in pg_analyze_and_rewrite (parsetree=0xf14e898, query_string=0xf14bb20 "SELECT NULL FROM pg_catalog.pg_class WHERE relname = 'plr_modules' AND relnamespace = 2200", paramTypes=0x0, numParams=0) at postgres.c:658
#33 0x00000000005be25d in _SPI_execute_plan (plan=0x898, plan@entry=0x387e770, paramLI=0x13af18, paramLI@entry=0x0, snapshot=0x387e840, snapshot@entry=0x0, crosscheck_snapshot=0x6538354f <plr_load_modules+82>, crosscheck_snapshot@entry=0x0, ) at spi.c:2077
#34 0x00000000005bea0b in SPI_execute (src=0xf14bb20 "SELECT NULL FROM pg_catalog.pg_class WHERE relname = 'plr_modules' AND relnamespace = 2200", read_only=read_only@entry=0 '\000', tcount=tcount@entry=1) at spi.c:383
#35 0x00000000005bea45 in SPI_exec (src=<optimized out>, tcount=tcount@entry=1) at spi.c:395
#36 0x000000006538354f in haveModulesTable (nspOid=2200) at plr.c:1945
#37 plr_load_modules () at plr.c:500
#38 0x0000000065384148 in plr_init_all (funcid=17045) at plr.c:577
#39 plr_call_handler (fcinfo=0xf145f80) at plr.c:211
#40 0x0000000000598027 in ExecMakeFunctionResult (fcache=fcache@entry=0xf145f10, econtext=econtext@entry=0xf145ce8, isNull=isNull@entry=0xf1468c0 "", isDone=isDone@entry=0xf146a00) at execQual.c:1840
#41 0x0000000000598264 in ExecEvalFunc (fcache=0xf145f10, econtext=0xf145ce8, isNull=0xf1468c0 "", isDone=0xf146a00) at execQual.c:2437
#42 0x000000000059b4ad in ExecTargetList (isDone=0xf145bd0, itemIsDone=0xf146a00, isnull=0xf1468c0 "", values=0x1, econtext=0x0, tupdesc=<optimized out>, targetlist=0xf145ce8) at execQual.c:5486
#43 ExecProject (projInfo=<optimized out>, isDone=0xf145bd0, isDone@entry=0x387ec1c) at execQual.c:5710
#44 0x00000000005b18c1 in ExecResult (node=node@entry=0xf145bd0) at nodeResult.c:155
#45 0x0000000000593ff1 in ExecProcNode (node=node@entry=0xf145bd0) at execProcnode.c:392
#46 0x000000000058ffde in ExecutePlan (dest=0xf143dd8, direction=<optimized out>, numberTuples=0, sendTuples=1 '\001', operation=CMD_SELECT, use_parallel_mode=<optimized out>, planstate=0xf145bd0, estate=0xf145ab8) at execMain.c:1567
#47 standard_ExecutorRun (queryDesc=0x1cd948, direction=<optimized out>, count=0) at execMain.c:338
#48 0x000000000059009a in ExecutorRun (queryDesc=queryDesc@entry=0x1cd948, direction=direction@entry=ForwardScanDirection, count=count@entry=0) at execMain.c:286
#49 0x00000000006c0a03 in PortalRunSelect (portal=0x1dc348, portal@entry=0xf143dd8, forward=forward@entry=1 '\001', count=0, count@entry=252984792, dest=dest@entry=0xe2ba8) at pquery.c:948
#50 0x00000000006c20f9 in PortalRun (portal=0xf143dd8, portal@entry=0x1dc348, count=252984792, count@entry=2147483647, isTopLevel=isTopLevel@entry=0 '\000', dest=0xe2ba8, dest@entry=0xf143dd8, altdest=<optimized out>, altdest@entry=0xf143dd8, completionTag=0x387f180 "", completionTag@entry=0x387f1b8 "¸ãi") at pquery.c:789
#51 0x00000000006be813 in exec_simple_query (query_string=0x7cd03f <string_compare+17> "HƒÄ ]ÃUH‰åHƒì H‰ÊH‹\réT*") at postgres.c:1094
#52 PostgresMain (argc=<optimized out>, argv=argv@entry=0x1344d8, dbname=<optimized out>, username=<optimized out>) at postgres.c:4070
#53 0x0000000000653d3a in BackendRun (port=0x387fc30) at postmaster.c:4260
#54 SubPostmasterMain (argc=argc@entry=3, argv=argv@entry=0x10ffa0) at postmaster.c:4750
#55 0x00000000005cf6b8 in main (argc=3, argv=0x10ffa0) at main.c:205

#0  PathNameOpenFile
... DO NOT CONTINTUE ... NEXT ... NEXT ... STEP INTO INTO BasicOpenFile

> bt
#0  BasicOpenFile (fileName=fileName@entry=0xf14f040 "base/12418/2663", fileFlags=fileFlags@entry=32770, fileMode=fileMode@entry=384) at fd.c:902
#1  0x0000000000690b2b in PathNameOpenFile (fileName=fileName@entry=0xf14f040 "base/12418/2663", fileFlags=fileFlags@entry=32770, fileMode=fileMode@entry=384) at fd.c:1252

Here, in BasicOpenFile, it tries to open the file twice. At the end of the 2nd failure, a -1 is returned.

int
BasicOpenFile(FileName fileName, int fileFlags, int fileMode)
{
    int         fd;

tryAgain:
    fd = open(fileName, fileFlags, fileMode);          *** NET: WAS TRIED TWICE ***

    if (fd >= 0)
        return fd;              /* success! */

    if (errno == EMFILE || errno == ENFILE)            *** 1ST ROUND IS TRUE *** *** 2ND ROUND IS FALSE ***
    {
        int         save_errno = errno;

        ereport(LOG,                                              *** 1ST ROUND EXECUTED ONCE ***
                (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
                 errmsg("out of file descriptors: %m; release and retry")));
        errno = 0;
        if (ReleaseLruFile())
            goto tryAgain;   *** END OF FIRST ROUND ...JUMP TO 'tryAgain:' ***
        errno = save_errno;
    }

    return -1;                  /* failure */    *** 2ND ROUND FAIL AND RETURN -1 ***
}

EMFILE is slightly explaned here

WTF is EMFILE and why does it happen to me
http://blog.izs.me/post/56827866110/wtf-is-emfile-and-why-does-it-happen-to-me

The comment is read.

On OS X, the default is 256, which is pretty low for many modern programs 
that do a lot of file system writing and reading.

Linux is explained here.

where are the default ulimit values set? (linux, centos)
http://serverfault.com/questions/356962/where-are-the-default-ulimit-values-set-linux-centos

It seem here that in 2013 Linux had over 1000 file descriptors

For example, the kernel default for maximum number of 
file descriptors (ulimit -n) was 1024/1024 (soft, hard), 
and has been raised to 1024/4096 in Linux 2.6.39.

I am on Windows, so what number do I have?

TargetUser@TARGETMACH /c/Users/Public
$ ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited
open files                      (-n) 256
pipe size            (512 bytes, -p) 8
stack size              (kbytes, -s) 2046
cpu time               (seconds, -t) unlimited
max user processes              (-u) 63
virtual memory          (kbytes, -v) 2097152

So,

http://ss64.com/bash/ulimit.html

syntax
      ulimit [-acdfHlmnpsStuv] [limit]

Here is mine.

TargetUser@TARGETMACH /c/Users/Public

$ ulimit --help
sh: ulimit: --: invalid option
ulimit: usage: ulimit [-SHacdfilmnpqstuvx] [limit]

Can I use ulimit?

This guy here says that on Windows using unlimit to try to 'set' will not work.

Resize stack - ulimit - mingw-msys@lists.sourceforge.net
http://mingw-msys.narkive.com/Z2J7KlRK/resize-stack-ulimit

He ( 14 years ago: 2004? ) says do this.

On Windows the stack size is stored in the executable's header so when you
link the program you will need to pass a flag such as "-Wl,--stack,4000000".

This other guy writes this in 2015.

How to increase the gcc executable stack size?
http://stackoverflow.com/questions/1156783/how-to-increase-the-gcc-executable-stack-size

Specifically, he says.

I use that in my compiler script:

CFLAGS += -Wl,--stack,10485760

Hopefully, this is something to try.

I am tired. I am going to bed again.

davecramer commented 7 years ago

Andre,

Good work!

0 PathNameOpenFile

... DO NOT CONTINTUE ... NEXT ... NEXT ... STEP INTO INTO BasicOpenFile

bt

0 BasicOpenFile (fileName=fileName@entry=0xf14f040 "base/12418/2663", fileFlags=fileFlags@entry=32770, fileMode=fileMode@entry=384) at fd.c:902

1 0x0000000000690b2b in PathNameOpenFile (fileName=fileName@entry=0xf14f040 "base/12418/2663", fileFlags=fileFlags@entry=32770, fileMode=fileMode@entry=384) at fd.c:1252

Here, in BasicOpenFile, it tries to open the file twice. At the end of the 2nd failure, a -1 is returned.

intBasicOpenFile(FileName fileName, int fileFlags, int fileMode) { int fd;

tryAgain: fd = open(fileName, fileFlags, fileMode); * NET: WAS TRIED TWICE *

if (fd >= 0)
    return fd;              /* success! */

if (errno == EMFILE || errno == ENFILE)            *** 1ST ROUND IS TRUE *** *** 2ND ROUND IS FALSE ***
{
    int         save_errno = errno;

    ereport(LOG,                                              *** 1ST ROUND EXECUTED ONCE ***
            (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
             errmsg("out of file descriptors: %m; release and retry")));
    errno = 0;
    if (ReleaseLruFile())
        goto tryAgain;   *** END OF FIRST ROUND ...JUMP TO 'tryAgain:' ***
    errno = save_errno;
}

return -1;                  /* failure */    *** 2ND ROUND FAIL AND RETURN -1 ***

}

EMFILE is slightly explaned here

WTF is EMFILE and why does it happen to mehttp://blog.izs.me/post/56827866110/wtf-is-emfile-and-why-does-it-happen-to-me

The comment is read.

On OS X, the default is 256, which is pretty low for many modern programs that do a lot of file system writing and reading.

Linux is explained here.

where are the default ulimit values set? (linux, centos)http://serverfault.com/questions/356962/where-are-the-default-ulimit-values-set-linux-centos

It seem here that in 2013 Linux had over 1000 file descriptors

In linux it can be set much higher.

For example, the kernel default for maximum number of file descriptors (ulimit -n) was 1024/1024 (soft, hard), and has been raised to 1024/4096 in Linux 2.6.39.

I am on Windows, so what number do I have?

TargetUser@TARGETMACH /c/Users/Public $ ulimit -a core file size (blocks, -c) unlimited data seg size (kbytes, -d) unlimited file size (blocks, -f) unlimited open files (-n) 256 pipe size (512 bytes, -p) 8 stack size (kbytes, -s) 2046 cpu time (seconds, -t) unlimited max user processes (-u) 63 virtual memory (kbytes, -v) 2097152

So,

http://ss64.com/bash/ulimit.html

syntax ulimit [-acdfHlmnpsStuv] [limit]

Here is mine.

TargetUser@TARGETMACH /c/Users/Public

$ ulimit --help sh: ulimit: --: invalid optionulimit: usage: ulimit [-SHacdfilmnpqstuvx] [limit]

Can I use ulimit?

This guy here says that on Windows using unlimit to try to 'set' will not work.

This has nothing to do with the stack.

http://stackoverflow.com/questions/35690916/windows-git-bash-ulimit-too-many-open-files

Suggests that ulimit can be used on Windows.

Dave

AndreMikulec commented 7 years ago

The science(so far) behind the problem is the following.

When postgreSQL (on windows) boots up ( and tries to open files) it generates many file open misses.

For example,

C:\MinGW\msys\1.0\local\pgsql_0ab9c56_debug>"%PGSQL%\bin\postgres" -d 5 -p 5434
DEBUG:  postgres: PostmasterMain: initial environment dump:
DEBUG:  -----------------------------------------

...

DEBUG:  mapped win32 error code 2 to 2
DEBUG:  mapped win32 error code 2 to 2
LOG:  database system is ready to accept connections
DEBUG:  mapped win32 error code 2 to 2
DEBUG:  mapped win32 error code 2 to 2
DEBUG:  mapped win32 error code 2 to 2
..

Maybe 50 or so of them are generated.

These is DOS error 2

DOS Error Codes 02 File not found ( http://stanislavs.org/helppc/dos_error_codes.html )

This error is also know as

ERROR_FILE_NOT_FOUND 2 (0x2) The system cannot find the file specified. ( https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx )

These are called in the function in here.

src/port/win32error.c

void _dosmaperr(unsigned long e)

Execution goes through this line.

mapped win32 error code %lu to %d"

So these errors are printed as

DEBUG:  mapped win32 error code 2 to 2

These have been results of the Window function CreateFile()

Also, create extension generates a file open misses (error 2) whenever, I try to create extension plr or 'create extension something else'

C:/MinGW/msys/1.0/local/pgsql_0ab9c56_debug/share/extension/plr--8.3.0.16.control
C:/MinGW/msys/1.0/local/pgsql_0ab9c56_debug/share/extension/cube--1.2.control

This happens on every contrib extension, I have tried (so this seems normal? on windows? )

Later, just after I type "select r_version(); PostgreSQL ( Windows function CreateFile ) tries to open "base/12418/2663".

The function returns INVALID_HANDLE_VALUE Then GetLastError() returns ERROR_PATH_NOT_FOUND

ERROR_PATH_NOT_FOUND
3 (0x3)
The system cannot find the path specified.
( https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx )

So, the output string is different. It says

DEBUG:  mapped win32 error code 3 to 2

I have tried two hacking attempts at a solution. Neither of them worked.

Try Number 1

I tried to eliminate any kind of OS buffering.

So, I changes this

C:\postgres-master_0ab9c56_debug\src\port open.c

pgwin32_open(const char *fileName, int fileFlags,...)

                           FILE_ATTRIBUTE_NORMAL |
                     ((fileFlags & O_RANDOM) ? FILE_FLAG_RANDOM_ACCESS : 0) |
               ((fileFlags & O_SEQUENTIAL) ? FILE_FLAG_SEQUENTIAL_SCAN : 0) |
              ((fileFlags & _O_SHORT_LIVED) ? FILE_ATTRIBUTE_TEMPORARY : 0) |
                ((fileFlags & O_TEMPORARY) ? FILE_FLAG_DELETE_ON_CLOSE : 0) |
                      ((fileFlags & O_DIRECT) ? FILE_FLAG_NO_BUFFERING : 0) |
                       ((fileFlags & O_DSYNC) ? FILE_FLAG_WRITE_THROUGH : 0),
                           NULL)) == INVALID_HANDLE_VALUE)

to this


                           FILE_ATTRIBUTE_NORMAL |
                     ((fileFlags & O_RANDOM) ? FILE_FLAG_RANDOM_ACCESS  | FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING :  0 ) |
               ((fileFlags & O_SEQUENTIAL) ? FILE_FLAG_SEQUENTIAL_SCAN | FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING : 0 ) |
              ((fileFlags & _O_SHORT_LIVED) ? FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING : 0 ) |
                ((fileFlags & O_TEMPORARY) ? FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING : 0 ) |
                      ((fileFlags & O_DIRECT) ? FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING : 0 ) |
                       ((fileFlags & O_DSYNC) ? FILE_FLAG_WRITE_THROUGH | FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING : 0 ),
                           NULL)) == INVALID_HANDLE_VALUE)

This ended up having no effect.

Try Number 2

I tried to have CreateFile 'try more often.'

So, I replaced

src/backend/storage/file fd.c

BasicOpenFile(FileName fileName, int fileFlags, int fileMode

int
BasicOpenFile(FileName fileName, int fileFlags, int fileMode)
{
    int         fd;

tryAgain:
    fd = open(fileName, fileFlags, fileMode);

    if (fd >= 0)
        return fd;              /* success! */

    if (errno == EMFILE || errno == ENFILE)
    {
        int         save_errno = errno;

        ereport(LOG,
                (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
                 errmsg("out of file descriptors: %m; release and retry")));
        errno = 0;
        if (ReleaseLruFile())
            goto tryAgain;
        errno = save_errno;
    }

    return -1;                  /* failure */
}

with this code here

Below, ENOENT and ENOENT mean 'path not found.'

BasicOpenFile(FileName fileName, int fileFlags, int fileMode)
{
    int         fd;

tryAgain:
    fd = open(fileName, fileFlags, fileMode);

    if (fd >= 0)
        return fd;              /* success! */

    if (errno == EMFILE || errno == ENFILE )
    {
        int         save_errno = errno;

        ereport(LOG,
                (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
                 errmsg("out of file descriptors: %m; release and retry")));
        errno = 0;
        if (ReleaseLruFile())
            goto tryAgain;
        errno = save_errno;
    }

    if (errno == ENOENT || errno == ENOENT )
    {
        int         save_errno = errno;

        ereport(LOG,
                (errno,
                 errmsg("BasicOpenFile errno ==ENOENT || ENOENT: %m")));
        errno = 0;
        if (ReleaseLruFile())
            goto tryAgain;
        errno = save_errno;
    }

    return -1;                  /* failure */
}

That did not help either. Tt only made everything run 'super super slow.'

Currently, I have not found a 'direct' solution

Since, the error occurred just after submitting the SQL that queries ( by SQL ) for modules, I eliminated the 'plr module' system'

src/contrib/plr plr.c

I replaced this ...

static bool
haveModulesTable(Oid nspOid)
{
    StringInfo      sql = makeStringInfo();
    char           *sql_format = "SELECT NULL "
                                 "FROM pg_catalog.pg_class "
                                 "WHERE "
                                 "relname = 'plr_modules' AND "
                                 "relnamespace = %u";
    int  spiRc;

    appendStringInfo(sql, sql_format, nspOid);

    spiRc = SPI_exec(sql->data, 1);
    if (spiRc != SPI_OK_SELECT)

        elog(ERROR, "haveModulesTable: select from pg_class failed");

    return SPI_processed == 1;
}

with this ...

static bool
haveModulesTable(Oid nspOid)
{

  SPI_processed = 0;

    return SPI_processed == 1;
}

So, haveModulesTable always returns FALSE and 'modules related SQL' is elminated. So, this now this works

postgres=# select r_version();
                    r_version
-------------------------------------------------
 (platform,x86_64-w64-mingw32)
 (arch,x86_64)
 (os,mingw32)
 (system,"x86_64, mingw32")
 (status,"")
 (major,3)
 (minor,3.1)
 (year,2016)
 (month,06)
 (day,21)
 ("svn rev",70800)
 (language,R)
 (version.string,"R version 3.3.1 (2016-06-21)")
 (nickname,"Bug in Your Hair")
(14 rows)

However, this does not free me completely from those errors.

postgres=# drop extension plr;
ERROR:  could not open file "base/12418/2673": No such file or directory
postgres=#

postgres=#  drop extension plr;
ERROR:  cannot drop extension plr because other objects depend on it
DETAIL:  function r_max(integer,integer) depends on language plr
HINT:  Use DROP ... CASCADE to drop the dependent objects too.
postgres=#  drop extension plr cascade;
NOTICE:  drop cascades to function r_max(integer,integer)
ERROR:  could not open file "global/1232": No such file or directory

Exiting out of psql and re-opening a new psql and re-trying again ( drop extendion plr [cascade] ) usually works.

My question is the following.

What does MemoryContextSwitchTo do?

I found these

plr_caller_context = CurrentMemoryContext;
MemoryContextSwitchTo(plr_caller_context);

Also I found these.

SWITCHTO_PLR_SPI_CONTEXT(oldcontext)
CLEANUP_PLR_SPI_CONTEXT(oldcontext);

Those last macros are defined in plr.h

#define SWITCHTO_PLR_SPI_CONTEXT(the_caller_context) \
    the_caller_context = MemoryContextSwitchTo(plr_SPI_context)
#define CLEANUP_PLR_SPI_CONTEXT(the_caller_context) \
    MemoryContextSwitchTo(the_caller_context)

MemoryContextSwitchTo is here src/include/utils/palloc.h

MemoryContextSwitchTo(MemoryContext context)
{
    MemoryContext old = CurrentMemoryContext;

    CurrentMemoryContext = context;
    return old;
}

MemoryContextData is here src/include/nodes/memnodes.h

/*
 * Type MemoryContextData is declared in nodes/memnodes.h.  Most users
 * of memory allocation should just treat it as an abstract type, so we
 * do not provide the struct contents here.
 */
typedef struct MemoryContextData *MemoryContext;
/*

typedef struct MemoryContextData
{
    NodeTag     type;           /* identifies exact kind of context */
    /* these two fields are placed here to minimize alignment wastage: */
    bool        isReset;        /* T = no space alloced since last reset */
    bool        allowInCritSection;     /* allow palloc in critical section */
    MemoryContextMethods *methods;      /* virtual function table */
    MemoryContext parent;       /* NULL if no parent (toplevel context) */
    MemoryContext firstchild;   /* head of linked list of children */
    MemoryContext prevchild;    /* previous child of same parent */
    MemoryContext nextchild;    /* next child of same parent */
    char       *name;           /* context name (just for debugging) */
    MemoryContextCallback *reset_cbs;   /* list of reset/delete callbacks */
} MemoryContextData;

When a memory context is turned 'on' what is happening? Why does PostgreSQL need them? Can I live without them?

Thanks, Andre Mikulec Andre_Mikulec@Hotmail.com

davecramer commented 7 years ago

My question is the following.

What does MemoryContextSwitchTo do?

I found these

plr_caller_context = CurrentMemoryContext;MemoryContextSwitchTo(plr_caller_context);

Also I found these.

SWITCHTO_PLR_SPI_CONTEXT(oldcontext) CLEANUP_PLR_SPI_CONTEXT(oldcontext);

Those last macros are defined in plr.h

define SWITCHTO_PLR_SPI_CONTEXT(the_caller_context) \

the_caller_context = MemoryContextSwitchTo(plr_SPI_context)

define CLEANUP_PLR_SPI_CONTEXT(the_caller_context) \

MemoryContextSwitchTo(the_caller_context)

MemoryContextSwitchTo is here src/include/utils/palloc.h

MemoryContextSwitchTo(MemoryContext context) { MemoryContext old = CurrentMemoryContext;

CurrentMemoryContext = context;
return old;

}

MemoryContextData is here src/include/nodes/memnodes.h

/* * Type MemoryContextData is declared in nodes/memnodes.h. Most users * of memory allocation should just treat it as an abstract type, so we * do not provide the struct contents here. _/typedef struct MemoryContextData MemoryContext;/typedef struct MemoryContextData{ NodeTag type; /* identifies exact kind of context / / these two fields are placed here to minimize alignment wastage: / bool isReset; / T = no space alloced since last reset / bool allowInCritSection; / allow palloc in critical section / MemoryContextMethods methods; / virtual function table / MemoryContext parent; / NULL if no parent (toplevel context) / MemoryContext firstchild; / head of linked list of children / MemoryContext prevchild; / previous child of same parent / MemoryContext nextchild; / next child of same parent / char name; / context name (just for debugging) / MemoryContextCallback resetcbs; / list of reset/delete callbacks */ } MemoryContextData;

When a memory context is turned 'on' what is happening?

PostgreSQL has code to manage it's own memory it's like a poor man's garbage collection.

All memory you allocate in a context can be freed by freeing the context.

Why does PostgreSQL need them? Can I live without them?

No you can't.

So the question you have to be asking yourself here is why does this only happen to you?

I cannot replicate this problem, nor does anyone else have the problem. What other software do you have running?

If you take a bare machine, and install windows, R, Postgres does this happen ?

Dave Cramer

davec@postgresintl.com www.postgresintl.com

AndreMikulec commented 7 years ago

I found another bare machine. I will try the original pl/r ( without my hack ) on that machine. I will post back and explain what happens.

AndreMikulec commented 7 years ago

Dave Cramer,

I now again have a working PostgreSQL 9.6.1 pl/r 08.03.00.17 ( https://github.com/postgres-plr/plr ) to R 3.3.2 on my old 11 years old Windows 7 computer. This pl/r is without ( unlike before ) any modifications (omissisions of code) to/from plr.c

I did only the following.

Used 9.6.1 code exactly.

Compiled/made pl/r in in an external directory that is different form the source. (I did not do this before.)

Specifically, in this external directory, I did the following

  1. Copied the(my simplified) pl/r Makefile into there
  2. Built plr (make -C contrib/plr)

I also built non-debug everything.

Specifically, I ran ./config (non-debug) and 'make' on the PostgreSQL source code and pl/r code.

What is different from before, is that that I can no longer run pl/r without putting the R executable in the PATH as seen by PostgreSQL.

Before in Postgre 10devel of early September and PostgreSQL 9.5.x I could just attempt ( and fail ) to run pl/r by just setting the R_HOME directory and that was fine.

I do not know if this 'workingness' will keep up. But, for this 'pl/r' back to' working' is good.

postgres=# create extension plr;

CREATE EXTENSION

postgres=# select r_version();

                    r_version

-------------------------------------------------

 (platform,x86_64-w64-mingw32)

 (arch,x86_64)

 (os,mingw32)

 (system,"x86_64, mingw32")

 (status,"")

 (major,3)

 (minor,3.2)

 (year,2016)

 (month,10)

 (day,31)

 ("svn rev",71607)

 (language,R)

 (version.string,"R version 3.3.2 (2016-10-31)")

 (nickname,"Sincere Pumpkin Patch")

(14 rows)
postgres=# select plr_version();

 plr_version

-------------

 08.03.00.17  # https://github.com/postgres-plr/plr 

(1 row)
postgres=# select reload_plr_modules();

 reload_plr_modules

--------------------

 OK

(1 row)
postgres=# select version();

                                                                                     version

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 PostgreSQL 9.6.1_CFLAGS_default_REL9_6_1_0ab9c56 on x86_64-w64-mingw32, compiled by x86_64-w64-mingw32-gcc.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 6.2.0, 64-bit

(1 row)
AndreMikulec commented 7 years ago

Dave and Joe,

From my 100+ hours of hard trial and error, I have came to this following conclusion. The case seem that sometimes a valid path exists, but Windows 'sometimes' forgets how to read the valid path. This problem seem to be isolated at 64bit Windows operating systems. 32bit Windows operating systems do not seem to have this problem.

This does not make any sense but this a fact. For some strange bad luck, the unique way that plr is written ( but written well! ) with both C and SQL seems to irritate 64bit Windows leading it to 'forgetting how to read a valid path.'

When I run into this problem, to make a solution, this is what I do.

I copy all of my PostgreSQL files to

C:\9.6

I copy all of my R files to

C:\R-3.3._

I change my PostgreSQL startup script PostgreSQL.9.6._.bat. Specifically,I change my R location to

set     R_HOME=C:\R-3.3._\App\R-Portable
set       PATH=C:\R-3.3._\App\R-Portable\bin\x64;%PATH%

After than, I startup PostgreSQL. Everything works wonderful.

C:\9.6>chcp 1252   1>nul  && "C:\9.6\bin\psql.exe"

psql (9.6.1_CFLAGS_default_REL9_6_1_0ab9c56)
Type "help" for help.

postgres=# \timing
Timing is on.
postgres=# create extension plr;
CREATE EXTENSION
Time: 124.366 ms
postgres=# select r_version();
                    r_version
-------------------------------------------------
 (platform,x86_64-w64-mingw32)
 (arch,x86_64)
 (os,mingw32)
 (system,"x86_64, mingw32")
 (status,"")
 (major,3)
 (minor,3.2)
 (year,2016)
 (month,10)
 (day,31)
 ("svn rev",71607)
 (language,R)
 (version.string,"R version 3.3.2 (2016-10-31)")
 (nickname,"Sincere Pumpkin Patch")
(14 rows)

Time: 206.388 ms
postgres=#

I have done this solution three times so far( once on 9.5 and twice on 9.6).

So, in a nutshell, sometimes 64bit Windows forgets how to read a valid path. So, when this happens, I just 'try another path, that is a 'very short' and a 'very simple' path. I do this for both PostgreSQL and R ( but I think that the R path is the one that really needs be 'short and simple')

Example.

C:\9.6
C:\R-3.3._

The end. Please, close this ticket.

davecramer commented 7 years ago

Hi Andre,

You don't set the variables in the system environment ?

AndreMikulec commented 7 years ago

Dave,

I do set my system variables in a 'startup' .bat script similar to ( and originally based on ) how garethflowers ( Gareth Flowers https://sourceforge.net/u/garethflowers/profile/ http://gareth.flowers) does it in his PostgreSQL Portable [for Windows]

PostgreSQL Portable - Portable version of the PostgreSQL Database Server

https://sourceforge.net/projects/postgresqlportable/

AndreMikulec commented 7 years ago

Update: The new pattern seems that I can only get 64-bit pl/r to work on Windows (10) computers that are running using an i7 processor. I have two machines that match this pattern. If any other processor ( i.e./e.g. i5 processor in my 3rd computer of the four) ) is tried than pl/r returns that old error.

postgres=# select r_max(5,3);
ERROR:  could not open file "base/12373/2663": No such file or directory
LINE 1: SELECT NULL FROM pg_catalog.pg_class WHERE relname 'plr_modules' AND relnamespace = 2200