Re: [HACKERS] broken documentation: BackgroundWorkerInitializeConnection(NULL, NULL);

2015-06-25 Thread Robert Haas
On Thu, May 21, 2015 at 11:47 PM, Alvaro Herrera
alvhe...@2ndquadrant.com wrote:
 Robert Haas wrote:
 On Fri, May 15, 2015 at 4:15 PM, Alvaro Herrera
 alvhe...@2ndquadrant.com wrote:
  Really?  I was thinking of the test code as throwaway.  I just wanted
  to fix the bug.
 
  Oh, that's fine then.  I thought you wanted to push it.

 Nah, sorry, I shoulda been more clear about that.  That was just so I
 could actually be sure I had the fix right.

 I think you forgot to push this one ...

Yeah, you're right.  Done now after two other people reminded me of
the same thing.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] broken documentation: BackgroundWorkerInitializeConnection(NULL, NULL);

2015-05-15 Thread Robert Haas
On Thu, May 14, 2015 at 8:25 AM, Pavel Stehule pavel.steh...@gmail.com wrote:
 The documentation (or this feature) is broken still

 If dbname is NULL or dboid is InvalidOid, the session is not connected to
 any particular database, but shared catalogs can be accessed. If username is
 NULL or useroid is InvalidOid, the process will run as the superuser created
 during initdb. A background worker can only call one of these two functions,
 and only once. It is not possible to switch databases.

 But it fails with error:

 FATAL:  database 0 does not exist

Ugh.  I think that's a bug.

Patch attached.

The test code I used to verify that this works is also attached.

If there are no objections, I will commit and back-patch.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] broken documentation: BackgroundWorkerInitializeConnection(NULL, NULL);

2015-05-15 Thread Robert Haas
On Fri, May 15, 2015 at 4:07 PM, Alvaro Herrera
alvhe...@2ndquadrant.com wrote:
 Robert Haas wrote:
 On Fri, May 15, 2015 at 3:53 PM, Robert Haas robertmh...@gmail.com wrote:

  The test code I used to verify that this works is also attached.
 
  If there are no objections, I will commit and back-patch.

 Oops.  Really attached this time.

 We have spi_worker in src/test/modules now -- I think it makes sense to
 add this one there too in master.

Really?  I was thinking of the test code as throwaway.  I just wanted
to fix the bug.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] broken documentation: BackgroundWorkerInitializeConnection(NULL, NULL);

2015-05-15 Thread Robert Haas
On Fri, May 15, 2015 at 4:15 PM, Alvaro Herrera
alvhe...@2ndquadrant.com wrote:
 Really?  I was thinking of the test code as throwaway.  I just wanted
 to fix the bug.

 Oh, that's fine then.  I thought you wanted to push it.

Nah, sorry, I shoulda been more clear about that.  That was just so I
could actually be sure I had the fix right.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] broken documentation: BackgroundWorkerInitializeConnection(NULL, NULL);

2015-05-15 Thread Robert Haas
On Fri, May 15, 2015 at 3:53 PM, Robert Haas robertmh...@gmail.com wrote:
 On Thu, May 14, 2015 at 8:25 AM, Pavel Stehule pavel.steh...@gmail.com 
 wrote:
 The documentation (or this feature) is broken still

 If dbname is NULL or dboid is InvalidOid, the session is not connected to
 any particular database, but shared catalogs can be accessed. If username is
 NULL or useroid is InvalidOid, the process will run as the superuser created
 during initdb. A background worker can only call one of these two functions,
 and only once. It is not possible to switch databases.

 But it fails with error:

 FATAL:  database 0 does not exist

 Ugh.  I think that's a bug.

 Patch attached.

 The test code I used to verify that this works is also attached.

 If there are no objections, I will commit and back-patch.

Oops.  Really attached this time.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c
index debadf0..28a4966 100644
--- a/src/backend/utils/init/postinit.c
+++ b/src/backend/utils/init/postinit.c
@@ -827,7 +827,7 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
 		/* take database name from the caller, just for paranoia */
 		strlcpy(dbname, in_dbname, sizeof(dbname));
 	}
-	else
+	else if (OidIsValid(dboid))
 	{
 		/* caller specified database by OID */
 		HeapTuple	tuple;
@@ -847,6 +847,18 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
 		if (out_dbname)
 			strcpy(out_dbname, dbname);
 	}
+	else
+	{
+		/*
+		 * If this is a background worker not bound to any particular
+		 * database, we're done now.  Everything that follows only makes
+		 * sense if we are bound to a specific database.  We do need to
+		 * close the transaction we started before returning.
+		 */
+		if (!bootstrap)
+			CommitTransactionCommand();
+		return;
+	}
 
 	/* Now we can mark our PGPROC entry with the database ID */
 	/* (We assume this is an atomic store so no lock is needed) */
