lock.patch

text/plain

Filename: lock.patch
Type: text/plain
Part: 0
Message: disallow LOCK on a view

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: context
File+
command.c 36 29
*** command.c.orig	Tue Aug 29 10:39:42 2000
--- command.c	Tue Aug 29 10:56:21 2000
***************
*** 54,59 ****
--- 54,62 ----
  
  
  static bool needs_toast_table(Relation rel);
+ static bool is_view(char *relname, char *command);
+ 
+ 
  
  
  /* --------------------------------
***************
*** 1086,1094 ****
  AlterTableAddConstraint(char *relationName,
  						bool inh, Node *newConstraint)
  {
- 	char rulequery[41+NAMEDATALEN]; 
- 	void *qplan;
- 	char nulls[1]="";
  
  	if (newConstraint == NULL)
  		elog(ERROR, "ALTER TABLE / ADD CONSTRAINT passed invalid constraint.");
--- 1089,1094 ----
***************
*** 1099,1117 ****
  #endif
  
  	/* check to see if the table to be constrained is a view. */
! 	sprintf(rulequery, "select * from pg_views where viewname='%s'", relationName);
! 	if (SPI_connect()!=SPI_OK_CONNECT)
! 		elog(ERROR, "ALTER TABLE: Unable to determine if %s is a view - SPI_connect failure..", relationName);
!         qplan=SPI_prepare(rulequery, 0, NULL);
! 	if (!qplan)
! 		elog(ERROR, "ALTER TABLE: Unable to determine if %s is a view - SPI_prepare failure.", relationName);
! 	qplan=SPI_saveplan(qplan);
! 	if (SPI_execp(qplan, NULL, nulls, 1)!=SPI_OK_SELECT) 
! 		elog(ERROR, "ALTER TABLE: Unable to determine if %s is a view - SPI_execp failure.", relationName);
!         if (SPI_processed != 0)
!                 elog(ERROR, "ALTER TABLE: Cannot add constraints to views.");
!         if (SPI_finish() != SPI_OK_FINISH)
!                 elog(NOTICE, "SPI_finish() failed in ALTER TABLE");
  		
  	switch (nodeTag(newConstraint))
  	{
--- 1099,1106 ----
  #endif
  
  	/* check to see if the table to be constrained is a view. */
! 	if (is_view(relationName, "ALTER TABLE"))
! 		 elog(ERROR, "ALTER TABLE: Cannot add constraints to views.");
  		
  	switch (nodeTag(newConstraint))
  	{
***************
*** 1254,1272 ****
  				}
  
  				/* check to see if the referenced table is a view. */
! 				sprintf(rulequery, "select * from pg_views where viewname='%s'", fkconstraint->pktable_name);
! 				if (SPI_connect()!=SPI_OK_CONNECT)
! 					elog(ERROR, "ALTER TABLE: Unable to determine if %s is a view.", relationName);
! 			        qplan=SPI_prepare(rulequery, 0, NULL);
! 				if (!qplan)
! 					elog(ERROR, "ALTER TABLE: Unable to determine if %s is a view.", relationName);
! 				qplan=SPI_saveplan(qplan);
! 				if (SPI_execp(qplan, NULL, nulls, 1)!=SPI_OK_SELECT) 
! 					elog(ERROR, "ALTER TABLE: Unable to determine if %s is a view.", relationName);
! 			        if (SPI_processed != 0)
! 			                elog(ERROR, "ALTER TABLE: Cannot add constraints to views.");
! 			        if (SPI_finish() != SPI_OK_FINISH)
! 			                elog(NOTICE, "SPI_finish() failed in RI_FKey_check()");
  
  				/*
  				 * Grab an exclusive lock on the pk table, so that someone
--- 1243,1250 ----
  				}
  
  				/* check to see if the referenced table is a view. */
! 				if (is_view(fkconstraint->pktable_name, "ALTER TABLE"))
! 				 elog(ERROR, "ALTER TABLE: Cannot add constraints to views.");
  
  				/*
  				 * Grab an exclusive lock on the pk table, so that someone
***************
*** 1635,1640 ****
--- 1613,1621 ----
  	Relation	rel;
  	int			aclresult;
  
+ 	if (is_view(lockstmt->relname, "LOCK TABLE")) 
+ 			elog(ERROR, "LOCK TABLE: cannot lock a view");
+ 
  	rel = heap_openr(lockstmt->relname, NoLock);
  
  	if (lockstmt->mode == AccessShareLock)
***************
*** 1650,1652 ****
--- 1631,1659 ----
  	heap_close(rel, NoLock);	/* close rel, keep lock */
  }
  
+ 
+ static
+ bool
+ is_view (char * relname, char *command)
+ {
+ 	bool retval;
+ 	char rulequery[41+NAMEDATALEN]; 
+ 	void *qplan;
+ 	char nulls[1]="";
+ 
+ 	sprintf(rulequery, "select * from pg_views where viewname='%s'", relname);
+ 	if (SPI_connect()!=SPI_OK_CONNECT)
+ 		elog(ERROR, "%s: Unable to determine if %s is a view - SPI_connect failure..", command, relname);
+ 	qplan=SPI_prepare(rulequery, 0, NULL);
+ 	if (!qplan)
+ 		elog(ERROR, "%s: Unable to determine if %s is a view - SPI_prepare failure.", command, relname);
+ 	qplan=SPI_saveplan(qplan);
+ 	if (SPI_execp(qplan, NULL, nulls, 1)!=SPI_OK_SELECT) 
+ 		elog(ERROR, "%s: Unable to determine if %s is a view - SPI_execp failure.", command, relname);
+ 
+ 	retval = (SPI_processed != 0);
+   if (SPI_finish() != SPI_OK_FINISH)
+ 	 elog(NOTICE, "SPI_finish() failed in %s", command);
+ 
+   return retval;
+ }