diff --git a/contrib/no_db_worker/Makefile b/contrib/no_db_worker/Makefile
new file mode 100644
index 000..2085c95
--- /dev/null
+++ b/contrib/no_db_worker/Makefile
@@ -0,0 +1,18 @@
+# contrib/no_db_worker
+
+MODULES = no_db_worker
+
+EXTENSION = no_db_worker
+DATA = no_db_worker--1.0.sql
+PGFILEDESC = no_db_worker - background worker without database
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+subdir = contrib/no_db_worker
+top_builddir = ../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
diff --git a/contrib/no_db_worker/no_db_worker--1.0.sql b/contrib/no_db_worker/no_db_worker--1.0.sql
new file mode 100644
index 000..a38ec63
--- /dev/null
+++ b/contrib/no_db_worker/no_db_worker--1.0.sql
@@ -0,0 +1,7 @@
+/* contrib/no_db_worker/no_db_worker--1.0.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use CREATE EXTENSION no_db_worker to load this file. \quit
+
+CREATE FUNCTION no_db_worker_launch() RETURNS pg_catalog.int4 STRICT
+AS 'MODULE_PATHNAME' LANGUAGE C;
diff --git a/contrib/no_db_worker/no_db_worker.c b/contrib/no_db_worker/no_db_worker.c
new file mode 100644
index 000..2a09bc4
--- /dev/null
+++ b/contrib/no_db_worker/no_db_worker.c
@@ -0,0 +1,103 @@
+/* -
+ *
+ * no_db_worker.c
+ *		A database worker that does not connect to any particular database.
+ *
+ * Copyright (C) 2015, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *		contrib/no_db_worker/no_db_worker.c
+ *
+ * -
+ */
+#include postgres.h
+
+#include access/relscan.h
+#include access/xact.h
+#include catalog/pg_database.h
+#include fmgr.h
+#include miscadmin.h
+#include postmaster/bgworker.h
+#include storage/ipc.h
+#include utils/rel.h
+#include utils/snapmgr.h
+
+PG_MODULE_MAGIC;
+
+PG_FUNCTION_INFO_V1(no_db_worker_launch);
+
+extern void no_db_worker_main(Datum main_arg);
+
+void
+no_db_worker_main(Datum main_arg)
+{
+	Relation	rel;
+	HeapScanDesc scan;
+	HeapTuple	tup;
+
+	BackgroundWorkerInitializeConnection(NULL, NULL);
+
+	StartTransactionCommand();
+	(void) GetTransactionSnapshot();
+
+	rel = heap_open(DatabaseRelationId, AccessShareLock);
+	scan = heap_beginscan_catalog(rel, 0, NULL);
+
+	while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection)))
+	{
+		Form_pg_database pgdatabase = (Form_pg_database) GETSTRUCT(tup);
+
+		elog(LOG, found database with OID %u and name \%s\,
+			HeapTupleGetOid(tup), NameStr(pgdatabase-datname));
+	}
+
+	elog(LOG, done scanning pg_database);
+
+	heap_endscan(scan);
+	heap_close(rel, AccessShareLock);
+
+	proc_exit(1);
+}
+
+/*
+ * Dynamically launch an SPI worker.
+ */
+Datum
+no_db_worker_launch(PG_FUNCTION_ARGS)
+{
+	BackgroundWorker worker;
+	

Re: [HACKERS] broken documentation: BackgroundWorkerInitializeConnection(NULL, NULL);

2015-05-15 Thread Alvaro Herrera
Robert Haas wrote:
 On Fri, May 15, 2015 at 3:53 PM, Robert Haas robertmh...@gmail.com wrote:

  The test code I used to verify that this works is also attached.
 
  If there are no objections, I will commit and back-patch.
 
 Oops.  Really attached this time.

We have spi_worker in src/test/modules now -- I think it makes sense to
add this one there too in master.

-- 
Álvaro Herrerahttp://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training  Services


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] broken documentation: BackgroundWorkerInitializeConnection(NULL, NULL);

2015-05-15 Thread Alvaro Herrera
Robert Haas wrote:
 On Fri, May 15, 2015 at 4:07 PM, Alvaro Herrera
 alvhe...@2ndquadrant.com wrote:
  Robert Haas wrote:
  On Fri, May 15, 2015 at 3:53 PM, Robert Haas robertmh...@gmail.com wrote:
 
   The test code I used to verify that this works is also attached.
  
   If there are no objections, I will commit and back-patch.
 
  Oops.  Really attached this time.
 
  We have spi_worker in src/test/modules now -- I think it makes sense to
  add this one there too in master.
 
 Really?  I was thinking of the test code as throwaway.  I just wanted
 to fix the bug.

Oh, that's fine then.  I thought you wanted to push it.

-- 
Álvaro Herrerahttp://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training  Services


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


[HACKERS] broken documentation: BackgroundWorkerInitializeConnection(NULL, NULL);

2015-05-14 Thread Pavel Stehule
Hi

The documentation (or this feature) is broken still

If dbname is NULL or dboid is InvalidOid, the session is not connected to
any particular database, but shared catalogs can be accessed. If username
is NULL or useroid is InvalidOid, the process will run as the superuser
created during initdb. A background worker can only call one of these two
functions, and only once. It is not possible to switch databases.

But it fails with error:

FATAL:  database 0 does not exist

I found some older thread related to this topic

http://www.postgresql.org/message-id/13994.1376145...@sss.pgh.pa.us

Regards

Pavel